LD2410Async
Asynchronous Arduino ESP32 library for the LD2410 mmWave radar sensor
Loading...
Searching...
No Matches
simplePresenceDetectionWebservice.ino File Reference
#include <Arduino.h>
#include <WiFi.h>
#include <ESPAsyncWebServer.h>
#include "LD2410Async.h"

Go to the source code of this file.

Macros

UART Configuration

Example: Webservice for Presence Detection with LD2410Async and ESP Async WebServer

This example demonstrates how to use the LD2410Async library with an ESP32 to provide a web-based presence detection service. The ESP32 connects to a WiFi network, starts a web server, and serves a simple HTML page. The page connects via WebSocket to receive live presence detection data (presenceDetected, detectedDistance) from the LD2410 radar sensor.

Dependencies:

Warning
Make sure to install the required libraries and adjust the configuration section below to match your hardware and network setup.
#define RADAR_RX_PIN   16
 ESP32 pin that receives data from the radar (radar TX)
 
#define RADAR_TX_PIN   17
 ESP32 pin that transmits data to the radar (radar RX)
 
#define RADAR_BAUDRATE   256000
 UART baudrate for the radar sensor (default is 256000)
 
Webserver Configuration
#define WEBSERVER_PORT   80
 HTTP/WebSocket server port.
 

Functions

HardwareSerial RadarSerial (1)
 HardwareSerial instance for UART1 (LD2410 sensor)
 
AsyncWebServer server (WEBSERVER_PORT)
 AsyncWebServer instance for HTTP/WebSocket.
 
AsyncWebSocket ws ("/ws")
 AsyncWebSocket instance for live data updates.
 
void notifyClients ()
 Notifies all connected WebSocket clients with the latest presence data.
 
void onDetectionDataReceived (LD2410Async *sender, bool presenceDetected)
 Callback function called whenever new detection data arrives from the radar.
 
void onWsEvent (AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventType type, void *arg, uint8_t *data, size_t len)
 Handles WebSocket events (client connect/disconnect).
 
void setupWiFi ()
 Connects to WiFi using the user configuration.
 
void setup ()
 Arduino setup function.
 
void loop ()
 Arduino loop function.
 

Variables

LD2410Async radar (RadarSerial)
 LD2410Async instance for radar communication.
 
volatile bool latestPresenceDetected = false
 Stores the latest presence detection state.
 
volatile uint16_t latestDetectedDistance = 0
 Stores the latest detected distance (cm)
 
const char index_html[] PROGMEM
 HTML page served to clients.
 

WiFi Configuration

#define USE_STATIC_IP   true
 
const char * WIFI_SSID = "YOUR_WIFI_SSID"
 WiFi SSID.
 
const char * WIFI_PASSWORD = "YOUR_WIFI_PASSWORD"
 WiFi password.
 
IPAddress LOCAL_IP (192, 168, 1, 123)
 ESP32 static IP address.
 
IPAddress GATEWAY (192, 168, 1, 1)
 Network gateway.
 
IPAddress SUBNET (255, 255, 255, 0)
 Network subnet mask.
 

Macro Definition Documentation

◆ RADAR_BAUDRATE

#define RADAR_BAUDRATE   256000

UART baudrate for the radar sensor (default is 256000)

Definition at line 34 of file simplePresenceDetectionWebservice.ino.

◆ RADAR_RX_PIN

#define RADAR_RX_PIN   16

ESP32 pin that receives data from the radar (radar TX)

Definition at line 32 of file simplePresenceDetectionWebservice.ino.

◆ RADAR_TX_PIN

#define RADAR_TX_PIN   17

ESP32 pin that transmits data to the radar (radar RX)

Definition at line 33 of file simplePresenceDetectionWebservice.ino.

◆ USE_STATIC_IP

#define USE_STATIC_IP   true

Definition at line 45 of file simplePresenceDetectionWebservice.ino.

◆ WEBSERVER_PORT

