Update na verzi 3.7.2. POZOR: vyzaduje do platformio.ini doplnit lib_compat_mode = strict
This commit is contained in:
8
idf_component_examples/serversentevents/CMakeLists.txt
Normal file
8
idf_component_examples/serversentevents/CMakeLists.txt
Normal file
@ -0,0 +1,8 @@
|
||||
# For more information about build system see
|
||||
# https://docs.espressif.com/projects/esp-idf/en/latest/api-guides/build-system.html
|
||||
# The following five lines of boilerplate have to be in your project's
|
||||
# CMakeLists in this exact order for cmake to work correctly
|
||||
cmake_minimum_required(VERSION 3.16)
|
||||
|
||||
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
|
||||
project(main)
|
1
idf_component_examples/serversentevents/README.md
Normal file
1
idf_component_examples/serversentevents/README.md
Normal file
@ -0,0 +1 @@
|
||||
### Basic example to show how to use ServerSentEvents
|
@ -0,0 +1,2 @@
|
||||
idf_component_register(SRCS "main.cpp"
|
||||
INCLUDE_DIRS ".")
|
@ -0,0 +1,6 @@
|
||||
## IDF Component Manager Manifest File
|
||||
dependencies:
|
||||
esp32async/espasyncwebserver:
|
||||
version: "*"
|
||||
override_path: "../../../"
|
||||
pre_release: true
|
95
idf_component_examples/serversentevents/main/main.cpp
Normal file
95
idf_component_examples/serversentevents/main/main.cpp
Normal file
@ -0,0 +1,95 @@
|
||||
// SPDX-License-Identifier: LGPL-3.0-or-later
|
||||
// Copyright 2016-2025 Hristo Gochkov, Mathieu Carbou, Emil Muratov
|
||||
|
||||
//
|
||||
// SSE example
|
||||
//
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <AsyncTCP.h>
|
||||
#include <WiFi.h>
|
||||
|
||||
#include <ESPAsyncWebServer.h>
|
||||
|
||||
static const char *htmlContent PROGMEM = R"(
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Server-Sent Events</title>
|
||||
<script>
|
||||
if (!!window.EventSource) {
|
||||
var source = new EventSource('/events');
|
||||
source.addEventListener('open', function(e) {
|
||||
console.log("Events Connected");
|
||||
}, false);
|
||||
source.addEventListener('error', function(e) {
|
||||
if (e.target.readyState != EventSource.OPEN) {
|
||||
console.log("Events Disconnected");
|
||||
}
|
||||
}, false);
|
||||
source.addEventListener('message', function(e) {
|
||||
console.log("message", e.data);
|
||||
}, false);
|
||||
source.addEventListener('heartbeat', function(e) {
|
||||
console.log("heartbeat", e.data);
|
||||
}, false);
|
||||
}
|
||||
</script>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Open your browser console!</h1>
|
||||
</body>
|
||||
</html>
|
||||
)";
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
12
idf_component_examples/serversentevents/sdkconfig.defaults
Normal file
12
idf_component_examples/serversentevents/sdkconfig.defaults
Normal file
@ -0,0 +1,12 @@
|
||||
#
|
||||
# Arduino ESP32
|
||||
#
|
||||
CONFIG_AUTOSTART_ARDUINO=y
|
||||
# end of Arduino ESP32
|
||||
|
||||
#
|
||||
# FREERTOS
|
||||
#
|
||||
CONFIG_FREERTOS_HZ=1000
|
||||
# end of FREERTOS
|
||||
# end of Component config
|
Reference in New Issue
Block a user