Created
May 26, 2019 09:33
-
-
Save alistairjevans/5968b5ae79e2d91912c1c192efb1d2d4 to your computer and use it in GitHub Desktop.
Complete WiFi connection example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <WiFiNINA.h> | |
#define NETSSID "MYNETWORK" | |
#define NETPASS "SECRETPASSWORD" | |
#define TOTAL_WAIT_TIME 60000 // 1 minute | |
#define ATTEMPT_TIME 5000 // 5 seconds between attempts | |
WiFiClient client; | |
void setup() | |
{ | |
Serial.begin(9600); | |
unsigned long startTime = millis(); | |
unsigned long lastAttemptTime = 0; | |
int wifiStatus; | |
// attempt to connect to Wifi network in a loop, | |
// until we connect. | |
while (wifiStatus != WL_CONNECTED) | |
{ | |
unsigned long currentTime = millis(); | |
if(currentTime - startTime > TOTAL_WAIT_TIME) | |
{ | |
// Exceeded the total timeout for trying to connect, so stop. | |
Serial.println("Failed to connect"); | |
while(true); | |
} | |
else if(currentTime - lastAttemptTime > ATTEMPT_TIME) | |
{ | |
// Exceeded our attempt delay, initiate again. | |
Serial.println("Attempting Wifi Connection"); | |
lastAttemptTime = currentTime; | |
wifiStatus = WiFi.begin(NETSSID, NETPASS); | |
} | |
else | |
{ | |
// wait 500ms before we check the WiFi status. | |
delay(500); | |
} | |
} | |
Serial.println("Connected!"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment