// websockets_CtoS_toggle_led_stat_upd_StoC_count.ino // // This is websocket Client to Server & Server to Client example // With LED text status update in the Browser (Client). // And a count update showing data from the Server. // // A browser button toggles the LED // and A text message showing the received websocket // message is embedded in the HTML page. // A count update field in embeded in the HTML page. #include #include #include const char* ssid = "YOUR_SSID"; const char* password = "YOUR_PASSWORD"; const int LED_PIN = 33; AsyncWebServer server(80); AsyncWebSocket webSocket("/ws"); int ledState = LOW; void handleWebSocketEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventType type, void *arg, uint8_t *data, size_t len) { switch (type) { case WS_EVT_DISCONNECT: Serial.printf("[%u] Disconnected!\n", client->id()); break; case WS_EVT_CONNECT: { IPAddress ip = client->remoteIP(); Serial.printf("[%u] Connected from %d.%d.%d.%d\n", client->id(), ip[0], ip[1], ip[2], ip[3]); // Initialise the browser entry box with the current LED state. // Create a JSON message containing the LED status String ledStatusUpdate = "{\"label\":\"ledStatus\",\"value\":\"" + String(ledState ? "LED ON" : "LED OFF") + "\"}"; // Send the LED status to the client client->text(ledStatusUpdate); } break; case WS_EVT_DATA: { AwsFrameInfo *info = (AwsFrameInfo*)arg; if (info->final && info->index == 0 && info->len == len && info->opcode == WS_TEXT) { data[len] = '\0'; String payload = String((char*)data); Serial.printf("[%u] Received: %s\n", client->id(), payload.c_str()); if (payload == "toggle") { ledState = !ledState; // digitalWrite(LED_PIN, ledState); digitalWrite(LED_PIN, !ledState); // For AI thinker LED control is inverted (pin33) String ledUpdate = "{\"label\":\"ledStatus\",\"value\":\"" + String(ledState ? "LED ON" : "LED OFF") + "\"}"; webSocket.textAll(ledUpdate); } } } break; } } void setup() { Serial.begin(115200); pinMode(LED_PIN, OUTPUT); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(1000); Serial.println("Connecting to WiFi..."); } Serial.println("Connected to WiFi"); Serial.print("IP Address: "); Serial.println(WiFi.localIP()); Serial.println("WebSockets: Client to Server, and Server to Client,"); Serial.println("with LED status using ESPAsyncWebServer.h"); server.on("/", HTTP_GET, handleRoot); server.addHandler(&webSocket); webSocket.onEvent(handleWebSocketEvent); server.begin(); } // Correct led start state. Changed all new led bits to prefix new... void handleRoot(AsyncWebServerRequest *request) { request->send(200, "text/html", R"HTML(

ESP32 Websocket Example - Client to Server, & Server to client

With LED status update.

LED Status:

Count:


)HTML"); } // END OF void handleRoot(AsyncWebServerRequest *request) int countTimeWas = millis(); long countVal = 0; void loop() { // Send data to browser, increasing number every second. if (millis() - countTimeWas >= 1000) { countTimeWas = millis(); String countUpdate = "{\"label\":\"count\",\"value\":" + String(countVal++) + "}"; webSocket.textAll(countUpdate); } }