TCP Echo example of W5500 Ethernet Kit for IoT.
Dependencies: WIZnet_Library mbed W5500HelloWorld
main.cpp@1:4f6bcf445501, 2015-07-16 (annotated)
- Committer:
- bangbh
- Date:
- Thu Jul 16 07:21:46 2015 +0000
- Revision:
- 1:4f6bcf445501
- Parent:
- 0:412f9c1172b7
"mbed_mac_address()" does not work on W5500 Ethernet kit.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bangbh | 0:412f9c1172b7 | 1 | #include "mbed.h" |
bangbh | 0:412f9c1172b7 | 2 | #include "WIZnetInterface.h" |
bangbh | 0:412f9c1172b7 | 3 | |
bangbh | 0:412f9c1172b7 | 4 | #define USE_DHCP 1 |
bangbh | 0:412f9c1172b7 | 5 | |
bangbh | 0:412f9c1172b7 | 6 | #define LOOPBACKPORT 5000 |
bangbh | 0:412f9c1172b7 | 7 | |
bangbh | 0:412f9c1172b7 | 8 | const char * IP_Addr = "192.168.11.194"; |
bangbh | 0:412f9c1172b7 | 9 | const char * IP_Subnet = "255.255.255.0"; |
bangbh | 0:412f9c1172b7 | 10 | const char * IP_Gateway = "192.168.11.1"; |
bangbh | 0:412f9c1172b7 | 11 | unsigned char MAC_Addr[6] = {0x00,0x08,0xDC,0x12,0x34,0x56}; |
bangbh | 0:412f9c1172b7 | 12 | |
bangbh | 0:412f9c1172b7 | 13 | DigitalOut myled1(LED1); |
bangbh | 0:412f9c1172b7 | 14 | Serial pc(USBTX, USBRX); |
bangbh | 0:412f9c1172b7 | 15 | |
bangbh | 0:412f9c1172b7 | 16 | #ifdef TARGET_LPC11U68 |
bangbh | 0:412f9c1172b7 | 17 | SPI spi(P0_9,P0_8,P1_29); |
bangbh | 0:412f9c1172b7 | 18 | WIZnetInterface ethernet(&spi,P0_2,P1_13); |
bangbh | 0:412f9c1172b7 | 19 | #endif |
bangbh | 0:412f9c1172b7 | 20 | |
bangbh | 0:412f9c1172b7 | 21 | int main() { |
bangbh | 0:412f9c1172b7 | 22 | |
bangbh | 0:412f9c1172b7 | 23 | //Set serial port baudrate speed: 115200 |
bangbh | 0:412f9c1172b7 | 24 | pc.baud(115200); |
bangbh | 0:412f9c1172b7 | 25 | |
bangbh | 0:412f9c1172b7 | 26 | pc.printf("Start\r\n"); |
bangbh | 0:412f9c1172b7 | 27 | |
bangbh | 0:412f9c1172b7 | 28 | char buffer[256]; |
bangbh | 0:412f9c1172b7 | 29 | |
bangbh | 0:412f9c1172b7 | 30 | while(1) |
bangbh | 0:412f9c1172b7 | 31 | { |
bangbh | 0:412f9c1172b7 | 32 | #if USE_DHCP |
bangbh | 0:412f9c1172b7 | 33 | int ret = ethernet.init(MAC_Addr); |
bangbh | 0:412f9c1172b7 | 34 | #else |
bangbh | 0:412f9c1172b7 | 35 | int ret = ethernet.init(MAC_Addr,IP_Addr,IP_Subnet,IP_Gateway); |
bangbh | 0:412f9c1172b7 | 36 | #endif |
bangbh | 0:412f9c1172b7 | 37 | |
bangbh | 0:412f9c1172b7 | 38 | if (!ret) { |
bangbh | 0:412f9c1172b7 | 39 | pc.printf("Initialized, MAC: %s\r\n", ethernet.getMACAddress()); |
bangbh | 0:412f9c1172b7 | 40 | ret = ethernet.connect(); |
bangbh | 0:412f9c1172b7 | 41 | if (!ret) { |
bangbh | 0:412f9c1172b7 | 42 | pc.printf("IP: %s, MASK: %s, GW: %s\r\n", |
bangbh | 0:412f9c1172b7 | 43 | ethernet.getIPAddress(), ethernet.getNetworkMask(), ethernet.getGateway()); |
bangbh | 0:412f9c1172b7 | 44 | } else { |
bangbh | 0:412f9c1172b7 | 45 | pc.printf("Error ethernet.connect() - ret = %d\r\n", ret); |
bangbh | 0:412f9c1172b7 | 46 | exit(0); |
bangbh | 0:412f9c1172b7 | 47 | } |
bangbh | 0:412f9c1172b7 | 48 | } else { |
bangbh | 0:412f9c1172b7 | 49 | pc.printf("Error ethernet.init() - ret = %d\r\n", ret); |
bangbh | 0:412f9c1172b7 | 50 | exit(0); |
bangbh | 0:412f9c1172b7 | 51 | } |
bangbh | 0:412f9c1172b7 | 52 | |
bangbh | 0:412f9c1172b7 | 53 | TCPSocketServer server; |
bangbh | 0:412f9c1172b7 | 54 | server.bind(LOOPBACKPORT); |
bangbh | 0:412f9c1172b7 | 55 | server.listen(); |
bangbh | 0:412f9c1172b7 | 56 | |
bangbh | 0:412f9c1172b7 | 57 | while (1) { |
bangbh | 0:412f9c1172b7 | 58 | pc.printf("\nWait for new connection...\r\n"); |
bangbh | 0:412f9c1172b7 | 59 | TCPSocketConnection client; |
bangbh | 0:412f9c1172b7 | 60 | server.accept(client); |
bangbh | 0:412f9c1172b7 | 61 | client.set_blocking(false, 0); // Timeout=0. |
bangbh | 0:412f9c1172b7 | 62 | pc.printf("Connection from: %s\r\n", client.get_address()); |
bangbh | 0:412f9c1172b7 | 63 | while (client.is_connected() == true) { |
bangbh | 0:412f9c1172b7 | 64 | int n = client.receive(buffer, sizeof(buffer)); |
bangbh | 0:412f9c1172b7 | 65 | if(n > 0) |
bangbh | 0:412f9c1172b7 | 66 | client.send_all(buffer, n); |
bangbh | 0:412f9c1172b7 | 67 | if(client.is_fin_received()) |
bangbh | 0:412f9c1172b7 | 68 | client.close(); |
bangbh | 0:412f9c1172b7 | 69 | } |
bangbh | 0:412f9c1172b7 | 70 | pc.printf("Disconnected.\r\n"); |
bangbh | 0:412f9c1172b7 | 71 | } |
bangbh | 0:412f9c1172b7 | 72 | } |
bangbh | 0:412f9c1172b7 | 73 | } |