|
LD2410Async
Asynchronous Arduino ESP32 library for the LD2410 mmWave radar sensor
|
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:
| |
| #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. | |
| #define RADAR_BAUDRATE 256000 |
UART baudrate for the radar sensor (default is 256000)
Definition at line 34 of file simplePresenceDetectionWebservice.ino.
| #define RADAR_RX_PIN 16 |
ESP32 pin that receives data from the radar (radar TX)
Definition at line 32 of file simplePresenceDetectionWebservice.ino.
| #define RADAR_TX_PIN 17 |
ESP32 pin that transmits data to the radar (radar RX)
Definition at line 33 of file simplePresenceDetectionWebservice.ino.
| #define USE_STATIC_IP true |
Definition at line 45 of file simplePresenceDetectionWebservice.ino.
| #define WEBSERVER_PORT 80 |
HTTP/WebSocket server port.
Definition at line 57 of file simplePresenceDetectionWebservice.ino.
| IPAddress GATEWAY | ( | 192 | , |
| 168 | , | ||
| 1 | , | ||
| 1 | ) |
Network gateway.
| IPAddress LOCAL_IP | ( | 192 | , |
| 168 | , | ||
| 1 | , | ||
| 123 | ) |
ESP32 static IP address.
| 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.
| 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.
| void onDetectionDataReceived | ( | LD2410Async * | sender, |
| bool | presenceDetected ) |
Callback function called whenever new detection data arrives from the radar.
| sender | Pointer to the LD2410Async instance (for multi-sensor setups) |
| presenceDetected | True if presence is detected, false otherwise |
Definition at line 112 of file simplePresenceDetectionWebservice.ino.
| void onWsEvent | ( | AsyncWebSocket * | server, |
| AsyncWebSocketClient * | client, | ||
| AwsEventType | type, | ||
| void * | arg, | ||
| uint8_t * | data, | ||
| size_t | len ) |
Handles WebSocket events (client connect/disconnect).
| server | Pointer to the AsyncWebSocket server |
| client | Pointer to the connected client |
| type | Event type (connect, disconnect, etc.) |
| arg | Event argument |
| data | Event data |
| len | Data length |
Definition at line 129 of file simplePresenceDetectionWebservice.ino.
| HardwareSerial RadarSerial | ( | 1 | ) |
HardwareSerial instance for UART1 (LD2410 sensor)
| AsyncWebServer server | ( | WEBSERVER_PORT | ) |
AsyncWebServer instance for HTTP/WebSocket.
| 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.
| 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.
| IPAddress SUBNET | ( | 255 | , |
| 255 | , | ||
| 255 | , | ||
| 0 | ) |
Network subnet mask.
| AsyncWebSocket ws | ( | "/ws" | ) |
AsyncWebSocket instance for live data updates.
| volatile uint16_t latestDetectedDistance = 0 |
Stores the latest detected distance (cm)
Definition at line 90 of file simplePresenceDetectionWebservice.ino.
| volatile bool latestPresenceDetected = false |
Stores the latest presence detection state.
Definition at line 85 of file simplePresenceDetectionWebservice.ino.
| 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.
| LD2410Async radar(RadarSerial) | ( | RadarSerial | ) |
LD2410Async instance for radar communication.
| const char* WIFI_PASSWORD = "YOUR_WIFI_PASSWORD" |
WiFi password.
Definition at line 42 of file simplePresenceDetectionWebservice.ino.
| const char* WIFI_SSID = "YOUR_WIFI_SSID" |
WiFi SSID.
Definition at line 41 of file simplePresenceDetectionWebservice.ino.