Remote ESP32 Firmware Upate (OTA)

2.9K217

Intro: Remote ESP32 Firmware Upate (OTA)

In this instructable we will demonstrate how to remotely update a ESP32 firmware.

  • No physical connection (means that no need to connect the ESP32 to the computer to upload the firmware).
  • No need to be in the same network (i.e. your ESP32 at your home and you at work)

STEP 1: Create a Google Firebase Storage

Create a Google Firebase console as link included on supplies


STEP 2: Upload the Code to ESP32

To implement this OTA, the first firmware (code attached) must be uploaded by cable. So, connect your ESP32 to your computer and upload it.

The following code lines must be modified for you account and project details:


const char* ssid = "YourWifiNetworkName";const char* password = "YourWifiPassword";#define API_KEY "GoogleFirebaseAPIKEY"               // Firebase: Define the API Key#define USER_EMAIL "GoogleFirebaseUserEmail"         // Firebase: Define the user Email#define USER_PASSWORD "GoogleFirebaseUserPassword"   // Firebase: Define password#define STORAGE_BUCKET_ID "GoogleFirebaseBucketID"   // Firebase: Define the Firebase storage bucket ID e.g bucket-name.appspot.com#define FIRMWARE_PATH "test/firmware.bin"            // Firebase: Define the firmware path on Firebase


STEP 3: Ok, and Now?

After successfully uploading the code to your ESP32 and restarting it, it will connect to your Wifi network and will search for the firmware file on the Firebase Storage.

If no file is present in the storage, the code will move on.

IP address: 10.92.61.230Firebase Client v4.0.0  Token info: type = id token, status = on requestToken info: type = id token, status = readyChecking for new firmware update available...New firmware update not available or download failed, Not Found.Not Found.


If a file is found, the ESP32 will download it, delete it after download and restart the device.

WiFi connectedIP address: 10.92.61.230Firebase Client v4.0.0Token info: type = id token, status = on requestToken info: type = id token, status = readyChecking for new firmware update available...New update foundDownloading firmware test/firmware.bin (1048000 bytes)Downloaded 0%Downloaded 2%Downloaded 4%Downloaded 6%Downloaded 8%Downloaded 10%...Downloaded 90%Downloaded 92%Downloaded 94%Downloaded 96%Downloaded 98%Downloaded 100%Donwload firmware completed.Delete file... okRestarting...

At this point, the new firmware will run at your device.

STEP 4: Import a New Firmware to the Storage

To provide a new firmware for your device, you shall upload to the Firebase Storage the binary file created by your compiler, usually it is named "firmware.bin".

If your are using Visual Studio with PlataformIO, the binary file can be found on the path similar to:

...\PlatformIO\Projects\WebUpdate\.pio\build\wemos_d1_mini32\firmware.bin


You shall have a storage like the picture.


11 Comments

People, please remember that this code will sit on top of your exisiting code. Otherwise it will work only one time and once the firmware is updated, your ESP wont be able to connect with firebase. If you add this code inside (or on top of) your existing code, you will able to rewrite new firmwares that support future OTAs.

For those who are facing the issue or message "Invalid HTTP method / URL ",

Please do not use gs:// in the #define STORAGE_BUCKET_ID

example : if the firebase storage path is gs://yourProject.yourDomain.appspot.com

use only "yourProject.yourDomain.appspot.com"


Hope this helps!

Thats simple!

You will have to use this code WITH your code. The good part is, this code checks for update ONLY when its booted, and not in-between. Everytime your device restarts, this code checks the firebase for updates. Increase the buffer size if the update stop abruptly. Remember, if for some reason ESP cant connect to firebase (internet issues etc), your regular code will not work because the above code tries connecting to firebase in a loop (you might want to get rid of it in that case).


Hope this helps.

Did you made this code work to check for firmware update repeatedly? This code works only 1st time when I upload it to ESP32 it deletes file from firebase storage. Next time it wont work when I upload new firmware to Firebase storage.

One moment. This code will work only once: downloadOTA, delete firebase file, restart ESP. How to make it run continuously if I want to automatically download every new firmware from firebase?

I'm facing the same issue. Did you get any solution?
Hello, i implement this it working but it takes 79% of my memory so i want to reduce it to 20% because of my code so is there any option .?
thank you
@usmodel3d, I solved this issue right now. I changed the ESP32 partition scheme. I did this:

put those lines on .ini file if using PlatformIO.

board_build.flash_mode = qio
board_build.f_flash = 80000000L
board_build.partitions = default_8MB.csv

If tou are using Arduino IDE, maybe it helps you:

https://iotespresso.com/how-to-set-partitions-in-esp32/
I just find out it after reading your answer. Any solution?