Merge branch 'allow-user-code' of https://git.siwatsystem.com/ise-senior-iot/iot-firmware into allow-user-code

This commit is contained in:
Siwat Sirichai 2023-09-30 09:39:37 +07:00
commit a715988f84
5 changed files with 108 additions and 3 deletions

View File

@ -21,5 +21,5 @@ lib_deps = siwats/ESPMegaPROR3@^1.3.0
robtillaart/DHTNEW@^0.4.18
seithan/Easy Nextion Library@^1.0.6
robtillaart/FRAM_I2C@^0.6.1
monitor_speed = 115200
board_build.partitions = no_ota.csv
esphome/ESPAsyncWebServer-esphome@^3.1.0
monitor_speed = 115200

View File

@ -1,7 +1,7 @@
#include <espmega_iot_core.hpp>
// OS Configuration
#define FASTBOOT
//#define FASTBOOT
// Network Connectivity
char HOSTNAME[15];
@ -11,6 +11,7 @@ IPAddress GATEWAY(0, 0, 0, 0);
IPAddress DNS(0, 0, 0, 0);
IPAddress MQTT_SERVER(0, 0, 0, 0);
uint16_t MQTT_PORT = 0;
WebServer otaserver(80);
bool standalone = true;
// #define MQTT_BASE_TOPIC "/espmega/ProR3"
char MQTT_BASE_TOPIC[20];
@ -141,6 +142,7 @@ void setup()
mqtt_connect();
lcd_send_command("boot_state.txt=\"Threads Initializing . . .\"");
thread_initialization();
ota_begin();
Serial.println("Initialization Completed.");
Serial.println("Jumping to User Code.");
user_init();
@ -151,6 +153,7 @@ void setup()
Serial2.write(0xFF);
Serial2.write(0xFF);
#endif
}
void loop()
@ -162,6 +165,9 @@ void loop()
thread_controller.run();
lcd_loop();
user_loop();
otaserver.handleClient();
}
void eeprom_retrieve_init()
@ -242,6 +248,37 @@ void eeprom_retrieve_init()
strcat(INPUTS_TOPIC, "/input/00");
}
void ota_begin() {
otaserver.on("/", HTTP_GET, []() {
otaserver.sendHeader("Connection", "close");
otaserver.send(200, "text/html", ota_upload_page);
});
otaserver.on("/update", HTTP_POST, []() {
otaserver.sendHeader("Connection", "close");
otaserver.send(200, "text/plain", (Update.hasError()) ? "FAIL" : "OK");
ESP.restart();
}, []() {
HTTPUpload& upload = otaserver.upload();
if (upload.status == UPLOAD_FILE_START) {
Serial.printf("Update: %s\n", upload.filename.c_str());
if (!Update.begin(UPDATE_SIZE_UNKNOWN)) {
Update.printError(Serial);
}
} else if (upload.status == UPLOAD_FILE_WRITE) {
if (Update.write(upload.buf, upload.currentSize) != upload.currentSize) {
Update.printError(Serial);
}
} else if (upload.status == UPLOAD_FILE_END) {
if (Update.end(true)) {
Serial.printf("Update Success: %u\nRebooting...\n", upload.totalSize);
} else {
Update.printError(Serial);
}
}
});
otaserver.begin();
}
void io_begin()
{
Serial.println("Initializing I/O . . .");

View File

@ -10,9 +10,15 @@
#include <dhtnew.h>
#include <time.h>
#include <EasyNextionLibrary.h>
#include <WiFiClient.h>
#include <WebServer.h>
#include <Update.h>
#include "lcd.hpp"
#include "user_code.hpp"
#include "ir_codes.hpp"
#include "espmega_iot_ota.hpp"
#include "espmega_iot_timer.hpp"
void virtual_interrupt_loop();
void virtual_interrupt_callback(int pin, int state);
@ -26,6 +32,8 @@ void state_request_callback(String topic, String message);
void io_begin();
void ir_loop();
void ota_begin();
void publish_pwm_states();
void publish_pwm_state(int id);
void pwm_set_state(int id, int state);

56
src/espmega_iot_ota.cpp Normal file
View File

@ -0,0 +1,56 @@
#include "espmega_iot_ota.hpp"
/* Style */
String ota_style =
"<style>#file-input,input{width:100%;height:44px;border-radius:4px;margin:10px auto;font-size:15px}"
"input{background:#f1f1f1;border:0;padding:0 15px}body{background:#3498db;font-family:sans-serif;font-size:14px;color:#777}"
"#file-input{padding:0;border:1px solid #ddd;line-height:44px;text-align:left;display:block;cursor:pointer}"
"#bar,#prgbar{background-color:#f1f1f1;border-radius:10px}#bar{background-color:#3498db;width:0%;height:10px}"
"form{background:#fff;max-width:258px;margin:75px auto;padding:30px;border-radius:5px;text-align:center}"
".btn{background:#3498db;color:#fff;cursor:pointer}</style>";
/* Server Index Page */
String ota_upload_page =
"<script src='https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js'></script>"
"<form method='POST' action='#' enctype='multipart/form-data' id='upload_form'>"
"<h1>ESPMega PRO</h1>"
"<h3>Programming Tool</h3>"
"<input type='file' name='update' id='file' onchange='sub(this)' style=display:none>"
"<label id='file-input' for='file'> Choose file...</label>"
"<input type='submit' class=btn value='Program'>"
"<br><br>"
"<div id='prg'></div>"
"<br><div id='prgbar'><div id='bar'></div></div><br></form>"
"<script>"
"function sub(obj){"
"var fileName = obj.value.split('\\\\');"
"document.getElementById('file-input').innerHTML = ' '+ fileName[fileName.length-1];"
"};"
"$('form').submit(function(e){"
"e.preventDefault();"
"var form = $('#upload_form')[0];"
"var data = new FormData(form);"
"$.ajax({"
"url: '/update',"
"type: 'POST',"
"data: data,"
"contentType: false,"
"processData:false,"
"xhr: function() {"
"var xhr = new window.XMLHttpRequest();"
"xhr.upload.addEventListener('progress', function(evt) {"
"if (evt.lengthComputable) {"
"var per = evt.loaded / evt.total;"
"$('#prg').html('progress: ' + Math.round(per*100) + '%');"
"$('#bar').css('width',Math.round(per*100) + '%');"
"}"
"}, false);"
"return xhr;"
"},"
"success:function(d, s) {"
"console.log('success!') "
"},"
"error: function (a, b, c) {"
"}"
"});"
"});"
"</script>" + ota_style;

4
src/espmega_iot_ota.hpp Normal file
View File

@ -0,0 +1,4 @@
#pragma once
#include <ESPMegaPRO.h>
extern String ota_upload_page;
extern String ota_style;