/* ESP8266 Web server with Web Socket to control an LED. The web server keeps all clients' LED status up to date and any client may turn the LED on or off. For example, clientA connects and turns the LED on. This changes the word "LED" on the web page to the color red. When clientB connects, the word "LED" will be red since the server knows the LED is on. When clientB turns the LED off, the word LED changes color to black on clientA and clientB web pages. References: https://github.com/Links2004/arduinoWebSockets https://gist.github.com/bbx10/667e3d4f5f2c0831d00b This has been modified to suit the relay's normally open configuration When the button is pressed, the LED comes on for half a second */ #include #include #include #include #include #include static const char ssid[] = "SSID"; static const char password[] = "Password"; MDNSResponder mdns; static void switchRELAY(bool); ESP8266WiFiMulti WiFiMulti; ESP8266WebServer server(80); WebSocketsServer webSocket = WebSocketsServer(81); static const char PROGMEM INDEX_HTML[] = R"rawliteral( Open Sesame
)rawliteral"; const int RELAYPIN = 5; // GPIO#5 bool relaystatus; // Commands sent through Web Socket const char relaytriggered[] = "relaytriggered"; void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length) { Serial.printf("webSocketEvent(%d, %d, ...)\r\n", num, type); switch(type) { case WStype_DISCONNECTED: Serial.printf("[%u] Disconnected!\r\n", num); break; case WStype_CONNECTED: { IPAddress ip = webSocket.remoteIP(num); Serial.printf("[%u] Connected from %d.%d.%d.%d url: %s\r\n", num, ip[0], ip[1], ip[2], ip[3], payload); // Send the current LED status if (relaystatus) { webSocket.sendTXT(num, relaytriggered, strlen(relaytriggered)); } } break; case WStype_TEXT: Serial.printf("[%u] get Text: %s\r\n", num, payload); if (strcmp(relaytriggered, (const char *)payload) == 0) { switchRELAY(true); } else { Serial.println("Unknown command"); } // send data to all connected clients webSocket.broadcastTXT(payload, length); break; case WStype_BIN: Serial.printf("[%u] get binary length: %u\r\n", num, length); hexdump(payload, length); // echo data back to browser webSocket.sendBIN(num, payload, length); break; default: Serial.printf("Invalid WStype [%d]\r\n", type); break; } } void handleRoot() { server.send_P(200, "text/html", INDEX_HTML); } void handleNotFound() { String message = "File Not Found\n\n"; message += "URI: "; message += server.uri(); message += "\nMethod: "; message += (server.method() == HTTP_GET)?"GET":"POST"; message += "\nArguments: "; message += server.args(); message += "\n"; for (uint8_t i=0; i 0; t--) { Serial.printf("[SETUP] BOOT WAIT %d...\r\n", t); Serial.flush(); delay(1000); } WiFiMulti.addAP(ssid, password); while(WiFiMulti.run() != WL_CONNECTED) { Serial.print("."); delay(100); } Serial.println(""); Serial.print("Connected to "); Serial.println(ssid); Serial.print("IP address: "); Serial.println(WiFi.localIP()); if (mdns.begin("espWebSock", WiFi.localIP())) { Serial.println("MDNS responder started"); mdns.addService("http", "tcp", 80); mdns.addService("ws", "tcp", 81); } else { Serial.println("MDNS.begin failed"); } Serial.print("Connect to http://espWebSock.local or http://"); Serial.println(WiFi.localIP()); server.on("/", handleRoot); server.onNotFound(handleNotFound); server.begin(); webSocket.begin(); webSocket.onEvent(webSocketEvent); } void loop() { webSocket.loop(); server.handleClient(); }