我有一个带有 HC-SR501 PIR 的 ESP8266,我已经将其解析到
电路板上以进行 3.3v 操作。
当我在上面运行一个非常简单的代码时,它运行得很好。
使用相同的接线设置,以及与之前使用温度传感器的更大程序的一部分相同的代码,它似乎总是在继续阅读?
这是简单的代码和输出。它检测到动作并立即启动。
代码:
全选int ledPin = 13; // choose the pin for the LED
int inputPin = 4; // choose the input pin (for PIR sensor)
int pirState = LOW; // we start, assuming no mo
tion detected
int val = 0; // variable for reading the pin status
void setup() {
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(inputPin, INPUT); // declare sensor as input
Serial.begin(115200);
Serial.println(\"Starting up\");
}
void loop(){
val = digitalRead(inputPin); // read input value
Serial.println((String)\"the value of val is \" +val);
if (val == HIGH) // check if the input is HIGH
{
Serial.println(\"Motion is on\");
if (pirState == LOW)
{
Serial.println(\"Motion detected!\"); // print on output change
pirState = HIGH;
}
}
else
{
digitalWrite(ledPin, LOW); // turn LED OFF
if (pirState == HIGH)
{
Serial.println(\"Motion ended!\"); // print on output change
pirState = LOW;
}
}
}
这是似乎给出错误读取的代码
代码:
全选void do_board_function() { // This boards function is to get and send DHT temp/humity to MQTT
Serial.println(\"\");
Serial.println(\"Starting Board Functions\");
//////START - TEMP + HUM//////
float readt = dht.readTemperature();
delay(2000);
float readh = dht.readHumidity();
// Check if any reads failed and exit early (to try again).
if (isnan(readt)) {
Serial.println(\"Failed to read from DHT sensor!\");
}
else {
Serial.print(\"Temperature = \");
Serial.print(readt);
Serial.print(\" .... \");
Serial.print(\"Humidity = \");
Serial.print(readh);
Serial.println();
String temp_strt = String(readt);
String hum_strt = String(readh);
temp_strt.toCharArray(temp, temp_strt.length() + 1); //packaging up the data to publish to mqtt
String temp_strh = String(readh);
hum_strt.toCharArray(hum, hum_strt.length() + 1); //packaging up the data to publish to mqtt
// String MQTTPublish = String(\"Temperature is\" + t + \"Humidity is\" + h);
client.publish(Topic_DataMessagesTemp, temp);
client.publish(Topic_DataMessagesHum, hum);
//////END - TEMP + HUM//////
//New Motion Sensor code
val = digitalRead(MOTIONPIN); // read input value
Serial.println((String)\"the value of val is \" +val);
if (val == HIGH) // check if the input is HIGH
{
Serial.println(\"Motion is on\");
if (pirState == LOW)
{
Serial.println(\"Motion detected!\"); // print on output change
pirState = HIGH;
}
}
else
{
if (pirState == HIGH)
{
Serial.println(\"Motion ended!\"); // print on output change
pirState = LOW;
}
}