Skip to content

Instantly share code, notes, and snippets.

@prohazko2
Last active June 25, 2021 17:56
Show Gist options
  • Save prohazko2/a2a2447e89a15c89a4c977d70f2b6b6a to your computer and use it in GitHub Desktop.
Save prohazko2/a2a2447e89a15c89a4c977d70f2b6b6a to your computer and use it in GitHub Desktop.
ESP8266 Local HTTP Arduino OTA Update
/**
!!! Should only be used in local trusted network
!!! Do not go outside without WiFiClientSecure and TLS
Based on:
https://github.com/esp8266/Arduino/blob/master/libraries/ESP8266httpUpdate/examples/httpUpdate/httpUpdate.ino
*/
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <ESP8266HTTPClient.h>
#include <ESP8266httpUpdate.h>
#define FIRMWARE_VERSION "0.1.1"
#define HTTP_OTA_URL "http://192.168.1.1:8266/firmware.bin"
const char *wifi_ssid = "...";
const char *wifi_pass = "...";
void progress(int cur, int total)
{
Serial.printf("CALLBACK: HTTP update process at %d of %d bytes...\n", cur, total);
}
void setup()
{
Serial.begin(9600);
Serial.print("\n\nWith FIRMWARE_VERSION: ");
Serial.print(FIRMWARE_VERSION);
Serial.println(" loading ...");
WiFi.mode(WIFI_STA);
WiFi.begin(wifi_ssid, wifi_pass);
while (WiFi.waitForConnectResult() != WL_CONNECTED)
{
Serial.println("Connection Failed! Rebooting...");
delay(5000);
ESP.restart();
}
Serial.println("Ready");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
ESPhttpUpdate.setLedPin(LED_BUILTIN, LOW);
ESPhttpUpdate.onProgress(progress);
t_httpUpdate_return ret = ESPhttpUpdate.update(HTTP_OTA_URL, FIRMWARE_VERSION);
switch (ret)
{
case HTTP_UPDATE_FAILED:
Serial.printf("HTTP_UPDATE_FAILD Error (%d): %s\n", ESPhttpUpdate.getLastError(), ESPhttpUpdate.getLastErrorString().c_str());
break;
case HTTP_UPDATE_NO_UPDATES:
Serial.println("HTTP_UPDATE_NO_UPDATES");
break;
case HTTP_UPDATE_OK:
Serial.println("HTTP_UPDATE_OK");
break;
}
}
void loop()
{
}
const fs = require("fs");
const http = require("http");
const crypto = require("crypto");
const port = 8266;
const path = require("path").resolve(__dirname, "./.pio/build/nodemcuv2/firmware.bin");
const server = http.createServer((req, res) => {
console.log(`${new Date().toISOString()} request to ${req.url} with`, req.headers);
const file = fs.readFileSync(path);
const hash = crypto.createHash("md5").update(file).digest("hex");
if (hash === req.headers["x-esp8266-sketch-md5"]) {
res.writeHead(304, {
"Content-Type": "text/plain",
});
return res.end("no updates");
}
res.writeHead(200, {
"Content-Type": "application/octet-stream",
"Content-Length": file.length,
});
res.end(file);
});
server.listen(port, () =>
console.log(`serving ${path} firmware at http://0.0.0.0:${port}/`)
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment