|
| 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