Problem detecting LDR with Blynk App

Hi! I am working with nodemcu, LDR, and an AC bulb. I want the Blynk to switch on the bulb when I press the allocated button and the bulb lightens up after detecting the LDR value but when I run the code after compilation of code the bulb lightens up without detecting the LDR. Here is my code. Please suggest me what should I do as soon as possible. Thanks in advance!

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

char auth[] = “1827835455ae4845b3bb53da474338dc”;

// Your WiFi credentials.
// Set password to “” for open networks.
char ssid[] = “NTC Offfice”;
char pass[] = “ntcswl00001”;

String relay1State = “off”;
String relay2State = “off”;
String relay3State = “off”;
String relay4State = “off”;

const int relay1 = 5; // GPIO5 D1
const int relay2 = 4; // GPIO4 D2
const int relay3 = 0; // GPIO0 D3
const int relay4 = 2; // GPIO2 D4

int LDR = A0;

void setup()
{
Serial.begin(9600);
Blynk.begin(auth, ssid, pass);
pinMode(relay1, OUTPUT);
pinMode(relay2, OUTPUT);
pinMode(relay3, OUTPUT);
pinMode(relay4, OUTPUT);
pinMode(LDR, INPUT);
}

void loop() {
int LDRValue = analogRead(LDR);
Serial.print("sensor = ");
Serial.print(LDRValue);

if (LDRValue <=15 )
{
Blynk.virtualWrite(D1 , HIGH );
Blynk.virtualWrite(D2 , HIGH );
Blynk.virtualWrite(D3 , HIGH );
Blynk.virtualWrite(D4 , HIGH );

Serial.println(“It’s Dark Outside; Lights status: ON”);
}
else
{
Blynk.virtualWrite(D1 , LOW);
Blynk.virtualWrite(D2 , LOW );
Blynk.virtualWrite(D3 , LOW );
Blynk.virtualWrite(D4 , LOW );

Serial.println(“It’s Bright Outside; Lights status: OFF”);
}
}

No Blynk.begin in your void setup and no Blynk.run in your void loop (although lots of other stuff in your void loop that needs to be moved into a function and called with a timer).

Pete.

Could you please do it for me. Please!

No, that’s not what this community forum is for.

Even if I had the time and the inclination, how would I test the finished code without your hardware?

You should take a look at the Sketch Builder - link at the top of the page, or here:
https://examples.blynk.cc/

and familiarise yourself with how good Blynk code is structured.

Pete.

https://examples.blynk.cc/?board=ESP8266&shield=ESP8266%20WiFi&example=More%2FDHT11

Thanks for your help but it doesn’t work as i want to be done

I don’t see any action in the example that “unlocks” the relais using a button.

And what is something like this supposed to do? It will surely not switch on a relais on D1 …

The solution is to learn a little about Arduino/C+ coding, so that you understand how the code that you find on the internet, forums or sketch builder actually works. Once you understand this, you’ll be able to tweak the code to do exactly what you want it to.
You’ll also get much more support from forum users if you come here with code that you’ve written or customised yourself, asking for guidance on how to overcome a particular problem.

Pete.


#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>


BlynkTimer timer;

char auth[] = "1827835455ae4845b3bb53da474338dc";

// Your WiFi credentials.
// Set password to "” for open networks.
char ssid[] = "NTC Offfice";
char pass[] = "ntcswl00001";

String relay1State = "off";
String relay2State = "off";
String relay3State = "off";
String relay4State = "off";

const int relay1 = 5; // GPIO5 D1
const int relay2 = 4; // GPIO4 D2
const int relay3 = 0; // GPIO0 D3
const int relay4 = 2; // GPIO2 D4

int LDR = A0;

void setup()
{
Serial.begin(9600);
Blynk.begin(auth, ssid, pass);
pinMode(relay1, OUTPUT);
pinMode(relay2, OUTPUT);
pinMode(relay3, OUTPUT);
pinMode(relay4, OUTPUT);
pinMode(LDR, INPUT);



timer.setInterval(1000L, MyLDR);


}

void loop() {
  Blynk.run();
  timer.run();

}




void MyLDR(){

int LDRValue = analogRead(LDR);
Serial.print("sensor = ");
Serial.print(LDRValue);

  Blynk.virtualWrite(V0, LDRValue);     //Value Display  V0


if (LDRValue <=15 )
{
digitalWrite(relay1 , HIGH );
digitalWrite(relay2 , HIGH );
digitalWrite(relay3 , HIGH );
digitalWrite(relay4 , HIGH );

Serial.println("It’s Dark Outside; Lights status: ON");
}
else
{
digitalWrite(relay1 , LOW );
digitalWrite(relay2 , LOW );
digitalWrite(relay3 , LOW );
digitalWrite(relay4 , LOW );

Serial.println("It’s Bright Outside; Lights status: OFF");
}
}





/********************** Relay 1  ON/OFF V1 *******************************/
BLYNK_WRITE(V1)
{
int pinValue = param.asInt();
  if (pinValue == 1) {
digitalWrite(relay1 , HIGH );
  }else {
digitalWrite(relay1 , LOW );
  }
}
/********************** End Relay 1 ON/OFF  *******************************/





/********************** Relay 2  ON/OFF V2 *******************************/
BLYNK_WRITE(V2)
{
int pinValue = param.asInt();
  if (pinValue == 1) {
digitalWrite(relay2 , HIGH );
  }else {
digitalWrite(relay2 , LOW );
  }
}
/********************** End Relay 2 ON/OFF  *******************************/






/********************** Relay 3 ON/OFF V3 *******************************/
BLYNK_WRITE(V3)
{
int pinValue = param.asInt();
  if (pinValue == 1) {
digitalWrite(relay3 , HIGH );
  }else {
digitalWrite(relay3 , LOW );
  }
}
/********************** End Relay 3 ON/OFF  *******************************/





/********************** Relay 4 ON/OFF V4 *******************************/
BLYNK_WRITE(V4)
{
int pinValue = param.asInt();
  if (pinValue == 1) {
digitalWrite(relay4 , HIGH );
  }else {
digitalWrite(relay4 , LOW );
  }
}
/********************** End Relay 4 ON/OFF  *******************************/

1 Like