Wondering if someone could help with the logic for this one...
void setup_wifi() {
Serial.print(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("WiFi Connected");
}
I'm looking to see I can give the Wifi 10 seconds to connect and then serial print something if not connected. As this isn't a loop I'm having trouble getting my head around what needs to be added.
Tried messing around with millis() but again think it need to be in a loop to work.
Increment a counter in the while loop and if the WiFi is still not connected when the counter reaches 10 print the message and break out of the loop
using you code as if you could add a counter as below:
void setup_wifi() {
uint8_t cnt=0;
Serial.print(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED && cnt<10) { //will loop 10 times (10s wait)
delay(1000);
Serial.print(".");
++cnt;
}
Serial.println("WiFi Connected");
}
if you want to use millis(), but don't really see the need since you'll be locked in the loop waiting for it to connect:
void setup_wifi() {
uint8_t cnt=0;
unsigned long oldTime;
Serial.print(ssid);
WiFi.begin(ssid, password);
oldTime = millis();
while (WiFi.status() != WL_CONNECTED && cnt<10) { // //will loop 10 times (10s wait)
if((millis() - oldTime)>1000){ //will excecute every second
oldTime = millis();
Serial.print(".");
++cnt;
}
}
Serial.println("WiFi Connected");
}
UKHeliBob:
Increment a counter in the while loop and if the WiFi is still not connected when the counter reaches 10 print the message and break out of the loop
Thanks, but only problem with this that if there is a problem during setup it has to run until the loop and then rerun if not connected.
But this does give food for though on where you initiate the voids from. Could move the initiation to the loop and I would have more control.
Are there any rules / best practice on what you run from setup and loop?
Call the setup_wifi() function as many times as as you need to from setup() until you decide that it really is not going to connect. Then what should happen ?
Are there any rules / best practice on what you run from setup and loop?
Generally things that need to run once go in setup() and those that you want to run once go in loop() but if you need to you can run something multiple times in setup() and only once in loop()
this does give food for though on where you initiate the voids from
They are not voids, they are functions. The void data type indicates that they do not return a value when called.