Reset Wemos D1 Mini (actually ESP32) when connection lost

I have an Wemos D1 Mini in my garage that do one simple thing. Meassuring temperature and sends the value to my Blynk app.

But sometimes it looses connection to Blynk and will not reconnect. Can someone help me with a code that resets Wemos D1 Mini if connection to blynk is lost?

Please post your sketch before we can help you.

1 Like

Why do you need the sketch? I do not need help to implement the code. I just need and example code that gives me an idea.

He’s asking for the code for a couple of reasons:

  1. To fully understand what you are doing and why your device doesn’t reconnect to Blynk (there may be issues with existing code);
  2. To assess how best to modify your code to check for Blynk connection and re-connect if required;
  3. To see if you’ve done any work yourself or simply asked others to write code for you . . .

We are mostly happy to help but simply asking for others to provide a solution with no attempt yourself won’t get very far.

Good Luck


PS - start with Google . . .

1 Like

You don’t normally need to do a reset, a re-connect will often be the best approach.
However, it depends on whether you are using Blynk.begin or Blynk.config and Blynk.connect as to what the best solution is.
Also, your disconnections may actually be caused by badly written code, so best to fix the cause rather than the symptom.

So, seeing your code would be a good starting point.


1 Like

That’s right Bill , usually we don’t need to reset MCU, but fix the cause of the trouble.

@PeteKnight, @Bill_Donnelly,
no news from @Bjerknez
I think Google had been better than us :joy::joy:

1 Like

I think his sketch is top secret :rofl:



Cia :joy::joy:

1 Like

Sorry for slow response. It have been an tough family situation.

But here is my code:

#include "OneWire.h" 
#include "DallasTemperature.h"

OneWire oneWire(22);
DallasTemperature tempSensor(&oneWire);

#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>

int relay = 13;
int magnet = 5;
int magnetValue;

//WiFi and token data------------------------------------------
char auth[] = "*****";
char ssid[] = "'''''";
char pass[] = "'''''";

BlynkTimer timer;

void setup(){
  Blynk.begin(auth, ssid, pass);
  digitalWrite(relay, HIGH);
  pinMode(relay, OUTPUT);
  timer.setInterval(10000L, TempReading);
  timer.setInterval(1000L, magnetSensor);

void magnetSensor(){
  magnetValue = digitalRead(magnet);
  if(magnetValue == 1){
    digitalWrite(relay, LOW);
    digitalWrite(relay, HIGH);

void TempReading(){
  Blynk.virtualWrite(V3, tempSensor.getTempCByIndex(0));

void loop() {;;

Sorry to hear that

The Wemos D1 Mini is an ESP8266 based board, but you’re using ESP32 libraries. Are you sure about your board type?


I’m really sorry. Yes, it’s an ESP32 i use, not Wemos D1 Mini.

I adjusted your title (type of MCU can matter :slight_smile: ).

Here is one of my examples of reconnection code, the basics should work with any MCU)… And there are many others in this forum, just search for reconnection, etc.

As for actually resetting the MCU… the command ESP.restart(); is (in theory) the command to do a soft reset on ESP boards… but may not always be reliable, at least on my ESP32 projects. A very quick Google refresher shows others agree… Google for more info.

Thanks for detailed answer :slight_smile:

Can you please tell my what part of code that controls the reconnect?

It’s the Blynk.connect command that re-connects if the connection is lost. But, this example isn’t using the Blynk.begin command you are using, so you can’t simply copy and paste part of the code into your sketch and expect it to work.
You need to adopt the same principal of establishing your own WiFi connection, configuring your Blynk settings, then connecting to Blynk.


I see.

But what is the differnce between blynk.begin() and

Everything… they are different commands that do different things. Blynk.begin() runs once and started the whole show. needs to be ran as often as possible, thus in the void loop(), as it is part of the library that does the monitoring and housekeeping.

The difference you might be referring too is Blynk.begin() and Blynk.config()… a subject that is referenced many times in this forum already, so to keep it short, the former handles everything and will not move past until there is a connection to the server, the latter leaves the connectivity up to you and only sets up the Blynk link, which is then connected via the Blynk.connect() command.

When learning something new, don’t forget to read the documentation :wink:

You have right.

So you mean that i can try to use blynk.config() instead of in my loop to call blynk.connect() in an interval of for example 10 minutes? Will that work?

I have an another microcontroller connected to the same network (Wemos D1 Mini (ESP32)) and when network is down, this unit reconnects when network comes back. But not my ESP32 with mutch simpler code. It’s just an temperature sensor (DS18B20) that sends the temperature to the Blynk app every 10 secconds.

The router is 2 meter away from the controller.

I just can’t understand why i have problems with microcontrollers dropping out. This is the first time i have that problem.

Look at the example I already gave above… basically, after WiFi, Blynk setup and initial Blynk connection, this keeps everything Blynk related running, when connected to the server as normal…

if (Blynk.connected()) {  // If connected run as normal;
  } else {
// The loop will keep doing stuff whilst occasionally attempting reconnection with whatever code you put here

Then with an ELSE addition, one can experiment with the best way of carrying on with the code and occasionally attempting a connection to Blynk Server. But don’t keep repeatedly hammering at the reconnection, as that process itself can take some time away from running the regular code

Yup, I also have an ESP32 (TTGO32+OLED+18650) running 24/7 with temp, humidity & barometer sensor, running a Nexion display, plus its own OLED and also feeding data back to a static wall display.

Sometimes it runs for days without any issues, other times it seems to stall out a few times a day, requiring a manual reset… no apparent rhyme or reason.

Now, mine is NOT running Blynk for its project (although it was at one time) and even then it still has the same flakiness… so it might just be an ESP32 core thing?? I can’t recall the core version I had when last flashing it.

I also have some rudimentary “reboot when locked up” code, but it doesn’t seem to work, or perhaps the aforementioned ESP.restart(); doesn’t to do the job… or at least not always?? I never got around to displaying any diagnostics, or uptime counters… One day :slight_smile:

Meanwhile, it is one of life’s mysteries I guess :thinking: