#include #include #include #include #define SDA_PIN 22 #define SCL_PIN 23 const uint8_t I2C_ADDRESS = 0x55; const uint32_t I2C_FREQUENCY = 100000; const char* WIFI_SSID = "LIARE"; const char* WIFI_PASSWORD = "j3tson-nan0"; bool motorState = false; WebServer server(80); void handleRoot() { String html = ""; html += "XIAO ESP32C6 HTTP"; html += "

XIAO ESP32C6 HTTP Server

"; html += "

Motor state: "; html += (motorState ? "ON" : "OFF"); html += "

"; html += "

Turn Motor ON

"; html += "

Turn Motor OFF

"; html += ""; server.send(200, "text/html", html); } void handleOn() { motorState = true; Wire.beginTransmission(I2C_ADDRESS); Wire.write((uint8_t*)"On", 2); // longitud sin el \0 final Wire.endTransmission(); server.send(200, "text/plain", "Motor is now ON"); } void handleOff() { motorState = false; Wire.beginTransmission(I2C_ADDRESS); Wire.write((uint8_t*)"Off", 3); // longitud sin el \0 final Wire.endTransmission(); server.send(200, "text/plain", "Motor is now OFF"); } void handleNotFound() { server.send(404, "text/plain", "Route not found"); } void setup() { Serial.begin(115200); delay(1000); Serial.println("Starting I2C..."); Wire.begin(SDA_PIN, SCL_PIN, I2C_FREQUENCY); Serial.println("Connecting to WiFi..."); WiFi.begin(WIFI_SSID, WIFI_PASSWORD); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(); Serial.println("Wi-Fi connected"); Serial.print("IP address: "); Serial.println(WiFi.localIP()); server.on("/", handleRoot); server.on("/motor/on", handleOn); server.on("/motor/off", handleOff); server.onNotFound(handleNotFound); server.begin(); Serial.println("HTTP server started"); } void loop() { server.handleClient(); }