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

Use Blynk for home automation and if internet not connects then a led switches on to indicate that the device is disconnected

I want to use Blynk standalone with smartconfig for my home automation but I want a change that if the internet fails to connect then a led light will indicate that it is not connected I am using D1 mini V2 NodeMcu board for that with 4 channel relay. Kindly help me with the code.

Are you talking about a physical LED attached to the Wemos D1 Mini, or an LED widget in the app?

Pete.

1 Like

I am talking about a physical LED. The led will turned off when it will be connected to internet and turns on when the internet connection is lost.

This is just a snippet code.

void connectionStat() {
  if (Blynk.connected()) {
    Blynk.run();
    digitalWrite(LED, LOW);/////// 
  } else if (ReCnctFlag == 0) {
    ReCnctFlag = 1;
    digitalWrite(LED, HIGH);/////// 
    Serial.println("Starting reconnection timer in 10 seconds...");
    timer.setTimeout(10000L, []() {
      ReCnctFlag = 0;
      ReCnctCount++;
      Serial.print("Attempting reconnection #");
      Serial.println(ReCnctCount);
      Blynk.connect();
    });
  }
}

I’m a beginner. Can you provide me with the full code? Thanks in advance :sweat_smile:

You can use Sketch Builder. Everything is ready here
https://examples.blynk.cc/?board=ESP8266&shield=ESP8266%20WiFi&example=GettingStarted%2FBlynkBlink

Study the code.
Later add the function i have given. call the function using Blynk timer or place it inside the loop.
define the LED pins and change it in the snippet code provided.

Then its better you start to learn basics of C++. You can find many good pieces of code in this forum which will help you to grab things more quickly…

the above post is from @GTT there are some really interesting code going on. Study this for having an idea.

1 Like

Thank you for your kind response, I will surely look out these codes. Thank you for the shared links.

Can you check what’s wrong with this code?

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "YourAuthToken";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "YourNetworkName";
char pass[] = "YourPassword";
int ReCnctFlag;
int ReCnctCount;
void setup()
{
  // Debug console
  Serial.begin(9600);
  pinMode(LED_BUILTIN, OUTPUT); 
  Blynk.begin(auth, ssid, pass);
}
void connectionStat() {
  if (Blynk.connected()) {
    Blynk.run();
    digitalWrite(LED_BUILTIN, LOW);/////// 
  } else if (ReCnctFlag == 0) {
    ReCnctFlag = 1;
    digitalWrite(LED_BUILTIN, HIGH);/////// 
    Serial.println("Starting reconnection timer in 10 seconds...");
    timer.setTimeout(10000L, []() {
      ReCnctFlag = 0;
      ReCnctCount++;
      Serial.print("Attempting reconnection #");
      Serial.println(ReCnctCount);
      Blynk.connect();
    });
  }
}

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

And can anyone tell me how to deal with multiple libraries found error?

Hey look at the above code. But I can’t find what’s wrong. Please help.

Try putting

timer.run(); 

in the void loop you have timers in your code but are never telling it to run. Also remove the simple timer library in the declarations and add a

BlynkTimer timer;

instance somewhere up top.

It compiled successfully but the led function is not working. Check the code any rectify my mistake.

    #define BLYNK_PRINT Serial
    #include <ESP8266WiFi.h>
    #include <BlynkSimpleEsp8266.h>

BlynkTimer timer;

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "";
char pass[] = "";
int ReCnctFlag;
int ReCnctCount;
void setup()
{
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, pass);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
    }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  // Missing and added. This part is responsible for the connection to the router!
  //Otherwise it's okay!
  Serial.begin(115200);
  pinMode(LED_BUILTIN, OUTPUT); 
  Blynk.begin(auth, ssid, pass);
}
void connectionStat() {
  if (Blynk.connected()) {
    Blynk.run();
    digitalWrite(LED_BUILTIN, LOW);/////// 
  } else if (ReCnctFlag == 0) {
    ReCnctFlag = 1;
    digitalWrite(LED_BUILTIN, HIGH);/////// 
    Serial.println("Starting reconnection timer in 10 seconds...");
    timer.setTimeout(10000L, []() {
      ReCnctFlag = 0;
      ReCnctCount++;
      Serial.print("Attempting reconnection #");
      Serial.println(ReCnctCount);
      Blynk.connect();
    });
  }
}

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

