![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
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
Ethernet.cpp@3:6085916c9d74, 2015-05-20 (annotated)
- Committer:
- rlinnmoran
- Date:
- Wed May 20 07:14:22 2015 +0000
- Revision:
- 3:6085916c9d74
- Parent:
- 2:bcd60a69583f
First release V1_0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rlinnmoran | 0:77857a36b4ff | 1 | #include "mbed.h" |
rlinnmoran | 0:77857a36b4ff | 2 | #include "EthernetInterface.h" |
rlinnmoran | 0:77857a36b4ff | 3 | #include "Endpoint.h" |
rlinnmoran | 2:bcd60a69583f | 4 | #include "debug.h" |
rlinnmoran | 0:77857a36b4ff | 5 | //#include "main.h" |
rlinnmoran | 0:77857a36b4ff | 6 | |
rlinnmoran | 2:bcd60a69583f | 7 | #define MBED_DEV_IP "192.168.5.12" |
rlinnmoran | 0:77857a36b4ff | 8 | #define MBED_DEV_MASK "255.255.255.0" |
rlinnmoran | 2:bcd60a69583f | 9 | #define MBED_DEV_GW "192.168.5.1" |
rlinnmoran | 0:77857a36b4ff | 10 | #define ECHO_SERVER_PORT 5000 |
rlinnmoran | 0:77857a36b4ff | 11 | |
rlinnmoran | 2:bcd60a69583f | 12 | #define DEST_IP "192.168.5.2" |
rlinnmoran | 2:bcd60a69583f | 13 | #define DEST_PORT 30000 |
rlinnmoran | 0:77857a36b4ff | 14 | |
rlinnmoran | 0:77857a36b4ff | 15 | //Serial pc_a(USBTX, USBRX); // Serial Port Config |
rlinnmoran | 0:77857a36b4ff | 16 | |
rlinnmoran | 2:bcd60a69583f | 17 | int EthernetInit(void); |
rlinnmoran | 2:bcd60a69583f | 18 | int UDPTransmit(char*,int); |
rlinnmoran | 2:bcd60a69583f | 19 | |
rlinnmoran | 2:bcd60a69583f | 20 | Endpoint dest; // Pointer to destination |
rlinnmoran | 2:bcd60a69583f | 21 | EthernetInterface eth; // Configure the Ethernet Port (see #define above) to assign an IP Address |
rlinnmoran | 2:bcd60a69583f | 22 | |
rlinnmoran | 2:bcd60a69583f | 23 | int EthernetInit (void) { |
rlinnmoran | 0:77857a36b4ff | 24 | |
rlinnmoran | 0:77857a36b4ff | 25 | // pc_a.baud(9600); // Serial Port Config (9600, 8 data, 1 stop), |
rlinnmoran | 0:77857a36b4ff | 26 | |
rlinnmoran | 2:bcd60a69583f | 27 | D(printf("Starting Ethernet Configuration\r\n")); |
rlinnmoran | 2:bcd60a69583f | 28 | |
rlinnmoran | 2:bcd60a69583f | 29 | if(eth.init(MBED_DEV_IP, MBED_DEV_MASK, MBED_DEV_GW) != NULL) //Assign a device ip, mask and gateway. Static (no DHCP) |
rlinnmoran | 2:bcd60a69583f | 30 | return -1; // Error initialising |
rlinnmoran | 0:77857a36b4ff | 31 | |
rlinnmoran | 2:bcd60a69583f | 32 | D(printf("Ethernet Interface Initialised\r\n")); |
rlinnmoran | 2:bcd60a69583f | 33 | |
rlinnmoran | 2:bcd60a69583f | 34 | if(eth.connect(1000) != NULL) |
rlinnmoran | 2:bcd60a69583f | 35 | // return -1; // Error connecting //##BUG## returns -1 on successful connection |
rlinnmoran | 2:bcd60a69583f | 36 | |
rlinnmoran | 2:bcd60a69583f | 37 | D(printf("IP Address is %s\r\n", eth.getIPAddress())); |
rlinnmoran | 0:77857a36b4ff | 38 | |
rlinnmoran | 0:77857a36b4ff | 39 | // Establish the destination endpoints IP Address / Port |
rlinnmoran | 2:bcd60a69583f | 40 | D(printf("Establish destination endpoints IP Address / Port\r\n")); |
rlinnmoran | 2:bcd60a69583f | 41 | |
rlinnmoran | 2:bcd60a69583f | 42 | if(dest.set_address(DEST_IP, DEST_PORT) != NULL) |
rlinnmoran | 2:bcd60a69583f | 43 | return -1; // Error establishing address (DHCP only) |
rlinnmoran | 2:bcd60a69583f | 44 | |
rlinnmoran | 2:bcd60a69583f | 45 | return 0; // Success |
rlinnmoran | 2:bcd60a69583f | 46 | |
rlinnmoran | 2:bcd60a69583f | 47 | } |
rlinnmoran | 2:bcd60a69583f | 48 | |
rlinnmoran | 2:bcd60a69583f | 49 | int UDPTransmit (char *tx_buffer, int len) { |
rlinnmoran | 0:77857a36b4ff | 50 | |
rlinnmoran | 0:77857a36b4ff | 51 | // Configure a UDP Socket |
rlinnmoran | 2:bcd60a69583f | 52 | D(printf("Configure UDP Socket\r\n")); |
rlinnmoran | 0:77857a36b4ff | 53 | UDPSocket sock; |
rlinnmoran | 2:bcd60a69583f | 54 | |
rlinnmoran | 2:bcd60a69583f | 55 | if(sock.init() != NULL) |
rlinnmoran | 2:bcd60a69583f | 56 | return -1; // Error initialising socket |
rlinnmoran | 0:77857a36b4ff | 57 | |
rlinnmoran | 2:bcd60a69583f | 58 | D(printf("Socket Initialised\r\n")); |
rlinnmoran | 0:77857a36b4ff | 59 | |
rlinnmoran | 2:bcd60a69583f | 60 | //##### NEED TO FIX LENGTH, how to know size of TX_Buffer????? |
rlinnmoran | 2:bcd60a69583f | 61 | // Transmit tx_buffer |
rlinnmoran | 2:bcd60a69583f | 62 | D(printf("Transmit tx buffer. Data: %s || Length: %d\n\r", tx_buffer, len)); |
rlinnmoran | 2:bcd60a69583f | 63 | |
rlinnmoran | 2:bcd60a69583f | 64 | |
rlinnmoran | 2:bcd60a69583f | 65 | int res=0; |
rlinnmoran | 2:bcd60a69583f | 66 | res=sock.sendTo(dest, tx_buffer, len); |
rlinnmoran | 2:bcd60a69583f | 67 | D(printf("Transmitted %d bytes\r\n",res)); |
rlinnmoran | 2:bcd60a69583f | 68 | |
rlinnmoran | 2:bcd60a69583f | 69 | if(res < NULL) |
rlinnmoran | 2:bcd60a69583f | 70 | return -1; // Error transmitting over UDP |
rlinnmoran | 0:77857a36b4ff | 71 | |
rlinnmoran | 2:bcd60a69583f | 72 | return 0; // Success |
rlinnmoran | 0:77857a36b4ff | 73 | } |