diff --git a/platformio.ini b/platformio.ini index 6781848f..b366abca 100644 --- a/platformio.ini +++ b/platformio.ini @@ -194,10 +194,10 @@ build_flags = [env:d1mini-lolintft24] platform = espressif8266@2.3.2 board = d1_mini -upload_port = COM4 ; Change to the correct port +upload_port = COM13 ; Change to the correct port ;upload_protocol = espota ; Use ArduinoOTA after flashing over serial ;upload_port = 10.1.0.148 ; IP of the ESP -monitor_port = COM4 ; Change to the correct port +monitor_port = COM13 ; Change to the correct port monitor_speed = 74880 board_build.f_flash = 40000000L board_build.flash_mode = dout diff --git a/src/hasp_dispatch.cpp b/src/hasp_dispatch.cpp index 80ebd42d..21e30497 100644 --- a/src/hasp_dispatch.cpp +++ b/src/hasp_dispatch.cpp @@ -76,8 +76,8 @@ void dispatchAttribute(String & strTopic, const char * payload) } // valid page } } else if(strTopic.startsWith(F("output"))) { - uint8_t state = isON(payload) ? HIGH : LOW; #if defined(ARDUINO_ARCH_ESP8266) + uint8_t state = isON(payload) ? HIGH : LOW; digitalWrite(D1, state); #endif @@ -222,7 +222,7 @@ void dispatchJsonl(char * strPayload) DynamicJsonDocument config(256); String output((char *)0); - output.reserve(MQTT_MAX_PACKET_SIZE+ 256); + output.reserve(MQTT_MAX_PACKET_SIZE + 256); StringStream stream((String &)output); stream.print(strPayload); diff --git a/src/hasp_http.cpp b/src/hasp_http.cpp index d925acad..73618c78 100644 --- a/src/hasp_http.cpp +++ b/src/hasp_http.cpp @@ -1,6 +1,7 @@ //#include "webServer.h" -#include +#include "Arduino.h" #include "ArduinoJson.h" +//#include "Update.h" #include "lvgl.h" #include "hasp_conf.h" @@ -241,7 +242,7 @@ void webHandleScreenshot() if(!httpIsAuthenticated(F("screenshot"))) return; if(webServer.hasArg(F("q"))) { - webServer.setContentLength(138 + 320 * 240 * 4); + webServer.setContentLength(122 + 320 * 240 * 2); webServer.send(200, PSTR("image/bmp"), ""); guiTakeScreenshot(webServer); @@ -336,7 +337,7 @@ void webHandleInfo() /* HASP Stats */ httpMessage += F("HASP Version: "); httpMessage += String(haspGetVersion()); - httpMessage += F("Build DateTime: "); + httpMessage += F("
Build DateTime: "); httpMessage += __DATE__; httpMessage += F(" "); httpMessage += __TIME__; @@ -385,7 +386,7 @@ void webHandleInfo() // +String(mqttClient.returnCode()); } httpMessage += F("
MQTT ClientID: "); - // +String(mqttClientId); + httpMessage += nodename; /* ESP Stats */ httpMessage += F("