#define WEBSERVER_PORT   80

HTTP/WebSocket server port.

Definition at line 57 of file simplePresenceDetectionWebservice.ino.

Function Documentation

◆ GATEWAY()

IPAddress GATEWAY ( 192 ,
168 ,
1 ,
1  )

Network gateway.

◆ LOCAL_IP()

IPAddress LOCAL_IP ( 192 ,
168 ,
1 ,
123  )

ESP32 static IP address.

◆ loop()

void loop ( )

Arduino loop function.

No code required; all logic is handled by callbacks and background tasks.

Definition at line 268 of file simplePresenceDetectionWebservice.ino.

◆ notifyClients()

void notifyClients ( )

Notifies all connected WebSocket clients with the latest presence data.

Sends a JSON string with the fields "presenceDetected" (bool) and "detectedDistance" (uint16_t).

Definition at line 97 of file simplePresenceDetectionWebservice.ino.

◆ onDetectionDataReceived()

void onDetectionDataReceived ( LD2410Async * sender,
bool presenceDetected )

Callback function called whenever new detection data arrives from the radar.

Parameters
senderPointer to the LD2410Async instance (for multi-sensor setups)
presenceDetectedTrue if presence is detected, false otherwise

Definition at line 112 of file simplePresenceDetectionWebservice.ino.

◆ onWsEvent()

void onWsEvent ( AsyncWebSocket * server,
AsyncWebSocketClient * client,
AwsEventType type,
void * arg,
uint8_t * data,
size_t len )

Handles WebSocket events (client connect/disconnect).

Parameters
serverPointer to the AsyncWebSocket server
clientPointer to the connected client
typeEvent type (connect, disconnect, etc.)
argEvent argument
dataEvent data
lenData length

Definition at line 129 of file simplePresenceDetectionWebservice.ino.

◆ RadarSerial()

HardwareSerial RadarSerial ( 1 )

HardwareSerial instance for UART1 (LD2410 sensor)

◆ server()

AsyncWebServer server ( WEBSERVER_PORT )

AsyncWebServer instance for HTTP/WebSocket.

◆ setup()

void setup ( )

Arduino setup function.

Initializes serial, WiFi, radar sensor, and webserver. Registers the detection data callback and sets up the WebSocket and HTTP handlers.

Definition at line 228 of file simplePresenceDetectionWebservice.ino.

◆ setupWiFi()

void setupWiFi ( )

Connects to WiFi using the user configuration.

If USE_STATIC_IP is true, configures a static IP, subnet, and gateway. Prints connection status and IP address to Serial.

Definition at line 198 of file simplePresenceDetectionWebservice.ino.

◆ SUBNET()

IPAddress SUBNET ( 255 ,
255 ,
255 ,
0  )

Network subnet mask.

◆ ws()

AsyncWebSocket ws ( "/ws" )

AsyncWebSocket instance for live data updates.

Variable Documentation

◆ latestDetectedDistance

volatile uint16_t latestDetectedDistance = 0

Stores the latest detected distance (cm)

Definition at line 90 of file simplePresenceDetectionWebservice.ino.

◆ latestPresenceDetected

volatile bool latestPresenceDetected = false

Stores the latest presence detection state.

Definition at line 85 of file simplePresenceDetectionWebservice.ino.

◆ PROGMEM

const char index_html [] PROGMEM

HTML page served to clients.

Connects to the WebSocket and displays live presence and distance data.

Definition at line 146 of file simplePresenceDetectionWebservice.ino.

◆ radar

LD2410Async instance for radar communication.

◆ WIFI_PASSWORD

const char* WIFI_PASSWORD = "YOUR_WIFI_PASSWORD"

WiFi password.

Definition at line 42 of file simplePresenceDetectionWebservice.ino.

◆ WIFI_SSID

const char* WIFI_SSID = "YOUR_WIFI_SSID"

WiFi SSID.

Definition at line 41 of file simplePresenceDetectionWebservice.ino.