Post topics, source code that relate to the Arduino Platform

User avatar
By markdh102
#94258 I have some very simple clocks. ESP8266, bit banged 7 seg LED module, DS1307 battery backed RTC.
They connect to my WiFi and then time server every 24 hours because of RTC drift. If they fail to connect they try again in 1 hour until they finally connect. I hold a reset count in EEPROM.
This has worked flawlessly for a few years now using the following connect code :
After the time has been obtained from the server I send myself an email to that effect, containing the last reset reason and then do a :

Code: Select allWiFi.disconnect();
WiFi.mode(WIFI_OFF);
WiFi.forceSleepBegin();
delay(100);


Code: Select allbool connectToInternet(void)
{
  bool blnRetVal = false;
  char bytC;
 
  WiFi.forceSleepWake(); 
  delay(1);
  WiFi.mode(WIFI_STA);
  wifi_station_connect();

  // Set WiFi to station mode and disconnect from an AP if it was previously connected
  WiFi.mode(WIFI_STA);
  WiFi.disconnect();
  delay(100);

  #ifdef WIFI_DEBUG
  Serial.print("Trying to connect to :\r\n");
  #endif

  #ifdef WIFI_DEBUG
  Serial.print("My SSID ");
  #endif
  WiFi.begin("SSID", "PWD");

  bytC = 0;
  while (WiFi.status() != WL_CONNECTED && bytC < 32)
  {
    #ifdef WIFI_DEBUG
    Serial.print(".");
    #endif
    bytC++;
    delay(500);
  }
 
  #ifdef WIFI_DEBUG
  Serial.println("\r\n");
  #endif   

  if (WiFi.status() == WL_CONNECTED)
  {
    #ifdef WIFI_DEBUG
    Serial.println("Connected");
    #endif
    bytC = 0;
    while (bytC < 10 && getInitialTimeFromServer() == false)
    {
      bytC++;
      delay(500);   
    }
   
    if (bytC < 10)
      blnRetVal = true;
  }
  else
  {
    #ifdef WIFI_DEBUG
    Serial.println("WiFi NOT connected");   
    #endif
  }
  return blnRetVal;
}


I've recently changed my WiFi extender (BT hotspot to TP Link). It doesn't have as strong a signal as the old one. So I now check for both my router's SSID and my TPLINK SSID to get a connection.
But my reset counter is increasing daily. So there is an issue. It's flagging REASON_EXT_SYS_RST.
I changed the code to this : Is this the correct way to do multiple SSID's?

Code: Select allbool connectToInternet(void)
{
  bool blnRetVal = false;
  char bytC, bytN;
 
  WiFi.forceSleepWake(); 
  delay(1);
  WiFi.mode(WIFI_STA);
  wifi_station_connect();

  // Set WiFi to station mode and disconnect from an AP if it was previously connected
  WiFi.mode(WIFI_STA);
  WiFi.disconnect();
  delay(100);

  #ifdef WIFI_DEBUG
  Serial.print("Trying to connect to :\r\n");
  #endif

  for (bytN = 0; bytN < 2; bytN++)
  {
    if (bytN == 0)
    {
      #ifdef WIFI_DEBUG
      Serial.print("SSID 1 ");
      #endif
      WiFi.begin("SSID 1", "PWD 1");
    }
    if (bytN == 1)
    {
      #ifdef WIFI_DEBUG
      Serial.print("SSID 2 ");
      #endif
      WiFi.begin("SSID 2", "PWD 2");
    }

    bytC = 0;
    while (WiFi.status() != WL_CONNECTED && bytC < 32)
    {
      #ifdef WIFI_DEBUG
      Serial.print(".");
      #endif
      bytC++;
      delay(500);
    }

    if (WiFi.status() == WL_CONNECTED) break;
 
    #ifdef WIFI_DEBUG
    Serial.println("\r\n");
    #endif   
  }

  if (WiFi.status() == WL_CONNECTED)
  {
    #ifdef WIFI_DEBUG
    Serial.println("Connected");
    #endif
    bytC = 0;
    while (bytC < 10 && getInitialTimeFromServer() == false)
    {
      bytC++;
      delay(500);   
    }
   
    if (bytC < 10)
      blnRetVal = true;
  }
  else
  {
    #ifdef WIFI_DEBUG
    Serial.println("WiFi NOT connected");   
    #endif
  }
  return blnRetVal;
}