From f71d2613a00213bc7e0af8f7593e89fa043b9907 Mon Sep 17 00:00:00 2001 From: fvanroie Date: Fri, 24 Jan 2020 19:50:07 +0100 Subject: [PATCH] Stypesweep ARDUINO_ARCH defines --- src/hasp.cpp | 5 +- src/hasp_config.cpp | 2 +- src/hasp_debug.cpp | 2 +- src/hasp_gui.cpp | 4 +- src/hasp_http.old | 863 -------------------------------------------- src/hasp_log.cpp | 2 +- src/hasp_mdns.cpp | 2 +- src/hasp_spiffs.cpp | 4 +- src/hasp_tft.cpp | 12 +- 9 files changed, 16 insertions(+), 880 deletions(-) delete mode 100644 src/hasp_http.old diff --git a/src/hasp.cpp b/src/hasp.cpp index c0971484..323a0981 100644 --- a/src/hasp.cpp +++ b/src/hasp.cpp @@ -12,7 +12,7 @@ #include "hasp_conf.h" #if LV_USE_HASP_SPIFFS -#ifdef ESP32 +#if defined(ARDUINO_ARCH_ESP32) //#include "lv_zifont.h" #include "SPIFFS.h" #endif @@ -83,7 +83,7 @@ static const char * btnm_map2[] = {"0", "1", "\n", "2", "3", "\n", "4", "5", "\n", "6", "7", "\n", "P1", "P2", "P3", ""}; */ -#ifdef ESP8266 +#if defined(ARDUINO_ARCH_ESP8266) lv_obj_t * pages[4]; // lv_style_t styles[6]; #else @@ -674,7 +674,6 @@ void haspSetup(JsonObject settings) my_font = (lv_font_t *)lv_mem_alloc(sizeof(lv_font_t)); lv_zifont_init(); - //#ifdef ESP32 if(lv_zifont_font_init(my_font, haspZiFontPath.c_str(), 24) != 0) { errorPrintln(String(F("HASP: %sFailed to set the custom font to ")) + haspZiFontPath); diff --git a/src/hasp_config.cpp b/src/hasp_config.cpp index ec738adb..f92ec296 100644 --- a/src/hasp_config.cpp +++ b/src/hasp_config.cpp @@ -1,7 +1,7 @@ #include "Arduino.h" #include "ArduinoJson.h" -#ifdef ESP32 +#if defined(ARDUINO_ARCH_ESP32) #include "SPIFFS.h" #endif #include // Include the SPIFFS library diff --git a/src/hasp_debug.cpp b/src/hasp_debug.cpp index 57af2f67..83ef28c8 100644 --- a/src/hasp_debug.cpp +++ b/src/hasp_debug.cpp @@ -1,7 +1,7 @@ #include #include "ArduinoJson.h" -#ifdef ESP8266 +#if defined(ARDUINO_ARCH_ESP8266) #include #include #else diff --git a/src/hasp_gui.cpp b/src/hasp_gui.cpp index 10dccc50..85f64c8d 100644 --- a/src/hasp_gui.cpp +++ b/src/hasp_gui.cpp @@ -5,7 +5,7 @@ #include "TFT_eSPI.h" -#ifdef ESP32 +#if defined(ARDUINO_ARCH_ESP32) //#include "png_decoder.h" #endif #include "lv_zifont.h" @@ -123,7 +123,7 @@ void guiSetup(TFT_eSPI & screen, JsonObject settings) tft = screen; lv_init(); -#if ESP32 +#if defined(ARDUINO_ARCH_ESP32) /* allocate on iram (or psram ?) */ buffer_size = 1024 * 8; static lv_color_t * guiVdbBuffer = (lv_color_t *)malloc(sizeof(lv_color_t) * buffer_size); diff --git a/src/hasp_http.old b/src/hasp_http.old deleted file mode 100644 index 1128bde8..00000000 --- a/src/hasp_http.old +++ /dev/null @@ -1,863 +0,0 @@ -//#include "webServer.h" -#include -#include "ArduinoJson.h" - -#include "hasp_log.h" -#include "hasp_debug.h" -#include "hasp_http.h" -#include "hasp_mqtt.h" -#include "hasp_wifi.h" -#include "hasp_config.h" -#include "hasp.h" - -#if defined(ARDUINO_ARCH_ESP32) -#include "SPIFFS.h" -#endif -#include -#include - -bool httpEnable = true; -bool webServerStarted = false; -uint16_t httpPort = 80; -FS * filesystem = &SPIFFS; -File fsUploadFile; -String httpUser = "admin"; -String httpPassword = ""; - -#if defined(ARDUINO_ARCH_ESP8266) -#include -ESP8266WebServer webServer(80); -#endif - -#if defined(ARDUINO_ARCH_ESP32) -#include // needed to get the ResetInfo -#include -WebServer webServer(80); - -// Compatibility function for ESP8266 getRestInfo -String esp32ResetReason(uint8_t cpuid) -{ - if(cpuid > 1) { - return F("Invalid CPU id"); - } - RESET_REASON reason = rtc_get_reset_reason(cpuid); - - switch(reason) { - case 1: - return F("POWERON_RESET"); - break; /**<1, Vbat power on reset*/ - case 3: - return F("SW_RESET"); - break; /**<3, Software reset digital core*/ - case 4: - return F("OWDT_RESET"); - break; /**<4, Legacy watch dog reset digital core*/ - case 5: - return F("DEEPSLEEP_RESET"); - break; /**<5, Deep Sleep reset digital core*/ - case 6: - return F("SDIO_RESET"); - break; /**<6, Reset by SLC module, reset digital core*/ - case 7: - return F("TG0WDT_SYS_RESET"); - break; /**<7, Timer Group0 Watch dog reset digital core*/ - case 8: - return F("TG1WDT_SYS_RESET"); - break; /**<8, Timer Group1 Watch dog reset digital core*/ - case 9: - return F("RTCWDT_SYS_RESET"); - break; /**<9, RTC Watch dog Reset digital core*/ - case 10: - return F("INTRUSION_RESET"); - break; /**<10, Instrusion tested to reset CPU*/ - case 11: - return F("TGWDT_CPU_RESET"); - break; /**<11, Time Group reset CPU*/ - case 12: - return F("SW_CPU_RESET"); - break; /**<12, Software reset CPU*/ - case 13: - return F("RTCWDT_CPU_RESET"); - break; /**<13, RTC Watch dog Reset CPU*/ - case 14: - return F("EXT_CPU_RESET"); - break; /**<14, for APP CPU, reseted by PRO CPU*/ - case 15: - return F("RTCWDT_BROWN_OUT_RESET"); - break; /**<15, Reset when the vdd voltage is not stable*/ - case 16: - return F("RTCWDT_RTC_RESET"); - break; /**<16, RTC Watch dog reset digital core and rtc module*/ - default: - return F("NO_MEAN"); - } -} - -// these need to be removed -const uint8_t D0 = 0; -const uint8_t D1 = 1; -const uint8_t D2 = 2; -#endif // ESP32 - -static const char HTTP_DOCTYPE[] PROGMEM = - ""; -static const char HTTP_META_GO_BACK[] PROGMEM = ""; -static const char HTTP_HEADER[] PROGMEM = "%s"; -static const char HTTP_STYLE[] PROGMEM = - ""; -static const char HTTP_SCRIPT[] PROGMEM = ""; -static const char HTTP_HEADER_END[] PROGMEM = - "
"; -static const char HTTP_END[] PROGMEM = ""; -// Additional CSS style to match Hass theme -static const char HASP_STYLE[] PROGMEM = - ""; - -// these need to be removed -uint8_t motionPin = 0; // GPIO input pin for motion sensor if connected and enabled -bool debugSerialEnabled = true; // Enable USB serial debug output -bool debugTelnetEnabled = false; // Enable telnet debug output - -//////////////////////////////////////////////////////////////////////////////////////////////////// -// These defaults may be overwritten with values saved by the web interface -char motionPinConfig[3] = "0"; -//////////////////////////////////////////////////////////////////////////////////////////////////// - -// URL for auto-update "version.json" -const char UPDATE_URL[] = "http://haswitchplate.com/update/version.json"; -// Default link to compiled Arduino firmware image -String espFirmwareUrl = "http://haswitchplate.com/update/HASwitchPlate.ino.d1_mini.bin"; -// Default link to compiled Nextion firmware images -String lcdFirmwareUrl = "http://haswitchplate.com/update/HASwitchPlate.tft"; - -//////////////////////////////////////////////////////////////////////////////////////////////////// -String formatBytes(size_t bytes); - -//////////////////////////////////////////////////////////////////////////////////////////////////// -bool httpIsAuthenticated(const String & page) -{ - if(httpPassword[0] != '\0') { // Request HTTP auth if httpPassword is set - if(!webServer.authenticate(httpUser.c_str(), httpPassword.c_str())) { - webServer.requestAuthentication(); - return false; - } - } - char buffer[128]; - sprintf(buffer, PSTR("HTTP: Sending %s page to client connected from: %s"), page.c_str(), - webServer.client().remoteIP().toString().c_str()); - debugPrintln(buffer); - return true; -} - -void webSendPage(String & nodename, uint32_t httpdatalength, bool gohome = false) -{ - char buffer[64]; - - /* Calculate Content Length upfront */ - uint16_t contentLength = 0; - contentLength += sizeof(HTTP_DOCTYPE) - 1; - contentLength += sizeof(HTTP_HEADER) - 1 - 2 + nodename.length(); - contentLength += sizeof(HTTP_SCRIPT) - 1; - contentLength += sizeof(HTTP_STYLE) - 1; - contentLength += sizeof(HASP_STYLE) - 1; - if(gohome) contentLength += sizeof(HTTP_META_GO_BACK) - 1; - contentLength += sizeof(HTTP_HEADER_END) - 1; - contentLength += sizeof(HTTP_END) - 1; - - webServer.setContentLength(contentLength + httpdatalength); - - webServer.send_P(200, PSTR("text/html"), HTTP_DOCTYPE); // 122 - sprintf_P(buffer, HTTP_HEADER, nodename.c_str()); - webServer.sendContent(buffer); // 17-2+len - webServer.sendContent_P(HTTP_SCRIPT); // 131 - webServer.sendContent_P(HTTP_STYLE); // 487 - webServer.sendContent_P(HASP_STYLE); // 145 - if(gohome) webServer.sendContent_P(HTTP_META_GO_BACK); // 47 - webServer.sendContent_P(HTTP_HEADER_END); // 80 -} - -void webHandleRoot() -{ - if(!httpIsAuthenticated(F("root"))) return; - - char buffer[64]; - String nodename = haspGetNodename(); - String httpMessage((char *)0); - httpMessage.reserve(1024); - - httpMessage += String(F("

