{"id":814,"date":"2023-11-15T17:14:58","date_gmt":"2023-11-15T16:14:58","guid":{"rendered":"https:\/\/www.hilltop-cottage.info\/blogs\/adam\/?p=814"},"modified":"2023-11-15T17:15:41","modified_gmt":"2023-11-15T16:15:41","slug":"uploading-esp8266-or-esp32-firmware-ota-from-android","status":"publish","type":"post","link":"https:\/\/www.hilltop-cottage.info\/blogs\/adam\/uploading-esp8266-or-esp32-firmware-ota-from-android\/","title":{"rendered":"Uploading ESP8266 or ESP32 Firmware OTA from Android"},"content":{"rendered":"<p>Using OTA firmware loading is rather useful for IoT devices once they are &#8220;in the wild&#8221;. I adopted the precaution of using a hardware jumper to enable OTA because my devices spend most of their time in deep sleep to save power. The down-side of this is that I need to visit the device before each upload. Rather than wander about with a laptop, I thought that using my phone would be more convenient. Setting this up and using is fairly straight-forward but took a bit of thinking-through and desk research. So, for others interested in doing the same&#8230;<\/p>\n<h2>Termux<\/h2>\n<p>The power-house is Termux, which runs a (command-line only) linux environment on your phone. I installed the <a href=\"https:\/\/f-droid.org\/packages\/com.termux\/\">APK from f-droid<\/a>, which seems to be common practice over Google Play Store.<\/p>\n<p>Install python with:<\/p>\n<pre>pkg install python<\/pre>\n<p>Working with phone storage is not as simple as you might think; <a href=\"https:\/\/wiki.termux.com\/wiki\/Internal_and_external_storage\">see the termux wiki<\/a>. For example, I believe it is not possible to see the filesystem which you see inside Termux when the phone is connected to a PC for file transfer. My operating procedure is to copy firmware binaries into the phone&#8217;s Downloads folder and then to use the file manager on my phone to move it over to the Termux &#8220;home&#8221; as described on the wiki page.<\/p>\n<p>The second issue is that working on the phone screen is a pain! I had previously installed <a href=\"https:\/\/github.com\/Genymobile\/scrcpy\">scrcpy<\/a> for use in demoing some software and this works a treat in making life easier. Keyboard joy! This just works. Read the documentation about enabling USB debugging in developer mode.<\/p>\n<h2>Doing the OTA<\/h2>\n<p>I&#8217;m using ESP8266&#8217;s so downloaded the <a href=\"https:\/\/github.com\/esp8266\/Arduino\/blob\/master\/tools\/espota.py\">espota.py script from the esp8266\/Arduino github repository<\/a>. You just need the one file. I expect the same procedure as I describe here would also work for ESP32 users with <a href=\"https:\/\/docs.espressif.com\/projects\/esp-idf\/en\/latest\/esp32\/api-reference\/system\/ota.html#ota-tool-otatool-py\">otatool.py<\/a>.<\/p>\n<p><em>What follows is based on having created a new directory in the termux &#8220;home&#8221;, placing espota.py in that directory and then creating one directory for each kind of device, which contains a small Bash script and the firmware binary file. I am also using PlatformIO inside VSCode.<\/em><\/p>\n<p>Compiled firmware can be found in the project folder at: .pio\\build\\{env}, where {env} is the environment name in platformio.ini. Use the firmware.bin file (not the .elf file).<\/p>\n<p>Create the bash script for ease of use (I call it upload.sh), substituting xxx.xxx.xxx.xxx with the device IP address:<\/p>\n<pre>#!\/bin\/bash\r\npython ..\/espota.py -d -i xxx.xxx.xxx.xxx -f firmware.bin<\/pre>\n<p>You can either use &#8220;nano&#8221; to edit these files inside Termux or move the .sh file between your phone and PC as noted above. Check the espota documentation for options to upload filesystem or if you&#8217;ve set up authentication.<\/p>\n<p>Make it executable using:<\/p>\n<pre>chmod +x<\/pre>\n<p>This is simply run by using (in the appropriate directory):<\/p>\n<pre>.\/upload.sh<\/pre>\n<p>A nice modification if you have multiple devices of the same type is to change upload.sh to allow the IP address to be entered at run-time, in response to a prompt. All my devices are on the same subnet (192.168.1.x), so I only need enter the last component. The modified script is:<\/p>\n<pre>#!\/bin\/bash\r\nread -p \"Last part of IP address: \"\r\npython ..\/espota.py -d -i 192.168.1.$REPLY -f firmware.bin<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Using OTA firmware loading is rather useful for IoT devices once they are &#8220;in the wild&#8221;. I adopted the precaution&#8230;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30,34],"tags":[],"class_list":["post-814","post","type-post","status-publish","format-standard","hentry","category-esp8266","category-iot","post-archive"],"_links":{"self":[{"href":"https:\/\/www.hilltop-cottage.info\/blogs\/adam\/wp-json\/wp\/v2\/posts\/814","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.hilltop-cottage.info\/blogs\/adam\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hilltop-cottage.info\/blogs\/adam\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hilltop-cottage.info\/blogs\/adam\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hilltop-cottage.info\/blogs\/adam\/wp-json\/wp\/v2\/comments?post=814"}],"version-history":[{"count":2,"href":"https:\/\/www.hilltop-cottage.info\/blogs\/adam\/wp-json\/wp\/v2\/posts\/814\/revisions"}],"predecessor-version":[{"id":816,"href":"https:\/\/www.hilltop-cottage.info\/blogs\/adam\/wp-json\/wp\/v2\/posts\/814\/revisions\/816"}],"wp:attachment":[{"href":"https:\/\/www.hilltop-cottage.info\/blogs\/adam\/wp-json\/wp\/v2\/media?parent=814"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hilltop-cottage.info\/blogs\/adam\/wp-json\/wp\/v2\/categories?post=814"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hilltop-cottage.info\/blogs\/adam\/wp-json\/wp\/v2\/tags?post=814"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}