23#include <ESPAsyncWebServer.h>
32#define RADAR_RX_PIN 16
33#define RADAR_TX_PIN 17
34#define RADAR_BAUDRATE 256000
45#define USE_STATIC_IP true
57#define WEBSERVER_PORT 80
80AsyncWebSocket
ws(
"/ws");
98 String msg =
"{\"presenceDetected\":";
100 msg +=
",\"detectedDistance\":";
129void onWsEvent(AsyncWebSocket*
server, AsyncWebSocketClient* client, AwsEventType type,
130 void* arg, uint8_t* data,
size_t len) {
131 if (type == WS_EVT_CONNECT) {
132 Serial.printf(
"WebSocket client #%u connected\n", client->id());
136 else if (type == WS_EVT_DISCONNECT) {
137 Serial.printf(
"WebSocket client #%u disconnected\n", client->id());
146const char index_html[]
PROGMEM = R
"rawliteral(
150 <meta charset="UTF-8">
151 <title>LD2410 Presence Detection</title>
153 body { font-family: Arial, sans-serif; margin: 2em; }
154 .status { font-size: 2em; margin-bottom: 1em; }
155 .distance { font-size: 1.5em; }
156 .present { color: green; }
157 .absent { color: red; }
161 <h2>LD2410 Presence Detection</h2>
162 <div class="status" id="presence">Connecting...</div>
163 <div class="distance" id="distance"></div>
165 let ws = new WebSocket('ws://' + location.host + '/ws');
166 ws.onmessage = function(event) {
167 let data = JSON.parse(event.data);
168 let presence = document.getElementById('presence');
169 let distance = document.getElementById('distance');
170 if (data.presenceDetected) {
171 presence.textContent = "Presence detected!";
172 presence.className = "status present";
173 distance.textContent = "Distance: " + data.detectedDistance + " cm";
175 presence.textContent = "No presence detected.";
176 presence.className = "status absent";
177 distance.textContent = "";
180 ws.onopen = function() {
181 document.getElementById('presence').textContent = "Waiting for data...";
183 ws.onclose = function() {
184 document.getElementById('presence').textContent = "WebSocket disconnected.";
185 document.getElementById('presence').className = "status";
199 Serial.print(
"Connecting to WiFi: ");
204 Serial.println(
"STA Failed to configure static IP");
209 while (WiFi.status() != WL_CONNECTED) {
212 if (++retries > 40) {
213 Serial.println(
"\nFailed to connect to WiFi!");
217 Serial.println(
"\nWiFi connected.");
218 Serial.print(
"IP address: ");
219 Serial.println(WiFi.localIP());
230 Serial.begin(115200);
231 while (!Serial) { ; }
232 Serial.println(
"LD2410Async Web Presence Detection Example");
242 Serial.println(
"Radar task started successfully.");
246 Serial.println(
"ERROR! Could not start radar task.");
254 server.on(
"/", HTTP_GET, [](AsyncWebServerRequest* request) {
255 request->send_P(200,
"text/html", index_html);
260 Serial.println(
"Webserver started.");
Asynchronous driver class for the LD2410 human presence radar sensor.
bool begin()
Starts the background task that continuously reads data from the sensor.
void onDetectionDataReceived(DetectionDataCallback callback)
Registers a callback for new detection data.
const LD2410Types::DetectionData & getDetectionDataRef() const
Access the current detection data without making a copy.
IPAddress SUBNET(255, 255, 255, 0)
Network subnet mask.
AsyncWebServer server(WEBSERVER_PORT)
AsyncWebServer instance for HTTP/WebSocket.
AsyncWebSocket ws("/ws")
AsyncWebSocket instance for live data updates.
const char * WIFI_PASSWORD
WiFi password.
IPAddress GATEWAY(192, 168, 1, 1)
Network gateway.
HardwareSerial RadarSerial(1)
HardwareSerial instance for UART1 (LD2410 sensor)
void setup()
Arduino setup function.
const char * WIFI_SSID
WiFi SSID.
IPAddress LOCAL_IP(192, 168, 1, 123)
ESP32 static IP address.
void onDetectionDataReceived(LD2410Async *sender, bool presenceDetected)
Callback function called whenever new detection data arrives from the radar.
LD2410Async radar(RadarSerial)
LD2410Async instance for radar communication.
void onWsEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventType type, void *arg, uint8_t *data, size_t len)
Handles WebSocket events (client connect/disconnect).
const char index_html[] PROGMEM
HTML page served to clients.
#define RADAR_BAUDRATE
UART baudrate for the radar sensor (default is 256000)
#define RADAR_RX_PIN
ESP32 pin that receives data from the radar (radar TX)
#define WEBSERVER_PORT
HTTP/WebSocket server port.
volatile bool latestPresenceDetected
Stores the latest presence detection state.
#define RADAR_TX_PIN
ESP32 pin that transmits data to the radar (radar RX)
void setupWiFi()
Connects to WiFi using the user configuration.
volatile uint16_t latestDetectedDistance
Stores the latest detected distance (cm)
void loop()
Arduino loop function.
void notifyClients()
Notifies all connected WebSocket clients with the latest presence data.
Holds the most recent detection data reported by the radar.
unsigned int detectedDistance
General detection distance (cm).
bool presenceDetected
True if any target is detected.