So in this project, I’m trying to connect my PIR sensor switch on Blynk to switch on/off through my phone.
Although this code compiles just fine, I’m not getting any output from the PIR sensor or LED. I connect it through an ESP8266-01 with my Arduino board. I can control the LED on/off with GPIO pin on my ESP8266-01 with Blynk, but I can’t do the same with digital pins
So now I’m confused with either my code has an error or just my PIR sensor is not working. Please help. Thank you guys in advance.
#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[] = "*******************";
char ssid[] = "***********";
char pass[] = "**********";
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; //????
}
}
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();
}