[Solved] Sudden unstable connection

Hi, my ESP8266-01 has a strange behavior. With the same sketch, my project ran perfectly but suddenly (at 01.00 pm) ESP8266 disconnects every 30 seconds/1 minute. What can i do?

paste your formatted sketch.

#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h>
#include <TimeLib.h>
#include <WidgetRTC.h>
#include <SimpleTimer.h>
#define BLYNK_RED       "#D3435C"
#define BLYNK_YELLOW    "#ED9D00"
#define BLYNK_WHITE     "#FFFFFF"

SimpleTimer timer;
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "";
bool flag;
bool ciao = true;
//bool flagnotif = true;
// Your WiFi credentials.
// Set password to "" for open networks.
WidgetRTC rtc;
BLYNK_ATTACH_WIDGET(rtc, V5);

float potenzamin = 3000;
float potenzamax = 0;
float accpower;
unsigned long lastmillis;
float potenza;
float tensione;
float corrente;
int rowIndex = 0;

void setup()
{
  Serial.begin(9600);
  WiFiManager wifiManager;
  wifiManager.autoConnect("Energy Meter");
  Serial.println("connected...yeey :)");
  Blynk.config(auth);
  Blynk.connect();
  rtc.begin();
  timer.setInterval(2000L, updates);
  lcd.clear();
  pinMode(1, OUTPUT);
  digitalWrite(1, HIGH);  //spegni
  lastmillis = millis();
}

bool isFirstConnect = true;

BLYNK_CONNECTED() {
  if (isFirstConnect) {
    Blynk.syncVirtual(V0);
    isFirstConnect = false;

  }
}

BLYNK_WRITE(V9)
{
  int state = param.asInt();
  if (state) {    //leave location
    Blynk.notify("Sei andato via");
    //flagnotif = false;
  }
}

BLYNK_WRITE(V0)
{
  accpower = param.asDouble();
  Blynk.virtualWrite(V0, accpower);
}

void loop()
{
  Blynk.run();
  timer.run();
  tensione = random(225, 231);
  corrente = random(1, 13);
  potenza = tensione * corrente;
  accpower += ((((millis() - lastmillis) * 0.001 * potenza)) / 3600000);
  lastmillis = millis();

}


void updates() {
  int gmthour = hour();
  if (gmthour == 24) {
    gmthour = 0;
  }
  String displayhour =   String(gmthour, DEC);
  int hourdigits = displayhour.length();
  if (hourdigits == 1) {
    displayhour = "0" + displayhour;
  }
  String displayminute = String(minute(), DEC);
  int minutedigits = displayminute.length();
  if (minutedigits == 1) {
    displayminute = "0" + displayminute;
  }
  String displaysecond = String(second(), DEC);
  int seconddigits = displaysecond.length();
  if (seconddigits == 1) {
    displaysecond = "0" + displaysecond;
  }
  String displaycurrenttime = displayhour + ":" + displayminute;
  char _accpower[5];
  dtostrf(accpower, 2, 2, _accpower);
  Blynk.virtualWrite(V0, _accpower);  // quasi 21.03.45 circa
  char _potenza[5];
  dtostrf((potenza / 1000), 2, 2, _potenza);
  Blynk.virtualWrite(V3, _potenza);
  if (potenza > 2000) {
    Blynk.setProperty(V3, "color", BLYNK_RED);
  } else if (potenza > 1000) {
    Blynk.setProperty(V3, "color", BLYNK_YELLOW);
  } else {
    Blynk.setProperty(V3, "color", BLYNK_WHITE);
  }
  Blynk.virtualWrite(V1, tensione);
  if (potenza > potenzamax) {
    potenzamax = potenza;
  }
  if (potenza < potenzamin) {
    potenzamin = potenza;
  }
  String currentDate = String(day()) + "/" + month() + "/" + year();
  Blynk.virtualWrite(V8, currentDate + "   |   " + displaycurrenttime) ;
  if ((hour() == 23) && (minute() == 0) && (ciao == true)) {
    String currentDate = String(day()) + "/" + month() + "/" + year();
    String minpower = String((potenzamin / 1000)) + " kW";
    String maxpower = String((potenzamax / 1000)) + " kW";
    String daily = String(accpower) + " kWh/d";
    String denaro = String((accpower * 0.160581)) + " €/d";
    Blynk.virtualWrite(V7, "add", rowIndex, "Data", currentDate);
    Blynk.virtualWrite(V7, "pick", rowIndex);
    rowIndex ++;
    Blynk.virtualWrite(V7, "add", rowIndex, "Potenza minima", minpower);
    rowIndex ++;
    Blynk.virtualWrite(V7, "add", rowIndex, "Potenza massima", maxpower);
    rowIndex ++;
    Blynk.virtualWrite(V7, "add", rowIndex, "kWh/day", daily);
    rowIndex ++;
    Blynk.virtualWrite(V7, "add", rowIndex, "€/day", denaro);
    rowIndex ++;
    Blynk.virtualWrite(V2, accpower);   //kWh/day
    if (rowIndex == 100) {
      rowIndex = 0;
    }
    potenzamin = 3000;
    potenzamax = 0;
    accpower = 0;
    ciao = false;
  }
  if ((hour() == 23) && (minute() == 1)) {
    ciao = true;
  }
}

I’m on first test stages of an energy meter. The sketch is unclear and confusing, sorry :sweat:

Change loop() to 2 lines of code and see how you go.

I will try ad soon ad possibile. But I don’t understand. Why did sketch run few hours ago. I will try later, thanks

Maybe you were lucky.

Maybe you added a bug.

Maybe neighbour has turned on his router.

Maybe …

I solved! I don’t know why, but I solved because I re-created the dashboard in a new dashboard with new auth token. Now It works perfectly! Thanks @Costas