Introduction
The ESP32 stands out as a versatile and powerful microcontroller, benefiting from the convenience of Over-The-Air (OTA) updates. This blog explores the ESP32 OTA Web Updater, a tool designed to simplify the process of wirelessly updating code on ESP32 devices, concretely on a ESP32 PLC.
We will delve into the functionalities that make the ESP32 OTA Web Updater valuable for developers and enthusiasts. From the initial code upload via the serial port to the creation of an embedded web server accessible through a web browser, each aspect contributes to a user-friendly experience on loading a code OTA to an ESP32 PLC.
Requirements
To successfully follow this tutorial, the following prerequisites must be met:
- ESP32 PLC: Be in possesion of any model of our ESP32 PLC device is powered within the specified range of 12-24V.
- Arduino IDE: Have Arduino IDE in your computer with the industrialshields-esp-32 board package integrated into the IDE. Follow this tutorial to install it in case you do not have it.
- WiFi Connection: Ensure a stable and accessible WiFi network is available for the ESP32 PLC to connect and communicate with other devices.
ESP32 OTA Web Updater
The ESP32 OTA Web Updater facilitates the wireless code update process for ESP32 devices. Here are the key functionalities:
Initial Code Upload via Serial Port: The first version of the code needs to be uploaded to the ESP32 through the serial port. This establishes the groundwork for future Over-The-Air updates.
Web Server Creation: An embedded web server within the ESP32 becomes accessible through a web browser. This server acts as the interface for remote code uploads, ensuring a straightforward and user-friendly experience.
Core Code Inclusion: Each code intended for Over-The-Air updates should incorporate the core code. This essential snippet allows the ESP32 to rewrite and reprogram itself through OTA. In case of not uploading this core code with the main sketch results in the loss of access to the web server for future updates.
Here is the core code needed for every upload to the ESP32 PLC:
OTA_ESP32.ino
DownloadWiFi Credentials Configuration: Within the code, placeholders for WiFi credentials are provided. You must replace YourSSID and YourPassword with your actual WiFi network details. This step enables the ESP32 to connect to the specified WiFi network.
/* Write your SSID and Password to access to your Wifi */
const char* ssid = "YourSSID";
const char* password = "YourPassword";
Loading the Code: After configuring the WiFi credentials and integrating the core code, load the sketch onto the ESP32. This action initiates the device's connection to the designated WiFi network and activates the web server for subsequent Over-The-Air updates. Note that you can load the core code without any other code in case you want to set the ESP32 ready for future OTA uploads.
Updating the Code
To initiate the code update process, open a web browser and enter either the ESP32's IP address (visible in the serial monitor upon connecting to WiFi) or use the local DNS esp32.local as specified in the code, utilizing mDNS. This action will display a login page, as shown in the figure below.
Enter the credentials of your choice. By default, the user ID is admin, and the password is admin. After successful login, you'll access the page for uploading new code to the ESP32 PLC using Over-The-Air (OTA), as depicted in the following figure.
Implementing the new code
For future code updates, it's crucial to reprogram the ESP32 PLC with the initial code. Additionally, insert your new code where indicated in the core code. Include your setup code below /* Additional set up code here */ and your loop code below /* Additional loop code here */.
For example, a simple program to blink a LED would be:
/* Code from the core code */
void setup(){
/* Set up code from the core code */
/* Additional set up code here */
pinMode(Q0_0, OUTPUT);
}
void loop() {
/* Loop code from the core code */
/* Additional loop code here */
digitalWrite(Q0_0, HIGH);
delay(1000);
digitalWrite(Q0_0, LOW);
delay(1000);
}
Generating a .bin file
To upload the code to the ESP32 using OTA, it needs to be in binary format. Follow these steps:
- Navigate to Sketch in the Arduino IDE.
- Click on Export compiled Binary, as illustrated in the following figure.
This action generates a .bin file containing the compiled code, which is needed for the OTA update process.
Uploading the new code Over The Air
Once the .bin file is generated, return to the OTA webpage. Use the file input field to select the generated .bin file, and click on the "Update" button. The progress bar will indicate the status of the upload. During this process, the ESP32 will reboot, and the new code will be applied. Monitor the progress in the serial monitor for any update-related messages or potential errors.
With these steps, you have successfully implemented, compiled, and uploaded new code to your ESP32 PLC over the air, providing a convenient and efficient way to keep your device up-to-date.
Web Interface for Programming ESP32 PLC Over The Air (OTA)