espFOTA: An OTA (Over-the-Air) update library for ESP8266, ESP32, and other devices supporting MicroPython.
espFOTA is an Over-the-Air (OTA) update library for MicroPython devices such as ESP8266, ESP32, and other supported hardware. This library simplifies the process of updating firmware over a Wi-Fi connection, ensuring your devices are always up-to-date.
- Easy configuration for OTA updates.
- Automatic Wi-Fi connection management.
- Continuous update checks with automatic application of updates.
- Network status indicator using GPIO pin.
Py-file
import mip
mip.install('github:raghulrajg/espFOTA/espFOTA.py')
To install using mpremote
mpremote mip install github:raghulrajg/espFOTA
To install directly using a WIFI capable board
mip.install("github:raghulrajg/espFOTA")
If you want to install library examples:
mpremote mip install github:raghulrajg/espFOTA/examples.json
To install directly using a WIFI capable board
mip.install("github:raghulrajg/espFOTA/examples.json")
On supported GNU/Linux systems like the Raspberry Pi, you can install the driver locally from PyPI <https://pypi.org/project/micropython-espFOTA/>
_.
To install for current user:
pip3 install micropython-espFOTA
To install system-wide (this may be required in some cases):
sudo pip3 install micropython-espFOTA
To install in a virtual environment in your current project:
mkdir project-name && cd project-name
python3 -m venv .venv
source .env/bin/activate
pip3 install micropython-espFOTA
Also see examples.
-
Wi-Fi and Server Configuration:
-
SSID
: Your Wi-Fi network name. -
Password
: Your Wi-Fi network password. -
host
: The server endpoint where the update file is hosted.
-
-
Example Program:
Save the following code in a file named
main.py
on your MicroPython device:import espFOTA # Avoid the GPIO pin number 2 because of predefine pin (Network status indicator) # Server connection config host = "package.xyz.com/newversion" # WiFi Network connection config SSID = "YOUR_APN_NAME" Password = "YOUR_APN_PASSWORD" OTAUpdate = espFOTA.espFOTA(SSID, Password, host) def loop(): while True: # Put your code here OTAUpdate.run() if __name__ == '__main__': loop()
- SSID: Replace with your Wi-Fi network name.
- Password: Replace with your Wi-Fi network password.
- host: Replace with your server's URL endpoint where the OTA update file is hosted.
- The device uses GPIO pin 2 as a network status indicator.
- If the Wi-Fi network is not connected, the LED on GPIO pin 2 will turn on.
- Once the Wi-Fi is connected, the LED will turn off.
- The
OTAUpdate.run()
method continuously checks for updates from the specified host. - If an update is found, it is automatically downloaded and applied.
- The device will restart to apply the new firmware.
To serve the OTA update file, you can set up a simple Node.js server. Here is an example:
const http = require('http');
const url1 = require('url');
const fs = require('fs');
const server1 = http.createServer((req, res) => {
const parsedUrl = url1.parse(req.url, true);
const pathname = parsedUrl.pathname;
if (pathname === '/download') {
const filePath = `filepath/ota.py`; // Path to your OTA update file
// Send the bin file to esp32
serveFile(res, filePath);
} else {
res.writeHead(404, { 'Content-Type': 'text/plain' });
res.end('Not found');
}
});
function serveFile(res, filePath) {
const stat = fs.statSync(filePath);
const fileStream = fs.createReadStream(filePath);
res.writeHead(200, {
'Content-Type': 'application/octet-stream',
'Content-Length': stat.size,
'Content-Disposition': 'attachment; filename=' + "ota.py"
});
console.log(stat.size);
fileStream.pipe(res);
console.log("Downloaded");
fs.unlinkSync(filePath);
}
server1.listen(3000, () => {
console.log('Server listening on port 3000');
});
- Save the above code in a file, e.g.,
server.js
. - Ensure you have Node.js installed.
- Run the server using the command: node
server.js
.
- The server listens on port 3000. The OTA update file should be accessible at
http://<server-ip>:3000/download
.
- File Naming: The example program file name must be
main.py
on your MicroPython device. - Wi-Fi and Host Configuration: Ensure that the SSID, Password, and host variables are correctly set to match your network and server configurations.
- Update Check Frequency: The
OTAUpdate.run()
method continuously checks for updates. Adjust the frequency or conditions within the loop function as needed.