Closed Hieromon closed 5 years ago
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:
updateserver.py
on your PC which can be running Python 3.5 or higher.python updateserver.py -b [your PC's IP address]
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)
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();
}
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.
maybe esp32 ota feature ? i add with autoconnect it crash.....
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
The update feature will be released next update turn as v1.0.0. This topic title changed. Development branch continues to use Enhance/v099.
To all contributors and Users,
Thank you for your interest in AutoConnect. I prepared for the prototype of the update server and application sketch which for sketch binary updating via OTA. I will implementation for v0.99 based on this prototype and will also add automatic updates by version level according to each device. The OTA update feature will be incorporated by only one parameter with AutoConnectConfig. Related issue #26 #50
Please evaluate this prototype if you can and post any comments if you have any requirements that I need to consider for implementation.
The following menu is in interactive mode. In automatic update mode, it will behave in the background.