Problems in Listening mode

I am having a problem when I erase wifi credentials in listening mode while also in manual system mode.

What I need to do is make sure that my device does not endlessly wait for wifi credentials so I need a timeout. See my code snippet is below.

Basically what is happening is when I get in the while (!WiFi.hasCredentials()) loop I get a few flashes of my led (not 50) and then it hangs. No matter how long I wait it never loops around and times out.

While in this state if I give it credentials via my smart config app it receives them no problem and beeps the 5 times.

So what I think is happening is some system level interrupt is being executed not allowing my device to continue its loop until creds come in.

Any thoughts or ideas on how to fix this would be most appreciated.

SYSTEM_MODE(MANUAL);

setup()
{
}

loop()
{

bool itListened = 0;

WiFi.clearCredentials();

// if wifi.connect is called while credentials are lost
// the device will go into listening mode where itListened will set set to 1

WiFi.connect();

if (itListened)
{
beep(3,FASTBEEP); //beep 3 times

  exit_time = millis() + (120 * 1000);
  while (!WiFi.hasCredentials())
     { //we do not have credentials
     if (millis() > exit_time)
        {
        // the device has not received credentials and rather than kill
        // the battery power the device down
        SetRelay(POWER_OFF);
        // will not get here as power has been lost
        }
     blink_external_led (50,FASTLED); //blink 50 times
     }
  }

beep(5, FASTBEEP); //beep 5 times
}

snippet of code that is in wiring_wifi.cpp

void WiFiClass::listen(void)
{
beep(1, FASTBEEP);
itListened = 1;
WLAN_SMART_CONFIG_START = 1;
}
'

Just a quick reply before I go and have a closer look at your code :wink:

You could try to add a call to SPARK_WLAN_Loop() within your while().

What’s happening inside your link_external_led()?
You could also try to do your timing-out via @peekay123 's very handy SparkIntervalTimer library.
This even works for blocking calls like Spark.connect().

@ScruffR Hi The way I have structured my code was not to rely on the spark cloud so I was not going to be using Spark.process(); ect. I am guessing SPARK_WLAN_Loop() is a spark cloud specific function. I will have to do some research to understand what SPARK_WLAN_Loop() does.

I also restructured my code to see if WiFi.hasCredentials() was the source of the problem, but it still hangs will trying the 50 led flashes.


SYSTEM_MODE(MANUAL);

setup()
   {
   }

loop()
   {

   bool itListened = 0;


   WiFi.clearCredentials();

   // if wifi.connect is called while credentials are lost 
   // the device will go into listening mode where itListened will set set to 1 

   WiFi.connect();


   if (itListened)
      {
      exit_time = millis() + (120 * 1000);

      while (millis() < exit_time)
         {
         blink_external_led (50,FASTLED); //blink 50 times
         }
 
     if (!WiFi.hasCredentials())
         {
         // the device has not received credentials and rather than kill
         // the battery power the device down
         SetRelay(POWER_OFF);
         // will not get here as power has been lost
         }
      }
   beep(5, FASTBEEP); //beep 5 times
   }
   
         

snippet of code that is in wiring_wifi.cpp

void WiFiClass::listen(void)
{
  beep(1, FASTBEEP);       
  itListened              = 1; 
  WLAN_SMART_CONFIG_START = 1;
}

No, as the name suggests it’s also useful without the cloud but with WiFi only.
Can we see your blink code?

I just came across this thread https://community.spark.io/t/can-you-turn-off-wifi-listen-once-it-starts-and-what-is-the-purpose-of-wifi-listening-solved/10051 this seems like the solution to my problem (i did search prior to posting but did not see it). Now I just have to figure out how to use @peekay123 timer library.

2 Likes