Skip to content

Instantly share code, notes, and snippets.

@kakopappa
Last active February 28, 2017 10:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kakopappa/7770d0b923878c900427180e53b29fb1 to your computer and use it in GitHub Desktop.
Save kakopappa/7770d0b923878c900427180e53b29fb1 to your computer and use it in GitHub Desktop.
// OTA Update using ESPhttpUpdate
/* ESPhttpUpdate.update("192.168.0.2", 80, "/arduino.bin");
t_httpUpdate_return ret = ESPhttpUpdate.update("192.168.0.2", 80, "/esp/update/arduino", "optional current version string here");
switch(ret) {
case HTTP_UPDATE_FAILED:
Serial.println("[update] Update failed.");
break;
case HTTP_UPDATE_NO_UPDATES:
Serial.println("[update] Update no Update.");
break;
case HTTP_UPDATE_OK:
Serial.println("[update] Update ok."); // may not called we reboot the ESP
break;
}*/
http = require("http");
url = require("url");
path = require("path");
fs = require("fs");
md5 = require('md5-file');
function check_header(request, header, value) {
if (!request.headers[header.toLowerCase()])
return false;
if (value && request.headers[header.toLowerCase()] !== value)
return false;
return true;
}
var available_version = "4712";
http.createServer(function(request,response){
if (!check_header(request, 'USER-AGENT', 'ESP8266-http-Update') ||
!check_header(request, 'X-ESP8266-STA-MAC') ||
!check_header(request, 'X-ESP8266-AP-MAC') ||
!check_header(request, 'X-ESP8266-FREE-SPACE') ||
!check_header(request, 'X-ESP8266-SKETCH-SIZE') ||
!check_header(request, 'X-ESP8266-SKETCH-MD5') ||
!check_header(request, 'X-ESP8266-CHIP-SIZE') ||
!check_header(request, 'X-ESP8266-SDK-VERSION' ||
!check_header(request, "X-ESP8266-VERSION"))) {
response.writeHeader(403, {"Content-Type": "text/plain"});
response.write("403 Forbidden");
}
else {
if (check_header(request, "X-ESP8266-VERSION", available_version)) {
response.writeHeader(304, {"Content-Type": "text/plain"});
response.write("304 Not Modified\n");
response.end();
}
else {
var my_path = url.parse(request.url).pathname;
var full_path = path.join(process.cwd(),my_path);
fs.exists(full_path,function(exists){
if (!exists) {
response.writeHeader(404, {"Content-Type": "text/plain"});
response.write("404 Not Found\n");
response.end();
}
else {
fs.readFile(full_path, "binary", function(err, file) {
if (err) {
response.writeHeader(500, {"Content-Type": "text/plain"});
response.write(err + "\n");
response.end();
}
else{
response.writeHeader(200,
{"Content-Type": "application/octet-stream",
"Content-Disposition": "attachment;filename="+path.basename(full_path),
"Content-Length": ""+fs.statSync(full_path)["size"],
"x-MD5": md5.sync(full_path)});
response.write(file, "binary");
response.end();
}
});
}
});
}
}
}).listen(8080);
console.log("Server running");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment