Created
July 29, 2021 12:00
-
-
Save vshymanskyy/5e70742d7ea2cf09c93ea27a2eb24b2f to your computer and use it in GitHub Desktop.
Arduino WiFi Test
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
#if defined(ARDUINO_WIO_TERMINAL) | |
# include <rpcWiFi.h> | |
#elif defined(ARDUINO_SAMD_MKRWIFI1010) | |
# include <WiFiNINA.h> | |
#elif defined(ARDUINO_SAMD_MKR1000) | |
# include <WiFi101.h> | |
#elif defined(ARDUINO_ARCH_ESP8266) | |
# include <ESP8266WiFi.h> | |
#else | |
# include <WiFi.h> | |
#endif | |
char ssid[] = "good ssid"; | |
char ssid_wrong[] = "WrongSSID"; | |
char pass[] = "good pass"; | |
char pass_wrong[] = "WrongPASS"; | |
String getWiFiStatusStr(int status) { | |
switch (status) { | |
case WL_IDLE_STATUS: return "Idle"; | |
case WL_NO_SHIELD: return "No WiFi module"; | |
case WL_SCAN_COMPLETED: return "Scan completed"; | |
case WL_NO_SSID_AVAIL: return "Network not available"; | |
case WL_CONNECTED: return "Connected"; | |
case WL_CONNECT_FAILED: return "Connection failed (auth failed?)"; | |
case WL_CONNECTION_LOST: return "Connection lost"; | |
case WL_DISCONNECTED: return "Disconnected"; | |
default: return String("<unknown>") + status; | |
} | |
} | |
void tryConnect(const char* ssid, const char* pass) | |
{ | |
WiFi.disconnect(); | |
delay(100); | |
Serial.println("------------------------"); | |
Serial.print("Connecting to "); | |
Serial.print(ssid); | |
Serial.print(", pass: "); | |
Serial.println(pass); | |
int result = WiFi.begin(ssid, pass); | |
Serial.println(String("Result: ") + getWiFiStatusStr(result)); | |
uint32_t start = millis(); | |
while (millis() - start < 10000) { | |
int s = WiFi.status(); | |
Serial.println(String("Status: ") + getWiFiStatusStr(s)); | |
if (s == WL_CONNECTED) { | |
Serial.print("IP address: "); | |
Serial.println(WiFi.localIP()); | |
break; | |
} else if (s == WL_CONNECT_FAILED) { | |
break; | |
} else { | |
delay(1000); | |
} | |
} | |
} | |
void setup() | |
{ | |
Serial.begin(115200); | |
while (!Serial) {} | |
Serial.println(); | |
Serial.println(); | |
tryConnect(ssid_wrong, NULL); | |
tryConnect(ssid, pass_wrong); | |
tryConnect(ssid, pass); | |
tryConnect(ssid, ""); | |
Serial.println("Finished."); | |
} | |
void loop() | |
{ | |
delay(1000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment