Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
M
mongoose
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
esp
mongoose
Commits
56e50c2d
Commit
56e50c2d
authored
Sep 29, 2016
by
Alexander Alashkin
Committed by
Cesanta Bot
Sep 29, 2016
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add CoAP client example doc
PUBLISHED_FROM=51fd5cd50ae8c9fce43d95d06fec2e7582018d00
parent
c1b5e9b2
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
61 additions
and
1 deletion
+61
-1
client_example.md
docs/coap/client_example.md
+61
-1
No files found.
docs/coap/client_example.md
View file @
56e50c2d
...
@@ -2,4 +2,64 @@
...
@@ -2,4 +2,64 @@
title
:
CoAP client example
title
:
CoAP client example
---
---
TBD
To create a CoAP client, follow this pattern:
1.
Create an outbound connection by calling
`mg_connect`
2.
Call
`mg_set_protocol_coap`
for created connection
3.
Create an event handler function that handles the following events:
-
`MG_EV_COAP_CON`
-
`MG_EV_COAP_NOC`
-
`MG_EV_COAP_ACK`
-
`MG_EV_COAP_RST`
Here's an example of the simplest CoAP client.
Error checking is omitted for the sake of clarity:
```
c
#include "mongoose.h"
static
int
s_time_to_exit
=
0
;
static
char
*
s_default_address
=
"udp://coap.me:5683"
;
static
void
coap_handler
(
struct
mg_connection
*
nc
,
int
ev
,
void
*
p
)
{
switch
(
ev
)
{
case
MG_EV_CONNECT
:
{
struct
mg_coap_message
cm
;
memset
(
&
cm
,
0
,
sizeof
(
cm
));
cm
.
msg_id
=
1
;
cm
.
msg_type
=
MG_COAP_MSG_CON
;
mg_coap_send_message
(
nc
,
&
cm
);
break
;
}
case
MG_EV_COAP_ACK
:
case
MG_EV_COAP_RST
:
{
struct
mg_coap_message
*
cm
=
(
struct
mg_coap_message
*
)
p
;
printf
(
"ACK/RST for message with msg_id = %d received
\n
"
,
cm
->
msg_id
);
s_time_to_exit
=
1
;
break
;
}
}
}
int
main
(
int
argc
,
char
*
argv
[])
{
struct
mg_mgr
mgr
;
struct
mg_connection
*
nc
;
mg_mgr_init
(
&
mgr
,
0
);
nc
=
mg_connect
(
&
mgr
,
s_default_address
,
coap_handler
);
mg_set_protocol_coap
(
nc
);
while
(
!
s_time_to_exit
)
{
mg_mgr_poll
(
&
mgr
,
1000000
);
}
mg_mgr_free
(
&
mgr
);
return
0
;
}
```
See full source code at
[
CoAP client example
](
https://github.com/cesanta/mongoose/tree/master/examples/coap_client
)
.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment