|
| 1 | +/* |
| 2 | + UDP Echo client |
| 3 | +
|
| 4 | + A simple UDP communication demo. |
| 5 | + The program send simple message to 'udp-echo-server-rpl' node, |
| 6 | + the server node send echo on the message received. |
| 7 | + The received echo message shows on serial port. |
| 8 | +
|
| 9 | + Even if RPL is enabled or disabled works. |
| 10 | + Use same settings to compile server side programs. |
| 11 | + This programs behavior is based on contiki os's |
| 12 | + example(examples/udp-ipv6/udp-client.c). |
| 13 | +
|
| 14 | + created 24 Jan 2016 |
| 15 | + modified 29 May 2018 |
| 16 | + by Tokita Hiroshi |
| 17 | +*/ |
| 18 | + |
1 | 19 | #include <MicroIp.h>
|
2 | 20 | #include <MicroIpUdp.h>
|
| 21 | +#include <IPAddress.h> |
3 | 22 |
|
4 |
| -IPAddress server(0xaaaa, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0001); |
5 |
| - |
6 |
| -unsigned int localPort = 7; |
7 |
| -char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; |
| 23 | +const size_t MAX_PAYLOAD_LEN = 40; |
| 24 | +const char* UDP_CONNECTION_ADDR = "contiki-udp-server.local"; |
| 25 | +const unsigned short LOCAL_PORT = 3001; |
| 26 | +const unsigned short DEST_PORT = 3000; |
| 27 | +const int INTERVAL = 15; |
8 | 28 |
|
9 | 29 | MicroIPUDP Udp;
|
| 30 | +IPAddress server; |
| 31 | + |
| 32 | +char packetBuffer[MAX_PAYLOAD_LEN]; |
| 33 | +long lastsend; |
| 34 | +long seq_id; |
10 | 35 |
|
11 | 36 | void setup() {
|
12 |
| - Serial.begin(115200); |
| 37 | + Serial.begin(1000000); |
| 38 | + Serial.println("Start udp-echo-cleint"); |
13 | 39 | MicroIP.begin();
|
14 | 40 |
|
15 |
| - server = MicroIP.lookup("udp-echo-server.local"); |
16 |
| - Udp.begin(localPort); |
| 41 | + server = MicroIP.lookup(UDP_CONNECTION_ADDR); |
| 42 | + |
| 43 | + if (server == IN6ADDR_ANY_INIT) { |
| 44 | + Serial.print("Server ["); |
| 45 | + Serial.print(UDP_CONNECTION_ADDR); |
| 46 | + Serial.println("] is not found."); |
| 47 | + while (true) { |
| 48 | + yield(); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + Serial.print("Server "); |
| 53 | + Serial.print(UDP_CONNECTION_ADDR); |
| 54 | + Serial.print(" is ["); |
| 55 | + Serial.print(server); |
| 56 | + Serial.println("]"); |
| 57 | + |
| 58 | + Udp.begin(LOCAL_PORT); |
| 59 | + Serial.print("Start listen port:"); |
| 60 | + Serial.println(LOCAL_PORT); |
17 | 61 | }
|
18 | 62 |
|
19 | 63 | void loop() {
|
20 | 64 |
|
21 |
| - int avail = Serial.available(); |
22 |
| - if(avail) { |
23 |
| - int len = min(avail, UDP_TX_PACKET_MAX_SIZE); |
| 65 | + long now = millis(); |
24 | 66 |
|
25 |
| - for(int i=0; i<len; i++) { |
26 |
| - packetBuffer[i] = Serial.read(); |
27 |
| - } |
| 67 | + // Periodically send. |
| 68 | + if ((now - lastsend) > (INTERVAL * 1000)) { |
| 69 | + // format message |
| 70 | + memset(packetBuffer, 0, MAX_PAYLOAD_LEN); |
| 71 | + strcpy(packetBuffer, "Hello "); |
| 72 | + itoa(++seq_id, packetBuffer + strlen(packetBuffer), 10); |
| 73 | + strcpy(packetBuffer + strlen(packetBuffer), " from the client"); |
28 | 74 |
|
29 |
| - Udp.beginPacket(server, localPort); |
30 |
| - Udp.write(packetBuffer, len); |
| 75 | + Serial.print("Client sending to "); |
| 76 | + Serial.print(server); |
| 77 | + Serial.print(" (msg: "); |
| 78 | + Serial.print(packetBuffer); |
| 79 | + Serial.println(");"); |
| 80 | + |
| 81 | + // send packet |
| 82 | + Udp.beginPacket(server, DEST_PORT); |
| 83 | + Udp.write(packetBuffer, strlen(packetBuffer)); |
31 | 84 | Udp.endPacket();
|
| 85 | + |
| 86 | + lastsend = now; |
32 | 87 | }
|
33 | 88 |
|
34 |
| - while(int packetSize = Udp.parsePacket()) { |
35 |
| - Serial.print("Received packet of size "); |
36 |
| - Serial.println(packetSize); |
37 |
| - Serial.print("From "); |
38 |
| - IPAddress remote = Udp.remoteIP(); |
39 |
| - Serial.print(remote); |
40 |
| - Serial.print(", port "); |
41 |
| - Serial.println(Udp.remotePort()); |
| 89 | + while (int packetSize = Udp.parsePacket()) { |
| 90 | + /* |
| 91 | + //more info |
| 92 | + Serial.print("Receive from "); |
| 93 | + Serial.print(Udp.remoteIP()); |
| 94 | + Serial.print(":"); |
| 95 | + Serial.print(Udp.remotePort()); |
| 96 | + Serial.print(" size:"); |
| 97 | + Serial.println(packetSize); |
| 98 | + */ |
42 | 99 |
|
43 | 100 | // read the packet into packetBufffer
|
44 |
| - Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE); |
45 |
| - Serial.println("Contents:"); |
46 |
| - for(int i=0; i<packetSize; i++) { |
47 |
| - Serial.print(packetBuffer[i]); |
48 |
| - } |
49 |
| - Serial.println(); |
| 101 | + Udp.read(packetBuffer, packetSize); |
| 102 | + Serial.print("Response from the server: '"); |
| 103 | + Serial.print(packetBuffer); |
| 104 | + Serial.println("'"); |
50 | 105 | }
|
51 | 106 |
|
52 | 107 | }
|
0 commit comments