#include <WiFi.h>
// Replace with your network credentials
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// Pin definitions
const int buttonPin = 13;
const int ledPin = 4;
void setup() {
// Initialize serial communication
Serial.begin(9600);
// Initialize button and LED pins
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
// Connect to Wi-Fi network
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
}
void loop() {
// Check if button is pressed
if (digitalRead(buttonPin) == HIGH) {
// Button is pressed, scan for available networks
Serial.println("Scanning for networks...");
int numNetworks = WiFi.scanNetworks();
Serial.print("Found ");
Serial.print(numNetworks);
Serial.println(" networks");
// Check if the desired network is found
bool foundNetwork = false;
for (int i = 0; i < numNetworks; i++) {
if (WiFi.SSID(i) == ssid) {
Serial.println("Network found!");
foundNetwork = true;
break;
}
}
// If network is found, turn on LED
if (foundNetwork) {
digitalWrite(ledPin, HIGH);
} else {
Serial.println("Network not found");
}
} else {
// Button is not pressed, turn off LED
digitalWrite(ledPin, LOW);
}
}