CoAP API¶
CoAP (Constrained Application Protocol) is a type of protocol similar to HTTP. But HTTP is TCP, whereas CoAP is UDP. It is developed to reduce the size of the data packet sent. It reduces the use of memory and processing power. Therefore it is suitable for Microcontrollers that have small memory and NB-IoT that use low power. The endpoint is coap://coap.netpie.io. The details are as follows:
Use NodeJS (install from https://nodejs.org/en/download/) for CoAP client installation. The command is:
npm i coap-cli@0.5.1 -g
1. Publish Messages to Various Topics
EndPoint |
coap://coap.netpie.io/message/{any}/{topic} |
Method |
PUT |
Parameter |
auth=<ClientID>:<Token> |
Payload |
-p message |
Return |
The response code here is |
Example (Command Line)
coap put "coap://coap.netpie.io/message/home/bedroom?auth=6c36fdee-5273-4318-xxxx-75dfd2c513db:nzxGsGMYnFdfET6xxxxfb32U9z5kuhvx" -p "Hello from CoAP"
In the example above, it will Publish message Hello from CoAP
, to the Topic @msg/home/bedroom
2. Reading Shadow Data of the Device
EndPoint |
coap://coap.netpie.io/shadow/data |
Method |
GET |
Parameter |
auth=<ClientID>:<Token> |
Return |
Response Object { |
Example (Command Line)
coap get "coap://coap.netpie.io/shadow/data?auth=6c36fdee-5273-4318-xxxx-75dfd2c513db:nzxGsGMYnFdfET6xxxxfb32U9z5kuhvx"
From the example above, it is reading the shadow information of Device ID: 6c36fdee-5273-4318-xxxx-75dfd2c513db . And the return value is:
{
"deviceid":"6c36fdee-5273-4318-xxxx-75dfd2c513db",
"data": {
"humid":76.2, "temp":25
},
"rev":3,
"modified":1605516471534
}
3. Writing Data to Shadow Data as Merge
EndPoint |
coap://coap.netpie.io/shadow/data |
Method |
PUT |
Parameter |
auth=<ClientID>:<Token> |
Payload |
-p {data: { Shadow Data (JSON) }} |
Return |
Response Object { |
Example (Command Line)
coap put "coap://coap.netpie.io/shadow/data?auth=6c36fdee-5273-4318-xxxx-75dfd2c513db:nzxGsGMYnFdfET6xxxxfb32U9z5kuhvx" -p "{data: {temp: 30.4} }"
From the example above, It is a merge shadow write of device ID: 6c36fdee-5273-4318-xxxx-75dfd2c513db. And return value is:
{
"deviceid":"6c36fdee-5273-4318-xxxx-75dfd2c513db",
"data": {
"temp":30.4
},
"modified":1605518877506,
"timestamp":1605518877506
}