initial lcd ota code

This commit is contained in:
Siwat Sirichai 2023-10-22 00:36:22 +07:00
parent 10b55e5aa5
commit e3514cef3c
2 changed files with 69 additions and 0 deletions

View File

@ -0,0 +1,52 @@
#include <espmega_iot_ledota.hpp>
ESPMega_LCDOTA::register_ota()
{
otaserver.on(
update_endpoint, HTTP_POST, []()
{
otaserver.sendHeader("Connection", "close");
otaserver.send(200, "text/plain", (Update.hasError()) ? "FAIL" : "OK");
ESP.restart(); },
this->update_action);
}
ESPMega_LCDOTA::update_action(AsyncWebServerRequest *request, String filename, size_t index, uint8_t *data, size_t len, bool final)
{
HTTPUpload &upload = otaserver.upload();
if (upload.status == UPLOAD_FILE_START)
{
send_stop_bit();
update_init();
}
else if (upload.status == UPLOAD_FILE_WRITE)
{
if (Update.write(upload.buf, upload.currentSize) != upload.currentSize)
{
panel.writeStr("otatxt.txt", "Update Failed, Rebooting . . .");
Update.printError(Serial);
}
if (upload.currentSize != 0 && upload.totalSize != 0)
{
lcd_send_stop_bit();
uint32_t totalsize_kb = upload.totalSize / 1000;
uint32_t upload_pct = 100 * upload.totalSize / 1000000;
String otafiletxt = "Downloading File : " + upload.filename + " (" + String(totalsize_kb) + "KB)";
panel.writeNum("prog.val", upload_pct);
panel.writeStr("otatxt.txt", otafiletxt);
}
}
else if (upload.status == UPLOAD_FILE_END)
{
if (Update.end(true))
{
panel.writeStr("otatxt.txt", "Update Completed, Rebooting . . .");
Serial.printf("Update Success: %u\nRebooting...\n", upload.totalSize);
}
else
{
panel.writeStr("otatxt.txt", "Update Failed, Rebooting . . .");
Update.printError(Serial);
}
}
}

View File

@ -0,0 +1,17 @@
#pragma once
#include <WebServer.h>
#include <ESPMegaPRO.h>
class ESPMega_LCDOTA {
public:
ESPMega_LCDOTA();
void update_init();
void write_data();
void update_end();
private:
char update_endpoint[20];
void register_ota();
void send_stop_bit();
void update_action();
HardwareSerial lcdserial;
WebServer otaserver;
}