Skip to content

Commit 55e37d7

Browse files
committed
add coap client example
1 parent 539756e commit 55e37d7

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

Examples/CoAPClient/CoAPClient.ino

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include <Arduino.h>
2+
#include <AltSoftSerial.h>
3+
#include "BC95Udp.h"
4+
#include "CoAP.h"
5+
6+
AltSoftSerial bc95serial;
7+
8+
BC95UDP udp;
9+
Coap coap(udp);
10+
11+
void responseHandler(CoapPacket *packet, IPAddress remoteIP, int remotePort) {
12+
char buff[6];
13+
14+
Serial.print("CoAP Response Code: ");
15+
sprintf(buff, "%d.%02d \n", packet->code >> 5, packet->code & 0b00011111);
16+
Serial.print(buff);
17+
18+
for (int i=0; i< packet->payloadlen; i++) {
19+
Serial.print((char) (packet->payload[i]));
20+
}
21+
}
22+
23+
void setup() {
24+
bc95serial.begin(9600);
25+
BC95.begin(bc95serial);
26+
BC95.reset();
27+
28+
Serial.begin(9600);
29+
Serial.println(F("Starting..."));
30+
31+
while (!BC95.attachNetwork()) {
32+
Serial.println("...");
33+
delay(1000);
34+
}
35+
Serial.println(F("NB-IOT attached.."));
36+
37+
coap.response(responseHandler);
38+
coap.start();
39+
coap.get("coap.me", 5683, "/hello");
40+
}
41+
42+
void loop() {
43+
coap.loop();
44+
}

0 commit comments

Comments
 (0)