config load and wifi connect works, create ap if no ssid is saved
This commit is contained in:
parent
a044c65503
commit
6ac7b31602
4 changed files with 56 additions and 18 deletions
|
@ -96,7 +96,8 @@ void setup() {
|
||||||
Serial.print("II To format LittleFS, pull GPIO ");
|
Serial.print("II To format LittleFS, pull GPIO ");
|
||||||
Serial.print(PinWIPE);
|
Serial.print(PinWIPE);
|
||||||
Serial.print(" (PinWIPE) to ");
|
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");
|
Serial.println(" - NOW! (2 seconds left) II");
|
||||||
|
|
||||||
|
|
||||||
|
@ -123,21 +124,16 @@ void setup() {
|
||||||
|
|
||||||
|
|
||||||
LFS_init();
|
LFS_init();
|
||||||
Serial.printf(":: Before config.test: %s\n", config.test);
|
|
||||||
Serial.print(":: Before configWifi.dhcp: ");
|
|
||||||
Serial.println(configWifi.dhcp);
|
|
||||||
loadConfig();
|
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 {
|
} 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;
|
bool alrdySaved = false;
|
||||||
|
|
|
@ -50,7 +50,7 @@ struct Config_WiFi {
|
||||||
byte netmask[4] = {255,255,255,0};
|
byte netmask[4] = {255,255,255,0};
|
||||||
byte gateway[4] = {192,168,4,254};
|
byte gateway[4] = {192,168,4,254};
|
||||||
byte dns[4] = {0,0,0,0};
|
byte dns[4] = {0,0,0,0};
|
||||||
bool dhcp = true;
|
bool dhcp;
|
||||||
};
|
};
|
||||||
|
|
||||||
Config_WiFi configWifi;
|
Config_WiFi configWifi;
|
||||||
|
@ -68,6 +68,7 @@ Config_System configSystem;
|
||||||
struct Config_Grow {
|
struct Config_Grow {
|
||||||
char growName[64];
|
char growName[64];
|
||||||
unsigned short dayOfGrow;
|
unsigned short dayOfGrow;
|
||||||
|
byte daysSeed;
|
||||||
byte daysVeg;
|
byte daysVeg;
|
||||||
byte daysBloom;
|
byte daysBloom;
|
||||||
byte lightHoursVeg;
|
byte lightHoursVeg;
|
||||||
|
|
|
@ -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() {
|
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);
|
Serial.printf(":: [WiFi] create access point: %s\n", CANGROW_SSID);
|
||||||
WiFi.softAPConfig(configWifi.ip, configWifi.gateway, configWifi.netmask);
|
WiFi.softAPConfig(configWifi.ip, configWifi.gateway, configWifi.netmask);
|
||||||
WiFi.softAP(CANGROW_SSID);
|
WiFi.softAP(CANGROW_SSID);
|
||||||
Serial.println(":: [WiFi] access point started");
|
Serial.print(":: [WiFi] access point started with IP: ");
|
||||||
|
Serial.println(WiFi.softAPIP());
|
||||||
}
|
}
|
||||||
|
|
|
@ -205,6 +205,7 @@ bool loadConfig() {
|
||||||
JsonObject objGrow = doc["grow"][0];
|
JsonObject objGrow = doc["grow"][0];
|
||||||
strlcpy(configGrow.growName, objGrow["growName"], sizeof(configGrow.growName));
|
strlcpy(configGrow.growName, objGrow["growName"], sizeof(configGrow.growName));
|
||||||
configGrow.dayOfGrow = objGrow["dayOfGrow"];
|
configGrow.dayOfGrow = objGrow["dayOfGrow"];
|
||||||
|
configGrow.daysSeed = objGrow["daysSeed"];
|
||||||
configGrow.daysVeg = objGrow["daysVeg"];
|
configGrow.daysVeg = objGrow["daysVeg"];
|
||||||
configGrow.daysBloom = objGrow["daysBloom"];
|
configGrow.daysBloom = objGrow["daysBloom"];
|
||||||
configGrow.lightHoursVeg = objGrow["lightHoursVeg"];
|
configGrow.lightHoursVeg = objGrow["lightHoursVeg"];
|
||||||
|
@ -218,7 +219,7 @@ bool loadConfig() {
|
||||||
|
|
||||||
// Close the file (Curiously, File's destructor doesn't close the file)
|
// Close the file (Curiously, File's destructor doesn't close the file)
|
||||||
file.close();
|
file.close();
|
||||||
Serial.println(":: [LittleFS] config successfully loaded.");
|
Serial.println(":: [LittleFS] config successfully loaded");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -233,7 +234,7 @@ void saveConfig() {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (!file) {
|
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;
|
return;
|
||||||
} else {
|
} else {
|
||||||
Serial.printf(":: [LittleFS] opened for writing %s\n", CANGROW_CFG);
|
Serial.printf(":: [LittleFS] opened for writing %s\n", CANGROW_CFG);
|
||||||
|
@ -271,6 +272,7 @@ void saveConfig() {
|
||||||
JsonObject objGrow = doc["grow"].add<JsonObject>();
|
JsonObject objGrow = doc["grow"].add<JsonObject>();
|
||||||
objGrow["growName"] = configGrow.growName;
|
objGrow["growName"] = configGrow.growName;
|
||||||
objGrow["dayOfGrow"] = configGrow.dayOfGrow;
|
objGrow["dayOfGrow"] = configGrow.dayOfGrow;
|
||||||
|
objGrow["daysSeed"] = configGrow.daysSeed;
|
||||||
objGrow["daysVeg"] = configGrow.daysVeg;
|
objGrow["daysVeg"] = configGrow.daysVeg;
|
||||||
objGrow["daysBloom"] = configGrow.daysBloom;
|
objGrow["daysBloom"] = configGrow.daysBloom;
|
||||||
objGrow["lightHoursVeg"] = configGrow.lightHoursVeg;
|
objGrow["lightHoursVeg"] = configGrow.lightHoursVeg;
|
||||||
|
@ -287,7 +289,7 @@ void saveConfig() {
|
||||||
|
|
||||||
// Serialize JSON to file
|
// Serialize JSON to file
|
||||||
if (serializeJson(doc, file) == 0) {
|
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 {
|
} else {
|
||||||
Serial.printf(":: [LittleFS] successfully written %s\n", CANGROW_CFG);
|
Serial.printf(":: [LittleFS] successfully written %s\n", CANGROW_CFG);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue