This example uses the mbed libraries to transmit UDP packets over Ethernet. #aiot

Dependencies:   EthernetInterface mbed-rtos mbed

Fork of FRDM_K64F-Ethernet by Rangel Alvarado

Files at this revision

API Documentation at this revision

Comitter:
rlinnmoran
Date:
Tue Mar 24 13:22:18 2015 +0000
Parent:
0:bbc9cfdee3bc
Commit message:
UDP Modifications

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Mon Sep 22 02:34:12 2014 +0000
+++ b/main.cpp	Tue Mar 24 13:22:18 2015 +0000
@@ -1,38 +1,50 @@
 #include "mbed.h"
 #include "EthernetInterface.h"
+#include "Endpoint.h" 
  
-#define MBED_DEV_IP       "192.168.0.52"
+#define MBED_DEV_IP       "192.168.1.12"
 #define MBED_DEV_MASK     "255.255.255.0"
-#define MBED_DEV_GW       "0.0.0.0"
+#define MBED_DEV_GW       "192.168.1.1"
 #define ECHO_SERVER_PORT   5000
 
+#define DEST_IP    "192.168.1.1"
+#define DEST_PORT  5000
+
+Serial pc(USBTX, USBRX);    // Serial Port Config
  
 int main (void) {
+    
+    pc.baud(9600);  // Serial Port Config (9600, 8 data, 1 stop), 
+    
+    printf("Starting Ethernet Configuration\n");
+    
+    // Configure the Ethernet Port (see #define above) to assign an IP Address
     EthernetInterface eth;
-    eth.init(MBED_DEV_IP, MBED_DEV_MASK, MBED_DEV_GW); //Assign a device ip, mask and gateway
+    eth.init(MBED_DEV_IP, MBED_DEV_MASK, MBED_DEV_GW); //Assign a device ip, mask and gateway. Static (no DHCP)
     eth.connect();
     printf("IP Address is %s\n", eth.getIPAddress());
     
-    TCPSocketServer server;
-    server.bind(ECHO_SERVER_PORT);
-    server.listen();
-    
-    while (true) {
-        printf("\nWait for new connection...\n");
-        TCPSocketConnection client;
-        server.accept(client);
-        client.set_blocking(false, 1500); // Timeout after (1.5)s
+    // Establish the destination endpoints IP Address / Port
+    printf("Establish destination endpoints IP Address / Port\n");
+    Endpoint dest;
+    dest.set_address(DEST_IP, DEST_PORT);       
+
+    // Configure a UDP Socket
+    printf("Configure UDP Socket\n");
+    UDPSocket sock;
+    sock.init();
+
+    // TX buffer for UDP interface
+    char tx_buffer[] = "#?#?#? Test Data Output #?#?#?";
+       
+    while (true){
         
-        printf("Connection from: %s\n", client.get_address());
-        char buffer[256];
-        while (true) {
-            int n = client.receive(buffer, sizeof(buffer));
-            if (n <= 0) break;
-            
-            client.send_all(buffer, n);
-            if (n <= 0) break;
-        }
-        
-        client.close();
-    }
+        // Transmit tx_buffer 
+        printf("Transmit tx buffer. Data: %s\n", tx_buffer);
+        sock.sendTo(dest, tx_buffer, sizeof(tx_buffer));
+
+        // Wait 1 second        
+        wait(1); 
+          
+    } 
 }
\ No newline at end of file