Chat freely about anything...

User avatar
By Sp0ngeB0b
#90069 Hi i successfully made a motion sensor . problem is that for some reason when i save the wifi config to spiffs it takes between 6 to 15seconds to connect to wifi. in the meantime same algorithm using hardcoded values connects within 0.2s . just wondering what i am missing :-/

note: this is part of a larger program which offers a "configuration" and a "deep sleep" mode the first being in softAP mode the second uses this function which is called in the setup() function portion in arduino ide when the device is configured and when it comes back from deepsleep.


(see commented lines for hardcode version)

Code: Select allvoid start_wifi_client() {

//const char* client_ssid     =   "test_iot_2g";
//const char* client_password =   "Test1234";

//IPAddress wifi_ip(10,0,0,144);
//IPAddress wifi_dns(10,0,0,4);
//IPAddress wifi_gateway(10,0,0,129);
//IPAddress wifi_subnet(255,255,255,224);

// **** loading from SPIFFS ****
String s,wifi_ssid="",wifi_password;
IPAddress wifi_ip,wifi_subnet,wifi_gateway;

   File file = SPIFFS.open("/wifi.cfg","r");

   if(!file) {
    Serial.println("Could not open config file");
   } else {
   Serial.println("Reading Config...");   
   do {
   s = file.readStringUntil('\n');
   
 if(s.startsWith("ssid")) {  wifi_ssid=s.substring(s.indexOf('=')+1,s.length());  Serial.print(wifi_ssid); }
 else if(s.startsWith("passkey")) {  wifi_password=s.substring(s.indexOf('=')+1,s.length()); } //Serial.print(wifi_password); }
 else if(s.startsWith("ip")) { wifi_ip.fromString(s.substring(s.indexOf('=')+1,s.length()));  Serial.print(wifi_ip.toString()); }
 else if(s.startsWith("subnet")) {  wifi_subnet.fromString(s.substring(s.indexOf('=')+1,s.length()));  Serial.print(wifi_subnet.toString()); }
 else if(s.startsWith("gateway")) { wifi_gateway.fromString(s.substring(s.indexOf('=')+1,s.length())); Serial.print(wifi_gateway.toString()); }
 Serial.print("\n");
   } while(file.available());
  delay(10);
    file.close();

// *** end loading config ***

Serial.println("");
Serial.print("Connecting to: ");
Serial.print(wifi_ssid);
Serial.print("\n");

//WiFi.forceSleepWake();
//WiFi.persistent(false);

WiFi.mode(WIFI_STA);


WiFi.config(wifi_ip,wifi_gateway,wifi_subnet);

 float time_count=0;
  Serial.println("connected to wifi");
 WiFi.begin(wifi_ssid, wifi_password);
 do {
 
   delay(100);
 //   led_blink();
    Serial.print(".");
    time_count+=100;
 } while(WiFi.status() != WL_CONNECTED);
 Serial.print("\ntook ");
 Serial.print((time_count/1000));
 Serial.print(" sec to connect\n");
 Serial.print("Our ip is: ");
 Serial.print(WiFi.localIP());
 Serial.print("\n");
 
 }
}
User avatar
By RichardS
#90070 Writing to SPIFFS depending on a bunch of factors can be very slow.

If you format the SPIFFS and do that first write its fast, after X writes which varies, you will see it slow....

Yes the normal WIFI connect on its own is fast.

RichardS
User avatar
By Sp0ngeB0b
#90073 i am only reading from the spiffs here and you will notice that i only count from wifi.begin ... this is what takes long. for the spiffs from what i gather its pretty fast reading from it (only reading a few bytes after-all...)

is there anything that changes that might conflict with the "caching" of WiFi parameters ? (i think this is what is used when its connecting fast to WiFi. it seems to just "restore" the connection instead of negotiating it completely, when its slow its pretty much exactly like the first connection hard-coded or not. )
User avatar
By urs_eppenberger
#90096 There are tons of guides on the net about fast network setup for ESP8266.
One reason for longer delays is, that the ESP scans the world around it for all SSIDs before it connects.

My devices are only used in my home they always need to connect to the same access point.
This is why I set the the channel number and the MAC address of the access point too. This is for battery powered devices, which sleep most of the time. I want a very quick network setup, send off the measurement data and go back to sleep. Here is the important part of the network setup:

Code: Select all  WiFi.persistent( false );                                    // do not store WiFi data in flash to avoid wear-out
  WiFi.mode( WIFI_STA );
  WiFi.config( ip, gateway, subnet );                          // we use fix IP to speed up the WiFi setup ( 1s instead of 4s with DHCP)
  WiFi.begin(WiFiSSID, WiFiPW, wifiChannel, apMAC, true);      // we use channel and Mac address from the access point for speed
  int wifiStatus = WiFi.status();
  int counter = 0;
  while (wifiStatus != WL_CONNECTED) {
    delay(5);                                                  // use small delays, NOT 500ms
    if (++counter > 1000) break;                               // timeout after 5 secs
    wifiStatus = WiFi.status();
  }