Simple UDP Client using the UIPEthernet library for ENC28J60 Ethernet boards.

Dependencies:   UIPEthernet

Committer:
hudakz
Date:
Sat Sep 07 18:12:05 2019 +0000
Revision:
3:6a6fe4ef95b9
Parent:
2:c69ad6e71f97
Updated.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hudakz 0:47d7b7b2bba3 1 /*
hudakz 2:c69ad6e71f97 2 * UipEthernet UdpClient example.
hudakz 0:47d7b7b2bba3 3 *
hudakz 2:c69ad6e71f97 4 * UipEthernet is a TCP/IP stack that can be used with an enc28j60 based
hudakz 0:47d7b7b2bba3 5 * Ethernet-shield.
hudakz 0:47d7b7b2bba3 6 *
hudakz 2:c69ad6e71f97 7 * UipEthernet uses the fine uIP stack by Adam Dunkels <adam@sics.se>
hudakz 0:47d7b7b2bba3 8 *
hudakz 0:47d7b7b2bba3 9 * -----------------
hudakz 0:47d7b7b2bba3 10 *
hudakz 2:c69ad6e71f97 11 * Mbed's UDPSocket example
hudakz 2:c69ad6e71f97 12 * https://os.mbed.com/docs/mbed-os/v5.13/apis/udpsocket.html#udpsocket-example
hudakz 2:c69ad6e71f97 13 * modified by Zoltan Hudak for UIPEthernet
hudakz 0:47d7b7b2bba3 14 *
hudakz 0:47d7b7b2bba3 15 */
hudakz 0:47d7b7b2bba3 16 #include "mbed.h"
hudakz 2:c69ad6e71f97 17 #include "UipEthernet.h"
hudakz 3:6a6fe4ef95b9 18
hudakz 0:47d7b7b2bba3 19 // MAC address must be unique within the connected network. Modify as appropriate.
hudakz 2:c69ad6e71f97 20 const uint8_t MAC[6] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05 };
hudakz 2:c69ad6e71f97 21 UipEthernet net(MAC, D11, D12, D13, D10); // mosi, miso, sck, cs
hudakz 2:c69ad6e71f97 22 typedef struct
hudakz 2:c69ad6e71f97 23 {
hudakz 2:c69ad6e71f97 24 uint32_t secs; // Transmit Time-stamp seconds.
hudakz 2:c69ad6e71f97 25 } ntp_packet;
hudakz 3:6a6fe4ef95b9 26
hudakz 0:47d7b7b2bba3 27 /**
hudakz 0:47d7b7b2bba3 28 * @brief
hudakz 0:47d7b7b2bba3 29 * @note
hudakz 0:47d7b7b2bba3 30 * @param
hudakz 0:47d7b7b2bba3 31 * @retval
hudakz 0:47d7b7b2bba3 32 */
hudakz 2:c69ad6e71f97 33 int main(void)
hudakz 2:c69ad6e71f97 34 {
hudakz 2:c69ad6e71f97 35 // Bring up the ethernet interface
hudakz 2:c69ad6e71f97 36 printf("UDP Socket example\n");
hudakz 2:c69ad6e71f97 37 if(net.connect() != 0) {
hudakz 2:c69ad6e71f97 38 printf("Error connecting\n");
hudakz 2:c69ad6e71f97 39 return -1;
hudakz 2:c69ad6e71f97 40 }
hudakz 3:6a6fe4ef95b9 41
hudakz 2:c69ad6e71f97 42 // Show the network address
hudakz 2:c69ad6e71f97 43 const char* ip = net.get_ip_address();
hudakz 2:c69ad6e71f97 44 const char* netmask = net.get_netmask();
hudakz 2:c69ad6e71f97 45 const char* gateway = net.get_gateway();
hudakz 3:6a6fe4ef95b9 46
hudakz 2:c69ad6e71f97 47 printf("IP address: %s\r\n", ip ? ip : "None");
hudakz 2:c69ad6e71f97 48 printf("Netmask: %s\r\n", netmask ? netmask : "None");
hudakz 2:c69ad6e71f97 49 printf("Gateway: %s\r\n\r\n", gateway ? gateway : "None");
hudakz 3:6a6fe4ef95b9 50
hudakz 2:c69ad6e71f97 51 UdpSocket socket(&net);
hudakz 2:c69ad6e71f97 52 SocketAddress sockAddr;
hudakz 2:c69ad6e71f97 53 char out_buffer[] = "time";
hudakz 3:6a6fe4ef95b9 54
hudakz 2:c69ad6e71f97 55 if (socket.sendto("time.nist.gov", 37, out_buffer, sizeof(out_buffer)) < 0) {
hudakz 2:c69ad6e71f97 56 printf("Error sending data\n");
hudakz 2:c69ad6e71f97 57 return -1;
hudakz 2:c69ad6e71f97 58 }
hudakz 3:6a6fe4ef95b9 59
hudakz 2:c69ad6e71f97 60 ntp_packet in_data;
hudakz 2:c69ad6e71f97 61 socket.recvfrom(&sockAddr, &in_data, sizeof(ntp_packet));
hudakz 2:c69ad6e71f97 62 in_data.secs = ntohl(in_data.secs) - 2208988800; // 1900-1970
hudakz 2:c69ad6e71f97 63 printf("Time received = %lu seconds since 1/01/1970 00:00 GMT\n", (uint32_t)in_data.secs);
hudakz 2:c69ad6e71f97 64 printf("Time = %s", ctime((const time_t*) &in_data.secs));
hudakz 2:c69ad6e71f97 65 printf("Time Server Address: %s\r\n", sockAddr.get_ip_address());
hudakz 2:c69ad6e71f97 66 printf("Time Server Port: %d\n\r", sockAddr.get_port());
hudakz 3:6a6fe4ef95b9 67
hudakz 2:c69ad6e71f97 68 // Close the socket and bring down the network interface
hudakz 2:c69ad6e71f97 69 socket.close();
hudakz 2:c69ad6e71f97 70 net.disconnect();
hudakz 2:c69ad6e71f97 71 return 0;
hudakz 3:6a6fe4ef95b9 72 }