Timo Karppinen / Mbed OS UDP_RoundTripEcho_OS6_H743ZI
Committer:
timo_k2
Date:
Sun Jan 17 20:11:12 2021 +0000
Revision:
8:e500ff7ddcaf
Parent:
7:59a37ccd06ef
Child:
9:264e9d879cf5
With static IP address.

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 3:9615ce1548c4 10 * Timo Karppinen 23.11.2020
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 0:2a5da7b2278c 15
timo_k2 3:9615ce1548c4 16 #define REMOTE_PORT 5001 // The port numbers the other way round
timo_k2 3:9615ce1548c4 17 #define LOCAL_PORT 5000 // than in the "Round Trip Delay"
timo_k2 8:e500ff7ddcaf 18 #define BUFF_SIZE 128 // test with 32, 128, 512, etc
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 1:961861d73841 48 //using namespace std::chrono;
timo_k2 3:9615ce1548c4 49
timo_k2 2:150dadff3c6b 50 Timer armedFor;
timo_k2 0:2a5da7b2278c 51
timo_k2 0:2a5da7b2278c 52 int main() {
timo_k2 7:59a37ccd06ef 53
timo_k2 3:9615ce1548c4 54 printf("\nEcho for the Round Trip Delay application (using Ethernet)\r\n");
timo_k2 7:59a37ccd06ef 55
timo_k2 0:2a5da7b2278c 56 //Bring up the network interface
timo_k2 8:e500ff7ddcaf 57 //eth.set_network(IP_Adress,GATEWAY,MASK);
timo_k2 8:e500ff7ddcaf 58 net.set_network("192.168.1.12","192.168.1.1","255.255.252.0");
timo_k2 0:2a5da7b2278c 59 net.connect();
timo_k2 0:2a5da7b2278c 60
timo_k2 0:2a5da7b2278c 61 // Show network address
timo_k2 0:2a5da7b2278c 62 SocketAddress netAddress;
timo_k2 0:2a5da7b2278c 63 net.get_ip_address(&netAddress);
timo_k2 3:9615ce1548c4 64 printf("\n\n UDPServer IP Address: %s\n", netAddress.get_ip_address() ? netAddress.get_ip_address():"None");
timo_k2 0:2a5da7b2278c 65
timo_k2 0:2a5da7b2278c 66
timo_k2 0:2a5da7b2278c 67 // UDP server
timo_k2 0:2a5da7b2278c 68
timo_k2 0:2a5da7b2278c 69 serverUDP.open(&net);
timo_k2 0:2a5da7b2278c 70 int err = serverUDP.bind(LOCAL_PORT);
timo_k2 0:2a5da7b2278c 71 printf("Port status is: %d\n",err);
timo_k2 0:2a5da7b2278c 72
timo_k2 0:2a5da7b2278c 73 recv_thread.start(udpReceive);
timo_k2 0:2a5da7b2278c 74 printf("Listening has been started at port number %d\n", LOCAL_PORT);
timo_k2 0:2a5da7b2278c 75
timo_k2 8:e500ff7ddcaf 76 //send_thread.start(udpSend);
timo_k2 3:9615ce1548c4 77 printf("Sending out \"Echo\" data to port number %d", REMOTE_PORT);
timo_k2 3:9615ce1548c4 78 printf(" will be armed for triggering by pushing Blue button.\n");
timo_k2 3:9615ce1548c4 79 printf("The IP is taken from the incoming message \n");
timo_k2 0:2a5da7b2278c 80
timo_k2 0:2a5da7b2278c 81 while(1) {
timo_k2 0:2a5da7b2278c 82 sw2state = sw2.read();
timo_k2 5:fb39c8c13b34 83 printf( "\nsw2state is %d\n", sw2state);
timo_k2 0:2a5da7b2278c 84
timo_k2 3:9615ce1548c4 85 if((sw2state == 1)&&(sw2state != sw2old)) {
timo_k2 3:9615ce1548c4 86 led2.write(1);
timo_k2 2:150dadff3c6b 87 armedFor.reset(); // reset timer to zero
timo_k2 2:150dadff3c6b 88 armedFor.start();
timo_k2 8:e500ff7ddcaf 89 snprintf(out_data, BUFF_SIZE, "Echo -server listening for 8 sec" );
timo_k2 3:9615ce1548c4 90 clientUDP.set_port(REMOTE_PORT);
timo_k2 8:e500ff7ddcaf 91 udpSend();
timo_k2 8:e500ff7ddcaf 92
timo_k2 3:9615ce1548c4 93 snprintf(out_data, BUFF_SIZE, "Echo" );
timo_k2 0:2a5da7b2278c 94
timo_k2 8:e500ff7ddcaf 95 // Start polling for the incoming "Echo" UDP datagram
timo_k2 2:150dadff3c6b 96 while ( armedFor.elapsed_time().count() < 8000000 ){
timo_k2 2:150dadff3c6b 97 if((newDatagram == 1)&&(newDatagram != newDatagramOld)){
timo_k2 2:150dadff3c6b 98 char firstChar;
timo_k2 2:150dadff3c6b 99 firstChar = in_data[0];
timo_k2 3:9615ce1548c4 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 8:e500ff7ddcaf 103 }
timo_k2 0:2a5da7b2278c 104 }
timo_k2 7:59a37ccd06ef 105 newDatagramOld = newDatagram; //Sending the "Echo" once only.
timo_k2 8:e500ff7ddcaf 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 3:9615ce1548c4 123
timo_k2 0:2a5da7b2278c 124 void udpReceive()
timo_k2 0:2a5da7b2278c 125 {
timo_k2 0:2a5da7b2278c 126 int bytes;
timo_k2 0:2a5da7b2278c 127 while(1) {
timo_k2 0:2a5da7b2278c 128 bytes = serverUDP.recvfrom(&clientUDP, &in_data, BUFF_SIZE);
timo_k2 5:fb39c8c13b34 129 newDatagram = 1; // set this before using time for printing
timo_k2 8:e500ff7ddcaf 130 ThisThread::sleep_for(100ms);
timo_k2 0:2a5da7b2278c 131 printf("\n");
timo_k2 0:2a5da7b2278c 132 printf("bytes received: %d\n",bytes);
timo_k2 0:2a5da7b2278c 133 printf("string: %s\n",in_data);
timo_k2 0:2a5da7b2278c 134 printf("client address: %s\n", clientUDP.get_ip_address());
timo_k2 0:2a5da7b2278c 135 printf("\n");
timo_k2 0:2a5da7b2278c 136 }
timo_k2 0:2a5da7b2278c 137 }
timo_k2 0:2a5da7b2278c 138
timo_k2 0:2a5da7b2278c 139 void udpSend()
timo_k2 0:2a5da7b2278c 140 {
timo_k2 3:9615ce1548c4 141 //char out_data[BUFF_SIZE];
timo_k2 0:2a5da7b2278c 142
timo_k2 3:9615ce1548c4 143 //snprintf(out_data, BUFF_SIZE, "Echo" );
timo_k2 3:9615ce1548c4 144 //clientUDP.set_port(REMOTE_PORT);
timo_k2 0:2a5da7b2278c 145 serverUDP.sendto(clientUDP, out_data, sizeof(out_data));
timo_k2 0:2a5da7b2278c 146 printf("Sending out: %s\n", out_data);
timo_k2 0:2a5da7b2278c 147 printf("with %d" , sizeof(out_data));
timo_k2 8:e500ff7ddcaf 148 printf(" data bytes in UDP datagram\n");
timo_k2 8:e500ff7ddcaf 149 }