diff --git a/Arduino/CanGrow/CanGrow.ino b/Arduino/CanGrow/CanGrow.ino index fe1a454..b6cbaa3 100644 --- a/Arduino/CanGrow/CanGrow.ino +++ b/Arduino/CanGrow/CanGrow.ino @@ -135,7 +135,7 @@ void setup() { LFS_Init(); LoadConfig(); Wifi_Init(); - SetupWebserver(); + Webserver_Init(); } diff --git a/Arduino/CanGrow/include/CanGrow.h b/Arduino/CanGrow/include/CanGrow.h index 9bed8b4..b6a2574 100644 --- a/Arduino/CanGrow/include/CanGrow.h +++ b/Arduino/CanGrow/include/CanGrow.h @@ -43,14 +43,17 @@ #define CANGROW_SSID "CanGrow-unconfigured" +// do we need a restart? (e.g. after wifi settings change) +bool needRestart; + struct Config_WiFi { char ssid[32]; char password[64]; + bool dhcp; byte ip[4] = {192,168,4,20}; byte netmask[4] = {255,255,255,0}; - byte gateway[4] = {192,168,4,255}; + byte gateway[4] = {0,0,0,0}; byte dns[4] = {0,0,0,0}; - bool dhcp; }; Config_WiFi configWifi; diff --git a/Arduino/CanGrow/include/CanGrow_LittleFS.h b/Arduino/CanGrow/include/CanGrow_LittleFS.h index 82642be..45e0b02 100644 --- a/Arduino/CanGrow/include/CanGrow_LittleFS.h +++ b/Arduino/CanGrow/include/CanGrow_LittleFS.h @@ -183,7 +183,8 @@ bool LoadConfig() { JsonObject objWifi = doc["wifi"][0]; strlcpy(configWifi.ssid, objWifi["ssid"], sizeof(configWifi.ssid)); strlcpy(configWifi.password, objWifi["password"], sizeof(configWifi.password)); - + // Copy bool / int directly into struct + configWifi.dhcp = objWifi["dhcp"]; // load the ip addresses as array int i; for(i=0; i <4 ; i++) { @@ -192,8 +193,7 @@ bool LoadConfig() { configWifi.gateway[i] = objWifi["gateway"][i]; configWifi.dns[i] = objWifi["dns"][i]; } - // Copy bool / int directly into struct - configWifi.dhcp = objWifi["dhcp"]; + // * System * JsonObject objSystem = doc["system"][0]; diff --git a/Arduino/CanGrow/include/CanGrow_Webserver.h b/Arduino/CanGrow/include/CanGrow_Webserver.h index 54fe7d9..b93aed3 100644 --- a/Arduino/CanGrow/include/CanGrow_Webserver.h +++ b/Arduino/CanGrow/include/CanGrow_Webserver.h @@ -43,6 +43,8 @@ #include "Webserver/Page_wifi.h" +#include "Webserver/Page_system.h" + AsyncWebServer webserver(80); // log incoming requests @@ -77,22 +79,26 @@ void WebserverNotFound(AsyncWebServerRequest* request) { /* * setup all the webhandlers */ -void SetupWebserver() { +void Webserver_Init() { Serial.println(":: [Webserver] initializing"); /* url handler definition */ webserver.on("/", HTTP_GET, WebPage_root); webserver.on("/cangrow.css", HTTP_GET, WebFile_cangrow_CSS); - webserver.on("/wifiSettings", HTTP_GET, WebPage_wifi); - webserver.on("/wifiSettings", HTTP_POST, WebPage_wifi); + webserver.on("/wifi/", HTTP_GET, WebPage_wifi); + webserver.on("/wifi/", HTTP_POST, WebPage_wifi); + webserver.on("/system/", HTTP_GET, WebPage_system); + webserver.on("/system/", HTTP_POST, WebPage_system); + webserver.on("/system/restart", HTTP_GET, WebPage_system_restart); + webserver.on("/system/restart", HTTP_POST, WebPage_system_restart); requestLogger.setOutput(Serial); // this activates the middleware if(configSystem.httpLogSerial == true) { - Serial.println(":: [Webserver] request logging to serial is enabled"); + Serial.println(":: [Webserver] serial logging: enabled"); webserver.addMiddleware(&requestLogger); } else { - Serial.println(":: [Webserver] request logging to serial is disabled"); + Serial.println(":: [Webserver] serial logging: disabled"); } webserver.onNotFound(WebserverNotFound); diff --git a/Arduino/CanGrow/include/CanGrow_Wifi.h b/Arduino/CanGrow/include/CanGrow_Wifi.h index 50fde1c..4533a77 100644 --- a/Arduino/CanGrow/include/CanGrow_Wifi.h +++ b/Arduino/CanGrow/include/CanGrow_Wifi.h @@ -33,10 +33,10 @@ void Wifi_Connect() { WiFi.begin(configWifi.ssid, configWifi.password); if(configWifi.dhcp == false) { Serial.println(":: [WiFi] using static ip configuration:"); - Serial.printf(":: [WiFi] IP : %s\n", IP2Char(configWifi.ip)); - Serial.printf(":: [WiFi] Netmask: %s\n", IP2Char(configWifi.netmask)); - Serial.printf(":: [WiFi] Gateway: %s\n", IP2Char(configWifi.gateway)); - Serial.printf(":: [WiFi] DNS : %s\n", IP2Char(configWifi.dns)); + Serial.printf(":: [WiFi] IP : %s\n", IP2Char(configWifi.ip)); + Serial.printf(":: [WiFi] Netmask: %s\n", IP2Char(configWifi.netmask)); + Serial.printf(":: [WiFi] Gateway: %s\n", IP2Char(configWifi.gateway)); + Serial.printf(":: [WiFi] DNS : %s\n", IP2Char(configWifi.dns)); WiFi.config(configWifi.ip, configWifi.dns, configWifi.gateway, configWifi.netmask); } else { @@ -53,10 +53,10 @@ void Wifi_Connect() { if(configWifi.dhcp == true) { Serial.println(":: [WiFi] DHCP offered ip configuration:"); - Serial.printf(":: [WiFi] IP : %s\n", IP2Char(WiFi.localIP())); - Serial.printf(":: [WiFi] Netmask: %s\n", IP2Char(WiFi.subnetMask())); - Serial.printf(":: [WiFi] Gateway: %s\n", IP2Char(WiFi.gatewayIP())); - Serial.printf(":: [WiFi] DNS : %s\n", IP2Char(WiFi.dnsIP())); + Serial.printf(":: [WiFi] IP : %s\n", IP2Char(WiFi.localIP())); + Serial.printf(":: [WiFi] Netmask: %s\n", IP2Char(WiFi.subnetMask())); + Serial.printf(":: [WiFi] Gateway: %s\n", IP2Char(WiFi.gatewayIP())); + Serial.printf(":: [WiFi] DNS : %s\n", IP2Char(WiFi.dnsIP())); } } @@ -65,17 +65,18 @@ void Wifi_AP() { Serial.printf(":: [WiFi] create access point: %s\n", CANGROW_SSID); WiFi.softAPConfig(configWifi.ip, configWifi.gateway, configWifi.netmask); WiFi.softAP(CANGROW_SSID); - Serial.print(":: [WiFi] access point started with IP: "); - Serial.println(WiFi.softAPIP()); + Serial.println(":: [WiFi] access point started:"); + Serial.printf(":: [WiFi] IP : %s\n", IP2Char(configWifi.ip)); + Serial.printf(":: [WiFi] Netmask: %s\n", IP2Char(configWifi.netmask)); } void Wifi_Init() { Serial.println(":: [WiFi] initializing"); if(strlen(configWifi.ssid) == 0) { - Serial.println(":: [WiFi] no value found in configWifi.ssid, creating access point"); + Serial.println(":: [WiFi] configWifi.ssid is unset"); Wifi_AP(); } else { - Serial.printf(":: [SETUP] value found in configWifi.ssid, connecting to SSID: %s\n", configWifi.ssid); + Serial.printf(":: [WiFi] connecting to SSID: %s\n", configWifi.ssid); Wifi_Connect(); } } diff --git a/Arduino/CanGrow/include/Webserver/Common.h b/Arduino/CanGrow/include/Webserver/Common.h index 43fd9b4..db28d78 100644 --- a/Arduino/CanGrow/include/Webserver/Common.h +++ b/Arduino/CanGrow/include/Webserver/Common.h @@ -37,12 +37,13 @@ bool TestHeaderFooter(const String& var) { #endif if( - (var == "HEADER") || - (var == "FOOTER") || - (var == "CGVER") || - (var == "CGBUILD") || - (var == "GROWNAME") || - (var == "CANGROW_CSS")) { + (var == "HEADER") || + (var == "FOOTER") || + (var == "CGVER") || + (var == "CGBUILD") || + (var == "GROWNAME") || + (var == "CANGROW_CSS") || + (var == "NEED_RESTART")) { return true; } else { return false; @@ -62,6 +63,12 @@ String AddHeaderFooter(const String& var) { return String(configGrow.growName); } else if(var == "CANGROW_CSS") { return String(File_cangrow_CSS); + } else if(var == "NEED_RESTART") { + if(needRestart == true) { + return String(Common_HTML_NEED_RESTART); + } else { + return String(); + } } else { return String(); } diff --git a/Arduino/CanGrow/include/Webserver/Common_HTML.h b/Arduino/CanGrow/include/Webserver/Common_HTML.h index 3d87dc8..72c98e1 100644 --- a/Arduino/CanGrow/include/Webserver/Common_HTML.h +++ b/Arduino/CanGrow/include/Webserver/Common_HTML.h @@ -36,3 +36,11 @@ const char Common_HTML_SAVE_MSG[] PROGMEM = R"EOF( const char Common_HTML_SAVE_MSG_ERR[] PROGMEM = R"EOF(
here you can set which features and sensors you use
Select your wifi network from the SSID list.
Reload the page, if your network is not listed.