Need Help with ESP8266 and Blynk Project!

Need Help with ESP8266 and Blynk Project!

Hi everyone,

I’m working on a project using an ESP8266 module with Blynk to connect to WiFi. However, I’m facing an issue: if there’s a power cut and the WiFi disconnects, I need a way to receive notifications. Additionally, I want the ESP8266 to automatically reconnect to Blynk once the power is restored.

Has anyone here tackled a similar problem or have any suggestions on how to achieve this? Any advice or code snippets would be greatly appreciated!

Thanks in advance!

code here

#define BLYNK_TEMPLATE_ID "TMPL63XbV8BX7"
#define BLYNK_TEMPLATE_NAME "HOME"
#define BLYNK_AUTH_TOKEN "------------"

#define BLYNK_PRINT Serial

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

int led1=14;
int led2=15;
int value=A0;

char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "Test"; //your hotspot name
char pass[] = "123456789"; //your hotspot password name

void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(value, INPUT);
  delay(10);

}

void loop() {
  Blynk.run();
  float voltagevalue = analogRead(value);
Serial.print("A0 value: ");
Serial.println(voltagevalue);     
if (voltagevalue <30){
  voltagevalue = 0;
}
else
voltagevalue = mapFloat(voltagevalue, 0, 1024, 0, 15.8);
Blynk.virtualWrite(V0, voltagevalue); 
Serial.print("voltage value: ");
Serial.println(voltagevalue,4);    
delay(1000);
}

BLYNK_WRITE(V1){
  int pinValue = param.asInt(); // assigning incoming value from pin V0 to a variable
  digitalWrite(led1,pinValue);
}

BLYNK_WRITE(V2){
  int pinValue = param.asInt(); // assigning incoming value from pin V0 to a variable
  digitalWrite(led2,pinValue);
}

@Nithurshan Please edit your post, using the pencil icon at the bottom, and add triple backticks at the beginning and end of your code so that it displays correctly.
Triple backticks look like this:
```

Copy and paste these if you can’t find the correct symbol on your keyboard.

Pete.

1 Like

Hi sir I edit my post.

This code shouldn’t be in your void loop, and you shouldn’t be using blocking delays with Blynk.

Read this:

What sort of notifications?

It would normally do that. What are you seeing in your serial monitor when the device reboots? (use triple backticks when posting serial output text).

Pete.

it’s by mistake, i don’t want receive notification when the wifi turn off while the power cut.

i want while the wifi signal turn off and turn on, bylink and cloud connect automatically

B⸮[76] Connecting to test [4404] Connected to WiFi

[4405] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v1.3.2 on ESP8266

 #StandWithUkraine    https://bit.ly/swua


[4530] Connecting to blynk.cloud:80
[4782] Ready (ping: 89ms).
A0 value: 329.00
voltage value: 5.0764
A0 value: 329.00
voltage value: 5.0764
A0 value: 329.00
voltage value: 5.0764
A0 value: 330.00
voltage value: 5.0918
A0 value: 331.00
voltage value: 5.1072
A0 value: 329.00
voltage value: 5.0764
A0 value: 329.00
voltage value: 5.0764
A0 value: 330.00
voltage value: 5.0918
A0 value: 329.00
voltage value: 5.0764
A0 value: 334.00
voltage value: 5.1535
A0 value: 334.00
voltage value: 5.1535
A0 value: 334.00
voltage value: 5.1535
A0 value: 337.00
voltage value: 5.1998
A0 value: 337.00
voltage value: 5.1998
A0 value: 334.00
voltage value: 5.1535
A0 value: 336.00
voltage value: 5.1844
A0 value: 336.00
voltage value: 5.1844
A0 value: 336.00
voltage value: 5.1844
A0 value: 336.00
voltage value: 5.1844
A0 value: 331.00
voltage value: 5.1072
A0 value: 331.00
voltage value: 5.1072
A0 value: 331.00
voltage value: 5.1072
A0 value: 329.00
voltage value: 5.0764
A0 value: 327.00
voltage value: 5.0455
A0 value: 327.00
voltage value: 5.0455
A0 value: 328.00
voltage value: 5.0609
A0 value: 329.00
voltage value: 5.0764
A0 value: 332.00
voltage value: 5.1227
A0 value: 329.00
voltage value: 5.0764
A0 value: 329.00
voltage value: 5.0764
A0 value: 331.00
voltage value: 5.1072
A0 value: 333.00
voltage value: 5.1381
A0 value: 329.00
voltage value: 5.0764

@Nithurshan please edit your post and replace the screenshot with the text copied from your serial monitor, and use triple backticks at tge ebgin8ng and end when you paste it into your post, as I said here…

Pete.

ok sir

Your serial output shows the device connecting correctly to Blynk, so I’m not really sure why you’ve posted it. I’d expected you to post serial output of the device failing to re-connect to Blynk after a power outage.

I don’t understand what this means…

Maybe you could clarify?

Pete.

After the router is powered off and restarted, how does eap8266 automatically connect to the wifi and blynk servers?

When Blynk.run() is executed, if there is no connection to the Blynk server then the Blynk library should attempt to re-connect.

Pete.