ESP32 and ESP8266: Programming in the Air

About: Do you like technology? Follow my channel on Youtube and my Blog. In them I put videos every week of microcontrollers, arduinos, networks, among other subjects. New tutorials on Tuesdays and Fridays.

Is it possible to program an ESP through the air? Yes. That's the answer. And what does that mean? That you will no longer record the microcontroller over the USB cable, but, yes, through WiFi. This possibility is legal to make our life easier, because it allows us to update the software of a device that is fixed on a roof, for example , without having to remove it from the place.

So in this article and in the video below I'm going to look at a basic example of OTA programming (Over The Air) in ESP32 and also show how to adapt the same project to ESP8266 with a small change in code. This program was developed in the C language of the Arduino IDE.

Step 1: Resources Used

  • 2 LEDs
  • 2 220 ohm resistors
  • ESP32
  • Protoboard

Step 2: OTA Code

Organization of code and mandatory codes

You need to have a piece of code inside Setup, plus a bunch of functions I called OTA, plus a little bit of code inside the Loop. This is because, when you record over the air, you have to put a code inside the ESP that allows this device to communicate over the IP network with the Arduino IDE. So, instead of you choosing a serial COM (communication port) you will choose an IP.

This code, therefore, must be within the ESP. The part I deal with here first displays three functions that must be within Setup. In the sequence I place other four functions involving OTA that are part of the source code coming through the air.

Step 3: Setup - ESP32

In this part of the code I bring the define. The OTA code brings pieces that you need to put inside your program and here, I deal with some mandatory parts. In this step we also work the LEDs, and the red will confirm when the device is updated by the air, for example.

#include <WiFi.h> //lib to the Wifi configuration
#include <ArduinoOTA.h> //lib to the ArduinoOTA functions #include <ESPmDNS.h> //lib to the network communication #include <WiFiUdp.h> //lib to the network communication

#define greenLed 23 //this led informs the ESP is connected //#define redLed 22 //this led informs the ESP was updated

const char* ssid = "robotica"; //network ssid const char* password = "12345678"; //network password

void setup()

Step 4: Setup - ESP8266

Here I show the parts of the source code of ESP32 that should be modified for application in ESP8266. I remember, however, that I leave the two versions available for download at the end of this article.

#include <ESP8266WiFi.h> //lib to the Wifi configuration
#include <ESP8266WiFiMulti.h> //lib to the Wifi configuration #include <ArduinoOTA.h> //lib to the ArduinoOTA functions

#define greenLed 23 //this led informs the ESP is connected //#define redLed 22 //this led informs the ESP was updated

ESP8266WiFiMulti wifiMulti;

const char* ssid = "robotica"; //network ssid const char* password = "12345678"; //network password

void setup()

Step 5: Continued - Setup ESP32

We continue here with the OTA source code with definitions that are standard.

//while the wifi not will be connectedm wait for it
while (WiFi.waitForConnectResult() != WL_CONNECTED) { //if a connection failure occurred, then restart the ESP Serial.println("Connection Failed! Rebooting..."); delay(5000); ESP.restart(); }

//the default port is 3232 (optional) //ArduinoOTA.setPort(3232);

//set the network host name (optional) ArduinoOTA.setHostname("myesp32");

//set the network password (optional) ArduinoOTA.setPassword("password123");

//it is possible to define a md5 hash cryptographic to the password using the following OTA function //md5 example for the string "admin" = 21232f297a57a5a743894a0e4a801fc3 // ArduinoOTA.setPasswordHash("21232f297a57a5a743894a0e4a801fc3");

Step 6: Setup - ArduinoOTA Instance Configuration

I made some changes to the original source code to make it easier to read, which can be well understood at this stage.

//configures what will be executed when ArduinoOTA starts
ArduinoOTA.onStart( startOTA ); //startOTA is a function created to simplificate the code

//configures what will be executed when ArduinoOTA ends ArduinoOTA.onEnd( endOTA ); //endOTA is a function created to simplificate the code

//configures what will be executed when the sketches is burning in the esp ArduinoOTA.onProgress( progressOTA ); //progressOTA is a function created to simplificate the code

//configures what will be executed when ArduinoOTA finds an error ArduinoOTA.onError( errorOTA );//errorOTA is a function created to simplificate the code //initializes ArduinoOTA ArduinoOTA.begin();

//prints the ip address used by ESP Serial.println("Ready"); Serial.print("IP address: "); Serial.println(WiFi.localIP()); digitalWrite(greenLed,HIGH); }

Step 7: Upload Stages Display Functions (start, Progress, and End)

I bring all the functions mentioned again.

//functions of uploads stages (start, progress, end and error)
void startOTA() { String type; //if the update is burning in external flash memory, then show "flash" if (ArduinoOTA.getCommand() == U_FLASH) type = "flash"; else //if the update is on the internal memory (file system),then show "filesystem" type = "filesystem"; // U_SPIFFS

//print message and the burn type Serial.println("Start updating " + type);

digitalWrite(greenLed,HIGH); delay(300); digitalWrite(greenLed,LOW); delay(300); digitalWrite(greenLed,HIGH); delay(300); digitalWrite(greenLed,LOW); delay(300); }

//just show message "End" void endOTA() { Serial.println("\nEnd"); }

//shows the progress in porcent void progressOTA(unsigned int progress, unsigned int total) { Serial.printf("Progress: %u%%\r", (progress / (total / 100))); }

Step 8: Display Stages of Upload (error)

The same I did in this part of the code that deals with possible error.

//if an error occurred, shows especificaly the error type
void errorOTA(ota_error_t error) { Serial.printf("Error[%u]: ", error); if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed"); else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed"); else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed"); else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed"); else if (error == OTA_END_ERROR) Serial.println("End Failed"); }

Step 9: Loop

Finally, I show our program here.

void loop()
{ //Handle is an abstract reference to a resource. That references variables on the blocks of memory ArduinoOTA.handle();

//update code /* digitalWrite(redLed,HIGH); delay(1000); digitalWrite(redLed,LOW); delay(1000); */ }

Step 10: Network Port Selection in the Arduino IDE

After the first recording made by the Serial port, you must select the network port as shown below. For this it is essential that the computer is connected to the same network as the ESP.

Step 11: Mounting the ESP32

In our assembly we put two resistors and two LEDs. The design is quite simple and just follow the image to play it. In our example, the green LED indicates that the ESP is connected, while the red LED is used to view the update made by OTA, as can be seen in the video, in our demo.

Step 12: Files

Share

    Recommendations

    • PCB Contest

      PCB Contest
    • Make it Glow Contest 2018

      Make it Glow Contest 2018
    • Big and Small Contest

      Big and Small Contest

    3 Discussions

    0
    None
    BlaineS1

    Question 3 months ago

    You mentioned the INOs were downloadable at the end of this Instructable. I'm unable to find the download links. Am I just missing them?

    0
    None
    Eric Brouwer

    8 months ago

    Thank you for sharing.

    I will try to incorporate the OTA code into my existing code for my IoT Timers

    Regards

    Eric

    0
    None
    Droxz

    8 months ago

    Nice instructable, thanks for sharing! :)