CoAP Communication#
Get the Metering Data#
The Raspberry Pi gets CoAP metering data from the CoAP meter node using the libcoap client, if you have a node running Wi-SUN SoC CoAP Meter in the same network with the Linux Border Router. Follow the steps below to get CoAP metering data on your Raspberry Pi:
Install the libcoap library.
$sudo apt-get install libcoap2 $sudo apt-get install libcoap2-bin
On more recent linux kernels, use libcoap3 and the commands with the
-notls
suffix to keep it simple (without TLS authentication), considering that your Wi-SUN network already supports encryption.
From the node cli, make sure that you are connected to the network.
> wisun get wisun.join_state wisun.connection_state = Operational (5)
Get your node global address.
> wisun get wisun.ip_address_global wisun.ip_address_global = fd12:3456::be33:acff:fef6:3161
Discover the Available Resources#
The CoAP protocol supports an interoperable discovery feature. A CoAP client can request the attributes hosted by a CoAP server. In the case of the CoAP Meter application, the available resources can be retrieved using the libcoap GET method with the standard discovery entry-point. The following command shows the sensor and LED resources.
$coap-client -m get -N coap://[fd12:3456::be33:acff:fef6:3161]:5683/.well-known/core
</gpio>;ct=40,</sensor>;ct=40,</gpio/led>;rt="led";if="gpio",</sensor/light>;rt="light";if="sensor",</sensor/humidity>;rt="humidity";if="sensor",</sensor/temperature>;rt="temperature";if="sensor",</sensor/all>;rt="all";if="sensor"
Get the CoAP Meter Sensor Data#
On your raspberry Pi, get the CoAP meter sensor data using libcoap with the following command:
$coap-client -m get -N coap://[fd12:3456::be33:acff:fef6:3161]:5683/sensor/all
{
"id": 0,
"temp": 28.18,
"hum": 46.36,
"lx": 512
}
Each sensor data value can be retrieved alone if you specify the corresponding resource in the libcoap command. To retrieve the humidity value, the user can use the following command:
$coap-client -m get -N coap://[fd12:3456::be33:acff:fef6:3161]:5683/sensor/humidity
46.59 %
Note that any machine that has a CoAP client and an IPv6 connectivity with the CoAP Meter node can get the metering data.
Get Help on CoAP Meter Capabilities#
$coap-client -t text -m put -N -B 1 coap://[fd12:3456::be33:acff:fef6:3161]:5683/meter/help
Command example:
coap-client -m get -N -B 10 -t text coap://[$METER_IPV6_ADDR]:5683/$URI_PATH -e "$PAYLOAD"
Requests:
[PUT/POST/GET] sensor/all:
- no payload or "async": GET request to get measurement data
- "register": POST/PUT request to register collector
- "remove": POST/PUT request to remove collector
[GET] sensor/temperature: Request to get temperature
[GET] sensor/humidity: Request to get humidity
[GET] sensor/light: Request to get light
[PUT/POST] gpio/led: Toggle LED0/LED1. Payload is index of the LED: "0" or "1"
Toggle the LEDs#
The libcoap PUT method allows you to toggle the CoAP Meter node LEDs remotely. Use the following command to toggle the LED0.
$coap-client -t text -m put -N -B 1 coap://[fd12:3456::be33:acff:fef6:3161]:5683/gpio/led -e "0"
LED1 can also be toggled with changing the payload to “1”.