![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
This example uses the mbed libraries to transmit UDP packets over Ethernet. #aiot
Dependencies: EthernetInterface mbed-rtos mbed
Fork of FRDM_K64F-Ethernet by
main.cpp@1:04392222691c, 2015-03-24 (annotated)
- Committer:
- rlinnmoran
- Date:
- Tue Mar 24 13:22:18 2015 +0000
- Revision:
- 1:04392222691c
- Parent:
- 0:bbc9cfdee3bc
UDP Modifications
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
issaiass | 0:bbc9cfdee3bc | 1 | #include "mbed.h" |
issaiass | 0:bbc9cfdee3bc | 2 | #include "EthernetInterface.h" |
rlinnmoran | 1:04392222691c | 3 | #include "Endpoint.h" |
issaiass | 0:bbc9cfdee3bc | 4 | |
rlinnmoran | 1:04392222691c | 5 | #define MBED_DEV_IP "192.168.1.12" |
issaiass | 0:bbc9cfdee3bc | 6 | #define MBED_DEV_MASK "255.255.255.0" |
rlinnmoran | 1:04392222691c | 7 | #define MBED_DEV_GW "192.168.1.1" |
issaiass | 0:bbc9cfdee3bc | 8 | #define ECHO_SERVER_PORT 5000 |
issaiass | 0:bbc9cfdee3bc | 9 | |
rlinnmoran | 1:04392222691c | 10 | #define DEST_IP "192.168.1.1" |
rlinnmoran | 1:04392222691c | 11 | #define DEST_PORT 5000 |
rlinnmoran | 1:04392222691c | 12 | |
rlinnmoran | 1:04392222691c | 13 | Serial pc(USBTX, USBRX); // Serial Port Config |
issaiass | 0:bbc9cfdee3bc | 14 | |
issaiass | 0:bbc9cfdee3bc | 15 | int main (void) { |
rlinnmoran | 1:04392222691c | 16 | |
rlinnmoran | 1:04392222691c | 17 | pc.baud(9600); // Serial Port Config (9600, 8 data, 1 stop), |
rlinnmoran | 1:04392222691c | 18 | |
rlinnmoran | 1:04392222691c | 19 | printf("Starting Ethernet Configuration\n"); |
rlinnmoran | 1:04392222691c | 20 | |
rlinnmoran | 1:04392222691c | 21 | // Configure the Ethernet Port (see #define above) to assign an IP Address |
issaiass | 0:bbc9cfdee3bc | 22 | EthernetInterface eth; |
rlinnmoran | 1:04392222691c | 23 | eth.init(MBED_DEV_IP, MBED_DEV_MASK, MBED_DEV_GW); //Assign a device ip, mask and gateway. Static (no DHCP) |
issaiass | 0:bbc9cfdee3bc | 24 | eth.connect(); |
issaiass | 0:bbc9cfdee3bc | 25 | printf("IP Address is %s\n", eth.getIPAddress()); |
issaiass | 0:bbc9cfdee3bc | 26 | |
rlinnmoran | 1:04392222691c | 27 | // Establish the destination endpoints IP Address / Port |
rlinnmoran | 1:04392222691c | 28 | printf("Establish destination endpoints IP Address / Port\n"); |
rlinnmoran | 1:04392222691c | 29 | Endpoint dest; |
rlinnmoran | 1:04392222691c | 30 | dest.set_address(DEST_IP, DEST_PORT); |
rlinnmoran | 1:04392222691c | 31 | |
rlinnmoran | 1:04392222691c | 32 | // Configure a UDP Socket |
rlinnmoran | 1:04392222691c | 33 | printf("Configure UDP Socket\n"); |
rlinnmoran | 1:04392222691c | 34 | UDPSocket sock; |
rlinnmoran | 1:04392222691c | 35 | sock.init(); |
rlinnmoran | 1:04392222691c | 36 | |
rlinnmoran | 1:04392222691c | 37 | // TX buffer for UDP interface |
rlinnmoran | 1:04392222691c | 38 | char tx_buffer[] = "#?#?#? Test Data Output #?#?#?"; |
rlinnmoran | 1:04392222691c | 39 | |
rlinnmoran | 1:04392222691c | 40 | while (true){ |
issaiass | 0:bbc9cfdee3bc | 41 | |
rlinnmoran | 1:04392222691c | 42 | // Transmit tx_buffer |
rlinnmoran | 1:04392222691c | 43 | printf("Transmit tx buffer. Data: %s\n", tx_buffer); |
rlinnmoran | 1:04392222691c | 44 | sock.sendTo(dest, tx_buffer, sizeof(tx_buffer)); |
rlinnmoran | 1:04392222691c | 45 | |
rlinnmoran | 1:04392222691c | 46 | // Wait 1 second |
rlinnmoran | 1:04392222691c | 47 | wait(1); |
rlinnmoran | 1:04392222691c | 48 | |
rlinnmoran | 1:04392222691c | 49 | } |
issaiass | 0:bbc9cfdee3bc | 50 | } |