Skip to content

Commit 12cd3be

Browse files
committed
add UDPClient example
1 parent e6344f4 commit 12cd3be

File tree

2 files changed

+73
-3
lines changed

2 files changed

+73
-3
lines changed

BC95.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ E-mail: [email protected]
66
This software is released under the MIT License.
77
*/
88

9-
109
#include "BC95.h"
1110

1211
char buffer[BC95_MAX_BUFFER_SIZE];
@@ -61,7 +60,7 @@ char* BC95Class::getSerialResponse(char *prefix, uint32_t timeout) {
6160
socketpool[sno].bc95_msglen = plen;
6261

6362
// if +NSONMI is unexpectly found, read serial again
64-
if (readUntilDone(buffer, timeout, BC95_MAX_BUFFER_SIZE) >= 0) { // 0
63+
if (readUntilDone(buffer, timeout, BC95_MAX_BUFFER_SIZE) >= 0) {
6564
return parseResponse(buffer, prefix);
6665
}
6766
else {
@@ -89,7 +88,7 @@ char* BC95Class::parseResponse(char *rawstr, char* pref) {
8988
if (pref==NULL || *pref=='\0') {
9089
return rawstr;
9190
}
92-
p = strstr((const char *)rawstr, (const char *)pref);
91+
p = strstr((const char *)rawstr, (const char *)pref);
9392
if (p != NULL) {
9493
p += strlen(pref);
9594
// skip STOPPER[0]

Examples/UDPClient/UDPClient.ino

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#include <Arduino.h>
2+
#include <AltSoftSerial.h>
3+
#include "BC95Udp.h"
4+
5+
AltSoftSerial bc95serial;
6+
7+
// 8.8.8.8 is the Google's public DNS server.
8+
#define SERVER_IP IPAddress(8, 8, 8, 8)
9+
#define SERVER_PORT 53
10+
11+
// This binary string represents a UDP paylaod of the DNS query for the domain name nexpie.com
12+
uint8_t udpdata[] = "\xC0\x5B\x01\x00\x00\x01\x00\x00\x00\x00\x00\x00\x06\x6E\x65\x78\x70\x69\x65\x03\x63\x6F\x6D\x00\x00\x01\x00\x01";
13+
14+
15+
BC95UDP udpclient;
16+
uint8_t buff[64];
17+
18+
void printHEX(uint8_t *buff, size_t len) {
19+
for (int i=0; i<len; i++) {
20+
if (buff[i]<16) Serial.print(" 0");
21+
else Serial.print(" ");
22+
Serial.print(buff[i], HEX);
23+
}
24+
}
25+
26+
void setup() {
27+
bc95serial.begin(9600);
28+
BC95.begin(bc95serial);
29+
BC95.reset();
30+
31+
Serial.begin(9600);
32+
Serial.println(F("Starting..."));
33+
34+
while (!BC95.attachNetwork()) {
35+
Serial.println("...");
36+
delay(1000);
37+
}
38+
Serial.println(F("NB-IOT attached.."));
39+
40+
Serial.print(F("NB-IOT module IP address : "));
41+
Serial.println(BC95.getIPAddress());
42+
43+
udpclient.begin(8053);
44+
udpclient.beginPacket(SERVER_IP, SERVER_PORT);
45+
46+
Serial.println("\n\nSending UDP payload : ");
47+
Serial.println(F(" n e x p i e c o m A IN"));
48+
printHEX(udpdata, 28);
49+
50+
udpclient.write(udpdata, 28);
51+
udpclient.endPacket();
52+
53+
while (udpclient.parsePacket() == 0) {
54+
delay(500);
55+
}
56+
57+
size_t len = udpclient.read(buff, 64);
58+
59+
Serial.println(F("\n\nReceive UDP payload : "));
60+
printHEX(buff, len);
61+
62+
Serial.print(F("\n\nThe last 4 bytes encodes IP address of the requested domain : "));
63+
64+
IPAddress hostip = IPAddress(buff[len-4], buff[len-3], buff[len-2], buff[len-1]);
65+
Serial.print(hostip);
66+
}
67+
68+
void loop() {
69+
70+
}
71+

0 commit comments

Comments
 (0)