├── src
├── coap
│ └── coap.h
├── simple-coap.h
└── simple-coap.cpp
├── library.properties
├── license.txt
├── examples
├── coaptest
│ └── coaptest.ino
└── coapserver
│ └── coapserver.ino
└── README.md
/src/coap/coap.h:
--------------------------------------------------------------------------------
1 | #include "../coap.h"
--------------------------------------------------------------------------------
/library.properties:
--------------------------------------------------------------------------------
1 | name=coap
2 | version=0.2.5
3 | license=MIT
4 | author=hirotakaster
5 | url=https://github.com/hirotakaster/CoAP/
6 | repository=https://github.com/hirotakaster/CoAP.git
7 |
--------------------------------------------------------------------------------
/license.txt:
--------------------------------------------------------------------------------
1 | Copyright (c) 2015 Hirotaka Niisato
2 | This software is released under the MIT License.
3 |
4 | Permission is hereby granted, free of charge, to any person obtaining
5 | a copy of this software and associated documentation files (the
6 | "Software"), to deal in the Software without restriction, including
7 | without limitation the rights to use, copy, modify, merge, publish,
8 | distribute, sublicense, and/or sell copies of the Software, and to
9 | permit persons to whom the Software is furnished to do so, subject to
10 | the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be
13 | included in all copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 |
--------------------------------------------------------------------------------
/examples/coaptest/coaptest.ino:
--------------------------------------------------------------------------------
1 | #include "simple-coap.h"
2 |
3 | // CoAP client response callback
4 | void callback_response(CoapPacket &packet, IPAddress ip, int port);
5 | Coap coap;
6 |
7 | // CoAP client response callback
8 | void callback_response(CoapPacket &packet, IPAddress ip, int port) {
9 | Serial.println("[Coap Response got]");
10 |
11 | char p[packet.payloadlen + 1];
12 | memcpy(p, packet.payload, packet.payloadlen);
13 | p[packet.payloadlen] = NULL;
14 |
15 | Serial.println(p);
16 | }
17 |
18 | void setup() {
19 | Serial.begin(9600);
20 |
21 | // client response callback.
22 | // this endpoint is single callback.
23 | Serial.println("Setup Response Callback");
24 | coap.response(callback_response);
25 |
26 | // start coap server/client
27 | coap.start();
28 | }
29 |
30 | void loop() {
31 | // send GET or PUT coap request to CoAP server.
32 | // for test, use libcoap, microcoap server...etc
33 | // int msgid = coap.put(IPAddress(10, 0, 0, 1), 5683, "light", "1");
34 | Serial.println("Send Request");
35 | int msgid = coap.get(IPAddress(0, 0, 0, 0), 5683, "time");
36 |
37 | delay(1000);
38 | coap.loop();
39 | }
40 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # CoAP client, server library for Spark Photon, Spark Core.
2 | CoAP simple server, client library for Particle Photon, Core.
3 |
4 | ## Source Code
5 | This lightweight library source code are only 2 files. simple-coap.cpp, simple-coap.h.
6 |
7 | ## Example
8 | Some sample sketches for Spark Core and Photon included(firmware/examples/).
9 |
10 | - coaptest.ino : simple request/response sample.
11 | - coapserver.ino : server endpoint url callback sample.
12 |
13 | ## How to use
14 | In this exmples need CoAP server libcoap or microcoap server for check the example program. There is setting the libcoap on Ubuntu Linux. But if there don't use CoAP server(request/reseponse), following setting don't be needed.
15 |
16 | git clone https://github.com/obgm/libcoap
17 | cd libcoap/
18 | ./autogen.sh
19 | ./configure --disable-examples
20 | gcc -o coap-server ./examples/coap-server.c -I./include/coap/ -I. -L.libs -lcoap-1 -DWITH_POSIX
21 | export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:.libs
22 | ./coap-server
23 | # next start Photon or Core, check the request/response.
24 |
25 | ## Arduino compatible
26 | This library is Arduino compatible. That's version is here.
27 |
--------------------------------------------------------------------------------
/examples/coapserver/coapserver.ino:
--------------------------------------------------------------------------------
1 | #include "simple-coap.h"
2 |
3 | // CoAP server endpoint url callback
4 | void callback_light(CoapPacket &packet, IPAddress ip, int port);
5 |
6 | // CoAP client response callback
7 | void callback_response(CoapPacket &packet, IPAddress ip, int port);
8 |
9 | Coap coap;
10 | bool LEDSTATE;
11 |
12 | // CoAP server endpoint URL
13 | void callback_light(CoapPacket &packet, IPAddress ip, int port) {
14 | Serial.println("[Light] ON/OFF");
15 |
16 | // send response
17 | char p[packet.payloadlen + 1];
18 | memcpy(p, packet.payload, packet.payloadlen);
19 | p[packet.payloadlen] = NULL;
20 |
21 | String message(p);
22 |
23 | if (message.equals("0"))
24 | LEDSTATE = false;
25 | else if(message.equals("1"))
26 | LEDSTATE = true;
27 |
28 | if (LEDSTATE) {
29 | coap.sendResponse(ip, port, packet.messageid, "1");
30 | RGB.color(255, 255, 255);
31 | } else {
32 | coap.sendResponse(ip, port, packet.messageid, "0");
33 | RGB.color(0, 0, 0);
34 | }
35 | }
36 |
37 | // CoAP client response callback
38 | void callback_response(CoapPacket &packet, IPAddress ip, int port) {
39 | Serial.println("[Coap Response got]");
40 |
41 | char p[packet.payloadlen + 1];
42 | memcpy(p, packet.payload, packet.payloadlen);
43 | p[packet.payloadlen] = NULL;
44 |
45 | Serial.println(p);
46 | }
47 |
48 | void setup() {
49 | Serial.begin(9600);
50 |
51 | // LED Controll
52 | RGB.control(true);
53 | RGB.color(255, 255, 255);
54 | LEDSTATE = true;
55 |
56 | // add server url endpoints.
57 | // can add multiple endpoint urls.
58 | // exp) coap.server(callback_switch, "switch");
59 | // coap.server(callback_env, "env/temp");
60 | // coap.server(callback_env, "env/humidity");
61 | Serial.println("Setup Callback Light");
62 | coap.server(callback_light, "light");
63 |
64 | // client response callback.
65 | // this endpoint is single callback.
66 | Serial.println("Setup Response Callback");
67 | coap.response(callback_response);
68 |
69 | // start coap server/client
70 | coap.start();
71 | }
72 |
73 | void loop() {
74 | // send GET or PUT coap request to CoAP server.
75 | // for test, use microcoap server...etc
76 | // int msgid = coap.put(IPAddress(10, 0, 0, 1), 5683, "light", "1");
77 | Serial.println("Send Request");
78 | int msgid = coap.get(IPAddress(0, 0, 0, 0), 5683, "time");
79 |
80 | delay(1000);
81 | coap.loop();
82 | }
83 |
84 | /*
85 | if you change LED, req/res test with coap-client(libcoap), run following.
86 | coap-client -m get coap://(photon ip addr)/light
87 | coap-client -e "1" -m put coap://(photon ip addr)/light
88 | coap-client -e "0" -m put coap://(photon ip addr)/light
89 | */
90 |
--------------------------------------------------------------------------------
/src/simple-coap.h:
--------------------------------------------------------------------------------
1 | /*
2 | CoAP library for Core/Photon.
3 | This software is released under the MIT License.
4 | Copyright (c) 2014 Hirotaka Niisato
5 | Permission is hereby granted, free of charge, to any person obtaining
6 | a copy of this software and associated documentation files (the
7 | "Software"), to deal in the Software without restriction, including
8 | without limitation the rights to use, copy, modify, merge, publish,
9 | distribute, sublicense, and/or sell copies of the Software, and to
10 | permit persons to whom the Software is furnished to do so, subject to
11 | the following conditions:
12 |
13 | The above copyright notice and this permission notice shall be
14 | included in all copies or substantial portions of the Software.
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 | */
23 | #ifndef __SIMPLE_COAP_H__
24 | #define __SIMPLE_COAP_H__
25 |
26 | #undef min
27 | #undef max
28 | #include