Ok, you have created the function connectionStat(), but now you need to call this function every x seconds. So, try to put

timer.setInterval(5000L, connectionStat); //call connectionStat every 5 seconds

inside the void setup().
Furthermore I would modify the void loop() in this way:

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

DG

1 Like

Thanks man it really worked.

Thanks to all who helped me for the coding specially [mimmo13], [daveblynk] and [Madhukesh] (https://community.blynk.cc/u/Madhukesh) (https://community.blynk.cc/u/daveblynk)(https://community.blynk.cc/u/mimmo13). Now my code is completed have a look at the code and suggest changes if any required. (project in brief: Making a home automation project with 4 channel relays and blynk app). So here’s the code:

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
BlynkTimer timer;

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "authcode";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "SSID";
char pass[] = "Pass";
int ReCnctFlag;
int ReCnctCount;
int LED = 16; //D0

int relay1State = LOW;
int relay2State = LOW;
int relay3State = LOW;
int relay4State = LOW;


#define RELAY_PIN_1      5   //D1
#define RELAY_PIN_2      4   //D2
#define RELAY_PIN_3      12   //D6
#define RELAY_PIN_4      13   //D7

#define VPIN_BUTTON_1    V12 
#define VPIN_BUTTON_2    V13
#define VPIN_BUTTON_3    V14
#define VPIN_BUTTON_4    V15  

BLYNK_CONNECTED() {

  Blynk.syncVirtual(VPIN_BUTTON_1);
  Blynk.syncVirtual(VPIN_BUTTON_2);
  Blynk.syncVirtual(VPIN_BUTTON_3);
  Blynk.syncVirtual(VPIN_BUTTON_4);

 }

BLYNK_WRITE(VPIN_BUTTON_1) {
  relay1State = param.asInt();
  digitalWrite(RELAY_PIN_1, relay1State);
}

BLYNK_WRITE(VPIN_BUTTON_2) {
  relay2State = param.asInt();
  digitalWrite(RELAY_PIN_2, relay2State);
}
BLYNK_WRITE(VPIN_BUTTON_3) {
  relay3State = param.asInt();
  digitalWrite(RELAY_PIN_3, relay3State);
}
BLYNK_WRITE(VPIN_BUTTON_4) {
  relay4State = param.asInt();
  digitalWrite(RELAY_PIN_4, relay4State);
}

void setup()
{
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, pass);
  timer.setInterval(5000L, connectionStat); //call connectionStat every 5 seconds

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
    }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  // Missing and added. This part is responsible for the connection to the router!
  //Otherwise it's okay!
  Serial.begin(9600);
  pinMode(LED, OUTPUT); 
  Blynk.begin(auth, ssid, pass);

  pinMode(RELAY_PIN_1, OUTPUT);
  digitalWrite(RELAY_PIN_1, relay1State);

   pinMode(RELAY_PIN_2, OUTPUT);
   digitalWrite(RELAY_PIN_2, relay2State);

   pinMode(RELAY_PIN_3, OUTPUT);
   digitalWrite(RELAY_PIN_3, relay3State);

   pinMode(RELAY_PIN_4, OUTPUT);
   digitalWrite(RELAY_PIN_4, relay4State);
}
void connectionStat() {
  if (Blynk.connected()) {
    Blynk.run();
    digitalWrite(LED, LOW);/////// 
  } else if (ReCnctFlag == 0) {
    ReCnctFlag = 1;
    digitalWrite(LED, HIGH);/////// 
    Serial.println("Starting reconnection timer in 1 seconds...");
    timer.setTimeout(1000L, []() {
      ReCnctFlag = 0;
      ReCnctCount++;
      Serial.print("Attempting reconnection #");
      Serial.println(ReCnctCount);
      Blynk.connect();
    });
  }
}

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

If any change is requied kindly comment. Every reply is appreciated ! Thank you once again.

1 Like

It’s almost midnight here in Italy so I’m not thinking straight :sleeping:. However the code seems ok.

DG

1 Like