In IoT products importance of OTA update is known to everyone. In this article we will discuss about how to achieve this using ESPIDF. ESP32 comes with OTA functionality. We need to enable OTA into the chip and then we can easily flash our device with new functionalities without even connecting it with our system. We can update a new version or add a new function to our device by just clicking a button on our system.
To make your ESP32 board OTA enabled we need to look into the partition table. In partition table we need to do three app partitions: factory, OTA_0 and OTA_1. To do this we will enable `CONFIG_PARTITION_TABLE_TWO_OTA` option in menuconfig. The OTA code which we flash in the beginning by connecting the board through data cable will be uploaded in factory_app partition which will then go to the bootloader. Next time when we will do over the air update without connecting the board wirelessly, the code will be stored in ota_0 partition. It is checked in that partition before uploading it to bootloader. Once check is done it will be transferred to ota_1 partition and then to the bootloader. For better understanding kindly refer the figure below.
When we will upload a code wirelessly we have to keep in mind that our new uploaded code must contain OTA enable code along with new update. If we will not add OTA code to our new code then our device will loose OTA functionality and we will never be able to upload a code wirelessly.
What is OTA?
What is OTA?Over-the-air is a method of transferring data or doing any transaction without making any physical connection. If a device is connected to internet we can send updates wirelessly using OTA functionality. OTA functionality is very useful in case of no physical access to the device. It reduces the time and efforts for updating each device at the time of maintenance. We need to add an extra code for OTA every time we update, so that we can use OTA in the next upload.
Steps to implement OTA in ESP32 using ESPIDF
Steps to implement OTA in ESP32 using ESPIDFWe already have a examples available online for numerous functionalities supported by ESP32. I have used one of the example to enable OTA in my ESP32 board.
Step1: Run idf.py menuconfig command on your terminal. Set your ssid id and password in Example Connection Configuration. Then go to Example configuration and set the IP adress of your system there along with the bin file of the code which you have to update wirelessly.
In my case I have made a file test.c in which I am doing updation. I have used test.bin in menuconfig as shown below
Likewise you can use bin file of your code which you want to update using OTA functionality. In the new code kindly add v=bin file of next code which you will update in future using same path flow. In my case in test.c I have added a new bin file blink.bin which I will update next.
Step2: Build your project using idf.py build command
Step3: Erase any leftover data in the ota_data partition by running the command idf.py erase_flash
Step4: Now flash your OTA code using idf.py flash command
Below I am attaching one screenshot of my terminal on flashing for the first time when my board is connected with my system.
In the above image you can see a circled portion, it represents the partition table. You can see clearly there are three partitions namely factory, ota_0 and ota_1. Since I have flashed my OTA code now I can send the updates wirelessly.
Steps to send updates over http server
Steps to send updates over http serverStep1: Build you project which you want to upload next by using idf.py build command
Step2: Run cd build command after idf.py build so that you can enter build folder
Step3: Now open http server inside build folder. Now you can connect your esp32 board with power bank and start server to upload wirelessly. You can also keep your board connected to the system if you want to monitor.
My command terminal on monitoring after uploading a code wirelessly is shown below
\From the above image you can see first my ESP32 got connected to my wifi and then Starting OTA example. After that it has written code over partition subtype and connection is closed. When all this will be completed successfully you will be able to see your new wirelessly uploaded code output.
Similarly, you can follow the same steps mentioned above and upload the new code wirelessly.
You can also check version of your software in your code and can update version if there is older version on your device. You can send version update over the air.
#OverTheAirUpdate #EmbeddedSystem #Software #InternetOfThings
Comments
Please log in or sign up to comment.