Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Complete WiFi connection example
#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
You can’t perform that action at this time.