#include <WiFi.h>
#include <WebServer.h>
#include <HTTPClient.h>
const char* ssid = "INFINITUM6ED9";
const char* password = "yx4CcfN2Ym";
WebServer server(80);
const char* slave1IP = "192.168.1.73"; // IP del esclavo 1 (LED)
const char* slave2IP = "192.168.1.65"; // IP del esclavo 2 (Motor)
bool ledStatus = false;
bool motorStatus = false;
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Conectando a WiFi...");
}
Serial.println("Conectado a WiFi");
Serial.println(WiFi.localIP());
server.on("/", handleRoot);
server.on("/toggleLED", toggleLED);
server.on("/toggleMotor", toggleMotor);
server.begin();
}
void loop() {
server.handleClient();
}
// Función para enviar una solicitud HTTP a un esclavo
void sendCommandToSlave(const char* ip, const char* endpoint) {
HTTPClient http;
String url = String("http://") + ip + endpoint;
http.begin(url);
int httpResponseCode = http.GET();
if (httpResponseCode > 0) {
Serial.println("Respuesta del esclavo: " + String(httpResponseCode));
} else {
Serial.println("Error en la comunicación con el esclavo");
}
http.end();
}
void handleRoot() {
String html = "<html><body><h1>Control de LED y Motor</h1>";
html += "<p>LED: " + String(ledStatus ? "Encendido" : "Apagado") + "</p>";
html += "<p>Motor: " + String(motorStatus ? "Encendido" : "Apagado") + "</p>";
html += "<button onclick=\"location.href='/toggleLED'\">Cambiar LED</button>";
html += "<button onclick=\"location.href='/toggleMotor'\">Cambiar Motor</button>";
html += "</body></html>";
server.send(200, "text/html", html);
}
// Cambiar estado del LED
void toggleLED() {
ledStatus = !ledStatus;
sendCommandToSlave(slave1IP, ledStatus ? "/on" : "/off");
handleRoot();
}
// Cambiar estado del Motor
void toggleMotor() {
motorStatus = !motorStatus;
sendCommandToSlave(slave2IP, motorStatus ? "/on" : "/off");
handleRoot();
}