Skip to content

Instantly share code, notes, and snippets.

@gregmac
Created March 9, 2022 03:21
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 gregmac/08c0b6ddbde9ced68f698168eeb69b60 to your computer and use it in GitHub Desktop.
Save gregmac/08c0b6ddbde9ced68f698168eeb69b60 to your computer and use it in GitHub Desktop.
PlatformIO IDE Upload with AsyncElegantOTA
#include <ESPAsyncWebServer.h>
#include <AsyncElegantOTA.h>
// Port to listen on
#define WEBSERVER_PORT 80
AsyncWebServer server(WEBSERVER_PORT);
void initWiFi() {
WiFi.mode(WIFI_STA);
WiFi.setHostname(mqttClientName.c_str());
WiFi.begin(WIFI_SSID, WIFI_PASS);
Serial.printf("Connecting to WiFi '%s'..", WIFI_SSID);
while (WiFi.status() != WL_CONNECTED) {
Serial.printf(".");
delay(1000);
}
Serial.printf("\nConnected to WiFi '%s', IP %s\n", WIFI_SSID, WiFi.localIP().toString().c_str());
}
void setup() {
Serial.begin(115200);
delay(1000); // give me time to bring up serial monitor
initWiFi();
// setup web server
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) {
auto response = request->beginResponseStream("text/html");
response->setCode(200);
response->printf("<html><body><h2>OTA Update demo %s</body></html>", WiFi.macAddress().c_str());
request->send(response);
});
AsyncElegantOTA.begin(&server); // support OTA updates
server.begin();
Serial.printf("Listening on http://%s:%i\n", WiFi.localIP().toString().c_str(), WEBSERVER_PORT);
}
void loop() {
# do stuf...
}
import hashlib
import http.client
import sys
import urllib.parse
if len(sys.argv) != 3:
raise Exception('Incorrect arguments')
fname = sys.argv[1]
target = sys.argv[2]
# read file
with open(fname, "rb") as binaryFile:
contents = binaryFile.read()
# get md5
md5 = hashlib.md5()
md5.update(contents)
md5hash = md5.hexdigest()
# upload
print('Uploading %s (md5 %s) to %s' % (fname, md5hash, target))
conn = http.client.HTTPConnection(target)
conn.request("POST", "/update",
urllib.parse.urlencode({'firmware': contents, 'md5': md5}),
{
"Content-type": "application/x-www-form-urlencoded",
"Accept": "text/plain"
})
response = conn.getresponse()
print(response.status, response.reason)
if (response.status != 200):
data = response.read()
print(data)
conn.close()
Import("env")
env.Replace(UPLOADCMD="python ota_upload.py $SOURCE $UPLOAD_PORT")
[env:esp32dev-http]
platform = espressif32
board = esp32dev
framework = arduino
monitor_speed = 115200
lib_deps =
ottowinter/ESPAsyncWebServer-esphome@^2.1.0
ayushsharma82/AsyncElegantOTA@^2.2.6
upload_port = 192.168.12.34
extra_scripts = ota_upload_pio.py
upload_protocol = custom
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment