BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

Change Manual and automatic mode in blynk

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();
}

A post was merged into an existing topic: Automatic and Manual mode In Blynk Project