Hello Blynk friends. Im trying to make a light sensor for my home so i can check if the lights in a room are on or off. The sensor im using is " Photosensitive Sensor Module" picture https://i.imgur.com/W15cXux.png with a nodemcu board. Sinds im new to blynk and coding this is giving me some problems, I want to send the text “lights are On” or “Lights are Off” to a labled value on the blynk app. Can anyone please tell me what part of my code is not correct … remember im new to this code must be terrible i know …
/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "xxxxxxxxxxx";
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "xxxxxxxxxx";
char pass[] = "xxxxxxxxxxx";
const int ledPin = 5;
const int ldrPin = A0;
void myTimerEvent()
{
// You can send any value at any time.
// Please don't send more that 10 values per second.
Blynk.virtualWrite(V7, millis() / 1000); // write to the Blynk app display every second via virtual pin 1
}
void setup() {
Serial.begin(9600);
Blynk.begin(auth, ssid, pass);
pinMode(ledPin, OUTPUT);
pinMode(ldrPin, INPUT);
}
void loop()
{
Blynk.run();
int ldrStatus = analogRead(ldrPin);
if (ldrStatus <=300) {
digitalWrite(ledPin, HIGH);
Serial.print(ldrStatus);
Serial.println("Lights are on");
}
else {
digitalWrite(ledPin, LOW);
Serial.println("Lights are off");
}
}
The module in the picture does not appear to an analog output, only a digital one. There appears to be a variable resistor, that most likely adjusts when the digital output goes active. You will need to determine if the digital output goes High or Low when the light level (set with variable resistor) is exceeded. Once you know that you can then code accordingly. You also need to put the stuff in the loop() inside the timed event. See below as an example, but some adjustments may be needed.
/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "xxxxxxxxxxx";
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "xxxxxxxxxx";
char pass[] = "xxxxxxxxxxx";
const int ledPin = 5; //GPIO 5, D1
const int ldrPin = 4; //GPIO 4, D2
BlynkTimer timer;
void myTimerEvent()
{
int ldrStatus = digitalRead(ldrPin);
if (ldrStatus == LOW) {
digitalWrite(ledPin, HIGH);
Serial.print(ldrStatus);
Serial.println("Lights are on");
Blynk.virtualWrite(V5, "Lights are on"); //Labeled value display on pin V5
}
else {
digitalWrite(ledPin, LOW);
Serial.println("Lights are off");
Blynk.virtualWrite(V5, "Lights are off"); //Labeled value display on pin V5
}
}
void setup() {
Serial.begin(9600);
Blynk.begin(auth, ssid, pass);
pinMode(ledPin, OUTPUT);
pinMode(ldrPin, INPUT);
// Setup a function to be called every second
timer.setInterval(1000L, myTimerEvent);
}
void loop()
{
Blynk.run();
timer.run(); // Initiates BlynkTimer
}