K64F based data logger for GPS (ublox MAX M8Q) & 6 Axis Sensor (FXOS8700Q) - Outputs to SD + UDP - Uses FRDM K64F + ublox "Cellular and positioning shield" (3G version)

Dependencies:   MAX_M8Q_Capture EthernetInterface FXOS8700Q SDFileSystem eCompass_FPU_Lib mbed-rtos mbed

Revision:
2:bcd60a69583f
Parent:
0:77857a36b4ff
--- a/Ethernet.cpp	Fri Mar 27 08:40:00 2015 +0000
+++ b/Ethernet.cpp	Thu Apr 02 20:09:44 2015 +0000
@@ -1,51 +1,73 @@
 #include "mbed.h"
 #include "EthernetInterface.h"
 #include "Endpoint.h" 
+#include "debug.h"
 //#include "main.h"
  
-#define MBED_DEV_IP       "192.168.1.12"
+#define MBED_DEV_IP       "192.168.5.12"
 #define MBED_DEV_MASK     "255.255.255.0"
-#define MBED_DEV_GW       "192.168.1.1"
+#define MBED_DEV_GW       "192.168.5.1"
 #define ECHO_SERVER_PORT   5000
 
-#define DEST_IP    "192.168.1.1"
-#define DEST_PORT  5000
+#define DEST_IP    "192.168.5.2"
+#define DEST_PORT  30000
 
 //Serial pc_a(USBTX, USBRX);    // Serial Port Config
  
-int EthernetMain (void) {
+int EthernetInit(void);
+int UDPTransmit(char*,int);
+
+Endpoint dest;  // Pointer to destination
+EthernetInterface eth;      // Configure the Ethernet Port (see #define above) to assign an IP Address
+ 
+int EthernetInit (void) {
     
 //    pc_a.baud(9600);  // Serial Port Config (9600, 8 data, 1 stop), 
     
-    printf("Starting Ethernet Configuration\n");
+    D(printf("Starting Ethernet Configuration\r\n"));
+        
+    if(eth.init(MBED_DEV_IP, MBED_DEV_MASK, MBED_DEV_GW) != NULL) //Assign a device ip, mask and gateway. Static (no DHCP)
+        return -1;      // Error initialising
     
-    // 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. Static (no DHCP)
-    eth.connect();
-    printf("IP Address is %s\n", eth.getIPAddress());
+    D(printf("Ethernet Interface Initialised\r\n"));
+        
+    if(eth.connect(1000) != NULL)
+//        return -1;      // Error connecting //##BUG## returns -1 on successful connection
+   
+    D(printf("IP Address is %s\r\n", eth.getIPAddress()));
     
     // Establish the destination endpoints IP Address / Port
-    printf("Establish destination endpoints IP Address / Port\n");
-    Endpoint dest;
-    dest.set_address(DEST_IP, DEST_PORT);       
+    D(printf("Establish destination endpoints IP Address / Port\r\n"));
+
+    if(dest.set_address(DEST_IP, DEST_PORT) != NULL)
+        return -1;      // Error establishing address (DHCP only)
+        
+    return 0;       // Success
+        
+}
+
+int UDPTransmit (char *tx_buffer, int len) {
 
     // Configure a UDP Socket
-    printf("Configure UDP Socket\n");
+    D(printf("Configure UDP Socket\r\n"));
     UDPSocket sock;
-    sock.init();
-
-    // TX buffer for UDP interface
-    char tx_buffer[] = "#?#?#? Test Data Output #?#?#?";
-       
-    while (true){
+    
+    if(sock.init() != NULL)
+        return -1;      // Error initialising socket
         
-        // Transmit tx_buffer 
-        printf("Transmit tx buffer. Data: %s\n", tx_buffer);
-        sock.sendTo(dest, tx_buffer, sizeof(tx_buffer));
+    D(printf("Socket Initialised\r\n"));
 
-        // Wait 1 second        
-        wait(1); 
+    //##### NEED TO FIX LENGTH, how to know size of TX_Buffer?????
+    // Transmit tx_buffer 
+    D(printf("Transmit tx buffer. Data: %s || Length: %d\n\r", tx_buffer, len));
+    
+    
+    int res=0;    
+    res=sock.sendTo(dest, tx_buffer, len);
+    D(printf("Transmitted %d bytes\r\n",res));
+    
+    if(res < NULL)
+        return -1;      // Error transmitting over UDP
           
-    } 
+    return 0;      // Success
 }
\ No newline at end of file