[Solved] D1 mini constantly disconnecting when I turn off internet connection

D1 mini was working without problem for 4 months, now it is acting weird when I disconnect internet connection time to time. When I disconnect the internet after couple of minutes my ESP powers off or shutdowns by itself. My sketch is simple it has two relay control and timer to turn off relays in certain time when I want to. What causes the disconnection I don’t know.
BTW I uploaded with latest library and using latest blynk app on android.

@speed57 are you using the RTC widget in your project?

If you are it might be worth looking into the recent changes for this widget.

@Costas. I used your ‘SimpleTimer and a slider’ example no RTC widget

@speed57 can you post your / my code.

When you say the ESP powers off or shuts down by itself, does it reset or does the ESP stay off?

If it’s the latter I would suspect a faulty connection with the relays.

Unlike Arduino’s the ESP’s are excellent at reconnecting to the server as long as you account for the known bug (hard reset after each local flash).

#include <ESP8266WiFi.h> 
#include <BlynkSimpleEsp8266.h>
#include <ArduinoOTA.h>
#include <SimpleTimer.h>

#define lightP1 D5
#define lightP2 D6
SimpleTimer timer;

char auth[] = "xxxx"; 
char ssid[] = "xxxx";                             
char pass[] = "xxxx";                     
                                       
char OTAhost[] = "PowerS";
int Countdown; 
bool ONstatus  = false;


WidgetLED light1(11);
WidgetLED light2(12);
WidgetTerminal terminal(V5);

/*************************Slide Timer OFF *********************************/
BLYNK_WRITE(V10){
  Countdown = param.asInt();
}
void runEveryMinute(){
  
  if((Countdown > 0)&& (ONstatus == true)){
    Countdown--;    //  reduce Countdown by 1 minute every 60s
    Serial.print(F("Device will switch OFF in "));
    Serial.print(Countdown);
    Serial.println(F(" minute(s)"));
    terminal.println(F("  12v LED   ЖАРЫҒЫ "));
    terminal.println(    Countdown);
    terminal.println(F("  минуттан кейін өшіріледі "));
    terminal.flush();
  }  
  
  if((Countdown > 0) && (ONstatus == false)){
    terminal.println(F("  ТАЙМЕР ІСКЕ ҚОСЫЛДЫ"));
    terminal.flush();
    ONstatus = true;
  }

  if((Countdown == 0) && (ONstatus == true)){
    Serial.println(F("Device is now OFF"));
    terminal.println(F("  12v LED   ЖАРЫҒЫ     ӨШІРІЛДІ"));
    terminal.flush();
    ONstatus = false;
    digitalWrite(lightP2, HIGH);
    light2.off();
  }
}
/************force turn off Lights******************/
#define TURN_OFF 1               
                                  
BLYNK_WRITE(V1)
{
  int v1 = param.asInt();
  if(v1 == 1)
  {
    digitalWrite(lightP1, LOW);
    light1.on();
    terminal.println("  12v LED   ЖАРЫҒЫ ҚOCЫЛДЫ");
    terminal.flush();
  }
  else
  {
    digitalWrite(lightP1, HIGH);
    light1.off();
    terminal.println("  12v LED   ЖАРЫҒЫ ӨШІРІЛДІ");
    terminal.flush();
  }
}
BLYNK_WRITE(V2)
{
  int v2 = param.asInt();
  if(v2 == 1)
  {
    digitalWrite(lightP2, LOW);
    light2.on();
    terminal.println("  Power Supply     ҚOCЫЛДЫ");
    terminal.flush();
  }
  else
  {
    digitalWrite(lightP2, HIGH);
    light2.off();
    terminal.println("  Power Supply     ӨШІРІЛДІ");
    terminal.flush();
  }
}

void setup() 
{
  Blynk.begin(auth, ssid, pass);
  pinMode(lightP1, OUTPUT);
  pinMode(lightP2, OUTPUT);
  digitalWrite(lightP1, TURN_OFF);
  digitalWrite(lightP2, TURN_OFF);
  while (Blynk.connect() == false) {}
  terminal.println(F(" Blynk v"BLYNK_VERSION ":СМАРТ ЖАРЫҚ СИСТЕМА"));
  terminal.flush();
  terminal.println("  --------------------------------------");
  terminal.flush();
  ArduinoOTA.begin();                          
  timer.setInterval(60000L, runEveryMinute);
}

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

@speed57 the section of code that I wrote did come with a disclaimer that it doesn’t use the full power of SimpleTimer i.e. when you move the Slider you have to wait for up to 60 seconds for “relay” to respond. It was just something I knocked up to demonstrate why we use SimpleTimer with Blynk.

When I look at the code now I have to say I could have written a much better example.

A few issues with your code. You should do pinMode and digitalWrite before Blynk.begin() because these are not Blynk related.

Blynk made Blynk.begin() a blocking function around a month ago so flashing with the new libraries has broken the while() loop. If the internet is off it will be stuck at Blynk.begin(). The fix is to look at Blynk.config() i.e. make a WiFi connection then call Blynk.config() rather than use the Blynk.begin() to make the WiFi connection and connect to the server.

@Costas thank you for pointing out some issues I have.

So I have to do is:

pinMode(lightP1, OUTPUT);
digitalWrite(lightP1, TURN_OFF);

then

blynk.config(auth);

befor the

void loop()

?

After digitalWrite() you need to make a WiFi connection (standard ESP / Arduino WiFi code) then Blynk.config(), rest of setup() then loop(). Capital B for Blynk.config().

@Costas. Sorry I couldn’t get it. Can you illustrate with some examples :sweat:

Check this example out, I have compiled it but haven’t flashed it.

// BlynkConfig.ino by Costas 24th Jan 2017
// to circumvent Blynk's new Blynk.begin() blocking function

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

void setup() {
  WiFi.begin("ssid", "password");
  while (WiFi.status() != WL_CONNECTED){
    delay(1);  
  }
  Blynk.config("auth");
  long timeout = 7000;
  long start = millis();
  while (Blynk.connect() == false) {
   if (millis() > start + timeout){
      break;    
   }
  }
}

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

@Costas I uploaded it but shows offline on the blynk app (added Vpin to test and also without Vpin)

@speed57 works fine here.

You will need to add all the regular stuff to see Serial Monitor output etc.
If you are wanting to turn your router on and off then you will also need to add what is included for checking the Blynk connection, to the WiFi connection section.

Perhaps you add all what is required and post your sketch.

I do have a full functional sketch that checks WiFi and Blynk connection on an ongoing basis but you will learn more if you try first.

1 Like

Thanks costas - your insights and code examples are so helpful.