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 libcoap-1-0 $sudo apt-get install libcoap-1-0-bin
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.
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 "LED0%00"
LED1 can also be toggled with changing the payload to “LED1%00”.