I wrote simple function to manage WiFi connection of my ESP8266.
Im read data from INI and I would like set wWiFi according INI setup.
I have this proble, IF I join to ESP AP Network work only with static IP (automatic IP assignement don't work).
As ESP client if I try to disconnect from one network and connetct to another cannection fail (return value 7). Any idea to help ?
void WiFiConnect(const char* filename)
{
IPAddress mask;
if(ini_open (filename))
{
NetworkChoice = ini_read ("NetworkChoice", "network","0");
ClientSSID = ini_read ("network" + NetworkChoice , "ClientSSID","BuddaceCasa");
ClientPassword = ini_read ("network" + NetworkChoice , "ClientPassword","25399750057853538077");
ClientSSID0 = ini_read ("network0", "ClientSSID","BuddaceCasa");
ClientPassword0 = ini_read ("network0", "ClientPassword","25399750057853538077");
ClientSSID1 = ini_read ("network1", "ClientSSID","BuddaceCasa");
ClientPassword1 = ini_read ("network1", "ClientPassword","25399750057853538077");
ClientSSID2 = ini_read ("network2", "ClientSSID","BuddaceCasa");
ClientPassword2 = ini_read ("network2", "ClientPassword","25399750057853538077");
HostSSID = ini_read ("host", "HostSSID","CV19Archimede");
HostPassword = ini_read ("host", "HostPassword","PlasticoCV19");
HostIp = ini_read ("host", "HostIp","10.10.0.1");
HostSubnetMask = ini_read ("host", "HostSubnetMask","255.0.0.0");
ini_close();
}
else
{
NetworkChoice = "0";
ClientSSID = "BuddaceCasa";
ClientPassword = "25399750057853538077";
HostSSID = "CV19ArchimedeDef";
HostPassword = "PlasticoCV19Def";
HostIp = "10.0.0.1";
HostSubnetMask = "255.0.0.0";
}
Serial.println(NetworkChoice);
Serial.println(ClientSSID);
Serial.println(ClientPassword);
Serial.println(HostSSID);
Serial.println(HostPassword);
Serial.println(HostIp);
Serial.println(HostSubnetMask);
// Connect to Wi-Fi
//WiFi.mode(WIFI_AP_STA);
WiFi.mode(WIFI_AP_STA);
WiFi.softAPdisconnect();
WiFi.disconnect();
delay(1000);
Serial.println("Controllo disconnessione");
Serial.println(WiFi.status());
ip.fromString(HostIp.c_str());
mask.fromString(HostSubnetMask.c_str());
WiFi.softAP(HostSSID, HostPassword);
WiFi.softAPConfig(ip, ip, mask);
WiFi.begin(ClientSSID, ClientPassword);
WiFi.hostname("CV19Archimede");
WifiRetry = 0;
while((WiFi.status() != WL_CONNECTED) && ( WifiRetry < WIFIRETRY))
{
delay(500);
WifiRetry ++;
if (WifiRetry > WIFIRETRY)
{
WiFi.disconnect();
}
}
Serial.println("stato corrente");
Serial.println(WiFi.status());
Serial.println(WiFi.localIP().toString());
////Serial.println("Stato Wifi = " + String(WiFi.status()) + "fine stato" );
MacAddress = WiFi.macAddress();
WiFi.macAddress(mac);
ClientIp = WiFi.localIP().toString();
HostIp_Read = WiFi.softAPIP().toString();
ip = WiFi.localIP();
hostip = WiFi.softAPIP();
WiFiEspNetwork();
}