")); - httpMessage += String(nodename); - httpMessage += String(F("

")); - - httpMessage += F("

"); - httpMessage += F("

"); - - httpMessage += - F("

"); - httpMessage += - F("

"); - - webSendPage(nodename, httpMessage.length(), false); - webServer.sendContent(httpMessage); // len - webServer.sendContent_P(HTTP_END); // 20 -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// -void webHandleAbout() -{ // http://plate01/about - if(!httpIsAuthenticated(F("/about"))) return; - - String nodename = haspGetNodename(); - String httpMessage((char *)0); - httpMessage.reserve(1250); - - httpMessage += F("

HASP OpenHardware edition

Copyright© 2020 Francis Van Roie " - "
MIT License

"); - httpMessage += F("

Based on the previous work of the following open source developers.


"); - httpMessage += - F("

HASwitchPlate

Copyright© 2019 Allen Derusha allen@derusha.org
MIT License

"); - httpMessage += - F("

LittlevGL

Copyright© 2016 Gábor Kiss-Vámosi
Copyright© 2019 " - "LittlevGL
MIT License

"); - httpMessage += F("

Lvgl ziFont Font Engine

Copyright© 2020 Francis Van Roie
MIT License

"); - httpMessage += F("

TFT_eSPI Library

Copyright© 2017 Bodmer (https://github.com/Bodmer) All " - "rights reserved.
FreeBSD License
"); - httpMessage += - F("includes parts from the Adafruit_GFX library - Copyright© 2012 Adafruit Industries. All rights " - "reserved. BSD License

"); - httpMessage += F("

ArduinoJson

Copyright© 2014-2019 Benoit BLANCHON
MIT License

"); - httpMessage += F("

PubSubClient

Copyright© 2008-2015 Nicholas O'Leary
MIT License

"); - httpMessage += F("

Syslog

Copyright© 2016 Martin Sloup
MIT License

"); - - httpMessage += F("

"); - - webSendPage(nodename, httpMessage.length(), false); - webServer.sendContent(httpMessage); // len - webServer.sendContent_P(HTTP_END); // 20 -} -//////////////////////////////////////////////////////////////////////////////////////////////////// -void webHandleInfo() -{ // http://plate01/ - if(!httpIsAuthenticated(F("/info"))) return; - - char buffer[64]; - String nodename = haspGetNodename(); - String httpMessage((char *)0); - httpMessage.reserve(1024); - - httpMessage += F("
MQTT Status: "); - if(mqttIsConnected()) { // Check MQTT connection - httpMessage += String(F("Connected")); - } else { - httpMessage += String(F("Disconnected, return code: ")); - // +String(mqttClient.returnCode()); - } - httpMessage += String(F("
MQTT ClientID: ")); - // +String(mqttClientId); - httpMessage += F("
HASP Version: "); - httpMessage += String(haspGetVersion()); - httpMessage += F("
Uptime: "); - httpMessage += String(long(millis() / 1000)); - - // httpMessage += String(F("
LCD Model: ")) + String(LV_HASP_HOR_RES_MAX) + " x " + - // String(LV_HASP_VER_RES_MAX); httpMessage += String(F("
LCD Version: ")) + String(lcdVersion); - httpMessage += F("

LCD Active Page: "); - httpMessage += String(haspGetPage()); - httpMessage += F("
CPU Frequency: "); - httpMessage += String(ESP.getCpuFreqMHz()); - - httpMessage += F("MHz

SSID: "); - httpMessage += String(WiFi.SSID()); - httpMessage += F("
Signal Strength: "); - httpMessage += String(WiFi.RSSI()); - httpMessage += F("
IP Address: "); - httpMessage += String(WiFi.localIP().toString()); - httpMessage += F("
Gateway: "); - httpMessage += String(WiFi.gatewayIP().toString()); - httpMessage += F("
DNS Server: "); - httpMessage += String(WiFi.dnsIP().toString()); - httpMessage += F("
MAC Aress: "); - httpMessage += String(WiFi.macAddress()); - - httpMessage += F("

ESP Chip Id: "); -#if defined(ARDUINO_ARCH_ESP32) - httpMessage += String(ESP.getChipRevision()); -#else - httpMessage += String(ESP.getChipId()); -#endif - httpMessage += F("
Flash Chip Size: "); - httpMessage += formatBytes(ESP.getFlashChipSize()); - httpMessage += F("
Program Size: "); - httpMessage += formatBytes(ESP.getSketchSize()); - httpMessage += F(" bytes
Free Program Space: "); - httpMessage += formatBytes(ESP.getFreeSketchSpace()); - httpMessage += F(" bytes
Free Memory: "); - httpMessage += formatBytes(ESP.getFreeHeap()); - -#if defined(ARDUINO_ARCH_ESP32) - // httpMessage += F("
Heap Max Alloc: "); - // httpMessage += String(ESP.getMaxAllocHeap()); - httpMessage += F("
Memory Fragmentation: "); - httpMessage += String((int16_t)(100.00f - (float)ESP.getMaxAllocHeap() / (float)ESP.getFreeHeap() * 100.00f)); - httpMessage += F("
ESP SDK version: "); - httpMessage += String(ESP.getSdkVersion()); - httpMessage += F("
Last Reset: CPU0: "); - httpMessage += String(esp32ResetReason(0)); - httpMessage += F(" / CPU1: "); - httpMessage += String(esp32ResetReason(1)); -#else - httpMessage += F("
Memory Fragmentation: "); - httpMessage += String(ESP.getHeapFragmentation()); - httpMessage += F("
ESP Core version: "); - httpMessage += String(ESP.getCoreVersion()); - httpMessage += F("
Last Reset: "); - httpMessage += String(ESP.getResetInfo()); -#endif - - httpMessage += F("

"); - - webSendPage(nodename, httpMessage.length(), false); - webServer.sendContent(httpMessage); // len - webServer.sendContent_P(HTTP_END); // 20 -} - -String formatBytes(size_t bytes) -{ - if(bytes < 1024) { - return String(bytes) + "B"; - } else if(bytes < (1024 * 1024)) { - return String(bytes / 1024.0) + "KB"; - } else if(bytes < (1024 * 1024 * 1024)) { - return String(bytes / 1024.0 / 1024.0) + "MB"; - } else { - return String(bytes / 1024.0 / 1024.0 / 1024.0) + "GB"; - } -} - -String getContentType(String filename) -{ - if(webServer.hasArg(F("download"))) { - return F("application/octet-stream"); - } else if(filename.endsWith(F(".htm"))) { - return F("text/html"); - } else if(filename.endsWith(F(".html"))) { - return F("text/html"); - } else if(filename.endsWith(F(".css"))) { - return F("text/css"); - } else if(filename.endsWith(F(".js"))) { - return F("application/javascript"); - } else if(filename.endsWith(F(".png"))) { - return F("image/png"); - } else if(filename.endsWith(F(".gif"))) { - return F("image/gif"); - } else if(filename.endsWith(F(".jpg"))) { - return F("image/jpeg"); - } else if(filename.endsWith(F(".ico"))) { - return F("image/x-icon"); - } else if(filename.endsWith(F(".xml"))) { - return F("text/xml"); - } else if(filename.endsWith(F(".pdf"))) { - return F("application/x-pdf"); - } else if(filename.endsWith(F(".zip"))) { - return F("application/x-zip"); - } else if(filename.endsWith(F(".gz"))) { - return F("application/x-gzip"); - } - return F("text/plain"); -} - -String urldecode(String str) -{ - String encodedString = ""; - char c; - char code0; - char code1; - for(int i = 0; i < str.length(); i++) { - c = str.charAt(i); - if(c == '+') { - encodedString += ' '; - } else if(c == '%') { - char buffer[3]; - i++; - buffer[0] = str.charAt(i); - i++; - buffer[1] = str.charAt(i); - buffer[2] = '\0'; - c = (char)strtol((const char *)&buffer, NULL, 16); - encodedString += c; - } else { - encodedString += c; - } - yield(); - } - return encodedString; -} - -bool handleFileRead(String path) -{ - path = urldecode(path).substring(0, 31); - if(!httpIsAuthenticated(path)) return false; - - if(path.endsWith("/")) { - path += F("index.htm"); - } - String pathWithGz = path + F(".gz"); - if(filesystem->exists(pathWithGz) || filesystem->exists(path)) { - if(filesystem->exists(pathWithGz)) path += F(".gz"); - - File file = filesystem->open(path, "r"); - String contentType = getContentType(path); - if(path == F("/edit.htm.gz")) { - contentType = F("text/html"); - } - webServer.streamFile(file, contentType); - file.close(); - return true; - } - return false; -} - -void handleFileUpload() -{ - if(webServer.uri() != "/edit") { - return; - } - HTTPUpload & upload = webServer.upload(); - if(upload.status == UPLOAD_FILE_START) { - String filename = upload.filename; - if(!filename.startsWith("/")) { - filename = "/" + filename; - } - debugPrintln(String(F("handleFileUpload Name: ")) + filename); - fsUploadFile = filesystem->open(filename, "w"); - filename.clear(); - } else if(upload.status == UPLOAD_FILE_WRITE) { - // DBG_OUTPUT_PORT.print("handleFileUpload Data: "); debugPrintln(upload.currentSize); - if(fsUploadFile) { - fsUploadFile.write(upload.buf, upload.currentSize); - } - } else if(upload.status == UPLOAD_FILE_END) { - if(fsUploadFile) { - fsUploadFile.close(); - } - debugPrintln(String(F("handleFileUpload Size: ")) + String(upload.totalSize)); - } -} - -void handleFileDelete() -{ - if(webServer.args() == 0) { - return webServer.send(500, PSTR("text/plain"), PSTR("BAD ARGS")); - } - String path = webServer.arg(0); - debugPrintln(String(F("handleFileDelete: ")) + path); - if(path == "/") { - return webServer.send(500, PSTR("text/plain"), PSTR("BAD PATH")); - } - if(!filesystem->exists(path)) { - return webServer.send(404, PSTR("text/plain"), PSTR("FileNotFound")); - } - filesystem->remove(path); - webServer.send(200, PSTR("text/plain"), ""); - path.clear(); -} - -void handleFileCreate() -{ - if(webServer.args() == 0) { - return webServer.send(500, PSTR("text/plain"), PSTR("BAD ARGS")); - } - String path = webServer.arg(0); - debugPrintln(String(F("handleFileCreate: ")) + path); - if(path == "/") { - return webServer.send(500, PSTR("text/plain"), PSTR("BAD PATH")); - } - if(filesystem->exists(path)) { - return webServer.send(500, PSTR("text/plain"), PSTR("FILE EXISTS")); - } - File file = filesystem->open(path, "w"); - if(file) { - file.close(); - } else { - return webServer.send(500, PSTR("text/plain"), PSTR("CREATE FAILED")); - } - webServer.send(200, PSTR("text/plain"), ""); - path.clear(); -} - -void handleFileList() -{ - if(!webServer.hasArg(F("dir"))) { - webServer.send(500, PSTR("text/plain"), PSTR("BAD ARGS")); - return; - } - - String path = webServer.arg(F("dir")); - debugPrintln(String(F("handleFileList: ")) + path); - path.clear(); - -#if defined(ARDUINO_ARCH_ESP32) - debugPrintln(PSTR("HTTP: Listing files on the internal flash:")); - File root = SPIFFS.open("/"); - File file = root.openNextFile(); - String output = "["; - - while(file) { - if(output != "[") { - output += ','; - } - bool isDir = false; - output += F("{\"type\":\""); - output += (isDir) ? F("dir") : F("file"); - output += F("\",\"name\":\""); - if(file.name()[0] == '/') { - output += &(file.name()[1]); - } else { - output += file.name(); - } - output += F("\"}"); - - char msg[64]; - sprintf(msg, PSTR("HTTP: * %s (%u bytes)"), file.name(), (uint32_t)file.size()); - debugPrintln(msg); - - // file.close(); - file = root.openNextFile(); - } - output += "]"; -#else - Dir dir = filesystem->openDir(path); - String output = "["; - while(dir.next()) { - File entry = dir.openFile("r"); - if(output != "[") { - output += ','; - } - bool isDir = false; - output += F("{\"type\":\""); - output += (isDir) ? F("dir") : F("file"); - output += F("\",\"name\":\""); - if(entry.name()[0] == '/') { - output += &(entry.name()[1]); - } else { - output += entry.name(); - } - output += F("\"}"); - entry.close(); - } - output += "]"; -#endif - webServer.send(200, PSTR("text/json"), output); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// -void webHandleConfig() -{ // http://plate01/ - if(!httpIsAuthenticated(F("/config"))) return; - - char buffer[64]; - String nodename = haspGetNodename(); - String httpMessage((char *)0); - httpMessage.reserve(1024); - - httpMessage += String(F("
")); - httpMessage += String(F("WiFi SSID (required)"; - httpMessage += String(F("
WiFi Password (required)"; - httpMessage += - F("

HASP Node Name (required. lowercase letters, numbers, and _ only)" - ""; - httpMessage += F("

Group Name (required)"; - httpMessage += F("

MQTT Broker (required)"; - httpMessage += F("
MQTT Port (required)"; - httpMessage += F("
MQTT User (optional)"; - httpMessage += F("
MQTT Password (optional)

HASP Admin Username (optional)"; - httpMessage += - String(F("
HASP Admin Password (optional)

Motion Sensor Pin: ")); - - httpMessage += String(F("
Serial debug output enabled:
Telnet debug output enabled:
mDNS enabled:

")); - - if (updateEspAvailable) - { - httpMessage += String(F("

HASP Update - available!

")); httpMessage += String(F("
")); - httpMessage += String(F(""; httpMessage += String(F("
")); - }*/ - - httpMessage += F("
"); - - httpMessage += F("

"); - - httpMessage += F("
"); - - webSendPage(nodename, httpMessage.length(), false); - webServer.sendContent(httpMessage); // len - webServer.sendContent_P(HTTP_END); // 20 -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// -void httpHandleNotFound() -{ // webServer 404 - if(handleFileRead(webServer.uri())) return; - - debugPrintln(String(F("HTTP: Sending 404 to client connected from: ")) + webServer.client().remoteIP().toString()); - - String httpMessage((char *)0); - httpMessage.reserve(128); - httpMessage += F("File Not Found\n\nURI: "); - httpMessage += webServer.uri(); - httpMessage += F("\nMethod: "); - httpMessage += (webServer.method() == HTTP_GET) ? F("GET") : F("POST"); - httpMessage += F("\nArguments: "); - httpMessage += webServer.args(); - httpMessage += "\n"; - for(uint8_t i = 0; i < webServer.args(); i++) { - httpMessage += " " + webServer.argName(i) + ": " + webServer.arg(i) + "\n"; - } - webServer.send(404, PSTR("text/plain"), httpMessage.c_str()); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// -void webHandleSaveConfig() -{ - if(!httpIsAuthenticated(F("/saveconfig"))) return; - - configWriteConfig(); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// -void webHandleFirmware() -{ - if(!httpIsAuthenticated(F("/firmware"))) return; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// -void httpHandleEspFirmware() -{ // http://plate01/espfirmware - if(!httpIsAuthenticated(F("/espfirmware"))) return; - - String nodename = haspGetNodename(); - char buffer[64]; - String httpMessage((char *)0); - httpMessage.reserve(128); - httpMessage += String(F("

")); - httpMessage += String(haspGetNodename()); - httpMessage += String(F(" ESP update


Updating ESP firmware from: ")); - httpMessage += String(webServer.arg("espFirmware")); - - webSendPage(nodename, httpMessage.length(), true); - webServer.sendContent(httpMessage); // len - webServer.sendContent_P(HTTP_END); // 20 - - debugPrintln(String(F("HTTP: Attempting ESP firmware update from: ")) + String(webServer.arg("espFirmware"))); - // espStartOta(webServer.arg("espFirmware")); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// -void httpHandleReboot() -{ // http://plate01/reboot - if(!httpIsAuthenticated(F("/reboot"))) return; - - String nodename = haspGetNodename(); - String httpMessage = F("Rebooting Device"); - webSendPage(nodename, httpMessage.length(), true); - webServer.sendContent(httpMessage); // len - webServer.sendContent_P(HTTP_END); // 20 - delay(500); - - debugPrintln(PSTR("HTTP: Reboot device")); - haspSetPage(0); - haspSetAttr(F("p[0].b[1].txt"), F("\"Rebooting...\"")); - - delay(500); - haspReset(); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// -void httpHandleResetConfig() -{ // http://plate01/resetConfig - if(!httpIsAuthenticated(F("/espfirmware"))) return; - - bool resetConfirmed = webServer.arg(F("confirm")) == F("yes"); - String nodename = haspGetNodename(); - char buffer[64]; - String httpMessage((char *)0); - httpMessage.reserve(128); - - if(resetConfirmed) { // User has confirmed, so reset everything - httpMessage += F("

"); - httpMessage += haspGetNodename(); - httpMessage += F("

Resetting all saved settings and restarting device into WiFi AP mode"); - } else { - httpMessage += F("

Warning

This process will reset all settings to the default values and " - "restart the device. You may need to connect to the WiFi AP displayed on the panel to " - "re-configure the device before accessing it again." - "


" - "

" - "


" - "
"); - } - - webSendPage(nodename, httpMessage.length(), resetConfirmed); - webServer.sendContent(httpMessage); // len - webServer.sendContent_P(HTTP_END); // 20 - - if(resetConfirmed) { - delay(1000); - // configClearSaved(); - } -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// -void httpSetup(const JsonObject & settings) -{ - webServer.on(F("/page/"), []() { - String pageid = webServer.arg(F("page")); - webServer.send(200, PSTR("text/plain"), "Page: '" + pageid + "'"); - haspSetPage(pageid.toInt()); - }); - - webServer.on("/list", HTTP_GET, handleFileList); - // load editor - webServer.on("/edit", HTTP_GET, []() { - if(!handleFileRead("/edit.htm")) { - webServer.send(404, "text/plain", "FileNotFound"); - } - }); - webServer.on("/edit", HTTP_PUT, handleFileCreate); - webServer.on("/edit", HTTP_DELETE, handleFileDelete); - // first callback is called after the request has ended with all parsed arguments - // second callback handles file uploads at that location - webServer.on("/edit", HTTP_POST, []() { webServer.send(200, "text/plain", ""); }, handleFileUpload); - // get heap status, analog input value and all GPIO statuses in one json call - webServer.on("/all", HTTP_GET, []() { - String json('{'); - json += "\"heap\":" + String(ESP.getFreeHeap()); - json += ", \"analog\":" + String(analogRead(A0)); - json += "}"; - webServer.send(200, "text/json", json); - json.clear(); - }); - - webServer.on(F("/"), webHandleRoot); - webServer.on(F("/about"), webHandleAbout); - webServer.on(F("/info"), webHandleInfo); - webServer.on(F("/config"), webHandleConfig); - webServer.on(F("/saveConfig"), webHandleSaveConfig); - webServer.on(F("/resetConfig"), httpHandleResetConfig); - webServer.on(F("/firmware"), webHandleFirmware); - webServer.on(F("/espfirmware"), httpHandleEspFirmware); - webServer.on(F("/reboot"), httpHandleReboot); - webServer.onNotFound(httpHandleNotFound); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// -void httpReconnect() -{ - if(!httpEnable) return; - - webServer.stop(); - webServer.begin(); - debugPrintln(String(F("HTTP: Server started @ http://")) + WiFi.localIP().toString()); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// -void httpLoop(bool wifiIsConnected) -{ - if(httpEnable) webServer.handleClient(); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// -bool httpGetConfig(const JsonObject & settings) -{ - if(!settings.isNull() && settings[F_CONFIG_ENABLE] == httpEnable && settings[F_CONFIG_PORT] == httpPort && - settings[F_CONFIG_USER] == httpUser && settings[F_CONFIG_PASS] == httpPassword) - return false; - - settings[F_CONFIG_ENABLE] = httpEnable; - settings[F_CONFIG_PORT] = httpPort; - settings[F_CONFIG_USER] = httpUser; - settings[F_CONFIG_PASS] = httpPassword; - - size_t size = serializeJson(settings, Serial); - Serial.println(); - - return true; -} \ No newline at end of file diff --git a/src/hasp_log.cpp b/src/hasp_log.cpp index c833a6d4..7eb1e702 100644 --- a/src/hasp_log.cpp +++ b/src/hasp_log.cpp @@ -1,6 +1,6 @@ #include -#ifdef ESP8266 +#if defined(ARDUINO_ARCH_ESP8266) #include #include #else diff --git a/src/hasp_mdns.cpp b/src/hasp_mdns.cpp index b8471c13..c573c222 100644 --- a/src/hasp_mdns.cpp +++ b/src/hasp_mdns.cpp @@ -1,7 +1,7 @@ #include "Arduino.h" #include "ArduinoJson.h" -#ifdef ESP32 +#if defined(ARDUINO_ARCH_ESP32) #include #else #include diff --git a/src/hasp_spiffs.cpp b/src/hasp_spiffs.cpp index 2819bae3..1ecff2eb 100644 --- a/src/hasp_spiffs.cpp +++ b/src/hasp_spiffs.cpp @@ -38,7 +38,7 @@ void spiffsList() */ void spiffsSetup() { - // no spiffs settings + // no SPIFFS settings, as settings depend on SPIFFS #if LV_USE_HASP_SPIFFS char msg[64]; @@ -48,7 +48,7 @@ void spiffsSetup() } else { sprintf(msg, PSTR("FILE: [SUCCESS] SPI flash FS mounted")); debugPrintln(msg); - // spiffsList(); + // spiffsList(); // Wait on debugSetup() } #endif } diff --git a/src/hasp_tft.cpp b/src/hasp_tft.cpp index 14d8e003..daca68a7 100644 --- a/src/hasp_tft.cpp +++ b/src/hasp_tft.cpp @@ -1,7 +1,7 @@ #include "TFT_eSPI.h" // Graphics and font library for ST7735 driver chip #include "ArduinoJson.h" -#ifdef ESP8266 +#if defined(ARDUINO_ARCH_ESP8266) ADC_MODE(ADC_VCC); // tftShowConfig measures the voltage on the pin #endif @@ -30,7 +30,7 @@ void tftSetup(TFT_eSPI & tft, JsonObject settings) tftShowConfig(tft); /* Load Calibration data */ -#ifdef ESP8266 +#if defined(ARDUINO_ARCH_ESP8266) uint16_t calData[5] = {238, 3529, 369, 3532, 6}; #else uint16_t calData[5] = {294, 3410, 383, 3491, 4}; @@ -106,7 +106,7 @@ void tftShowConfig(TFT_eSPI & tft) sprintf_P(buffer, PSTR("TFT: Frequency = %i MHz"), ESP.getCpuFreqMHz()); debugPrintln(buffer); -#ifdef ESP8266 +#if defined(ARDUINO_ARCH_ESP8266) sprintf_P(buffer, PSTR("TFT: Voltage = %2.2f V"), ESP.getVcc() / 918.0); debugPrintln(buffer); // 918 empirically determined #endif @@ -114,7 +114,7 @@ void tftShowConfig(TFT_eSPI & tft) debugPrintln(buffer); sprintf_P(buffer, PSTR("TFT: Interface = %s"), (tftSetup.serial == 1) ? PSTR("SPI") : PSTR("Parallel")); debugPrintln(buffer); -#ifdef ESP8266 +#if defined(ARDUINO_ARCH_ESP8266) sprintf_P(buffer, PSTR("TFT: SPI overlap = %s"), (tftSetup.overlap == 1) ? PSTR("Yes") : PSTR("No")); debugPrintln(buffer); #endif @@ -147,7 +147,7 @@ void tftShowConfig(TFT_eSPI & tft) tftPinInfo(F("MISO "), tftSetup.pin_tft_miso); tftPinInfo(F("SCLK "), tftSetup.pin_tft_clk); -#ifdef ESP8266 +#if defined(ARDUINO_ARCH_ESP8266) if(tftSetup.overlap == true) { debugPrintln(F("Overlap selected, following pins MUST be used:\n")); @@ -204,7 +204,7 @@ void tftShowConfig(TFT_eSPI & tft) int8_t getPinName(int8_t pin) { // For ESP32 pin labels on boards use the GPIO number -#ifdef ESP32 +#if defined(ARDUINO_ARCH_ESP32) return pin; #endif