Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp@3:9615ce1548c4, 2020-11-23 (annotated)
- Committer:
- timo_k2
- Date:
- Mon Nov 23 12:56:05 2020 +0000
- Revision:
- 3:9615ce1548c4
- Parent:
- 2:150dadff3c6b
- Child:
- 6:11edc8f9d870
Initial commit. Completed testing with the UDP_RoundTripDelay.
Who changed what in which revision?
User | Revision | Line number | New 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 | 0:2a5da7b2278c | 18 | #define BUFF_SIZE 128 |
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 | 0:2a5da7b2278c | 25 | Thread send_thread; |
timo_k2 | 0:2a5da7b2278c | 26 | |
timo_k2 | 0:2a5da7b2278c | 27 | // UDP |
timo_k2 | 3:9615ce1548c4 | 28 | uint8_t ip[4] = {192, 168, 1, 104}; |
timo_k2 | 3:9615ce1548c4 | 29 | SocketAddress clientUDP(ip, NSAPI_IPv4); |
timo_k2 | 3:9615ce1548c4 | 30 | //SocketAddress clientUDP(strIP); // Client on remote device |
timo_k2 | 3:9615ce1548c4 | 31 | //SocketAddress clientUDP; |
timo_k2 | 0:2a5da7b2278c | 32 | UDPSocket serverUDP; // UDP server in this device |
timo_k2 | 0:2a5da7b2278c | 33 | |
timo_k2 | 0:2a5da7b2278c | 34 | |
timo_k2 | 0:2a5da7b2278c | 35 | |
timo_k2 | 0:2a5da7b2278c | 36 | // Functions |
timo_k2 | 0:2a5da7b2278c | 37 | void udpReceive( void ); |
timo_k2 | 0:2a5da7b2278c | 38 | void udpSend( void ); |
timo_k2 | 0:2a5da7b2278c | 39 | |
timo_k2 | 3:9615ce1548c4 | 40 | DigitalIn sw2(PC_13); // Blue button on H743ZI, button pressed = TRUE |
timo_k2 | 3:9615ce1548c4 | 41 | DigitalOut led2(PE_1); // Yellow LED on H743ZI |
timo_k2 | 0:2a5da7b2278c | 42 | int sw2state = 0; |
timo_k2 | 0:2a5da7b2278c | 43 | int sw2old = 1; |
timo_k2 | 0:2a5da7b2278c | 44 | |
timo_k2 | 0:2a5da7b2278c | 45 | char in_data[BUFF_SIZE]; |
timo_k2 | 0:2a5da7b2278c | 46 | int newDatagram = 0; |
timo_k2 | 2:150dadff3c6b | 47 | int newDatagramOld = 0; |
timo_k2 | 3:9615ce1548c4 | 48 | |
timo_k2 | 3:9615ce1548c4 | 49 | char out_data[BUFF_SIZE]; |
timo_k2 | 0:2a5da7b2278c | 50 | |
timo_k2 | 1:961861d73841 | 51 | //using namespace std::chrono; |
timo_k2 | 3:9615ce1548c4 | 52 | |
timo_k2 | 2:150dadff3c6b | 53 | Timer armedFor; |
timo_k2 | 0:2a5da7b2278c | 54 | |
timo_k2 | 0:2a5da7b2278c | 55 | int main() { |
timo_k2 | 3:9615ce1548c4 | 56 | printf("\nEcho for the Round Trip Delay application (using Ethernet)\r\n"); |
timo_k2 | 0:2a5da7b2278c | 57 | |
timo_k2 | 0:2a5da7b2278c | 58 | //Bring up the network interface |
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 | 3:9615ce1548c4 | 76 | |
timo_k2 | 3:9615ce1548c4 | 77 | |
timo_k2 | 3:9615ce1548c4 | 78 | |
timo_k2 | 3:9615ce1548c4 | 79 | |
timo_k2 | 0:2a5da7b2278c | 80 | send_thread.start(udpSend); |
timo_k2 | 3:9615ce1548c4 | 81 | printf("Sending out \"Echo\" data to port number %d", REMOTE_PORT); |
timo_k2 | 3:9615ce1548c4 | 82 | printf(" will be armed for triggering by pushing Blue button.\n"); |
timo_k2 | 3:9615ce1548c4 | 83 | printf("The IP is taken from the incoming message \n"); |
timo_k2 | 0:2a5da7b2278c | 84 | |
timo_k2 | 0:2a5da7b2278c | 85 | |
timo_k2 | 0:2a5da7b2278c | 86 | while(1) { |
timo_k2 | 0:2a5da7b2278c | 87 | sw2state = sw2.read(); |
timo_k2 | 0:2a5da7b2278c | 88 | printf( "sw2state is %d\n", sw2state); |
timo_k2 | 0:2a5da7b2278c | 89 | |
timo_k2 | 3:9615ce1548c4 | 90 | if((sw2state == 1)&&(sw2state != sw2old)) { |
timo_k2 | 3:9615ce1548c4 | 91 | led2.write(1); |
timo_k2 | 2:150dadff3c6b | 92 | armedFor.reset(); // reset timer to zero |
timo_k2 | 2:150dadff3c6b | 93 | armedFor.start(); |
timo_k2 | 3:9615ce1548c4 | 94 | snprintf(out_data, BUFF_SIZE, "Echo -server listening" ); |
timo_k2 | 3:9615ce1548c4 | 95 | clientUDP.set_port(REMOTE_PORT); |
timo_k2 | 3:9615ce1548c4 | 96 | udpSend(); |
timo_k2 | 3:9615ce1548c4 | 97 | snprintf(out_data, BUFF_SIZE, "Echo" ); |
timo_k2 | 0:2a5da7b2278c | 98 | |
timo_k2 | 2:150dadff3c6b | 99 | // Start polling for the incomening "Echo" UDP datagram |
timo_k2 | 2:150dadff3c6b | 100 | while ( armedFor.elapsed_time().count() < 8000000 ){ |
timo_k2 | 2:150dadff3c6b | 101 | if((newDatagram == 1)&&(newDatagram != newDatagramOld)){ |
timo_k2 | 2:150dadff3c6b | 102 | char firstChar; |
timo_k2 | 2:150dadff3c6b | 103 | firstChar = in_data[0]; |
timo_k2 | 3:9615ce1548c4 | 104 | if (firstChar == 85){ // ASCII symbol 85 = "U" for the incoming |
timo_k2 | 3:9615ce1548c4 | 105 | udpSend(); // Sending out first and then printing! |
timo_k2 | 2:150dadff3c6b | 106 | printf( "firstChar: %s\n", &firstChar); |
timo_k2 | 2:150dadff3c6b | 107 | } |
timo_k2 | 2:150dadff3c6b | 108 | for (int k =0; k < BUFF_SIZE; k++){ |
timo_k2 | 2:150dadff3c6b | 109 | in_data[k] = 0; |
timo_k2 | 2:150dadff3c6b | 110 | } |
timo_k2 | 0:2a5da7b2278c | 111 | } |
timo_k2 | 3:9615ce1548c4 | 112 | newDatagramOld = newDatagram; //Sending the "Echo" once only. |
timo_k2 | 2:150dadff3c6b | 113 | |
timo_k2 | 0:2a5da7b2278c | 114 | } |
timo_k2 | 2:150dadff3c6b | 115 | |
timo_k2 | 0:2a5da7b2278c | 116 | } |
timo_k2 | 2:150dadff3c6b | 117 | newDatagram = 0; |
timo_k2 | 0:2a5da7b2278c | 118 | sw2old = sw2state; // Once only with pushing the button as long as you like. |
timo_k2 | 3:9615ce1548c4 | 119 | led2.write(0); |
timo_k2 | 3:9615ce1548c4 | 120 | armedFor.stop(); // Stop the "armed for" if did not receive the request. |
timo_k2 | 3:9615ce1548c4 | 121 | |
timo_k2 | 0:2a5da7b2278c | 122 | printf("\nWe stopped sending more UDP packets to the server.\nYou can unplug your device!\n"); |
timo_k2 | 2:150dadff3c6b | 123 | ThisThread::sleep_for(4000ms); |
timo_k2 | 0:2a5da7b2278c | 124 | } |
timo_k2 | 0:2a5da7b2278c | 125 | } |
timo_k2 | 0:2a5da7b2278c | 126 | |
timo_k2 | 0:2a5da7b2278c | 127 | // The functions |
timo_k2 | 0:2a5da7b2278c | 128 | |
timo_k2 | 3:9615ce1548c4 | 129 | |
timo_k2 | 0:2a5da7b2278c | 130 | |
timo_k2 | 0:2a5da7b2278c | 131 | void udpReceive() |
timo_k2 | 0:2a5da7b2278c | 132 | { |
timo_k2 | 0:2a5da7b2278c | 133 | int bytes; |
timo_k2 | 0:2a5da7b2278c | 134 | while(1) { |
timo_k2 | 0:2a5da7b2278c | 135 | bytes = serverUDP.recvfrom(&clientUDP, &in_data, BUFF_SIZE); |
timo_k2 | 3:9615ce1548c4 | 136 | newDatagram = 1; // set this before using time for printing |
timo_k2 | 0:2a5da7b2278c | 137 | printf("\n"); |
timo_k2 | 0:2a5da7b2278c | 138 | printf("bytes received: %d\n",bytes); |
timo_k2 | 0:2a5da7b2278c | 139 | printf("string: %s\n",in_data); |
timo_k2 | 0:2a5da7b2278c | 140 | printf("client address: %s\n", clientUDP.get_ip_address()); |
timo_k2 | 0:2a5da7b2278c | 141 | printf("\n"); |
timo_k2 | 0:2a5da7b2278c | 142 | } |
timo_k2 | 0:2a5da7b2278c | 143 | } |
timo_k2 | 0:2a5da7b2278c | 144 | |
timo_k2 | 0:2a5da7b2278c | 145 | void udpSend() |
timo_k2 | 0:2a5da7b2278c | 146 | { |
timo_k2 | 3:9615ce1548c4 | 147 | //char out_data[BUFF_SIZE]; |
timo_k2 | 0:2a5da7b2278c | 148 | |
timo_k2 | 3:9615ce1548c4 | 149 | //snprintf(out_data, BUFF_SIZE, "Echo" ); |
timo_k2 | 3:9615ce1548c4 | 150 | //clientUDP.set_port(REMOTE_PORT); |
timo_k2 | 0:2a5da7b2278c | 151 | serverUDP.sendto(clientUDP, out_data, sizeof(out_data)); |
timo_k2 | 0:2a5da7b2278c | 152 | printf("Sending out: %s\n", out_data); |
timo_k2 | 0:2a5da7b2278c | 153 | printf("with %d" , sizeof(out_data)); |
timo_k2 | 0:2a5da7b2278c | 154 | printf(" data bytes in UDP datagram\n"); |
timo_k2 | 0:2a5da7b2278c | 155 | |
timo_k2 | 0:2a5da7b2278c | 156 | } |