Code Execution at startup problem solution or better

  • When starting a core , it connects directly to the internet and in case the core disconnects it will try to reconnect but continue executing code . which is perfect .

The problem is when the core Start up without internet connection the core will not execute any kind of code. so in case you put it deep sleep and then wake up and didnt see internet connection it will never work offline mode .

The solution i found is the following :

 #include "application.h"
#include "spark_disable_wlan.h"
#include "spark_disable_cloud.h"
#include "TimeAlarms/TimeAlarms.h"
void setup()
{
  WiFi.off();
}

void loop(){
 if (WiFi.status() != WIFI_ON)
  {
  WiFi.on();
  Spark.connect();
  }
}

Now the core execute code from startup .

The question is :
Is there a better solution for that ? or is there a better way to do that ?

ping @kennethlimcp @Dave
Thanks , Maroun

This is a workaround for now :smile:

Haha ken . I saw some use something like wlan loop do you know if i should use it to turn off and on . and why using it ?

you mean SPARK_WLAN_Loop()?

this is used to reach out to :spark: cloud so that you will not get a time-out if your program is stuck in a loop for a long time.

void loop(){
 if (WiFi.status() != WIFI_ON)
  {
  WiFi.on();
  Spark.connect();
SPARK_WLAN_Loop()
  }
}

shall i use it like that ? and shall i use it to turn off or no need ?

Actually, you don’t need to call SPARK_WLAN_Loop().

It will be handled by WiFi.on()

You only need to use when for example:

void loop(){
  while(!Serial.available()) SPARK_WLAN_Loop();
}

This code is waiting for the user to press a key on the keyboard. We don’t know when the use will press and the code might be stuck here for more than 5 seconds.

Only then we will use SPARK_WLAN_Loop() :smiley:

1 Like

@Gentmat, you don’t need the WiFi.off() in setup() since the includes turns both wifi and cloud off.

@kennethlimcp is partially right ( :stuck_out_tongue: ) regarding WiFi.on() except that only sets a flag. For it to work, your loop must end to pass control back to the background task. :smile:

3 Likes

incase user input u mean ? i need to loop.

@Gentmat, when your code gets to the end of loop(), control goes back to the background task which does its thing and gives control back to loop() again (from the top).

1 Like

lol i lost you. so do i need to use it ? or no need :stuck_out_tongue: . i dont have user input all i care for is reconnext when wifi on and code startup in case no internet available from reset

thanks :smiley:

@Gentmat, ignore everything I said and if/when you start having problems I will step in. I really don’t want to confuse you at this point :stuck_out_tongue:

1 Like

mmm problems like what. so i know what i wanna go through :slight_smile:
my exemple is simple or my request is. i want the core to execute code in case the connection was off after coming from deep sleep

For completeness - if you come across this topic when you search for how to execute code at startup please read the documentation for the STARTUP macro.
STARTUP macro

1 Like