// SPDX-License-Identifier: LGPL-3.0-or-later // Copyright 2016-2025 Hristo Gochkov, Mathieu Carbou, Emil Muratov // // SSE example // #include #include #include #include static const char *htmlContent PROGMEM = R"( Server-Sent Events

Open your browser console!

)"; static const size_t htmlContentLength = strlen_P(htmlContent); static AsyncWebServer server(80); static AsyncEventSource events("/events"); void setup() { Serial.begin(115200); #ifndef CONFIG_IDF_TARGET_ESP32H2 WiFi.mode(WIFI_AP); WiFi.softAP("esp-captive"); #endif // curl -v http://192.168.4.1/ server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) { // need to cast to uint8_t* // if you do not, the const char* will be copied in a temporary String buffer request->send(200, "text/html", (uint8_t *)htmlContent, htmlContentLength); }); events.onConnect([](AsyncEventSourceClient *client) { Serial.printf("SSE Client connected! ID: %" PRIu32 "\n", client->lastId()); client->send("hello!", NULL, millis(), 1000); }); events.onDisconnect([](AsyncEventSourceClient *client) { Serial.printf("SSE Client disconnected! ID: %" PRIu32 "\n", client->lastId()); }); server.addHandler(&events); server.begin(); } static uint32_t lastSSE = 0; static uint32_t deltaSSE = 3000; static uint32_t lastHeap = 0; void loop() { uint32_t now = millis(); if (now - lastSSE >= deltaSSE) { events.send(String("ping-") + now, "heartbeat", now); lastSSE = millis(); } if (now - lastHeap >= 2000) { Serial.printf("Free heap: %" PRIu32 "\n", ESP.getFreeHeap()); lastHeap = now; } }