Sunday, August 27, 2023

0000 0000 1101 0000

The return of a classic - Mailbag #31

After a long-ish break, mailbags have returned.

I have a lot of parcels to go through - enjoy this mix of old and new project material.

More to come! Watch this space...



Friday, August 11, 2023

0000 0000 1100 1111

Over the Air Access Point ESP32 Programming

In 2022 I did a couple of videos and blogs about "over the air" programming of IoT devices, otherwise known as jedi master quality OMG magic!

The first video was a straight blinky on an ESP8266 where the blinking rate was changed wirelessly "over the air" or OTA. I used readily available code and part of the code involved entering my own network WiFi credentials before the resultant binary file is uploaded to the device.

The second video used an ESP32 in combination with a relay where the rate of "blinking" of the relay and an LED changed via OTA programming. I liked the idea of the relay so much (because it can control bigger loads) that I build a little prototype PCB that worked a treat!

One thing that niggled away in my mind about both of these projects is that in order to use the OTA there was a "hard coding" of WiFi credentials, and then the OTA programming is conducted via whatever network the device is using for connection.

That meant remembering to scribble out my WiFi credentials in the code before making the code public, as well as not being able to share binary files with this information embedded inside.

One possible solution is to use code that asks for the WiFi credentials via a WiFi manager before the loading - but...you still need WiFi access to the ESP32, and that might not always be possible. Also of course your WiFi credentials are still stored with the device - possible security issue?

There might also be a problem with access to the device if the WiFi drops out or simply isn't available.

For instance, sometimes on our rural property we have power but no WiFi. If we had a mission critical device (e.g. controlling watering) then not having access to it could be a bit of an issue. Running outside at 2am when it is 2 degrees celsius (36F) to manually turn off a tap is not my idea of good times.

But, recently when doing another ESP32 project (the LED Bin) I learned how to put together a web server on the ESP32 which was broadcast via it's own access point (or AP).

So I started thinking about combining the idea of OTA upgrades/coding using the device as it's own AP.

I combined what code I could find and then it needed quite a bit of tweaking. In particular, there were two important issues to be solved:

A. The existing OTA bin loading code used some "serverIndex" code snippets found live at time of execution of the program which is sourced directly from the internet as follows...

"<script src='https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js'></script>"

...but of course if the ESP32 was in AP mode then the internet (and hence the script from ajax.googleapis.com) would not be available!

After a few days of scratching about I found a project on github that looked like it could replace the online ajax.googleapis.com jquery code. It seemed to work seamlessly, but there is a downside in that it is pretty huge (148kB).

I'm still looking around for an alternative if you have any ideas to share!

The final challenge of this project therefore was to split up the code into three distinct files:

1. An OTA shell Arduino IDE sketch file (ESP32-OTA-AP.ino) which has all of the OTA magic in it, including the SSID/PASS variables for your local ESP32 access point. It also has variables for the login to the ESP32 OTA page, and an IP address variable. I decided to "cleave" off the jquery section which is found in...

2. A separate file (tjquery.h) which contains all the code needed to facilitate OTA upload, but without using the live internet version from ajax.googleapis.com, and finally...

3. A file that contains your actual project code (mymain.h) which is of the same format as a normal Arduino IDE sketch file, which is to say it has three sections as follows:

+variables +setup +loop

All of these files and a "README" is found on my github repository at the following link:

https://github.com/bovineck/ESP32-OTA-AP

B. The mDNS server didn't initiate properly until I added a line above it:

WiFi.mode(WIFI_AP);

Sadly, I cannot remember where I found this solution - I do know it was buried in some back street of Nerdville - so thank you to the solver of this problem! I also added the ability to change the IP address for the ESP32 AP, because it seemed important at the time (default was 192.168.4.1, it is now 192.168.19.63 and yes I am that old).

I haven't posted the final code/files on this blog as the best thing to do if you want to try this magic is download the zip file from github and give it a whirl.

Of course the first time you upload the code, you will need a cable! After that you can simply locate your subsequent binary files on your local drive after compilation, then upload them easily by logging into your ESP32 over the air.

Select the "bin" file (highlighted), and then hit the "Update" button on the serverIndex page as shown below.



So good - I'm really happy with the outcome, and if you have any interest in ESP32 projects then I encourage you to give it a whirl - feedback very welcome.