Compare commits

...

2 Commits

5 changed files with 96 additions and 2 deletions

View File

@ -4,6 +4,9 @@
#include <MD_MAX72xx.h> #include <MD_MAX72xx.h>
#include <SPI.h> #include <SPI.h>
#include "MD_RobotEyes.h" #include "MD_RobotEyes.h"
extern "C" {
#include "user_interface.h"
}
#define DISPLAY_CLK_PIN D5 #define DISPLAY_CLK_PIN D5
#define DISPLAY_DATA_PIN D7 #define DISPLAY_DATA_PIN D7
@ -181,6 +184,7 @@ void Display::loop() {
} }
void Display::reInitializeDisplay() { void Display::reInitializeDisplay() {
uint32_t free = system_get_free_heap_size();
#define REINIT_AFTER_ms 5000 #define REINIT_AFTER_ms 5000
#define AVOID_REINIT_BEFORE_AND_AFTER_FULLMINUTE_FOR_s 3 #define AVOID_REINIT_BEFORE_AND_AFTER_FULLMINUTE_FOR_s 3
@ -190,7 +194,7 @@ void Display::reInitializeDisplay() {
&& currentTime->getSeconds() < 60 - AVOID_REINIT_BEFORE_AND_AFTER_FULLMINUTE_FOR_s && currentTime->getSeconds() < 60 - AVOID_REINIT_BEFORE_AND_AFTER_FULLMINUTE_FOR_s
&& currentTime->getSeconds() > AVOID_REINIT_BEFORE_AND_AFTER_FULLMINUTE_FOR_s) { && currentTime->getSeconds() > AVOID_REINIT_BEFORE_AND_AFTER_FULLMINUTE_FOR_s) {
P.begin(); P.begin();
Serial.println("reinit display"); Serial.print("reinit display, free="); Serial.println(free);
last_reinit_ts = millis(); last_reinit_ts = millis();
} }
} }

View File

@ -133,6 +133,51 @@ void WebServer::begin() {
json = String(); json = String();
}); });
server.on("/sys/swupdate", HTTP_GET, [&](AsyncWebServerRequest *request){
#define UPDATE_DELAY 100
Serial.println(F("\n Check for SWUpdate"));
// create json return
String json = "{";
if (ias != NULL) {
json += "\"result\":\"OK\"";
} else {
json += "\"result\":\"FAILED\",";
json += "\"message\":\"IAS not initialized in web server, use setIAS method.\"";
}
json += "}";
// return json to WebApp
request->send(200, F("text/json"), json);
json = String();
if (ias != NULL) {
Serial.println("Checking for software updates");
checkSoftwareUpdate_ts = millis() + UPDATE_DELAY;
}
});
server.on("/sys/reboot", HTTP_GET, [&](AsyncWebServerRequest *request){
#define REBOOT_DELAY 100
Serial.println(F("\n Reboot device"));
String json = "{";
json += "\"result\":\"OK\"";
json += "}";
// return json to WebApp
request->send(200, F("text/json"), json);
json = String();
Serial.println("Rebooting device");
rebootDevice_ts = millis() + REBOOT_DELAY;
});
server.on("/sys/heap", HTTP_GET, [](AsyncWebServerRequest *request){
String json = "{";
json += "\"result\":\"OK\",";
json += "\"heap\":" + String(ESP.getFreeHeap());
json += "}";
request->send(200, "text/json", json);
});
server.serveStatic("/", SPIFFS, "/"); server.serveStatic("/", SPIFFS, "/");
server.onNotFound(onRequest); server.onNotFound(onRequest);
@ -143,3 +188,24 @@ void WebServer::begin() {
Serial.println(WiFi.localIP()); Serial.println(WiFi.localIP());
Serial.println(""); Serial.println("");
} }
void WebServer::loop() {
if (rebootDevice_ts > 0 && rebootDevice_ts < millis()) {
rebootDevice_ts = 0;
Serial.println("Rebooting device.");
delay(20);
ESP.restart();
}
if (checkSoftwareUpdate_ts > 0 && checkSoftwareUpdate_ts < millis()) {
checkSoftwareUpdate_ts = 0;
Serial.println("Checking for software updates.");
delay(20);
if (ias != NULL) {
ias->callHome(true);
} else {
Serial.println("ERROR: IAS not set, use setIAS method in web server");
}
}
return;
}

View File

@ -5,18 +5,23 @@
#include <ESPAsyncTCP.h> // https://github.com/me-no-dev/AsyncTCP #include <ESPAsyncTCP.h> // https://github.com/me-no-dev/AsyncTCP
#include <ESPAsyncWebServer.h> #include <ESPAsyncWebServer.h>
#include <FS.h> #include <FS.h>
#include <IOTAppStory.h>
#include "Relays.h" #include "Relays.h"
#include "Clock.h" #include "Clock.h"
class WebServer { class WebServer {
public: public:
WebServer(Relays *r, Clock *real_t, Clock *model_t):relays(r),realTime(real_t),modelTime(model_t) {}; WebServer(Relays *r, Clock *real_t, Clock *model_t):
relays(r),realTime(real_t),modelTime(model_t), checkSoftwareUpdate_ts(0), rebootDevice_ts(0) {};
void begin(); void begin();
void loop(); void loop();
void setIAS(IOTAppStory *_ias) { ias = _ias;};
protected: protected:
Relays *relays; Relays *relays;
Clock *realTime, *modelTime; Clock *realTime, *modelTime;
unsigned long checkSoftwareUpdate_ts, rebootDevice_ts;
IOTAppStory *ias;
}; };
#endif #endif

View File

@ -83,6 +83,22 @@
ajaxGet("/clock/real", handleChangeClockResponse); ajaxGet("/clock/real", handleChangeClockResponse);
} }
function handleTriggerSWUpdateCheck(result) {
showMessage(JSON.stringify(result, null, 2));
};
function triggerSWUpdateCheck() {
ajaxGet("/sys/swupdate", handleTriggerSWUpdateCheck);
}
function handleTriggerReboot(result) {
showMessage(JSON.stringify(result, null, 2));
};
function triggerReboot() {
ajaxGet("/sys/reboot", handleTriggerReboot);
}
function onBodyLoad() { function onBodyLoad() {
var button = document.getElementById("button"); var button = document.getElementById("button");
button.onmousedown = function(event) { button.onmousedown = function(event) {
@ -117,6 +133,8 @@
<div> <div>
<button onClick="setClockToModel()">Model time</button> <button onClick="setClockToModel()">Model time</button>
<button onClick="setClockToReal()">Real time</button> <button onClick="setClockToReal()">Real time</button>
<button onClick="triggerSWUpdateCheck()">Check SW Update</button>
<button onClick="triggerReboot()">Reboot</button>
</div> </div>
<div id="displayMessageId" class="logmsg"></div> <div id="displayMessageId" class="logmsg"></div>
<div class="btn_cnt"><img id="led" src="led-off.png"><a href="#"><img id="button" src="btn.png"></a></div> <div class="btn_cnt"><img id="led" src="led-off.png"><a href="#"><img id="button" src="btn.png"></a></div>

View File

@ -183,6 +183,7 @@ void setup(void)
setupFS(); setupFS();
setupIAS(); setupIAS();
W.begin(); W.begin();
W.setIAS(&IAS);
delay(100); delay(100);
R.begin(relay1Pin, relay2Pin); R.begin(relay1Pin, relay2Pin);
timeClient.begin(); timeClient.begin();