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.
Revision 2:01b03970605d, committed 2019-07-03
- Comitter:
- JohnnyK
- Date:
- Wed Jul 03 05:41:00 2019 +0000
- Parent:
- 1:9c712ebb90d3
- Child:
- 3:9ab431fc54e2
- Commit message:
- Rework - switch between auto IP or Static IP via definition ROUTER
Changed in this revision
| main.cpp | Show annotated file Show diff for this revision Revisions of this file |
| mbed-os.lib | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Sun May 05 16:17:26 2019 +0000
+++ b/main.cpp Wed Jul 03 05:41:00 2019 +0000
@@ -1,8 +1,16 @@
-#include "mbed.h" //MbedOS 5.12.2
+#include "mbed.h" //MbedOS 5.13
#include "EthernetInterface.h"
-#define ADDRESS "10.0.1.14" //Here place IP of your PC
-#define PORT 20
+#define ADDRESS "192.168.1.10" //Here place IP of your PC. Run cmd.exe and write command ipconfig
+#define LOCALPORT 20
+#define REMOTEPORT 2000
+
+#define ROUTER
+#ifndef ROUTER
+ #define IP "192.168.1.1"
+ #define GATEWAY "0.0.0.0"
+ #define MASK "255.255.255.0"
+#endif
Thread thread;
DigitalOut led1(LED1);
@@ -11,17 +19,27 @@
void udpEcho(){
EthernetInterface eth;
UDPSocket sock;
- SocketAddress addr(0,PORT);
- eth.connect();
+ SocketAddress addr(0,LOCALPORT);
+ int eth_stat;
+#ifndef ROUTER
+ eth.disconnect();
+ eth_stat = eth.set_network(IP,MASK,GATEWAY);
+ printf("set IP status: %i\n",eth_stat);
+#endif
+ eth_stat = eth.connect();
+ printf("connect status: %i\n",eth_stat);
const char *ip = eth.get_ip_address();
+ const char *mac = eth.get_mac_address();
+ printf("MAC address is: %s\n", mac ? mac : "No MAC");
+ printf("IP address is: %s\n", ip ? ip : "No IP");
// Check ip
- if(ip){
- printf("IP address is: %s\n", ip);
+ if(ip){// Check ip
sock.open(ð);
- sock.bind(PORT);
+ sock.bind(LOCALPORT);
+ printf("Listen on local port: %d and send to remote port %d\n", LOCALPORT, REMOTEPORT);
char buffer[] = "Hello World, UDP Echo server";
printf("Sending message '%s' to server (%s)\n",buffer,ip);
- sock.sendto(ADDRESS, PORT, buffer, sizeof(buffer));
+ sock.sendto(ADDRESS, REMOTEPORT, buffer, sizeof(buffer));
printf("Waiting for message...\n");
while(1) {
@@ -34,7 +52,8 @@
n = sprintf(out_buffer,"Echo - %s", in_buffer);
out_buffer[n] = '\0';
printf("Send back: %s\n", out_buffer);
- sock.sendto(addr.get_ip_address(),PORT , out_buffer, sizeof(out_buffer));
+ sock.sendto(addr.get_ip_address(), REMOTEPORT, out_buffer, sizeof(out_buffer));
+ printf("IP address is: %s\n", addr.get_ip_address());
led2 =! led2;
}
/*sock.close();
--- a/mbed-os.lib Sun May 05 16:17:26 2019 +0000 +++ b/mbed-os.lib Wed Jul 03 05:41:00 2019 +0000 @@ -1,1 +1,1 @@ -https://github.com/ARMmbed/mbed-os/#cfa7938a4dd6d1a4c501945b9c26ca68f70e284e +https://github.com/ARMmbed/mbed-os/#54b94ec68c1161b8d0443a068153e93970eebee7