Timo Karppinen / Mbed OS UDP_RoundTripEcho_OS6_H743ZI
Committer:
timo_k2
Date:
Sat Apr 24 15:31:36 2021 +0000
Revision:
9:264e9d879cf5
Parent:
8:e500ff7ddcaf
Receives an UDP messages, reads the number of bytes and sends an UDP message back with the same number of bytes.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
timo_k2 0:2a5da7b2278c 1 /*
timo_k2 0:2a5da7b2278c 2 * Copyright (c) 2006-2020 Arm Limited and affiliates.
timo_k2 0:2a5da7b2278c 3 * SPDX-License-Identifier: Apache-2.0
timo_k2 0:2a5da7b2278c 4 ***********************************
timo_k2 3:9615ce1548c4 5 * Round trip delay meter. This is the "echo" module.
timo_k2 0:2a5da7b2278c 6 * A microcontroller board with an Ethernet interface.
timo_k2 3:9615ce1548c4 7 * An other microcontroller with "Round Trip Delay" will be needed.
timo_k2 3:9615ce1548c4 8 * ST NUCLEO H743ZI2 used for testing.
timo_k2 0:2a5da7b2278c 9 *
timo_k2 9:264e9d879cf5 10 * Timo Karppinen 24.04.2021
timo_k2 0:2a5da7b2278c 11 ***********************************/
timo_k2 0:2a5da7b2278c 12 #include "mbed.h"
timo_k2 0:2a5da7b2278c 13 #include "EthernetInterface.h"
timo_k2 3:9615ce1548c4 14
timo_k2 3:9615ce1548c4 15 #define REMOTE_PORT 5001 // The port numbers the other way round
timo_k2 3:9615ce1548c4 16 #define LOCAL_PORT 5000 // than in the "Round Trip Delay"
timo_k2 9:264e9d879cf5 17 #define BUFF_SIZE 1024 // 512 can be takes as largest UDP byte number.
timo_k2 9:264e9d879cf5 18 int UDP_SIZE = 128; // the actual size will be taken from incoming message
timo_k2 0:2a5da7b2278c 19
timo_k2 0:2a5da7b2278c 20 //Network interface
timo_k2 0:2a5da7b2278c 21 EthernetInterface net;
timo_k2 0:2a5da7b2278c 22
timo_k2 0:2a5da7b2278c 23 //Threads
timo_k2 0:2a5da7b2278c 24 Thread recv_thread;
timo_k2 8:e500ff7ddcaf 25 // Thread send_thread;
timo_k2 0:2a5da7b2278c 26
timo_k2 0:2a5da7b2278c 27 // UDP
timo_k2 8:e500ff7ddcaf 28 uint8_t ip[4] = {192, 168, 1, 10}; // The remote IP address
timo_k2 7:59a37ccd06ef 29 SocketAddress clientUDP(ip, NSAPI_IPv4); // The remote device
timo_k2 0:2a5da7b2278c 30 UDPSocket serverUDP; // UDP server in this device
timo_k2 0:2a5da7b2278c 31
timo_k2 0:2a5da7b2278c 32 // Functions
timo_k2 0:2a5da7b2278c 33 void udpReceive( void );
timo_k2 0:2a5da7b2278c 34 void udpSend( void );
timo_k2 0:2a5da7b2278c 35
timo_k2 3:9615ce1548c4 36 DigitalIn sw2(PC_13); // Blue button on H743ZI, button pressed = TRUE
timo_k2 3:9615ce1548c4 37 DigitalOut led2(PE_1); // Yellow LED on H743ZI
timo_k2 7:59a37ccd06ef 38
timo_k2 0:2a5da7b2278c 39 int sw2state = 0;
timo_k2 0:2a5da7b2278c 40 int sw2old = 1;
timo_k2 0:2a5da7b2278c 41
timo_k2 0:2a5da7b2278c 42 char in_data[BUFF_SIZE];
timo_k2 0:2a5da7b2278c 43 int newDatagram = 0;
timo_k2 2:150dadff3c6b 44 int newDatagramOld = 0;
timo_k2 3:9615ce1548c4 45
timo_k2 3:9615ce1548c4 46 char out_data[BUFF_SIZE];
timo_k2 0:2a5da7b2278c 47
timo_k2 2:150dadff3c6b 48 Timer armedFor;
timo_k2 0:2a5da7b2278c 49
timo_k2 0:2a5da7b2278c 50 int main() {
timo_k2 7:59a37ccd06ef 51
timo_k2 3:9615ce1548c4 52 printf("\nEcho for the Round Trip Delay application (using Ethernet)\r\n");
timo_k2 7:59a37ccd06ef 53
timo_k2 0:2a5da7b2278c 54 //Bring up the network interface
timo_k2 9:264e9d879cf5 55 //eth.set_network(IP_Adress,MASK,GATEWAY);
timo_k2 9:264e9d879cf5 56 net.set_network("192.168.1.12","255.255.252.0","192.168.1.1");
timo_k2 0:2a5da7b2278c 57 net.connect();
timo_k2 0:2a5da7b2278c 58
timo_k2 0:2a5da7b2278c 59 // Show network address
timo_k2 0:2a5da7b2278c 60 SocketAddress netAddress;
timo_k2 0:2a5da7b2278c 61 net.get_ip_address(&netAddress);
timo_k2 3:9615ce1548c4 62 printf("\n\n UDPServer IP Address: %s\n", netAddress.get_ip_address() ? netAddress.get_ip_address():"None");
timo_k2 0:2a5da7b2278c 63
timo_k2 0:2a5da7b2278c 64
timo_k2 0:2a5da7b2278c 65 // UDP server
timo_k2 0:2a5da7b2278c 66
timo_k2 0:2a5da7b2278c 67 serverUDP.open(&net);
timo_k2 0:2a5da7b2278c 68 int err = serverUDP.bind(LOCAL_PORT);
timo_k2 0:2a5da7b2278c 69 printf("Port status is: %d\n",err);
timo_k2 0:2a5da7b2278c 70
timo_k2 0:2a5da7b2278c 71 recv_thread.start(udpReceive);
timo_k2 0:2a5da7b2278c 72 printf("Listening has been started at port number %d\n", LOCAL_PORT);
timo_k2 0:2a5da7b2278c 73
timo_k2 8:e500ff7ddcaf 74 //send_thread.start(udpSend);
timo_k2 3:9615ce1548c4 75 printf("Sending out \"Echo\" data to port number %d", REMOTE_PORT);
timo_k2 9:264e9d879cf5 76 printf("Will be armed for triggering by pushing Blue button.\n");
timo_k2 9:264e9d879cf5 77 printf("The IP for the \"Delay metering station\" is fixed! \n");
timo_k2 0:2a5da7b2278c 78
timo_k2 0:2a5da7b2278c 79 while(1) {
timo_k2 0:2a5da7b2278c 80 sw2state = sw2.read();
timo_k2 9:264e9d879cf5 81 printf( "\nsw2 %d - ", sw2state);
timo_k2 9:264e9d879cf5 82 printf("Not listening - Push the blue button!\n");
timo_k2 0:2a5da7b2278c 83
timo_k2 3:9615ce1548c4 84 if((sw2state == 1)&&(sw2state != sw2old)) {
timo_k2 3:9615ce1548c4 85 led2.write(1);
timo_k2 2:150dadff3c6b 86 armedFor.reset(); // reset timer to zero
timo_k2 2:150dadff3c6b 87 armedFor.start();
timo_k2 9:264e9d879cf5 88 snprintf(out_data, BUFF_SIZE, "Echo -server listening for 60 sec" );
timo_k2 3:9615ce1548c4 89 clientUDP.set_port(REMOTE_PORT);
timo_k2 8:e500ff7ddcaf 90 udpSend();
timo_k2 8:e500ff7ddcaf 91
timo_k2 9:264e9d879cf5 92 snprintf(out_data, UDP_SIZE, "Echo" ); // Updates size on pushing sw2.
timo_k2 0:2a5da7b2278c 93
timo_k2 8:e500ff7ddcaf 94 // Start polling for the incoming "Echo" UDP datagram
timo_k2 9:264e9d879cf5 95 while ( armedFor.elapsed_time().count() < 60000000 ){
timo_k2 9:264e9d879cf5 96 //snprintf(out_data, UDP_SIZE, "Echo" );
timo_k2 2:150dadff3c6b 97 if((newDatagram == 1)&&(newDatagram != newDatagramOld)){
timo_k2 9:264e9d879cf5 98 //char firstChar;
timo_k2 9:264e9d879cf5 99 //firstChar = in_data[0];
timo_k2 9:264e9d879cf5 100 //if (firstChar == 85){ // ASCII symbol 85 = "U" for the incoming
timo_k2 3:9615ce1548c4 101 udpSend(); // Sending out first and then printing!
timo_k2 8:e500ff7ddcaf 102 //printf( "firstChar: %s\n", &firstChar);
timo_k2 9:264e9d879cf5 103 // }
timo_k2 0:2a5da7b2278c 104 }
timo_k2 7:59a37ccd06ef 105 newDatagramOld = newDatagram; //Sending the "Echo" once only.
timo_k2 9:264e9d879cf5 106 newDatagram = 0;
timo_k2 0:2a5da7b2278c 107 }
timo_k2 0:2a5da7b2278c 108 }
timo_k2 0:2a5da7b2278c 109 sw2old = sw2state; // Once only with pushing the button as long as you like.
timo_k2 3:9615ce1548c4 110 led2.write(0);
timo_k2 3:9615ce1548c4 111 armedFor.stop(); // Stop the "armed for" if did not receive the request.
timo_k2 8:e500ff7ddcaf 112 for (int k =0; k < BUFF_SIZE; k++){
timo_k2 8:e500ff7ddcaf 113 in_data[k] = 0;
timo_k2 8:e500ff7ddcaf 114 }
timo_k2 3:9615ce1548c4 115
timo_k2 8:e500ff7ddcaf 116 //printf("\nIn sleep mode for one \n");
timo_k2 8:e500ff7ddcaf 117 ThisThread::sleep_for(1000ms);
timo_k2 0:2a5da7b2278c 118 }
timo_k2 0:2a5da7b2278c 119 }
timo_k2 0:2a5da7b2278c 120
timo_k2 0:2a5da7b2278c 121 // The functions
timo_k2 0:2a5da7b2278c 122
timo_k2 0:2a5da7b2278c 123 void udpReceive()
timo_k2 0:2a5da7b2278c 124 {
timo_k2 0:2a5da7b2278c 125 int bytes;
timo_k2 0:2a5da7b2278c 126 while(1) {
timo_k2 0:2a5da7b2278c 127 bytes = serverUDP.recvfrom(&clientUDP, &in_data, BUFF_SIZE);
timo_k2 9:264e9d879cf5 128 //ThisThread::sleep_for(100ms); // For testing only! Comment this line!
timo_k2 9:264e9d879cf5 129 UDP_SIZE = bytes; // updated for the responce
timo_k2 5:fb39c8c13b34 130 newDatagram = 1; // set this before using time for printing
timo_k2 8:e500ff7ddcaf 131 ThisThread::sleep_for(100ms);
timo_k2 0:2a5da7b2278c 132 printf("\n");
timo_k2 0:2a5da7b2278c 133 printf("bytes received: %d\n",bytes);
timo_k2 0:2a5da7b2278c 134 printf("string: %s\n",in_data);
timo_k2 0:2a5da7b2278c 135 printf("client address: %s\n", clientUDP.get_ip_address());
timo_k2 0:2a5da7b2278c 136 printf("\n");
timo_k2 0:2a5da7b2278c 137 }
timo_k2 0:2a5da7b2278c 138 }
timo_k2 0:2a5da7b2278c 139
timo_k2 0:2a5da7b2278c 140 void udpSend()
timo_k2 0:2a5da7b2278c 141 {
timo_k2 3:9615ce1548c4 142 //char out_data[BUFF_SIZE];
timo_k2 3:9615ce1548c4 143 //snprintf(out_data, BUFF_SIZE, "Echo" );
timo_k2 3:9615ce1548c4 144 //clientUDP.set_port(REMOTE_PORT);
timo_k2 9:264e9d879cf5 145 //ThisThread::sleep_for(200ms); // For testing only! Comment this line!
timo_k2 9:264e9d879cf5 146 serverUDP.sendto(clientUDP, out_data, UDP_SIZE);
timo_k2 0:2a5da7b2278c 147 printf("Sending out: %s\n", out_data);
timo_k2 9:264e9d879cf5 148 printf("with %d" , UDP_SIZE);
timo_k2 8:e500ff7ddcaf 149 printf(" data bytes in UDP datagram\n");
timo_k2 8:e500ff7ddcaf 150 }