Problem is that, when wifi off, all switches remain ON as on connection

hi. i am using nodemcu esp8226 in my home automation project. problem is that, when wifi off, all switches remain ON as on connection. my code is
#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[] = "d89adaa1a4f742b58ced23d60.........";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "----";
char pass[] = "roora";



  const int Relay_1 = 16;       //D0
  const int Relay_2 = 5;        //D1
  const int Relay_3 = 4;        //D2
  const int Relay_4 = 0;        //D3
  const int Relay_5 = 2;        //D4
  const int Relay_6 = 14;       //D5
  const int Relay_7 = 12;       //D6
  const int Relay_8 = 13;       //D7
  const int Relay_9 = 15;       //D8





void setup()
{
  pinMode(Relay_1, OUTPUT);
  pinMode(Relay_2, OUTPUT);
  pinMode(Relay_3, OUTPUT);
  pinMode(Relay_4, OUTPUT);
  pinMode(Relay_5, OUTPUT);
  pinMode(Relay_6, OUTPUT);
  pinMode(Relay_7, OUTPUT);
  pinMode(Relay_8, OUTPUT);
  pinMode(Relay_9, OUTPUT);

    digitalWrite(Relay_1, HIGH);
    digitalWrite(Relay_2, HIGH);
    digitalWrite(Relay_3, HIGH);
    digitalWrite(Relay_4, HIGH);
    digitalWrite(Relay_5, HIGH);
    digitalWrite(Relay_6, HIGH);
    digitalWrite(Relay_7, HIGH);
    digitalWrite(Relay_8, HIGH);

  
  // Debug console
  Serial.begin(115200);
  Blynk.begin(auth, ssid, pass);





}

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










/********************** Relay 1  ON/OFF V1 *******************************/
BLYNK_WRITE(V1)
  {
  int pinValue = param.asInt();
  if (pinValue == 1)
    {
    digitalWrite(Relay_1, HIGH);
    Serial.println("Relay 1 On");
    }
  else if (pinValue == 0)
    {
    digitalWrite(Relay_1, LOW);
    Serial.println("Relay 1 Off");
    }
  }



/********************** Relay 2  ON/OFF V2 *******************************/
BLYNK_WRITE(V2)
  {
  int pinValue = param.asInt();
  if (pinValue == 1)
    {
    digitalWrite(Relay_2, HIGH);
    Serial.println("Relay 2 On");
    }
  else if (pinValue == 0)
    {
    digitalWrite(Relay_2, LOW);
    Serial.println("Relay 2 Off");
    }
  }



/********************** Relay 3  ON/OFF V3 *******************************/
BLYNK_WRITE(V3)
  {
  int pinValue = param.asInt();
  if (pinValue == 1)
    {
    digitalWrite(Relay_3, HIGH);
    Serial.println("Relay 3 On");
    }
  else if (pinValue == 0)
    {
    digitalWrite(Relay_3, LOW);
    Serial.println("Relay 3 Off");
    }
  }



/********************** Relay 4  ON/OFF V4 *******************************/
BLYNK_WRITE(V4)
  {
  int pinValue = param.asInt();
  if (pinValue == 1)
    {
    digitalWrite(Relay_4, HIGH);
    Serial.println("Relay 4 On");
    }
  else if (pinValue == 0)
    {
    digitalWrite(Relay_4, LOW);
    Serial.println("Relay 4 Off");
    }
  }



/********************** Relay 5  ON/OFF V5 *******************************/
BLYNK_WRITE(V5)
  {
  int pinValue = param.asInt();
  if (pinValue == 1)
    {
    digitalWrite(Relay_5, HIGH);
    Serial.println("Relay 5 On");
    }
  else if (pinValue == 0)
    {
    digitalWrite(Relay_5, LOW);
    Serial.println("Relay 5 Off");
    }
  }



/********************** Relay 6  ON/OFF V6 *******************************/
BLYNK_WRITE(V6)
  {
  int pinValue = param.asInt();
  if (pinValue == 1)
    {
    digitalWrite(Relay_6, HIGH);
    Serial.println("Relay 6 On");
    }
  else if (pinValue == 0)
    {
    digitalWrite(Relay_6, LOW);
    Serial.println("Relay 6 Off");
    }
  }



/********************** Relay 7  ON/OFF V7 *******************************/
BLYNK_WRITE(V7)
  {
  int pinValue = param.asInt();
  if (pinValue == 1)
    {
    digitalWrite(Relay_7, HIGH);
    Serial.println("Relay 7 On");
    }
  else if (pinValue == 0)
    {
    digitalWrite(Relay_7, LOW);
    Serial.println("Relay 7 Off");
    }
  }



/********************** Relay 8  ON/OFF V8 *******************************/
BLYNK_WRITE(V8)
  {
  int pinValue = param.asInt();
  if (pinValue == 1)
    {
    digitalWrite(Relay_8, HIGH);
    Serial.println("Relay 8 On");
    }
  else if (pinValue == 0)
    {
    digitalWrite(Relay_8, LOW);
    Serial.println("Relay 8 Off");
    }
  }

Please don’t tack your issue into another topic… particularly one that was two years old.

Also, please properly format your posted code (as constantly directed in this forum) or the likelihood of anyone even bothering to look at it will be low… you post might even be deleted.

Blynk - FTFC

sorry, now help me please…

You mean device can’t connect to WIFI or it looses the connection? That’s two totally different situations. If it’s the first case, then perhaps just change

digitalWrite(Relay_1, HIGH);
    digitalWrite(Relay_2, HIGH);
    digitalWrite(Relay_3, HIGH);
    digitalWrite(Relay_4, HIGH);
    digitalWrite(Relay_5, HIGH);
    digitalWrite(Relay_6, HIGH);
    digitalWrite(Relay_7, HIGH);
    digitalWrite(Relay_8, HIGH);

onto this:

digitalWrite(Relay_1, LOW);
    digitalWrite(Relay_2, LOW);
    digitalWrite(Relay_3, LOW);
    digitalWrite(Relay_4, LOW);
    digitalWrite(Relay_5, LOW);
    digitalWrite(Relay_6, LOW);
    digitalWrite(Relay_7, LOW);
    digitalWrite(Relay_8, LOW);

no, problem is that i want when wifi off, all switches off automatically, anyhow i try on this

Try reading the documentation regarding the Blynk.connected function
http://docs.blynk.cc/#blynk-firmware-connection-management-blynkconnected
then search the forum for examples of how to integrate it into your code and turn your lights off if Blynk.connected returns ‘false’.

Pete.

2 Likes

Check this link: [SOLVED] How to run Blynk.run() only when WiFi connection is established it’ll do what you need…