Automatic and Manual mode In Blynk Project

Hey guys, i am trying to make a home automation project with an LDR, DHT11, nodemcu and Blynk.
The automatic part of the project works okay… the relay turns on automatically when the value of the LDR is less than 800.
I have tried to use a manual button to turn on the relay but the relay only flashes on and off
Please Help…
I am a total noob.

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include "DHT.h"
#define DHTPIN              D1
#define DHTTYPE DHT11
#define relaypin D2
#define LED2     D3
#define LDR_PIN             A0

const int maxLight = 1000;
const int minLight = 800;
int   ldrVal;
DHT dht(DHTPIN, DHTTYPE);
int selectMode;
char auth[] = "*****";
char ssid[] = "*****";
char pass[] = "*****";

BlynkTimer timer;
float t;                                   // Declare the variables
float h;

void setup()
{
  // Blynk will work through Serial
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  pinMode(LED2, OUTPUT);
  pinMode(relaypin, OUTPUT);
  timer.setInterval(2000L, Mode);
  timer.setInterval(2000L, sensorDataSend);
  dht.begin();
}

BLYNK_WRITE(V4)
{
  selectMode = param.asInt();
  Mode;
}
void Mode()
{
  if (selectMode == 1) //If button MANUAL on app
  {
    digitalWrite(LED2, HIGH);
    Serial.println("Manual");
    digitalWrite(relaypin, HIGH);
    Serial.println("Bulb ON");
  }
  else
  {
    digitalWrite(LED2, LOW);
    Serial.println("Automatic");
    sensorDataSend;
  }
}

void sensorDataSend()
{
ldrVal = analogRead(LDR_PIN);
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  if (ldrVal < minLight) {
    digitalWrite(relaypin, HIGH);
    Blynk.virtualWrite(V3, "DARK");
  }
  else {
    digitalWrite(relaypin, LOW);
    Blynk.virtualWrite(V3, "LIGHT");
  }

  Serial.print(F("Humidity: "));
  Serial.print(h);
  Serial.print(F("%  Temperature: "));

  Serial.print(t);
  Serial.println(F("°C "));
  Serial.println( ldrVal);
  Blynk.virtualWrite(V0, t);
  Blynk.virtualWrite(V1, h);
  Blynk.virtualWrite(V2, ldrVal);

}

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

How is your switch widget configured in the app?
Push or Switch mode?

Pete.

Switch Mode

I suspect this is due to the fact that you have the two timers checking each function. When in manual mode it turns On the relay, but then checks the value of the LDR and if not high enough it turns the relay off. You need to add in a check in your sensorDataSend() function that checks if it is in manual mode.

You would also want to get rid of the else statement in the mode() function as this will override the LDR function. Or move it all to the BLYNK_WRITE() function.

For Example:

void setup()
{
  // Blynk will work through Serial
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  pinMode(LED2, OUTPUT);
  pinMode(relaypin, OUTPUT);
  timer.setInterval(2000L, sensorDataSend);
  dht.begin();
}

BLYNK_WRITE(V4)
{
  selectMode = param.asInt();
   if (selectMode == 1) //If button MANUAL on app
  {
    digitalWrite(LED2, HIGH);
    Serial.println("Manual");
    digitalWrite(relaypin, HIGH);
    Serial.println("Bulb ON");
  }
  else
  {
    digitalWrite(LED2, LOW);
    Serial.println("Automatic");
    }
}

void sensorDataSend()
{
  ldrVal = analogRead(LDR_PIN);
   h = dht.readHumidity();
   t = dht.readTemperature();
   if (selectMode != 1)
 {
     if (ldrVal < minLight) {
      digitalWrite(relaypin, HIGH);
      Blynk.virtualWrite(V3, "DARK");
      }
     else {
      digitalWrite(relaypin, LOW);
      Blynk.virtualWrite(V3, "LIGHT");
     }
  }

  Serial.print(F("Humidity: "));
  Serial.print(h);
  Serial.print(F("%  Temperature: "));

  Serial.print(t);
  Serial.println(F("°C "));
  Serial.println( ldrVal);
  Blynk.virtualWrite(V0, t);
  Blynk.virtualWrite(V1, h);
  Blynk.virtualWrite(V2, ldrVal);

}

Thanks let me try it…

Thanks it works …
I would like to make the button switch on and off while in manual instead of just moving to the “void sensorDataSend()” when off

I’d use two buttons. One for auto/manual, and one for On/Off in Manual Mode

