|
| 1 | +// SPDX-License-Identifier: LGPL-3.0-or-later |
| 2 | +// Copyright 2016-2025 Hristo Gochkov, Mathieu Carbou, Emil Muratov |
| 3 | + |
| 4 | +// |
| 5 | +// WebSocket example using the easy to use AsyncWebSocketMessageHandler handler that only supports unfragmented messages |
| 6 | +// |
| 7 | + |
| 8 | +#include <Arduino.h> |
| 9 | +#ifdef ESP32 |
| 10 | +#include <AsyncTCP.h> |
| 11 | +#include <WiFi.h> |
| 12 | +#elif defined(ESP8266) |
| 13 | +#include <ESP8266WiFi.h> |
| 14 | +#include <ESPAsyncTCP.h> |
| 15 | +#elif defined(TARGET_RP2040) || defined(TARGET_RP2350) || defined(PICO_RP2040) || defined(PICO_RP2350) |
| 16 | +#include <RPAsyncTCP.h> |
| 17 | +#include <WiFi.h> |
| 18 | +#endif |
| 19 | + |
| 20 | +#include <ESPAsyncWebServer.h> |
| 21 | + |
| 22 | +static AsyncWebServer server(80); |
| 23 | + |
| 24 | +// create an easy-to-use handler |
| 25 | +static AsyncWebSocketMessageHandler wsHandler; |
| 26 | + |
| 27 | +// add it to the websocket server |
| 28 | +static AsyncWebSocket ws("/ws", wsHandler.eventHandler()); |
| 29 | + |
| 30 | +// alternatively you can do as usual: |
| 31 | +// |
| 32 | +// static AsyncWebSocket ws("/ws"); |
| 33 | +// ws.onEvent(wsHandler.eventHandler()); |
| 34 | + |
| 35 | +static const char *htmlContent PROGMEM = R"( |
| 36 | +<!DOCTYPE html> |
| 37 | +<html> |
| 38 | +<head> |
| 39 | + <title>WebSocket</title> |
| 40 | +</head> |
| 41 | +<body> |
| 42 | + <h1>WebSocket Example</h1> |
| 43 | + <>Open your browser console!</p> |
| 44 | + <input type="text" id="message" placeholder="Type a message"> |
| 45 | + <button onclick='sendMessage()'>Send</button> |
| 46 | + <script> |
| 47 | + var ws = new WebSocket('ws://192.168.4.1/ws'); |
| 48 | + ws.onopen = function() { |
| 49 | + console.log("WebSocket connected"); |
| 50 | + }; |
| 51 | + ws.onmessage = function(event) { |
| 52 | + console.log("WebSocket message: " + event.data); |
| 53 | + }; |
| 54 | + ws.onclose = function() { |
| 55 | + console.log("WebSocket closed"); |
| 56 | + }; |
| 57 | + ws.onerror = function(error) { |
| 58 | + console.log("WebSocket error: " + error); |
| 59 | + }; |
| 60 | + function sendMessage() { |
| 61 | + var message = document.getElementById("message").value; |
| 62 | + ws.send(message); |
| 63 | + console.log("WebSocket sent: " + message); |
| 64 | + } |
| 65 | + </script> |
| 66 | +</body> |
| 67 | +</html> |
| 68 | + )"; |
| 69 | +static const size_t htmlContentLength = strlen_P(htmlContent); |
| 70 | + |
| 71 | +void setup() { |
| 72 | + Serial.begin(115200); |
| 73 | + |
| 74 | +#ifndef CONFIG_IDF_TARGET_ESP32H2 |
| 75 | + WiFi.mode(WIFI_AP); |
| 76 | + WiFi.softAP("esp-captive"); |
| 77 | +#endif |
| 78 | + |
| 79 | + // serves root html page |
| 80 | + server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) { |
| 81 | + request->send(200, "text/html", (const uint8_t *)htmlContent, htmlContentLength); |
| 82 | + }); |
| 83 | + |
| 84 | + wsHandler.onConnect([](AsyncWebSocket *server, AsyncWebSocketClient *client) { |
| 85 | + Serial.printf("Client %" PRIu32 " connected\n", client->id()); |
| 86 | + server->textAll("New client: " + String(client->id())); |
| 87 | + }); |
| 88 | + |
| 89 | + wsHandler.onDisconnect([](AsyncWebSocket *server, uint32_t clientId) { |
| 90 | + Serial.printf("Client %" PRIu32 " disconnected\n", clientId); |
| 91 | + server->textAll("Client " + String(clientId) + " disconnected"); |
| 92 | + }); |
| 93 | + |
| 94 | + wsHandler.onError([](AsyncWebSocket *server, AsyncWebSocketClient *client, uint16_t errorCode, const char *reason, size_t len) { |
| 95 | + Serial.printf("Client %" PRIu32 " error: %" PRIu16 ": %s\n", client->id(), errorCode, reason); |
| 96 | + }); |
| 97 | + |
| 98 | + wsHandler.onData([](AsyncWebSocket *server, AsyncWebSocketClient *client, const uint8_t *data, size_t len) { |
| 99 | + Serial.printf("Client %" PRIu32 " data: %s\n", client->id(), (const char *)data); |
| 100 | + }); |
| 101 | + |
| 102 | + server.addHandler(&ws); |
| 103 | + server.begin(); |
| 104 | +} |
| 105 | + |
| 106 | +static uint32_t lastWS = 0; |
| 107 | +static uint32_t deltaWS = 2000; |
| 108 | + |
| 109 | +void loop() { |
| 110 | + uint32_t now = millis(); |
| 111 | + |
| 112 | + if (now - lastWS >= deltaWS) { |
| 113 | + ws.cleanupClients(); |
| 114 | + ws.printfAll("now: %" PRIu32 "\n", now); |
| 115 | + lastWS = millis(); |
| 116 | +#ifdef ESP32 |
| 117 | + Serial.printf("Free heap: %" PRIu32 "\n", ESP.getFreeHeap()); |
| 118 | +#endif |
| 119 | + } |
| 120 | +} |
0 commit comments