这是一个将在8小时内上交的项目!我们坚持这个问题,并认为这个地方是一种最后的手段。
我正试图用Arduino打开/关闭遥控插座。
这样做会导致不一致。有时当它进入if
”should be ON”
语句时// on off remote control
int off = 12;
int on = 13;
void setup() {
pinMode(off, OUTPUT); // sets the digital pin as output
pinMode(on, OUTPUT);
}
void loop() {
// ..first we getting response from server if remote control should be on/off,
// working fine so not really relevant to problem.
// then we determine if outlet should be ON or OFF:
response.toCharArray(responseCharArray,100);
if(strstr( responseCharArray, "active") && strstr( responseCharArray, "1")) {
// This should turn ON the outlet.
digitalWrite(on, HIGH);
delay(250);
digitalWrite(on, LOW);
Serial.println("should be ON");
}
else if(strstr( responseCharArray, "active") && strstr( responseCharArray, "0")) {
// This should turn OFF the outlet.
digitalWrite(off, HIGH);
delay(250);
digitalWrite(off, LOW);
Serial.println("should be OFF");
}
}
而是关闭电源,反之亦然。
代码:
{{1}}
布线图片
问题: 这里可能缺少什么?因为它在输入相同的if语句时随机打开/关闭它。
答案 0 :(得分:0)
这里的信息太少了。
请注意,如果responseCharArray
包含例如"retroactively from 1941"
之类的内容,您的代码将会触发(启用) "active for 37 seconds"
,或关闭if(strstr(responseCharArray, "active=1") != NULL)
之类的内容。
换句话说,字符串匹配不是很精确,但很难知道它应该是什么,因为我对响应的格式一无所知。
也许它至少应该是
1
或者其他什么,至少要将active
锁定到responseCharArray
部分。
在您执行的日志记录中,也打印出{{1}}的值。这将让您分析决策是否合理。
此外,与往常一样,三重检查您的接线并注意例如备份馈送。