ESP Chip Id: "); @@ -504,6 +505,33 @@ bool handleFileRead(String path) return false; } +/* +void handleFirmwareUpdate() +{ + upload = &webServer.upload(); + if(upload->status == UPLOAD_FILE_START) { + if(!httpIsAuthenticated(F("firmwareupdate"))) return false; + Serial.printf("Update: %s\n", upload->filename.c_str()); + if(!Update.begin(UPDATE_SIZE_UNKNOWN)) { // start with max available size + Update.printError(Serial); + } + } else if(upload->status == UPLOAD_FILE_WRITE) { + // flashing firmware to / +if(Update.write(upload->buf, upload->currentSize) != upload->currentSize) { + Update.printError(Serial); +} +} +else if(upload->status == UPLOAD_FILE_END) +{ + if(Update.end(true)) { // true to set the size to the current progress + Serial.printf("Update Success: %u\nRebooting...\n", upload->totalSize); + } else { + Update.printError(Serial); + } +} +} +*/ + void handleFileUpload() { if(webServer.uri() != "/edit") { diff --git a/src/hasp_mdns.cpp b/src/hasp_mdns.cpp index 9b9b3a3b..f76f6ab3 100644 --- a/src/hasp_mdns.cpp +++ b/src/hasp_mdns.cpp @@ -41,6 +41,13 @@ void mdnsStart() MDNS.addService(F("http"), F("tcp"), 80); MDNS.addService(F("telnet"), F("tcp"), 23); MDNS.addServiceTxt(hasp2Node, F("tcp"), F("app_name"), F("HASP-lvgl")); + /* + addService("arduino", "tcp", port); + addServiceTxt("arduino", "tcp", "tcp_check", "no"); + addServiceTxt("arduino", "tcp", "ssh_upload", "no"); + addServiceTxt("arduino", "tcp", "board", ARDUINO_BOARD); + addServiceTxt("arduino", "tcp", "auth_upload", (auth) ? "yes" : "no"); + */ } else { errorPrintln(String(F("MDNS: %sResponder failed to start ")) + hasp2Node); }; diff --git a/src/hasp_mqtt.cpp b/src/hasp_mqtt.cpp index 41dc5c6e..fac72f2d 100644 --- a/src/hasp_mqtt.cpp +++ b/src/hasp_mqtt.cpp @@ -201,6 +201,13 @@ void mqttStatusUpdate() mqttStatusPayload += F("\"espCore\":\""); mqttStatusPayload += halGetCoreVersion(); mqttStatusPayload += F("\""); + +#if defined(ARDUINO_ARCH_ESP8266) + mqttStatusPayload += F(","); + mqttStatusPayload += F("\"Vcc\":\""); + mqttStatusPayload += (float)ESP.getVcc() / 1000; + mqttStatusPayload += F("\""); +#endif mqttStatusPayload += "}"; // mqttClient.publish(mqttSensorTopic, mqttStatusPayload); diff --git a/src/hasp_wifi.cpp b/src/hasp_wifi.cpp index 557b889a..b5d0abe1 100644 --- a/src/hasp_wifi.cpp +++ b/src/hasp_wifi.cpp @@ -10,6 +10,7 @@ #include "hasp_log.h" #include "hasp_debug.h" #include "hasp_config.h" +#include "hasp_dispatch.h" #include "hasp_gui.h" #include "hasp.h" @@ -37,6 +38,7 @@ char wifiPassword[32] = WIFI_PASSW; #else char wifiPassword[32] = ""; #endif +uint8_t wifiReconnectCounter = 0; // const byte DNS_PORT = 53; // DNSServer dnsServer; @@ -67,19 +69,24 @@ void wifiConnected(IPAddress ipaddress) debugPrintln(buffer); if(isConnected) { - mqttReconnect(); - haspReconnect(); + /* mqttReconnect(); + haspReconnect();*/ httpReconnect(); - mdnsStart(); + // mdnsStart(); } } void wifiDisconnected(const char * ssid, uint8_t reason) { char buffer[128]; + wifiReconnectCounter++; + if(wifiReconnectCounter > 45) { + snprintf_P(buffer, sizeof(buffer), PSTR("WIFI: %%s Retries exceed %u: Rebooting..."), wifiReconnectCounter); + errorPrintln(buffer); + dispatchReboot(false); + } snprintf_P(buffer, sizeof(buffer), PSTR("WIFI: Disconnected from %s (Reason: %d)"), ssid, reason); debugPrintln(buffer); - WiFi.reconnect(); } void wifiSsidConnected(const char * ssid) @@ -87,6 +94,7 @@ void wifiSsidConnected(const char * ssid) char buffer[128]; snprintf_P(buffer, sizeof(buffer), PSTR("WIFI: Connected to SSID %s. Requesting IP..."), ssid); debugPrintln(buffer); + wifiReconnectCounter = 0; } #if defined(ARDUINO_ARCH_ESP32) @@ -141,7 +149,7 @@ void wifiSetup(JsonObject settings) WiFi.mode(WIFI_AP); WiFi.softAP(apSsdid.c_str(), buffer); - haspDisplayAP(apSsdid.c_str(), buffer); + // haspDisplayAP(apSsdid.c_str(), buffer); /* Setup the DNS server redirecting all the domains to the apIP */ // dnsServer.setErrorReplyCode(DNSReplyCode::NoError); @@ -152,7 +160,7 @@ void wifiSetup(JsonObject settings) debugPrintln(buffer); sprintf_P(buffer, PSTR("WIFI: AP IP address : %s"), IP.toString().c_str()); debugPrintln(buffer); - httpReconnect(); + // httpReconnect(); } else { WiFi.mode(WIFI_STA); @@ -177,34 +185,6 @@ void wifiSetup(JsonObject settings) bool wifiLoop() { return WiFi.status() == WL_CONNECTED; - - /* - if(WiFi.status() == WL_CONNECTED) { - if(wifiWasConnected) return true; - - debugPrintln(F("WIFI: Reconnected")); - wifiWasConnected = true; - wifiReconnectAttempt = 1; - wifiPrevMillis = millis(); - haspOnline(); - return true; - - } else if(millis() - wifiPrevMillis > 1000) { - if(wifiReconnectAttempt < 20) { - if(wifiReconnectAttempt == 1) { // <0 means we were never connected yet - // haspOffline(); - warningPrintln(String(F("WIFI: %sConnection lost. Reconnecting... #")) + - String(wifiReconnectAttempt)); WiFi.reconnect(); } else { debugPrintln(F("WIFI: Waiting for connection...")); - } - } else { - // haspOffline(); - debugPrintln(F("WIFI: Connection lost. Reconnecting...")); - WiFi.reconnect(); - } - wifiReconnectAttempt++; - wifiPrevMillis = millis(); - } - return false;*/ } bool wifiGetConfig(const JsonObject & settings) diff --git a/src/hasp_wifi.h b/src/hasp_wifi.h index 725a47f0..73628d8c 100644 --- a/src/hasp_wifi.h +++ b/src/hasp_wifi.h @@ -4,8 +4,8 @@ #include "ArduinoJson.h" void wifiSetup(JsonObject settings); -bool wifiLoop(); -void wifiStop(); +bool wifiLoop(void); +void wifiStop(void); bool wifiGetConfig(const JsonObject & settings); bool wifiSetConfig(const JsonObject & settings); diff --git a/src/main.cpp b/src/main.cpp index b3add7cd..0da8f769 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -49,6 +49,7 @@ #endif bool isConnected; +uint8_t mainLoopCounter = 0; void setup() { @@ -88,15 +89,15 @@ void setup() wifiSetup(settings[F("wifi")]); #if HASP_USE_MQTT - // mqttSetup(settings[F("mqtt")]); + mqttSetup(settings[F("mqtt")]); #endif #if HASP_USE_TELNET - // telnetSetup(settings[F("telnet")]); + telnetSetup(settings[F("telnet")]); #endif #if HASP_USE_MDNS - // mdnsSetup(settings[F("mdns")]); + mdnsSetup(settings[F("mdns")]); #endif #if HASP_USE_HTTP @@ -134,27 +135,26 @@ void loop() /* Network Services Loops */ #if HASP_USE_WIFI isConnected = wifiLoop(); + #if HASP_USE_MQTT mqttLoop(isConnected); #endif - if(isConnected) { #if HASP_USE_HTTP - httpLoop(); + httpLoop(); #endif #if HASP_USE_TELNET - telnetLoop(); + telnetLoop(); #endif #if HASP_USE_MDNS - mdnsLoop(); + mdnsLoop(); #endif #if HASP_USE_BUTTON - buttonLoop(); + buttonLoop(); #endif - } otaLoop(); debugLoop(); @@ -162,10 +162,15 @@ void loop() static unsigned long mainLastLoopTime = 0; - // Every Secons Loop + // Every Second Loop if(millis() - mainLastLoopTime >= 1000) { mainLastLoopTime += 1000; + httpEverySecond(); otaEverySecond(); + mainLoopCounter++; + if(mainLoopCounter >= 10) { + mainLoopCounter = 0; + } } // delay(1);