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

Dependencies:   UIPEthernet

Revision:
0:47d7b7b2bba3
Child:
1:f6b6e2c173b9
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Sat Jul 01 08:23:21 2017 +0000
@@ -0,0 +1,110 @@
+/*
+ * UIPEthernet UdpClient example.
+ *
+ * UIPEthernet is a TCP/IP stack that can be used with a enc28j60 based
+ * Ethernet-shield.
+ *
+ * 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.0.1
+ * on port 5000 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
+ *
+ */
+#include "mbed.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);
+const IPAddress  SRV_IP(192, 168, 1, 181);   // UDP server IP address
+const uint16_t   UDP_PORT = 7;               // UDP 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;
+
+/**
+ * @brief
+ * @note
+ * @param
+ * @retval
+ */
+int main(void) {
+    const char* message = "Hello World from mbed";
+    time_t  next = time(NULL) + 5;
+    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;
+
+        if (time(NULL) > next) {
+            next = time(NULL) + 5;
+            
+            do {
+                success = udp.beginPacket(SRV_IP, UDP_PORT);
+                pc.printf("beginPacket: ");
+                if (success)
+                    pc.printf("succeeded.\r\n");
+                else
+                    pc.printf("failed.\r\n");
+
+                //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;
+            }
+
+            success = udp.write((uint8_t*)message, strlen(message));
+            pc.printf("bytes written: %d\r\n", success);
+
+            success = udp.endPacket();
+
+            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 (!success) {
+                udp.stop();
+                next = time(NULL) + 5;
+                continue;
+            }
+
+            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();
+        }
+    }
+}