udp works in both directions: udp.send() transmits fire-and-forget
datagrams (no delivery confirmation — ideal for high-rate telemetry and
syslog targets), and udp.handle() listens on a port and hands received
packets to a callback, optionally decrypting them with AES-256.
Functions
udp.send() — Transmits a UDP packet to a host and port.
udp.handle() — Listens for UDP packets on a port and passes them to a callback — only one listener is supported.
Listens for UDP packets on a port and passes them to a callback — only one listener is supported.
Registers the device's single UDP listener. The handler receives a request
object whose req.data holds the packet payload — parse JSON payloads with
JSON.parse(). When encrypt is true, payloads are decrypted with AES-256 and the integrity validated with a SHA256 checksum before the handler runs.
Arguments
Name
Type
Required
Default
Description
port
number
yes
—
UDP port to listen on, e.g. 9000.
handler
function
yes
—
Callback function(req){ … } — req.data is the received payload.
encrypt
boolean
yes
—
true to expect AES-256 encrypted payloads.
Example
udp.handle(9000, function(req){ // only a single udp.handle listener is supported
//req = {port:4000,host:"172.16.220.1",data:"{msg:\"Hello World!\"}"}
let data = JSON.parse(req.data); // parse the received payload as JSON
print(data.msg);
}, false); // false: do not use AES-256 encryption or checksums