Over The Air Firmware updates

Updating ESP8266 firmware over WiFi is the coolest thing *ever*.  I recently got it working on an ESP-01S board, and it was a breeze.  There is 1M of flash memory on my ESP-01S, and that might be important.  We need at least twice the sketch size space in order for the code to work.  There is something very satisfying about sending a program off over the home WiFi to an ESP8266 board stashed in another room!

My first foray into this area was not successful.  I read about using the ArduinoOTA, but to cut a long story short, the version of the ESP boards I am using in the Arduino IDE (2.3.0) was not compatible with OTA, and when I updated or rolled back the versions to get an ArduinoOTA compatible version I couldn't compile my code.  I decided therefore to compile my code as normal and then upload using the python script directly that otherwise works "behind the scenes", namely espota.py

So the steps are very simple:
1) Add the following to the code.  In setup(), after WiFi connection is established add:
  ArduinoOTA.onStart([]() {
    Serial.println("Start");
  });
  ArduinoOTA.onEnd([]() {
    Serial.println("\nEnd");
  });
  ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
    Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
  });
  ArduinoOTA.onError([](ota_error_t error) {
    Serial.printf("Error[%u]: ", error);
    if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
    else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
    else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
    else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
    else if (error == OTA_END_ERROR) Serial.println("End Failed");
  });
  ArduinoOTA.begin();
  Serial.println("Ready");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
2) In loop() we need add a call to ArduinoOTA handler, so that when we want to program over WiFi it is correctly handled:
    ArduinoOTA.handle();
In terms of code, that's it! I told you it was easy.  Note if we upload a sketch that doesn't have this OTA (Over The Air) code, then of course we won't subsequently be able to upload over WiFi - we'll be back to boring old serial updates.

3) Now we need to generate a binary file.  Press Ctrl+Alt+S, or Sketch->Export compiled binary.  The Arduino IDE will then compile your code into a .BIN file.


Below it is the option to "Show Sketch Folder" (Ctrl+K).  Clicking on this will open the sketch folder where you will now see your .BIN file.

4) Now we need to use espota.py, the latest version of which can be downloaded from here:


I'm using Python 2.7, and using a .BAT file in the same folder, or the cmd line in Windows, run:

c:\python27\python.exe espota.py -i 192.168.0.19 -f C:\Users\Duncan\Documents\Arduino\fauxmoESP_Basic\fauxmoESP_Basic.cpp.generic.bin 

Substituting your IP address and BIN file path will then upload the sketch to your ESP8266.  Ensure you have WiFi connection established before running.  The sketch will upload quite quickly, then the ESP8266 will reboot.  The python script finishes, but the process on the ESP8266 isn't finished. WAIT for it to boot up again, it can take up to 10 seconds or so.  DO NOT manually reboot otherwise the uploading process will get scrambled and you'll have to upload again using the serial port.

And that's it! When the ESP8266 reboots of it's own accord, it will be running the uploaded code!  There is something uniquely satisfying about programming chips over a distance.

5) Finally, I set a hostname for the ESP8266 (it is the IRegg!), so that I could check my router table to find the IP address.  This will be useful in case the IP address changes, and avoids me having to keep track of lots of IP addresses as I add ESP8266 devices to my home network.  Simply add:
  WiFi.hostname("IRegg");
before WiFi.begin.  NB: I tried using "ArduinoOTA.setHostname("IRegg");" but this didn't work!

Happy OTAing! :)

Comments

Popular posts from this blog

Getting started with the Pro Micro Arduino Board

Arduino and Raspberry Pi serial communciation