Comments (5)
Here, the update server prototyping. You can try it in Python 3.5 or higher environment.
You need the AutoConnect version of the development branch and ArduinoJson V6 to try this prototype.
To start the update server:
- Store the following script named
updateserver.py
on your PC which can be running Python 3.5 or higher. - Place some sketch binaries in the same place. You can retrieve the sketch binary from the Export Compiled Binary menu in Arduino IDE.
- Start the update server.
python updateserver.py -b [your PC's IP address]
- Flash the application sketch that implements the update function paired with this server to the ESP8266 module. An example implementation of the sketch is posted at the end. It's the prototype of the AutoConnectUpdate class with v0.99.
Update server script
Please change the first line Shebang according to your environment.
#!python3.*
import argparse
import hashlib
import http.server
import json
import os
import re
import urllib.parse
class UpdateHttpServer:
def __init__(self, port, bind, catalog_dir):
def handler(*args):
UpdateHTTPRequestHandler(catalog_dir, *args)
httpd = http.server.HTTPServer((bind, port), handler)
sa = httpd.socket.getsockname()
print('http server starting {0}:{1} {2}'.format(sa[0], sa[1], catalog_dir))
try:
httpd.serve_forever()
except KeyboardInterrupt:
print('Shutting down...')
httpd.socket.close()
class UpdateHTTPRequestHandler(http.server.BaseHTTPRequestHandler):
def __init__(self, catalog_dir, *args):
self.catalog_dir = catalog_dir
http.server.BaseHTTPRequestHandler.__init__(self, *args)
def do_GET(self):
request_path = urllib.parse.urlparse(self.path)
if request_path.path == '/_catalog':
err = ''
query = urllib.parse.urlparse(self.path).query
try:
op = urllib.parse.parse_qs(query)['op'][0]
if op == 'list':
try:
path = urllib.parse.parse_qs(query)['path'][0]
except KeyError:
path = '.'
self.__send_dir(path)
result = True
else:
err = '{0} unknown operation'.format(op)
result = False
except KeyError:
err = '{0} invaid catalog request'.format(self.path)
result = False
if not result:
print(err)
self.send_response(http.HTTPStatus.FORBIDDEN, err)
self.end_headers()
else:
self.__send_file(self.path)
def __check_header(self):
ex_headers_templ = ['x-*-STA-MAC', 'x-*-AP-MAC', 'x-*-FREE-SPACE', 'x-*-SKETCH-SIZE', 'x-*-SKETCH-MD5', 'x-*-CHIP-SIZE', 'x-*-SDK-VERSION']
ex_headers = []
ua = re.match('(ESP8266|ESP32)-http-Update', self.headers.get('User-Agent'))
if ua:
arch = ua.group().split('-')[0]
ex_headers = list(map(lambda x: x.replace('*', arch), ex_headers_templ))
else:
print('User-Agent {0} is not HTTPUpdate'.format(ua))
return False
for ex_header in ex_headers:
if ex_header not in self.headers:
print('Missing header {0} to identify a legitimate request'.format(ex_header))
return False
return True
def __send_file(self, path):
if not self.__check_header():
self.send_response(http.HTTPStatus.FORBIDDEN, 'The request available only from ESP8266 or ESP32 http updater.')
self.end_headers()
return
filename = os.path.join(self.catalog_dir, path.lstrip('/'))
print('Request file:' + filename)
try:
fsize = os.path.getsize(filename)
self.send_response(http.HTTPStatus.OK)
self.send_header('Content-Type', 'application/octet-stream')
self.send_header('Content-Disposition', 'attachment; filename=' + os.path.basename(filename))
self.send_header('Content-Length', fsize)
self.send_header('x-MD5', get_MD5(filename))
self.end_headers()
f = open(filename, 'rb')
self.wfile.write(f.read())
f.close()
except Exception as e:
err = str(e)
print(err)
self.send_response(http.HTTPStatus.INTERNAL_SERVER_ERROR, err)
self.end_headers()
def __send_dir(self, path):
content = self.__dir_json(path)
d = json.dumps(content).encode('UTF-8', 'replace')
print(d)
self.send_response(http.HTTPStatus.OK)
self.send_header('Content-Type', 'application/json')
self.send_header('Content-Length', str(len(d)))
self.end_headers()
self.wfile.write(d)
def __dir_json(self, path):
d = list()
for entry in os.listdir(path):
e = {'name': entry}
if os.path.isdir(entry):
e['type'] = "directory"
else:
e['type'] = "file"
if os.path.splitext(entry)[1] == '.bin':
try:
f = open(os.path.join(path, entry), 'rb')
c = f.read(1)
f.close()
except Exception as e:
print(str(e))
c = b'\x00'
if c == b'\xe9':
e['type'] = "bin"
d.append(e)
return d
def get_MD5(filename):
try:
f = open(filename, 'rb')
bin = f.read()
f.close()
md5 = hashlib.md5(bin).hexdigest()
return md5
except Exception as e:
print(str(e))
return None
def run(port=8000, bind='127.0.0.1', catalog_dir=''):
UpdateHttpServer(port, bind, catalog_dir)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('--port', '-p', action='store', default=8000, type=int,
help='Port number [default:8000]')
parser.add_argument('--bind', '-b', action='store', default='127.0.0.1',
help='Specifies address to which it should bind. [default:127.0.0.1]')
parser.add_argument('--catalog', '-d', action='store',
default=os.getcwd(), help='Catalog directory')
args = parser.parse_args()
run(args.port, args.bind, args.catalog)
Initially implementation of the framework for AutoConnectUpdate class.
Please change _host
for your environment.
You don't have to write a long coding like below with v0.99. It will be provided by the library as the AutoConnectUpdate class.
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <ESP8266HTTPClient.h>
#include <ESP8266httpUpdate.h>
#include <ArduinoJson.h>
#include <AutoConnect.h>
#ifdef LED_BUILTIN
#define UPDATE_SETLED(s) do {ESPhttpUpdate.setLedPin(LED_BUILTIN, s);} while(0)
#else
#define UPDATE_SETLED(s) do {} while(0)
#endif
const char PAGE_CATALOG[] PROGMEM = R"(
{
"uri": "/catalog",
"title": "Update",
"menu": true,
"element": [
{
"name": "caption",
"type": "ACText"
},
{
"name": "firmwares",
"type": "ACRadio"
},
{
"name": "update",
"type": "ACSubmit",
"value": "UPDATE",
"uri": "/update"
}
]
}
)";
const char PAGE_UPDATE[] PROGMEM = R"(
{
"uri": "/update",
"title": "Update",
"menu": false,
"element": [
{
"name": "spinner",
"type": "ACElement",
"value": "<style>.loader {border:2px solid #f3f3f3;border-radius:50%;border-top:2px solid #555;width:12px;height:12px;-webkit-animation:spin 1s linear infinite;animation:spin 1s linear infinite;} @-webkit-keyframes spin {0% {-webkit-transform:rotate(0deg);} 100% {-webkit-transform:rotate(360deg);}} @keyframes spin {0% {transform:rotate(0deg);} 100% {transform:rotate(360deg);}}</style>"
},
{
"name": "caption",
"type": "ACText",
"value": "<h2>Update start...</h2>"
},
{
"name": "flash",
"type": "ACText",
"format": "%s<span style=\"display:inline-block;vertical-align:middle;margin-left:5px;\"><div class=\"loader\"></div></span>"
},
{
"name": "inprogress",
"type": "ACElement",
"value": "<script type=\"text/javascript\">setTimeout(\"location.href='/result'\",1000*15);</script>"
}
]
}
)";
const char PAGE_RESULT[] PROGMEM = R"(
{
"uri": "/result",
"title": "Update",
"menu": false,
"element": [
{
"name": "status",
"type": "ACText"
},
{
"name": "restart",
"type": "ACElement",
"value": "<script type=\"text/javascript\">setTimeout(\"location.href='/_ac'\",1000*5);</script>"
}
]
}
)";
ESP8266WebServer webServer;
AutoConnect portal(webServer);
AutoConnectAux catalog;
AutoConnectAux update;
AutoConnectAux result;
// Replace the IP address or host name for the update server
String _host = "***.***.***.***";
int _port = 8000;
String _uri = "/_catalog";
String _op = "op=list&path=";
// Here, the folder which is stored binary sketches.
// Specifies the reletive location from the update server script.
String _path = ".";
typedef enum {
UPDATE_RESET,
UPDATE_IDLE,
UPDATE_START,
UPDATE_DOING,
UPDATE_SUCCESS,
UPDATE_FAIL
} UpdateStatus_t;
UpdateStatus_t _stat;
size_t insertCatalog(AutoConnectRadio& radio, JsonVariant& listDoc) {
JsonArray firmwares = listDoc.as<JsonArray>();
for (JsonObject entry : firmwares)
if (entry["type"] == "bin")
radio.add(entry["name"].as<String>());
return firmwares.size();
}
String onCatalog(AutoConnectAux& aux, PageArgument& args) {
WiFiClient client;
HTTPClient http;
AutoConnectText& caption = catalog["caption"].as<AutoConnectText>();
AutoConnectRadio& firmwares = catalog["firmwares"].as<AutoConnectRadio>();
AutoConnectSubmit& submit = catalog["update"].as<AutoConnectSubmit>();
firmwares.empty();
submit.enable = false;
String qs = _uri + '?' + _op + _path;
if (http.begin(client, _host, _port, qs, false)) {
int responseCode = http.GET();
if (responseCode == HTTP_CODE_OK) {
DynamicJsonDocument json(2048);
Stream& responseBody = http.getStream();
DeserializationError err = deserializeJson(json, responseBody);
if (!err) {
caption.value = "<h2>Available firmwares</h2>";
JsonVariant firmwareList = json.as<JsonVariant>();
if (insertCatalog(firmwares, firmwareList) > 0)
submit.enable = true;
}
else
caption.value = String("Invalid catalog list:") + String(err.c_str());
}
else {
caption.value = String("Update server responds (") + String(responseCode) + String(')');
String errString = HTTPClient::errorToString(responseCode);
if (errString.length())
caption.value += String(", ") + errString;
catalog["update"].as<AutoConnectSubmit>().enable = false;
}
http.end();
}
else
caption.value = String("http failed connect to ") + _host + String(':') + String(_port);
return String();
}
String onUpdate(AutoConnectAux& aux, PageArgument& args) {
update["flash"].value = catalog["firmwares"].as<AutoConnectRadio>().value();
_stat = UPDATE_START;
return String();
}
String onResult(AutoConnectAux& aux, PageArgument& args) {
String status;
String color;
bool restart = false;
switch (_stat) {
case UPDATE_SUCCESS:
status = " sucessfully updated. restart...";
color = "blue";
restart = true;
break;
case UPDATE_FAIL:
status = " update failed.";
color = "red";
break;
default:
status = ", no update needed";
break;
}
AutoConnectText& resultElm = result["status"].as<AutoConnectText>();
resultElm.value = update["flash"].value + status;
resultElm.style = String("font-size:120%;") + color;
result["restart"].enable = restart;
if (restart)
_stat = UPDATE_RESET;
return String();
}
void doUpdate(const String& path) {
WiFiClient client;
UPDATE_SETLED(LOW);
ESPhttpUpdate.rebootOnUpdate(false);
_stat = UPDATE_DOING;
t_httpUpdate_return ret = ESPhttpUpdate.update(client, _host, _port, path);
switch (ret) {
case HTTP_UPDATE_FAILED:
_stat = UPDATE_FAIL;
Serial.printf("HTTP_UPDATE_FAILD Error (%d): %s\n", ESPhttpUpdate.getLastError(), ESPhttpUpdate.getLastErrorString().c_str());
break;
case HTTP_UPDATE_NO_UPDATES:
_stat = UPDATE_IDLE;
Serial.println("HTTP_UPDATE_NO_UPDATES");
break;
case HTTP_UPDATE_OK:
_stat = UPDATE_SUCCESS;
Serial.println("HTTP_UPDATE_OK");
break;
}
}
void setup() {
Serial.begin(115200);
Serial.println();
catalog.load(PAGE_CATALOG);
update.load(PAGE_UPDATE);
result.load(PAGE_RESULT);
portal.join({ catalog, update, result });
catalog.on(onCatalog, AC_EXIT_AHEAD);
update.on(onUpdate, AC_EXIT_AHEAD);
result.on(onResult, AC_EXIT_AHEAD);
_stat = UPDATE_IDLE;
portal.begin();
}
void loop() {
if (_stat == UPDATE_START) {
AutoConnectRadio& firmwares = catalog["firmwares"].as<AutoConnectRadio>();
String firmwarePath = String(_path) + '/' + firmwares.value();
doUpdate(firmwarePath);
}
if (_stat == UPDATE_RESET) {
Serial.println("Restart...");
ESP.restart();
}
portal.handleClient();
}
from autoconnect.
Defer this feature enhancement and give priority to esp8266 core 2.5.2 support.
The release of the update feature is expected to be v0.9.10 or later.
from autoconnect.
maybe esp32 ota feature ? i add with autoconnect it crash.....
from autoconnect.
The commit section of the development branch is unstable. It does not always work.
It should work with resources staged to the development branch currently and has already implemented most of the above update features.
You can try with the Update.ino example and updateserver.py script included in the resources of the development branch
from autoconnect.
The update feature will be released next update turn as v1.0.0.
This topic title changed.
Development branch continues to use Enhance/v099.
from autoconnect.
Related Issues (20)
- ESP32 problem HOT 8
- Mqtt Example HOT 2
- Using "AutoConnectCore.h" header and running into errors HOT 2
- AutoConnect to offer captive portal for 5min even if ESP32 is WiFi-Connected HOT 1
- AutoConnect on WiFi connection error shows too many redirects. HOT 2
- AutoConnectCore Not Working HOT 1
- Failing to store network HOT 2
- AutoConnect not longer runningh on ESP8266 HOT 2
- Use Autoconnect library with ESP now HOT 1
- Issue after updating libraries in my PIO project: enableCors() HOT 2
- Strange Issue
- WiFiClientSecure is not working with AutoConnect HOT 1
- how to use esp8266 with WiFiClientSecure
- AutoConnectPageImpl.hpp:1001:20: error: there are no arguments to 'spi_flash_get_chip_size' that depend on a template parameter, so a declaration of 'spi_flash_get_chip_size' must be available [-fpermissive] HOT 1
- Save data from Custom Page to flash HOT 2
- esp32: frozen loop() and kicked out of the WiFi connection at Configure new AP
- WPA2 Enterprise support
- Captive portal cause watchdog triggering
- Possibility to change pass for SoftAP from webui.
- Fresh Install and even the simplest of code will not compile HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from autoconnect.