diff --git a/Arduino/CanGrow/CanGrow.ino b/Arduino/CanGrow/CanGrow.ino index 7acd431..e5a9578 100644 --- a/Arduino/CanGrow/CanGrow.ino +++ b/Arduino/CanGrow/CanGrow.ino @@ -96,7 +96,8 @@ void setup() { Serial.print("II To format LittleFS, pull GPIO "); Serial.print(PinWIPE); Serial.print(" (PinWIPE) to "); - Serial.print(PinWIPE_default); + // we need to invert the default to tell that user the state for an action + Serial.print(1 - PinWIPE_default); Serial.println(" - NOW! (2 seconds left) II"); @@ -123,21 +124,16 @@ void setup() { LFS_init(); - Serial.printf(":: Before config.test: %s\n", config.test); - Serial.print(":: Before configWifi.dhcp: "); - Serial.println(configWifi.dhcp); loadConfig(); - if(!existFile(CANGROW_CFG)) { - writeFile(CANGROW_CFG, "asd"); + + if(strlen(configWifi.ssid) == 0) { + Serial.println(":: [SETUP] configWifi.ssid is empty, creating access point"); + WifiAP(); } else { - readFile(CANGROW_CFG); + Serial.printf(":: [SETUP] configWifi.ssid is set, connecting to ssid: %s\n", configWifi.ssid); + WifiConnect(); } - Serial.printf(":: After config.test: %s\n", config.test); - Serial.print(":: After configWifi.dhcp: "); - Serial.println(configWifi.dhcp); - - WifiAp(); } bool alrdySaved = false; diff --git a/Arduino/CanGrow/include/CanGrow.h b/Arduino/CanGrow/include/CanGrow.h index 5442aa8..12348e3 100644 --- a/Arduino/CanGrow/include/CanGrow.h +++ b/Arduino/CanGrow/include/CanGrow.h @@ -50,7 +50,7 @@ struct Config_WiFi { byte netmask[4] = {255,255,255,0}; byte gateway[4] = {192,168,4,254}; byte dns[4] = {0,0,0,0}; - bool dhcp = true; + bool dhcp; }; Config_WiFi configWifi; @@ -68,6 +68,7 @@ Config_System configSystem; struct Config_Grow { char growName[64]; unsigned short dayOfGrow; + byte daysSeed; byte daysVeg; byte daysBloom; byte lightHoursVeg; diff --git a/Arduino/CanGrow/include/CanGrow_Core.h b/Arduino/CanGrow/include/CanGrow_Core.h index 9729c79..24d65a6 100644 --- a/Arduino/CanGrow/include/CanGrow_Core.h +++ b/Arduino/CanGrow/include/CanGrow_Core.h @@ -45,13 +45,52 @@ void Panic() { } + +// IP2Char helper function to convert ip arrarys to char arrays +char* IP2Char(IPAddress ipaddr){ + // https://forum.arduino.cc/t/trouble-returning-char-array-string/473246/6 + static char buffer[18]; + sprintf(buffer, "%d.%d.%d.%d", ipaddr[0], ipaddr[1], ipaddr[2], ipaddr[3] ); + return buffer; +} + void WifiConnect() { + Serial.printf(":: [WiFi] connecting to ssid: %s\n", configWifi.ssid); + 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)); + + WiFi.config(configWifi.ip, configWifi.dns, configWifi.gateway, configWifi.netmask); + } else { + Serial.println(":: [WiFi] using DHCP for ip configuration"); + } + + Serial.print(":: [WiFi] "); + // wait until WiFi connection is established + while (WiFi.status() != WL_CONNECTED) { + delay(500); + Serial.print("."); + } + Serial.println("CONNECTED!"); + + 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())); + } } -void WifiAp() { +void WifiAP() { Serial.printf(":: [WiFi] create access point: %s\n", CANGROW_SSID); WiFi.softAPConfig(configWifi.ip, configWifi.gateway, configWifi.netmask); WiFi.softAP(CANGROW_SSID); - Serial.println(":: [WiFi] access point started"); + Serial.print(":: [WiFi] access point started with IP: "); + Serial.println(WiFi.softAPIP()); } diff --git a/Arduino/CanGrow/include/CanGrow_LittleFS.h b/Arduino/CanGrow/include/CanGrow_LittleFS.h index 3e2dbb9..7075a0d 100644 --- a/Arduino/CanGrow/include/CanGrow_LittleFS.h +++ b/Arduino/CanGrow/include/CanGrow_LittleFS.h @@ -205,6 +205,7 @@ bool loadConfig() { JsonObject objGrow = doc["grow"][0]; strlcpy(configGrow.growName, objGrow["growName"], sizeof(configGrow.growName)); configGrow.dayOfGrow = objGrow["dayOfGrow"]; + configGrow.daysSeed = objGrow["daysSeed"]; configGrow.daysVeg = objGrow["daysVeg"]; configGrow.daysBloom = objGrow["daysBloom"]; configGrow.lightHoursVeg = objGrow["lightHoursVeg"]; @@ -218,7 +219,7 @@ bool loadConfig() { // Close the file (Curiously, File's destructor doesn't close the file) file.close(); - Serial.println(":: [LittleFS] config successfully loaded."); + Serial.println(":: [LittleFS] config successfully loaded"); return true; } @@ -233,7 +234,7 @@ void saveConfig() { #endif if (!file) { - Serial.printf(":: [LittleFS] FAILED to open configfile for writing: %s\n", CANGROW_CFG); + Serial.printf("!! [LittleFS] FAILED to open configfile for writing: %s\n", CANGROW_CFG); return; } else { Serial.printf(":: [LittleFS] opened for writing %s\n", CANGROW_CFG); @@ -271,6 +272,7 @@ void saveConfig() { JsonObject objGrow = doc["grow"].add(); objGrow["growName"] = configGrow.growName; objGrow["dayOfGrow"] = configGrow.dayOfGrow; + objGrow["daysSeed"] = configGrow.daysSeed; objGrow["daysVeg"] = configGrow.daysVeg; objGrow["daysBloom"] = configGrow.daysBloom; objGrow["lightHoursVeg"] = configGrow.lightHoursVeg; @@ -287,7 +289,7 @@ void saveConfig() { // Serialize JSON to file if (serializeJson(doc, file) == 0) { - Serial.printf(":: [LittleFS] FAILED to write configfile: %s\n", CANGROW_CFG); + Serial.printf("!! [LittleFS] FAILED to write configfile: %s\n", CANGROW_CFG); } else { Serial.printf(":: [LittleFS] successfully written %s\n", CANGROW_CFG); }