DEV Community

zhuyue
zhuyue

Posted on

How to implement OTA function for ESP8266 with SDK 2.xx(script programmable controller)

As an Internet of Things device that supports remote connection to the Internet, local or remote OTA upgrade is also an important function of the script programming controller, because there is no guarantee that the relay is completely free of bugs, found in the use of bugs, if there is no OTA upgrade interface, it is difficult to allow customers to upgrade the firmware through the serial port, the controller can only be upgraded back to the factory, the cost is high, the impact of the big.

In addition, the customer may have some personalized functions that can not be achieved through the Chinese programming, and need to upgrade the controller firmware to achieve.

These two situations need to be solved through the local or remote OTA function.

These days spent a little time to complete this function, now summarized as follows:
(1) The FLASH of ESP8266 can store two firmwares with the name user1.bin to user2.bin, and the addresses of the two firmwares are determined by the boot program boot.bin stored at address 0x0000, and the storage addresses are different for different flash capacities.
For example, ESP8266-01S with Flash capacity of 1Mbyte, the storage address is 0x01000 and 0x81000 respectively, while ESP8266-07S with FLASH capacity of 4Mbyte, the storage address is 0x01000 and 0x101000 respectively.

(2) through the function system_upgrade_userbin_check return value to obtain the current program is running user1.bin or user2.bin, firmware upgrade, to another firmware storage area to write the complete firmware, for example, if the current run is stored in the 0x1000 of the user1.bin, then to address 0x81000(ESP8266-01S) or 0x101000(EPS8266-07S) to write the firmware user2.bin, after writing, and then through the function system_upgrade_flag_set(0x02) to inform the SDK write is complete, the SDK set the system parameters, so that when the ESP8266 startup boot to another firmware.

3) The official recommended practice is to download the firmware to the cloud server to the memory via http protocol, I did not use this program, mainly because of two considerations, firstly, whether this program has been rigorously verified, and whether there is sufficient consideration of data validation to avoid upgrading to brick, I doubt it. Secondly, this program requires the controller to be able to connect to an external network to download firmware from a server, which is not easy to do.

I am currently using delphi to develop a set of firmware upgrade software, the software as a TCP client to connect directly to the controller, or connect to the cloud server's TCP gateway and then transferred to the controller, and then according to the 512 bytes for a frame of data to push the firmware to the controller.

Next this set of logic can be ported to the server, the customer can open the web page through the browser to push the firmware to the controller.

4) The integrity of the data is fully verified, the first frame of data to do crc32 checksum, the firmware is written to the flash and then read out to compare with the written value, and at the same time calculate the value of crc32, and the upper computer to send the value of crc32 compared to the read and write data are the same, and also the same value of the crc, it is considered that the frame of data is written successfully;
At the same time, the crc32 value of each frame of data and then further do crc32 value, to get a total crc32 value, compared with the total crc32 value sent by the host computer, the value is the same before calling the function system_upgrade_flag_set(0x02) to allow the SDK to switch to boot firmware.

(5) Write to two different firmware areas of the firmware user1.bin, user2.bin is not the same, for RTOS sdk development, you need to modify the value of the Makefile variable APP to generate user1.bin, user2.bin, respectively, the host computer according to the controller to return to the current run of the firmware of the storage area, select another storage area of the firmware sent to the controller. The host computer selects the firmware in the other storage area according to the storage area of the currently running firmware returned by the controller.

If the same firmware is used in both storage areas, the ESP8266 will not be able to start normally.

Boot log due to wrong configuration

PC tool written by delphi used to OTA update

Top comments (0)