how to do this?

Hello Blynk community.
i have a project for home automation which uses LDR, Dht11, Nodemcu and a couple of relays.
the idea is to have a manual and automatic mode. when in automatic mode, the relay automatically activates when the LDR value is lower than 800.
the automatic part works well but the manual section doesn’t seem to work, the Lights do not switch on when the button is pressed in the Blynk…

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include "DHT.h"
#define DHTPIN              D1
#define DHTTYPE DHT11
#define relaypin D2
#define LED2     D3
#define LDR_PIN             A0

const int maxLight = 1000;
const int minLight = 800;
int   ldrVal;
DHT dht(DHTPIN, DHTTYPE);
int selectMode;
int Switch;
char auth[] = "*****";
char ssid[] = "*****";
char pass[] = "*****";

BlynkTimer timer;
float t;                                   // Declare the variables
float h;

void setup()
{
  // Blynk will work through Serial
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  pinMode(LED2, OUTPUT);
  pinMode(relaypin, OUTPUT);
  timer.setInterval(1000L, Mode);
  timer.setInterval(1000L, sensorDataSend);
  dht.begin();
}
BLYNK_WRITE(V5)
{
  Switch = param.asInt();
  Lamp;
}
void Lamp()
{
  if (Switch == 1)
  {
  digitalWrite(relaypin, HIGH);
}
else
{
  digitalWrite(relaypin, LOW);
}
}  
BLYNK_WRITE(V4)
{
  selectMode = param.asInt();
  Mode;
}

void Mode()
{
  if (selectMode == 1) //If button MANUAL on app
{
  digitalWrite(LED2, HIGH);
   Serial.println("Manual");
   Lamp;
 }
  else
  {
    digitalWrite(LED2, LOW);
    Serial.println("Automatic");
    sensorDataSend;
  }
}
void sensorDataSend()
{
ldrVal = analogRead(LDR_PIN);
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  if (selectMode != 1)
 { 
  if (ldrVal < minLight) {
    digitalWrite(relaypin, HIGH);
    Blynk.virtualWrite(V3, "DARK");
  }
  else {
    digitalWrite(relaypin, LOW);
    Blynk.virtualWrite(V3, "LIGHT");
  }
 }

  Serial.print(F("Humidity: "));
  Serial.print(h);
  Serial.print(F("%  Temperature: "));

  Serial.print(t);
  Serial.println(F("°C "));
  Serial.println( ldrVal);
  Blynk.virtualWrite(V0, t);
  Blynk.virtualWrite(V1, h);
  Blynk.virtualWrite(V2, ldrVal);

}

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

Please don’t keep creating new topics about the same subject.
I’ve merged your new topic into this one.

Pete.

BLYNK_WRITE(V4)
{
  selectMode = param.asInt();
   if (selectMode == 1) //If button MANUAL on app
  {
   Serial.println("Manual");
   Blynk.syncVirtual(V5);
  }
  else
  {
     Serial.println("Automatic");
   }
}

BLYNK_WRITE(V5)
{
  manualMode = param.asInt();
   if (selectMode == 1) //If button MANUAL on app
  {
      if (manualMode == 1) 
       {
          digitalWrite(LED2, HIGH);
          Serial.println("Manual");
          digitalWrite(relaypin, HIGH);
         Serial.println("Bulb ON");
       }
     else
       {
        digitalWrite(LED2, LOW);
        Serial.println("Manual");
         digitalWrite(relaypin, LOW);
         Serial.println("Bulb OFF");
       }
   }
}

void sensorDataSend()
{
  ldrVal = analogRead(LDR_PIN);
   h = dht.readHumidity();
   t = dht.readTemperature();
   if (selectMode != 1)
 {
     if (ldrVal < minLight) {
      digitalWrite(relaypin, HIGH);
      Blynk.virtualWrite(V3, "DARK");
     Blynk.virtualWrite(V5, HIGH);
      }
     else {
      digitalWrite(relaypin, LOW);
      Blynk.virtualWrite(V3, "LIGHT");
      Blynk.virtualWrite(V5, LOW);
     }
  }

  Serial.print(F("Humidity: "));
  Serial.print(h);
  Serial.print(F("%  Temperature: "));

  Serial.print(t);
  Serial.println(F("°C "));
  Serial.println( ldrVal);
  Blynk.virtualWrite(V0, t);
  Blynk.virtualWrite(V1, h);
  Blynk.virtualWrite(V2, ldrVal);

}