Part 1 — How to build your own ESP8266/ESP32 Over-The-Air firmware updater

Image for post
Image for post
ESP8266-ESP12E
ESP8266-ESP12E

NodeJS Application

express ota_updater
void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED_BUILTIN, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
Image for post
Image for post
Image for post
Image for post
var express = require('express');
var path = require('path');
var fs = require("fs");
var router = express.Router();
var md5 = require("md5-file");
router.get('/update', function(req, res, next) { console.log(req.headers); var filePath = path.join(__dirname, '../updates/blink.ino.bin'); var options = {
headers: {
"x-MD5": md5.sync(filePath)
}
}
res.sendFile(file, function (err) {
if (err) {
next(err)
} else {
console.log('Sent:', file)
}
});
});module.exports = router;
Download dialog box
Download dialog box

ESP8266 sketch

#include <ESP8266httpUpdate.h>const char* ssid =     "ssid";     // Set your router SSID
const char* password = "password"; // Set your router password
void setup() {

Serial.begin(74880);
Serial.setDebugOutput(true);
WiFi.begin(ssid, password);
/*connection to WiFi*/
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(1000);
}
t_httpUpdate_return ret = ESPhttpUpdate.update("http://domain.com/update","1.0");

switch(ret) {
case HTTP_UPDATE_FAILED:
Serial.printf("[update] Update failed (%d): %s", ESPhttpUpdate.getLastError(), ESPhttpUpdate.getLastErrorString().c_str());
break;
case HTTP_UPDATE_NO_UPDATES:
Serial.println("[update] Update no Update.");
break;
case HTTP_UPDATE_OK:
Serial.println("[update] Update ok.");
break;
}
}void loop() {}
Image for post
Image for post
{ connection: 'upgrade',
host: 'domain.com',
'content-length': '0',
'user-agent': 'ESP8266-http-Update',
'x-esp8266-chip-id': '14454826',
'x-esp8266-sta-mac': 'CC:50:E3:DC:90:2A',
'x-esp8266-ap-mac': 'CE:50:E3:DC:90:2A',
'x-esp8266-free-space': '659456',
'x-esp8266-sketch-size': '302832',
'x-esp8266-sketch-md5': '97bbf0d228c88673b9c040df1f7317f4',
'x-esp8266-chip-size': '4194304',
'x-esp8266-sdk-version': '2.2.2-dev(38a443e)',
'x-esp8266-mode': 'sketch',
'x-esp8266-version': '1.0' }

Co-founder at Shotstack.io. Previously Marketplace @ Uber.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store