$USD
  • EUR€
  • £GBP
  • $USD
TUTORIALS ESP8266

ESP8266 Arduino Tutorial: Software restart

DFRobot Jan 09 2018 687

In this tutorial we will check how to perform a software restart on the ESP8266 using the Arduino core. This tutorial was tested on a DFRobot’s ESP8266 FireBeetle board.

 

Introduction

In this esp8266 tutorial we will check how to perform a software restart on the ESP8266 using the Arduino core.

To illustrate the functionality, we will create a simple program that decrements a counter each second and restarts the ESP8266 when it reaches zero.

If you are looking for a simular tutorial for the Arduino core running on the ESP32, please consult this post.

This tutorial was tested on a DFRobot’s ESP8266 FireBeetle board.

The code

We will start our code by declaring a global variable which will be our counter. I will initialize it with the value 10, but you may use other value. We will later access this variable on the Arduino main loop and decrement it at each iteration.

int cnt=10;

Moving on to the Arduino setup function, we will open a serial connection by calling the begin method on the Serial object. Note that this method receives as input the baud rate for the connection, which we will later need to use on the Arduino IDE serial monitor, to get the output of the program.

We will only do this method call on the whole setup function, which can be seen below.

void setup() {
  Serial.begin(115200);
}

On the Arduino main loop, we will start by printing the current counter value.

Serial.println(cnt);

Next we will check if the counter has already reached the value zero. If it does, we will print a simple informative message and restart the device.

To restart the ESP8266, we need to call the restart method on the ESP extern variable, which is an object of class EspClass (header file available here). This ESP object can be accessed in our code without the need for any .h file include.

if(cnt==0){
    Serial.println("Reset..");
    ESP.restart();
}

Note that there’s actually a reset method also available on the ESP object. However, this method call is a hard reset that can leave some ESP8266 registers in their old state, which may lead to problems [1]. On the other hand, the restart method we have used indicates to the ESP8266 SDK to reboot, which is cleaner and is the recommended method [1].

Under the hood, these two functions call two different low level functions, as can be seen by the implementation of the EspClass.

Moving on to the remaining code, after the previous conditional block, we will decrement the global counter and perform a one second delay. The final complete source code can be seen below.

int cnt=10;
 
void setup() {
  Serial.begin(115200);
}
 
void loop() {
 
  Serial.println(cnt);
 
  if(cnt==0){
    Serial.println("Reset..");
    ESP.restart();
  }
 
  cnt--;
  delay(1000);
}

Testing the code

To test the code, we just need to upload the previous Arduino sketch to the ESP8266 and open the Arduino IDE serial monitor. Remember that we need to use the same baud reate we have passed as input to the begin method of the Serial object.

You should get an output similar to figure 1, which shows the counter decrementing and the ESP8266 restarting when it reaches zero.

Figure 1 – Output of the program for restarting the ESP8266.

REVIEW