#include <WiFi.h>
// Replace these with your Wi-Fi credentials
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// IP address of the receiver ESP32
const char* serverIP = "10.10.0.2"; // Change this to the IP address of the receiver ESP32
// LED Pins
const int ledPin1 = 33; // Change this to the pin connected to your first LED
const int ledPin2 = 25; // Change this to the pin connected to your second LED
// Button Pins
const int buttonPin1 = 5; // Change this to the pin connected to your first button
const int buttonPin2 = 17; // Change this to the pin connected to your second button
// Variables to store the button states
int buttonState1 = 0;
int buttonState2 = 0;
WiFiClient client;
void setup_wifi() {
delay(10);
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void setup() {
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
Serial.begin(115200);
setup_wifi();
}
void loop() {
// Read the state of the buttons
buttonState1 = digitalRead(buttonPin1);
buttonState2 = digitalRead(buttonPin2);
// If button 1 is pressed, send an HTTP request to turn on LED 1
if (buttonState1 == HIGH) {
digitalWrite(ledPin1, HIGH);
sendRequest("/led1Control?state=on");
delay(1000); // Debounce delay
} else {
digitalWrite(ledPin1, LOW);
sendRequest("/led1Control?state=off");
delay(1000); // Debounce delay
}
// If button 2 is pressed, send an HTTP request to turn on LED 2
if (buttonState2 == HIGH) {
digitalWrite(ledPin2, HIGH);
sendRequest("/led2Control?state=on");
delay(1000); // Debounce delay
} else {
digitalWrite(ledPin2, LOW);
sendRequest("/led2Control?state=off");
delay(1000); // Debounce delay
}
}
void sendRequest(String request) {
if (client.connect(serverIP, 80)) {
Serial.println("Connected to server");
client.print(String("GET ") + request + " HTTP/1.1\r\n" +
"Host: " + serverIP + "\r\n" +
"Connection: close\r\n\r\n");
delay(10);
while (client.available()) {
String line = client.readStringUntil('\r');
Serial.print(line);
}
Serial.println();
client.stop();
} else {
Serial.println("Connection failed");
}
}