#include <WiFi.h> // Include the WiFi library
// Define SSID and password
const char* ssid = "wokwi=GUEST"; // Corrected to use double quotes for string literals
const char* pass = ""; // Password is empty
void setup() {
// Initialize serial communication
Serial.begin(115200);
// Set Wi-Fi mode to STA (Station mode)
WiFi.mode(WIFI_STA);
// Attempt to connect to Wi-Fi network
Serial.print("Attempting to connect to ");
Serial.print(ssid);
Serial.println("...");
// Attempt to connect to Wi-Fi network
WiFi.begin(ssid, pass);
// Wait for connection
int attempts = 0;
while (WiFi.status() != WL_CONNECTED && attempts < 20) { // Limit attempts to avoid infinite loop
Serial.print(".");
delay(5000); // Wait 5 seconds before retrying
attempts++;
}
// Check if connected
if (WiFi.status() == WL_CONNECTED) {
Serial.println("\nConnected to Wi-Fi.");
} else {
Serial.println("\nFailed to connect to Wi-Fi.");
}
}
void loop() {
// Your main code here, if any
delay(10000); // Delay to avoid excessive serial output; adjust as needed
}