CanGrow/include/CanGrow_Wifi.h

136 lines
4.5 KiB
C

/*
*
* include/CanGrow_Wifi.h - Wifi stuff header file
*
*
*
*/
void Wifi_AP() {
const static char LogLoc[] PROGMEM = "[WiFi:AP]";
char randNr[5];
itoa(random(9999), randNr, 10);
//WiFi.softAPConfig(config.wifi.ip, config.wifi.gateway, config.wifi.netmask);
IPAddress ip(192,168,4,20);
IPAddress gateway(0,0,0,0);
IPAddress netmask(255,255,255,0);
WiFi.softAPConfig(ip, gateway, netmask);
/* when no ssid is configured, we assume here cangrow is in a fresh factory reset mode
* when a ssid is already configured, we seem not to be able to connect to it. so we protect
* our already configured cangrow controller with setting a temporary wifi ap password
* and log it to serial. */
if(strlen(config.wifi.ssid) > 0) {
const char * password = RandomString();
/* growName[64] + 8 */
char ssid[20+5];
strcpy(ssid, "CanGrow-FAILED-WIFI-");
/* random maximum 4 digit number for ssid
* https://arduino.stackexchange.com/a/42987*/
strcat(ssid, randNr);
Log.error(F("%s create access point" CR), LogLoc);
Log.error(F("%s SSID : %s" CR), LogLoc, ssid);
Log.error(F("%s Password: %s" CR), LogLoc, password);
WiFi.softAP(ssid, password);
} else {
char ssid[21+4];
strcpy(ssid, CANGROW_DEFAULT_WIFI_SSID);
//strcat(ssid, "-");
/* random maximum 4 digit number for ssid
* https://arduino.stackexchange.com/a/42987*/
//strcat(ssid, randNr);
/* start access point default password when being unconfigured */
Log.notice(F("%s create access point" CR), LogLoc);
Log.notice(F("%s SSID : %S" CR), LogLoc, ssid);
Log.notice(F("%s Password: %S" CR), LogLoc, CANGROW_DEFAULT_WIFI_PASSWORD);
WiFi.softAP(ssid, CANGROW_DEFAULT_WIFI_PASSWORD);
//WiFi.softAP(CANGROW_DEFAULT_WIFI_SSID);
}
Log.notice(F("%s access point started." CR), LogLoc);
Log.notice(F("%s IP : %s" CR), LogLoc, IP2Char(ip));
Log.notice(F("%s Netmask : %s" CR), LogLoc, IP2Char(netmask));
}
void Wifi_Connect() {
const static char LogLoc[] PROGMEM = "[WiFi:Connect]";
Log.notice(F("%s connecting to SSID: %s" CR), LogLoc, config.wifi.ssid);
WiFi.begin(config.wifi.ssid, config.wifi.password);
if(config.wifi.dhcp == false) {
Log.notice(F("%s using static ip configuration:" CR), LogLoc);
Log.notice(F("%s IP : %s" CR), LogLoc, IP2Char(config.wifi.ip));
Log.notice(F("%s Netmask: %s" CR), LogLoc, IP2Char(config.wifi.netmask));
Log.notice(F("%s Gateway: %s" CR), LogLoc, IP2Char(config.wifi.gateway));
Log.notice(F("%s DNS : %s" CR), LogLoc, IP2Char(config.wifi.dns));
WiFi.config(config.wifi.ip, config.wifi.dns, config.wifi.gateway, config.wifi.netmask);
} else {
Log.notice(F("%s using DHCP for ip configuration" CR), LogLoc);
}
Log.notice("%s ", LogLoc);
const byte max = 30;
byte count = 0;
// wait until WiFi connection is established
while (count < max) {
/* check connection stations */
if(WiFi.status() != WL_CONNECTED) {
/* if not connected, print dot and increment count */
delay(500);
Serial.print(".");
count++;
} else {
/* if connected, set count to 10 to exit loop*/
count = max+1;
}
}
/* check connection status. */
if(WiFi.status() != WL_CONNECTED) {
/* if connection failed, create AP */
Log.error(F("FAILED! Fallback to AP mode" CR), LogLoc);
WiFi.disconnect();
/*
* TODO / BUG
*
* On ESP32 there are no scan results shown in wifi tab, when connect to
* a saved network failed and the esp created then its own network.
*
* without trying to connect it works fine, like when doing a factory reset.
*
* switch mode to softAP
/* WiFi.mode(WIFI_AP_STA);
*/
Wifi_AP();
} else {
Serial.println("CONNECTED!");
if(config.wifi.dhcp == true) {
Log.notice(F("%s DHCP offered ip configuration:" CR), LogLoc);
Log.notice(F("%s IP : %s" CR), LogLoc, IP2Char(WiFi.localIP()));
Log.notice(F("%s Netmask: %s" CR), LogLoc, IP2Char(WiFi.subnetMask()));
Log.notice(F("%s Gateway: %s" CR), LogLoc, IP2Char(WiFi.gatewayIP()));
Log.notice(F("%s DNS : %s" CR), LogLoc, IP2Char(WiFi.dnsIP()));
}
}
}
void Wifi_Init() {
const static char LogLoc[] PROGMEM = "[WiFi:Init]";
Log.notice(F("%s" CR), LogLoc);
if(strlen(config.wifi.ssid) == 0) {
Log.notice(F("%s config.wifi.ssid is unset" CR), LogLoc);
Wifi_AP();
} else {
Wifi_Connect();
}
}