Created
August 24, 2025 03:57
-
-
Save jurgen-kluft/1486835b8ef5f3bd65b356dbb4bc52d5 to your computer and use it in GitHub Desktop.
esp32 WiFi test
This file contains hidden or 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[] = "correct SSID"; | |
char ssid_wrong[] = "wrong SSID"; | |
char pass[] = "correct passcode"; | |
char pass_wrong[] = "wrong passcode"; | |
WiFiClient localClient; | |
const char* serverIP = "10.0.0.72"; | |
const uint serverPort = 31337; | |
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; | |
} | |
} | |
bool 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(); | |
bool connected = false; | |
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()); | |
connected = true; | |
break; | |
} | |
else if (s == WL_CONNECT_FAILED) | |
{ | |
break; | |
} | |
else | |
{ | |
delay(1000); | |
} | |
} | |
return connected; | |
} | |
void setup() | |
{ | |
Serial.begin(115200); | |
while (!Serial) {} | |
Serial.println(); | |
Serial.println(); | |
if (!tryConnect(ssid_wrong, NULL)) | |
{ | |
Serial.println("SSID wrong, PASSWORD = null, failed to connect"); | |
} | |
else | |
{ | |
Serial.println("SSID wrong, PASSWORD = null, connected ??"); | |
} | |
if (!tryConnect(ssid, pass_wrong)) | |
{ | |
Serial.println("SSID correct, PASSWORD = wrong, failed to connect"); | |
} | |
else | |
{ | |
Serial.println("SSID correct, PASSWORD = wrong, connected ??"); | |
} | |
if (!tryConnect(ssid, pass)) | |
{ | |
Serial.println("SSID correct, PASSWORD = correct, failed to connect"); | |
} | |
else | |
{ | |
Serial.println("SSID correct, PASSWORD = correct, connected succesfully!"); | |
} | |
if (!tryConnect(ssid, "")) | |
{ | |
Serial.println("SSID wrong, PASSWORD = empty, failed to connect"); | |
} | |
else | |
{ | |
Serial.println("SSID correct, PASSWORD = empty, connected ??"); | |
} | |
if (!tryConnect(ssid, pass)) | |
{ | |
Serial.println("SSID correct, PASSWORD = correct, failed to connect"); | |
} | |
else | |
{ | |
Serial.println("SSID correct, PASSWORD = correct, connected succesfully!"); | |
} | |
Serial.println("Finished."); | |
} | |
void loop() | |
{ | |
int s = WiFi.status(); | |
Serial.println(String("Status: ") + getWiFiStatusStr(s)); | |
if (s == WL_CONNECTED) | |
{ | |
Serial.print("IP address: "); | |
Serial.println(WiFi.localIP()); | |
Serial.print("Mac address: "); | |
byte mac[6]; | |
WiFi.macAddress(mac); | |
Serial.print(mac[0], HEX); | |
Serial.print(":"); | |
Serial.print(mac[1], HEX); | |
Serial.print(":"); | |
Serial.print(mac[2], HEX); | |
Serial.print(":"); | |
Serial.print(mac[3], HEX); | |
Serial.print(":"); | |
Serial.print(mac[4], HEX); | |
Serial.print(":"); | |
Serial.println(mac[5], HEX); | |
if (!localClient.connected()) | |
{ | |
if (localClient.connect(serverIP, serverPort)) | |
{ | |
Serial.println("localClient connected"); | |
} | |
} | |
else | |
{ | |
Serial.println("localClient connected"); | |
} | |
} | |
delay(5000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment