ESP32 Firmware Update via Cloud
This guide will include deploying the server application on Heroku and preparing the ESP32 to check for and download firmware updates.
Setting Up the Heroku Application
Prerequisites:
- Heroku account
- Git and Python installed on your computer
Steps:
1. Create a New Project Directory
- ‘mkdir my-heroku-app’ ,‘cd my-heroku-app’
- Initialize a Git repository: ‘git init’
2. Setup Python Virtual Environment
- ‘python -m venv venv’
- Activate it: ‘source venv/bin/activate’ (On Windows: ‘venv\Scripts\activate’)
3. Create ‘requirements.txt’
Flask==2.2.2
Werkzeug==2.2.2
4. Create the Flask Application (‘app.py’)
- Use the provided code.
import os
from flask import Flask, send_from_directory, request
app = Flask(__name__)
def get_latest_version(serial_id):
# Replace this with logic to determine the latest version
return "1.0.1"
@app.route('/check-update/<serial_id>/<current_version>')
def check_update(serial_id, current_version):
latest_version = get_latest_version(serial_id)
if current_version != latest_version:
return latest_version
else:
return "No update required", 204
@app.route('/get-firmware/<serial_id>/<version>')
def get_firmware(serial_id, version):
firmware_path = f"firmwares/{serial_id}/{version}/firmware.bin"
try:
return send_from_directory(directory='.', path=firmware_path, as_attachment=True)
except FileNotFoundError:
return "Firmware not found", 404
if __name__ == '__main__':
port = int(os.environ.get('PORT', 5000))
app.run(host='0.0.0.0', port=port)
- Make sure to replace ‘firmwares/{serial_id}/{version}/firmware.bin’ with the correct path where your firmware files will be stored.
5. Create a ‘Procfile’
- This file tells Heroku how to run your application.
web: python app.py
6. Deploying to Heroku
- Commit your changes: ‘git add . && git commit -m “Initial commit”’
- Log in to Heroku and create a new app.
- Connect your GitHub repository or set up Heroku CLI for deployment.
- Push to Heroku: ‘git push heroku main’
Preparing the ESP32 Firmware
Requirements:
- Arduino IDE or other ESP32-compatible IDE
- ESP32 board
Firmware Preparation:
- Write the ESP32 Code
#include <HTTPClient.h>
#include <WiFi.h>
#include <ESP32httpUpdate.h>
const String currentFirmwareVersion = "1.0.1";
String serialId = "123412fs12";
void checkForFirmwareVersionUpdate() {
HTTPClient http;
String versionUrl = "https://your-heroku-app-name.herokuapp.com/check-update/" + serialId + "/" + currentFirmwareVersion;
http.begin(versionUrl);
int httpCode = http.GET();
if (httpCode == HTTP_CODE_OK) {
String newVersion = http.getString();
if (newVersion != currentFirmwareVersion) {
updateFirmware(newVersion);
}
}
http.end();
}
void updateFirmware(const String &newVersion) {
String updateUrl = "https://your-heroku-app-name.herokuapp.com/get-firmware/" + serialId + "/" + newVersion;
ESPhttpUpdate.update(updateUrl);
}
void setup() {
// Initialize WiFi, etc.
}
void loop() {
// Regular tasks
checkForFirmwareVersionUpdate();
// Add delay or non-blocking mechanism
}
- The code should periodically check for updates by calling the ‘/check-update/’ endpoint.
- If an update is available, it should download the firmware from the ‘/get-firmware/’ endpoint.
2. Export the Firmware as ‘.bin’ File
- From your IDE, compile and export the firmware.
Managing Firmware Files
1. Adding Firmware to Your Project
- Place the ‘.bin’ file in the ‘firmwares/{serial_id}/{version}/’ directory in your project.
- For example, ‘firmwares/12345678/1.0.1/firmware.bin’
2. Pushing Updates to Heroku
- After adding new firmware files to your project directory, commit the changes.
git add .
git commit -m "Add firmware version 1.0.2"
git push heroku main
ESP32 Update Process
- ESP32 devices will check the ‘/check-update/’ endpoint periodically to see if a new firmware version is available.
- If a new version is available, it will download it from the ‘/get-firmware/’ endpoint and proceed with the update.
Now after the setup, if you can update the firmware of the esp32 through heroku application.
Contact us for any IoT prototype
Protonest for more details.
Protonest specializes in transforming IoT ideas into reality. We offer prototyping services from concept to completion. Our commitment ensures that your visionary IoT concepts become tangible, innovative, and advanced prototypes.
Our Website: https://www.protonest.co/
Email: udara@protonest.co
If you enjoyed this article and would like to show some support, consider buying me a coffee on Ko-Fi.
Your support not only helps me keep creating content like this but also fuels me! ☕️📚
Thank you for being an amazing reader!
Here’s my Ko-Fi link: https://ko-fi.com/udarakumarasena
Cheers!
Have a nice day