Zoltan Hudak
/
UdpClient_ENC28J60
Simple UDP Client using the UIPEthernet library for ENC28J60 Ethernet boards.
Diff: main.cpp
- Revision:
- 2:c69ad6e71f97
- Parent:
- 1:f6b6e2c173b9
- Child:
- 3:6a6fe4ef95b9
diff -r f6b6e2c173b9 -r c69ad6e71f97 main.cpp --- a/main.cpp Sat Jul 01 08:37:21 2017 +0000 +++ b/main.cpp Tue Sep 03 09:19:55 2019 +0000 @@ -1,37 +1,28 @@ /* - * UIPEthernet UdpClient example. + * UipEthernet UdpClient example. * - * UIPEthernet is a TCP/IP stack that can be used with an enc28j60 based + * UipEthernet is a TCP/IP stack that can be used with an enc28j60 based * Ethernet-shield. * - * UIPEthernet uses the fine uIP stack by Adam Dunkels <adam@sics.se> + * UipEthernet uses the fine uIP stack by Adam Dunkels <adam@sics.se> * * ----------------- * - * This UdpClient example tries to send a packet via udp to 192.168.1.181 - * on port 7 every 5 seconds. After successfully sending the packet it - * waits for up to 5 seconds for a response on the local port that has been - * implicitly opened when sending the packet. - * - * Copyright (C) 2013 by Norbert Truchsess (norbert.truchsess@t-online.de) - * - * Modified by Zoltan Hudak + * Mbed's UDPSocket example + * https://os.mbed.com/docs/mbed-os/v5.13/apis/udpsocket.html#udpsocket-example + * modified by Zoltan Hudak for UIPEthernet * */ #include "mbed.h" -#include "UIPEthernet.h" +#include "UipEthernet.h" // MAC address must be unique within the connected network. Modify as appropriate. -const uint8_t MY_MAC[6] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05 }; -// IP address must be unique and compatible with your network. -const IPAddress MY_IP(192, 168, 1, 182); // this UDP client IP address -const IPAddress SRV_IP(192, 168, 1, 181); // UDP server IP address -const uint16_t PORT = 7; // port used -const char* message = "Hello World from mbed!"; - -Serial pc(USBTX, USBRX); -UIPEthernet uIPEthernet(D11, D12, D13, D10); // mosi, miso, sck, cs -UIPUDP udp; +const uint8_t MAC[6] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05 }; +UipEthernet net(MAC, D11, D12, D13, D10); // mosi, miso, sck, cs +typedef struct +{ + uint32_t secs; // Transmit Time-stamp seconds. +} ntp_packet; /** * @brief @@ -39,73 +30,43 @@ * @param * @retval */ -int main(void) { - const char* message = "Hello World from mbed"; - time_t next = time(NULL); - uIPEthernet.begin(MY_MAC, MY_IP); - - IPAddress localIP = uIPEthernet.localIP(); - pc.printf("Local IP = %s\r\n", localIP.toString()); - - while (1) { - int success; - int len = 0; +int main(void) +{ + // Bring up the ethernet interface + printf("UDP Socket example\n"); + if(net.connect() != 0) { + printf("Error connecting\n"); + return -1; + } - if (time(NULL) > next) { - next = time(NULL) + 5; - - do { - success = udp.beginPacket(SRV_IP, PORT); - pc.printf("beginPacket: "); - if (success) - pc.printf("succeeded.\r\n"); - else - pc.printf("failed.\r\n"); + // Show the network address + const char* ip = net.get_ip_address(); + const char* netmask = net.get_netmask(); + const char* gateway = net.get_gateway(); - //beginPacket fails if remote ethaddr is unknown. In this case an - //arp-request is send out first and beginPacket succeeds as soon - //the arp-response is received. - } while (!success && (time(NULL) < next)); - - if (!success) { - udp.stop(); - next = time(NULL) + 5; - continue; - } + printf("IP address: %s\r\n", ip ? ip : "None"); + printf("Netmask: %s\r\n", netmask ? netmask : "None"); + printf("Gateway: %s\r\n\r\n", gateway ? gateway : "None"); - success = udp.write((uint8_t*)message, strlen(message)); - pc.printf("bytes written: %d\r\n", success); - - success = udp.endPacket(); + UdpSocket socket(&net); + SocketAddress sockAddr; + char out_buffer[] = "time"; - if (success) - pc.printf("endPacket: succeeded%\r\n"); - else - pc.printf("endPacket: failed%\r\n"); - - do { - success = udp.parsePacket(); //check for new udp-packet - } while (!success && (time(NULL) < next)); + if (socket.sendto("time.nist.gov", 37, out_buffer, sizeof(out_buffer)) < 0) { + printf("Error sending data\n"); + return -1; + } - if (!success) { - udp.stop(); - next = time(NULL) + 5; - continue; - } + ntp_packet in_data; + socket.recvfrom(&sockAddr, &in_data, sizeof(ntp_packet)); + in_data.secs = ntohl(in_data.secs) - 2208988800; // 1900-1970 + printf("Time received = %lu seconds since 1/01/1970 00:00 GMT\n", (uint32_t)in_data.secs); + printf("Time = %s", ctime((const time_t*) &in_data.secs)); + printf("Time Server Address: %s\r\n", sockAddr.get_ip_address()); + printf("Time Server Port: %d\n\r", sockAddr.get_port()); - pc.printf("received: '"); - - do { - int c = udp.read(); - pc.putc(c); - len++; - } while (udp.available() > 0); - - pc.printf("', %d bytes\r\n", len); - - //finish reading this packet: - udp.flush(); - next = time(NULL) + 5; - } - } + // Close the socket and bring down the network interface + socket.close(); + net.disconnect(); + return 0; }