Zoltan Hudak
/
UdpServer_ENC28J60
Simple UDP Server using the UIPEthernet library for ENC28J60 Ethernet boards.
Diff: main.cpp
- Revision:
- 2:6bbdab30a1c1
- Parent:
- 1:29bb0a32f61d
- Child:
- 3:e2462f078d64
--- a/main.cpp Sat Jul 01 08:16:18 2017 +0000 +++ b/main.cpp Tue Sep 03 10:59:59 2019 +0000 @@ -17,18 +17,19 @@ * */ #include "mbed.h" -#include "UIPEthernet.h" +#include "UipEthernet.h" + +#define IP "192.168.1.35" +#define GATEWAY "192.168.1.1" +#define NETMASK "255.255.255.0" +#define PORT 80 // 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, 181); -const uint16_t MY_PORT = 7; -const char* message = "Hello World from mbed!"; +const uint8_t MAC[6] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05 }; +UipEthernet net(MAC, D11, D12, D13, D10); // mac, mosi, miso, sck, cs -Serial pc(USBTX, USBRX); -UIPEthernet uIPEthernet(D11, D12, D13, D10); // mosi, miso, sck, cs -UIPUDP udp; +const char message[] = "Hello World from mbed!"; + /** * @brief @@ -37,67 +38,80 @@ * @retval */ int main(void) { - uIPEthernet.begin(MY_MAC, MY_IP); + // Bring up the ethernet interface + //net.set_network(IP, NETMASK, GATEWAY); // include this for using static IP address + printf("UDP Server example\n"); + if(net.connect() != 0) { + printf("Error connecting\n"); + return -1; + } - IPAddress localIP = uIPEthernet.localIP(); - pc.printf("Local IP = %s\r\n", localIP.toString()); - pc.printf("Initialization "); - if (udp.begin(MY_PORT)) - 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(); + + 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"); + + UdpSocket socket(&net); + + socket.begin(PORT); // listen at port PORT for user datagrams + + printf("Waiting for datagrams from UDP clients\r\n"); while (1) { int success; - int size = udp.parsePacket(); + int size = socket.parsePacket(); if (size > 0) { do { - char* msg = (char*)malloc(size + 1); - int len = udp.read(msg, size + 1); - msg[len] = 0; - printf("received: '%s", msg); - free(msg); - } while ((size = udp.available()) > 0); + char* data = (char*)malloc(size + 1); + int len = socket.read(data, size + 1); + data[len] = 0; + printf("received: '%s", data); + free(data); + } while ((size = socket.available()) > 0); //finish reading this packet: - udp.flush(); + socket.flush(); printf("'\r\n"); do { //send new packet back to ip/port of client. This also //configures the current connection to ignore packets from //other clients! - success = udp.beginPacket(udp.remoteIP(), udp.remotePort()); + success = socket.beginPacket(socket.remoteIP(), socket.remotePort()); if (success) - pc.printf("beginPacket: succeeded%\r\n"); + printf("beginPacket: succeeded%\r\n"); else - pc.printf("beginPacket: failed%\r\n"); + printf("beginPacket: 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); - success = udp.write((uint8_t*)message, strlen(message)); + success = socket.write((uint8_t*)message, strlen(message)); if (success) - pc.printf("bytes written: %d\r\n", success); + printf("bytes written: %d\r\n", success); - success = udp.endPacket(); + success = socket.endPacket(); if (success) - pc.printf("endPacket: succeeded%\r\n"); + printf("endPacket: succeeded%\r\n"); else - pc.printf("endPacket: failed%\r\n"); + printf("endPacket: failed%\r\n"); - udp.stop(); + socket.close(); //restart with new connection to receive packets from other clients - if (udp.begin(MY_PORT)) - pc.printf("restart connection: succeeded%\r\n"); + if (socket.begin(PORT)) + printf("restart connection: succeeded%\r\n"); else - pc.printf("restart connection: failed%\r\n"); + printf("restart connection: failed%\r\n"); } } }