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.
Diff: main.cpp
- Revision:
- 9:264e9d879cf5
- Parent:
- 8:e500ff7ddcaf
--- a/main.cpp Sun Jan 17 20:11:12 2021 +0000
+++ b/main.cpp Sat Apr 24 15:31:36 2021 +0000
@@ -7,15 +7,15 @@
* An other microcontroller with "Round Trip Delay" will be needed.
* ST NUCLEO H743ZI2 used for testing.
*
- * Timo Karppinen 23.11.2020
+ * Timo Karppinen 24.04.2021
***********************************/
#include "mbed.h"
#include "EthernetInterface.h"
-
#define REMOTE_PORT 5001 // The port numbers the other way round
#define LOCAL_PORT 5000 // than in the "Round Trip Delay"
-#define BUFF_SIZE 128 // test with 32, 128, 512, etc
+#define BUFF_SIZE 1024 // 512 can be takes as largest UDP byte number.
+int UDP_SIZE = 128; // the actual size will be taken from incoming message
//Network interface
EthernetInterface net;
@@ -45,8 +45,6 @@
char out_data[BUFF_SIZE];
-//using namespace std::chrono;
-
Timer armedFor;
int main() {
@@ -54,8 +52,8 @@
printf("\nEcho for the Round Trip Delay application (using Ethernet)\r\n");
//Bring up the network interface
- //eth.set_network(IP_Adress,GATEWAY,MASK);
- net.set_network("192.168.1.12","192.168.1.1","255.255.252.0");
+ //eth.set_network(IP_Adress,MASK,GATEWAY);
+ net.set_network("192.168.1.12","255.255.252.0","192.168.1.1");
net.connect();
// Show network address
@@ -75,35 +73,37 @@
//send_thread.start(udpSend);
printf("Sending out \"Echo\" data to port number %d", REMOTE_PORT);
- printf(" will be armed for triggering by pushing Blue button.\n");
- printf("The IP is taken from the incoming message \n");
+ printf("Will be armed for triggering by pushing Blue button.\n");
+ printf("The IP for the \"Delay metering station\" is fixed! \n");
while(1) {
sw2state = sw2.read();
- printf( "\nsw2state is %d\n", sw2state);
+ printf( "\nsw2 %d - ", sw2state);
+ printf("Not listening - Push the blue button!\n");
if((sw2state == 1)&&(sw2state != sw2old)) {
led2.write(1);
armedFor.reset(); // reset timer to zero
armedFor.start();
- snprintf(out_data, BUFF_SIZE, "Echo -server listening for 8 sec" );
+ snprintf(out_data, BUFF_SIZE, "Echo -server listening for 60 sec" );
clientUDP.set_port(REMOTE_PORT);
udpSend();
- snprintf(out_data, BUFF_SIZE, "Echo" );
+ snprintf(out_data, UDP_SIZE, "Echo" ); // Updates size on pushing sw2.
// Start polling for the incoming "Echo" UDP datagram
- while ( armedFor.elapsed_time().count() < 8000000 ){
+ while ( armedFor.elapsed_time().count() < 60000000 ){
+ //snprintf(out_data, UDP_SIZE, "Echo" );
if((newDatagram == 1)&&(newDatagram != newDatagramOld)){
- char firstChar;
- firstChar = in_data[0];
- if (firstChar == 85){ // ASCII symbol 85 = "U" for the incoming
+ //char firstChar;
+ //firstChar = in_data[0];
+ //if (firstChar == 85){ // ASCII symbol 85 = "U" for the incoming
udpSend(); // Sending out first and then printing!
//printf( "firstChar: %s\n", &firstChar);
- }
+ // }
}
newDatagramOld = newDatagram; //Sending the "Echo" once only.
- newDatagram = 0;
+ newDatagram = 0;
}
}
sw2old = sw2state; // Once only with pushing the button as long as you like.
@@ -120,12 +120,13 @@
// The functions
-
void udpReceive()
{
int bytes;
while(1) {
bytes = serverUDP.recvfrom(&clientUDP, &in_data, BUFF_SIZE);
+ //ThisThread::sleep_for(100ms); // For testing only! Comment this line!
+ UDP_SIZE = bytes; // updated for the responce
newDatagram = 1; // set this before using time for printing
ThisThread::sleep_for(100ms);
printf("\n");
@@ -139,11 +140,11 @@
void udpSend()
{
//char out_data[BUFF_SIZE];
-
//snprintf(out_data, BUFF_SIZE, "Echo" );
//clientUDP.set_port(REMOTE_PORT);
- serverUDP.sendto(clientUDP, out_data, sizeof(out_data));
+ //ThisThread::sleep_for(200ms); // For testing only! Comment this line!
+ serverUDP.sendto(clientUDP, out_data, UDP_SIZE);
printf("Sending out: %s\n", out_data);
- printf("with %d" , sizeof(out_data));
+ printf("with %d" , UDP_SIZE);
printf(" data bytes in UDP datagram\n");
}
\ No newline at end of file