USER MANUAL RC188x-GPR
2019 Radiocrafts AS
RIIM User Manual (rev.1.2)
Page
13
of
23
8.3. CoAP
The CoAP module lets you start and run a CoAP server and lets you transmit CoAP messages to a server.
The following example demonstrates how you initialize the CoAP module, set up a response handler and a
resource access handler. When this node receives a PUT, POST, GET or DELETE CoAP message, it will call the
CoAP_Handler
routine.
Example : ICI code
static
const
uint8_t
Resource_Name
[]=
"data"
;
static
void
CoAP_Handler
(
RequestType type
,
uint8_t
*
payload
,
uint8_t
payloadSize
,
uint8_t
*
response
,
uint8_t
*
responseSize
)
{
// In this example we only print the payload directly to the UART
Debug
.
printPayload
(
payload
,
payloadSize
);
Util
.
printf
(
"\n"
);
// Add a newline for readability in console
return
;
}
void
ResponseHandler
(
const
uint8_t
*
payload
,
uint8_t
payload_size
)
{
Util
.
printf
(
"# Got CoAP Response. Doing nothing with it...\n"
);
return
;
}
RIIM_SETUP
()
{
Network
.
startBorderRouter
(
NULL
,
NULL
,
NULL
,
NULL
);
// Setup coap resource
CoAP
.
init
(
ResponseHandler
);
CoAP
.
registerResource
(
Resource_Name
,
1
,
CoAP_Handler
);
return
UAPI_OK
;
}
Example 3 - CoAP example code
The following functions are part of the CoAP module.
Function
Description
Init
()
Initializes the CoAP module
connectToServer
(IPAddress, Secure)
Connect to a CoAP server. This must be done before accessing it.
registerResource
(Path, resourceID,
CoAP_Handler)
Register a new resource that will be accessible on the node
generateResponse
(Payload,
PayloadSize, Response,
ResponseSize)
Generate a response message and send it back to the caller
send
(RequestType, IPAddress,
Resource, Payload, PayloadSize)
Send a GET, PUT, POST or DELETE message to a CoAP server
Table 3 - Coap functions