UDP echo with thread

Committer:
JohnnyK
Date:
Fri May 08 18:33:18 2020 +0000
Revision:
4:2579ef3b025f
Parent:
3:9ab431fc54e2
Child:
5:58fe518111d7
repair

Who changed what in which revision?

UserRevisionLine numberNew contents of line
JohnnyK 3:9ab431fc54e2 1 #include "mbed.h" //MbedOS 5.15
JohnnyK 0:9f56faada164 2 #include "EthernetInterface.h"
JohnnyK 0:9f56faada164 3
JohnnyK 2:01b03970605d 4 #define ROUTER
JohnnyK 2:01b03970605d 5 #ifndef ROUTER
JohnnyK 4:2579ef3b025f 6 #define IP "192.168.1.1" //Here place your Static IP of Mbed, when you want it to connect directly to PC
JohnnyK 3:9ab431fc54e2 7 #define GATEWAY "0.0.0.0"
JohnnyK 3:9ab431fc54e2 8 #define MASK "255.255.255.0"
JohnnyK 2:01b03970605d 9 #endif
JohnnyK 4:2579ef3b025f 10 #define ADDRESS "192.168.1.10" //Here place IP of your PC. Run cmd.exe and write command ipconfig
JohnnyK 3:9ab431fc54e2 11 #define LOCALPORT 20
JohnnyK 3:9ab431fc54e2 12 #define REMOTEPORT 2000
JohnnyK 0:9f56faada164 13
JohnnyK 0:9f56faada164 14 Thread thread;
JohnnyK 0:9f56faada164 15 DigitalOut led1(LED1);
JohnnyK 0:9f56faada164 16 DigitalOut led2(LED2);
JohnnyK 3:9ab431fc54e2 17 SocketAddress targetAddr(ADDRESS,REMOTEPORT);
JohnnyK 0:9f56faada164 18
JohnnyK 0:9f56faada164 19 void udpEcho(){
JohnnyK 0:9f56faada164 20 EthernetInterface eth;
JohnnyK 2:01b03970605d 21 int eth_stat;
JohnnyK 2:01b03970605d 22 #ifndef ROUTER
JohnnyK 2:01b03970605d 23 eth.disconnect();
JohnnyK 4:2579ef3b025f 24 eth_stat = eth.set_network((SocketAddress)IP,(SocketAddress)MASK,(SocketAddress)GATEWAY);
JohnnyK 2:01b03970605d 25 printf("set IP status: %i\n",eth_stat);
JohnnyK 2:01b03970605d 26 #endif
JohnnyK 2:01b03970605d 27 eth_stat = eth.connect();
JohnnyK 2:01b03970605d 28 printf("connect status: %i\n",eth_stat);
JohnnyK 3:9ab431fc54e2 29
JohnnyK 3:9ab431fc54e2 30 SocketAddress ip;
JohnnyK 3:9ab431fc54e2 31 eth.get_ip_address(&ip);
JohnnyK 3:9ab431fc54e2 32 const char *p_ip = ip.get_ip_address();
JohnnyK 3:9ab431fc54e2 33 printf("IP address: %s\n", p_ip ? p_ip : "None");
JohnnyK 3:9ab431fc54e2 34 SocketAddress mask;
JohnnyK 3:9ab431fc54e2 35 eth.get_netmask(&mask);
JohnnyK 3:9ab431fc54e2 36 const char *p_mask = mask.get_ip_address();
JohnnyK 3:9ab431fc54e2 37 printf("Netmask: %s\n", p_mask ? p_mask : "None");
JohnnyK 3:9ab431fc54e2 38 SocketAddress gateway;
JohnnyK 3:9ab431fc54e2 39 eth.get_gateway(&gateway);
JohnnyK 3:9ab431fc54e2 40 const char *p_gateway = gateway.get_ip_address();
JohnnyK 3:9ab431fc54e2 41 printf("Gateway: %s\n", p_gateway ? p_gateway : "None");
JohnnyK 3:9ab431fc54e2 42
JohnnyK 0:9f56faada164 43 // Check ip
JohnnyK 4:2579ef3b025f 44 if(ip){
JohnnyK 3:9ab431fc54e2 45 UDPSocket sock;
JohnnyK 3:9ab431fc54e2 46 SocketAddress addr;
JohnnyK 0:9f56faada164 47 sock.open(&eth);
JohnnyK 2:01b03970605d 48 sock.bind(LOCALPORT);
JohnnyK 4:2579ef3b025f 49
JohnnyK 2:01b03970605d 50 printf("Listen on local port: %d and send to remote port %d\n", LOCALPORT, REMOTEPORT);
JohnnyK 3:9ab431fc54e2 51 char buffer[] = "Hello World, UDP Echo";
JohnnyK 3:9ab431fc54e2 52 printf("Sending message '%s' to (%s)\n",buffer,targetAddr.get_ip_address());
JohnnyK 3:9ab431fc54e2 53 sock.sendto(targetAddr, buffer, sizeof(buffer));
JohnnyK 0:9f56faada164 54 printf("Waiting for message...\n");
JohnnyK 0:9f56faada164 55
JohnnyK 0:9f56faada164 56 while(1) {
JohnnyK 0:9f56faada164 57 char in_buffer[80];
JohnnyK 0:9f56faada164 58 int n = sock.recvfrom(&addr, &in_buffer, sizeof(in_buffer));
JohnnyK 0:9f56faada164 59 in_buffer[n] = '\0';
JohnnyK 0:9f56faada164 60 printf("Recieved: %s\n", in_buffer);
JohnnyK 0:9f56faada164 61 char out_buffer[80];
JohnnyK 0:9f56faada164 62 n = sprintf(out_buffer,"Echo - %s", in_buffer);
JohnnyK 0:9f56faada164 63 out_buffer[n] = '\0';
JohnnyK 0:9f56faada164 64 printf("Send back: %s\n", out_buffer);
JohnnyK 3:9ab431fc54e2 65 sock.sendto(targetAddr, out_buffer, sizeof(out_buffer));
JohnnyK 0:9f56faada164 66 led2 =! led2;
JohnnyK 0:9f56faada164 67 }
JohnnyK 0:9f56faada164 68 }else{
JohnnyK 0:9f56faada164 69 printf("No IP\n");
JohnnyK 0:9f56faada164 70 //eth.disconnect();
JohnnyK 0:9f56faada164 71 printf("Thread end\n");
JohnnyK 0:9f56faada164 72 }
JohnnyK 0:9f56faada164 73 }
JohnnyK 0:9f56faada164 74
JohnnyK 0:9f56faada164 75
JohnnyK 0:9f56faada164 76 int main() {
JohnnyK 1:9c712ebb90d3 77 printf("UDP echo starting...\n");
JohnnyK 0:9f56faada164 78 thread.start(callback(udpEcho));
JohnnyK 3:9ab431fc54e2 79 ThisThread::sleep_for(1000);
JohnnyK 0:9f56faada164 80
JohnnyK 0:9f56faada164 81 while(1) {
JohnnyK 0:9f56faada164 82 led1 =! led1;
JohnnyK 3:9ab431fc54e2 83 ThisThread::sleep_for(1000);
JohnnyK 0:9f56faada164 84 }
JohnnyK 0:9f56faada164 85 }