所以在这个项目中,我试图将我的 PIR 传感器开关连接到 Blynk 上,以通过我的手机打开/关闭。虽然这段代码编译得很好,但我没有从 PIR 传感器或 LED 获得任何输出。我通过 ESP8266-01 将它与我的 Arduino 板连接起来。所以现在我很困惑,要么我的代码有错误,要么只是我的 PIR 传感器不工作。请帮忙。提前谢谢你们。
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
int led = 9;
int sensor =2;
int state = LOW; //default, no motion detected
int val = 0; //variable to store sensor data status
char auth[] = "aIUaQ3TPypvl9uajcOgMHhaWpSkc8dkz";
char ssid[] = "im kinda sus";
char pass[] = "ilovearduino";
void setup(){
Serial.begin(115200); //getting ready to communicate
Blynk.begin(auth, ssid, pass);
pinMode(led,OUTPUT); //define led as the output
pinMode(sensor, INPUT); //define sensor as the input
}
void pirSensor(){
val = digitalRead(sensor); //read sensor input
if(val == HIGH){
digitalWrite(led,HIGH); //if value is high, led light up
delay(500);
if(state == LOW){ //if pir state is low
Serial.println("Motion Detected!"); //print this line
state = HIGH; //pir sensor detect motion
}
}
else if(state == HIGH) {
digitalWrite(led, LOW); // if not led turn off
delay(500);
Serial.println("Motion Stopped!"); // print this line
state = LOW;
}
}
BLYNK_WRITE(V0){
int led = param.asInt(); //led is connected to virtual pin 0
}
void loop(){
Blynk.run(); //blynk starts to run
}