#include <WiFi.h>
#include <WebServer.h>
#include <HTTPClient.h>
const char* ssid = "ESP32-Network";
const char* password = "12345678";
WebServer server(80);
#define BUTTON_PIN 14
#define BUZZER_PIN 26
String serverIP = "192.168.4.1"; // ESP32 #1’s IP (Server)
void handleOn() {
digitalWrite(BUZZER_PIN, HIGH);
server.send(200, "text/plain", "Buzzer ON");
}
void handleOff() {
digitalWrite(BUZZER_PIN, LOW);
server.send(200, "text/plain", "Buzzer OFF");
}
void sendRequest(String path) {
HTTPClient http;
http.begin("http://" + serverIP + path);
http.GET();
http.end();
}
void setup() {
Serial.begin(115200);
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(BUZZER_PIN, OUTPUT);
digitalWrite(BUZZER_PIN, LOW);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nConnected to AP");
server.on("/on", handleOn);
server.on("/off", handleOff);
server.begin();
}
void loop() {
server.handleClient();
if (digitalRead(BUTTON_PIN) == LOW) {
sendRequest("/on"); // Tell Server to turn buzzer ON
} else {
sendRequest("/off"); // Tell Server to turn buzzer OFF
}
delay(200);
}