Zoltan Hudak
/
UdpClient_ENC28J60
Simple UDP Client using the UIPEthernet library for ENC28J60 Ethernet boards.
main.cpp@1:f6b6e2c173b9, 2017-07-01 (annotated)
- Committer:
- hudakz
- Date:
- Sat Jul 01 08:37:21 2017 +0000
- Revision:
- 1:f6b6e2c173b9
- Parent:
- 0:47d7b7b2bba3
- Child:
- 2:c69ad6e71f97
Updated.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hudakz | 0:47d7b7b2bba3 | 1 | /* |
hudakz | 0:47d7b7b2bba3 | 2 | * UIPEthernet UdpClient example. |
hudakz | 0:47d7b7b2bba3 | 3 | * |
hudakz | 1:f6b6e2c173b9 | 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 | 0:47d7b7b2bba3 | 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 | 1:f6b6e2c173b9 | 11 | * This UdpClient example tries to send a packet via udp to 192.168.1.181 |
hudakz | 1:f6b6e2c173b9 | 12 | * on port 7 every 5 seconds. After successfully sending the packet it |
hudakz | 0:47d7b7b2bba3 | 13 | * waits for up to 5 seconds for a response on the local port that has been |
hudakz | 0:47d7b7b2bba3 | 14 | * implicitly opened when sending the packet. |
hudakz | 0:47d7b7b2bba3 | 15 | * |
hudakz | 0:47d7b7b2bba3 | 16 | * Copyright (C) 2013 by Norbert Truchsess (norbert.truchsess@t-online.de) |
hudakz | 0:47d7b7b2bba3 | 17 | * |
hudakz | 0:47d7b7b2bba3 | 18 | * Modified by Zoltan Hudak |
hudakz | 0:47d7b7b2bba3 | 19 | * |
hudakz | 0:47d7b7b2bba3 | 20 | */ |
hudakz | 0:47d7b7b2bba3 | 21 | #include "mbed.h" |
hudakz | 0:47d7b7b2bba3 | 22 | #include "UIPEthernet.h" |
hudakz | 0:47d7b7b2bba3 | 23 | |
hudakz | 0:47d7b7b2bba3 | 24 | // MAC address must be unique within the connected network. Modify as appropriate. |
hudakz | 0:47d7b7b2bba3 | 25 | const uint8_t MY_MAC[6] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05 }; |
hudakz | 0:47d7b7b2bba3 | 26 | // IP address must be unique and compatible with your network. |
hudakz | 1:f6b6e2c173b9 | 27 | const IPAddress MY_IP(192, 168, 1, 182); // this UDP client IP address |
hudakz | 1:f6b6e2c173b9 | 28 | const IPAddress SRV_IP(192, 168, 1, 181); // UDP server IP address |
hudakz | 1:f6b6e2c173b9 | 29 | const uint16_t PORT = 7; // port used |
hudakz | 0:47d7b7b2bba3 | 30 | const char* message = "Hello World from mbed!"; |
hudakz | 0:47d7b7b2bba3 | 31 | |
hudakz | 0:47d7b7b2bba3 | 32 | Serial pc(USBTX, USBRX); |
hudakz | 0:47d7b7b2bba3 | 33 | UIPEthernet uIPEthernet(D11, D12, D13, D10); // mosi, miso, sck, cs |
hudakz | 0:47d7b7b2bba3 | 34 | UIPUDP udp; |
hudakz | 0:47d7b7b2bba3 | 35 | |
hudakz | 0:47d7b7b2bba3 | 36 | /** |
hudakz | 0:47d7b7b2bba3 | 37 | * @brief |
hudakz | 0:47d7b7b2bba3 | 38 | * @note |
hudakz | 0:47d7b7b2bba3 | 39 | * @param |
hudakz | 0:47d7b7b2bba3 | 40 | * @retval |
hudakz | 0:47d7b7b2bba3 | 41 | */ |
hudakz | 0:47d7b7b2bba3 | 42 | int main(void) { |
hudakz | 0:47d7b7b2bba3 | 43 | const char* message = "Hello World from mbed"; |
hudakz | 1:f6b6e2c173b9 | 44 | time_t next = time(NULL); |
hudakz | 0:47d7b7b2bba3 | 45 | uIPEthernet.begin(MY_MAC, MY_IP); |
hudakz | 0:47d7b7b2bba3 | 46 | |
hudakz | 0:47d7b7b2bba3 | 47 | IPAddress localIP = uIPEthernet.localIP(); |
hudakz | 0:47d7b7b2bba3 | 48 | pc.printf("Local IP = %s\r\n", localIP.toString()); |
hudakz | 0:47d7b7b2bba3 | 49 | |
hudakz | 0:47d7b7b2bba3 | 50 | while (1) { |
hudakz | 0:47d7b7b2bba3 | 51 | int success; |
hudakz | 0:47d7b7b2bba3 | 52 | int len = 0; |
hudakz | 0:47d7b7b2bba3 | 53 | |
hudakz | 0:47d7b7b2bba3 | 54 | if (time(NULL) > next) { |
hudakz | 0:47d7b7b2bba3 | 55 | next = time(NULL) + 5; |
hudakz | 0:47d7b7b2bba3 | 56 | |
hudakz | 0:47d7b7b2bba3 | 57 | do { |
hudakz | 1:f6b6e2c173b9 | 58 | success = udp.beginPacket(SRV_IP, PORT); |
hudakz | 0:47d7b7b2bba3 | 59 | pc.printf("beginPacket: "); |
hudakz | 0:47d7b7b2bba3 | 60 | if (success) |
hudakz | 0:47d7b7b2bba3 | 61 | pc.printf("succeeded.\r\n"); |
hudakz | 0:47d7b7b2bba3 | 62 | else |
hudakz | 0:47d7b7b2bba3 | 63 | pc.printf("failed.\r\n"); |
hudakz | 0:47d7b7b2bba3 | 64 | |
hudakz | 0:47d7b7b2bba3 | 65 | //beginPacket fails if remote ethaddr is unknown. In this case an |
hudakz | 0:47d7b7b2bba3 | 66 | //arp-request is send out first and beginPacket succeeds as soon |
hudakz | 0:47d7b7b2bba3 | 67 | //the arp-response is received. |
hudakz | 0:47d7b7b2bba3 | 68 | } while (!success && (time(NULL) < next)); |
hudakz | 0:47d7b7b2bba3 | 69 | |
hudakz | 0:47d7b7b2bba3 | 70 | if (!success) { |
hudakz | 0:47d7b7b2bba3 | 71 | udp.stop(); |
hudakz | 0:47d7b7b2bba3 | 72 | next = time(NULL) + 5; |
hudakz | 0:47d7b7b2bba3 | 73 | continue; |
hudakz | 0:47d7b7b2bba3 | 74 | } |
hudakz | 0:47d7b7b2bba3 | 75 | |
hudakz | 0:47d7b7b2bba3 | 76 | success = udp.write((uint8_t*)message, strlen(message)); |
hudakz | 0:47d7b7b2bba3 | 77 | pc.printf("bytes written: %d\r\n", success); |
hudakz | 0:47d7b7b2bba3 | 78 | |
hudakz | 0:47d7b7b2bba3 | 79 | success = udp.endPacket(); |
hudakz | 0:47d7b7b2bba3 | 80 | |
hudakz | 0:47d7b7b2bba3 | 81 | if (success) |
hudakz | 0:47d7b7b2bba3 | 82 | pc.printf("endPacket: succeeded%\r\n"); |
hudakz | 0:47d7b7b2bba3 | 83 | else |
hudakz | 0:47d7b7b2bba3 | 84 | pc.printf("endPacket: failed%\r\n"); |
hudakz | 0:47d7b7b2bba3 | 85 | |
hudakz | 0:47d7b7b2bba3 | 86 | do { |
hudakz | 0:47d7b7b2bba3 | 87 | success = udp.parsePacket(); //check for new udp-packet |
hudakz | 0:47d7b7b2bba3 | 88 | } while (!success && (time(NULL) < next)); |
hudakz | 0:47d7b7b2bba3 | 89 | |
hudakz | 0:47d7b7b2bba3 | 90 | if (!success) { |
hudakz | 0:47d7b7b2bba3 | 91 | udp.stop(); |
hudakz | 0:47d7b7b2bba3 | 92 | next = time(NULL) + 5; |
hudakz | 0:47d7b7b2bba3 | 93 | continue; |
hudakz | 0:47d7b7b2bba3 | 94 | } |
hudakz | 0:47d7b7b2bba3 | 95 | |
hudakz | 0:47d7b7b2bba3 | 96 | pc.printf("received: '"); |
hudakz | 0:47d7b7b2bba3 | 97 | |
hudakz | 0:47d7b7b2bba3 | 98 | do { |
hudakz | 0:47d7b7b2bba3 | 99 | int c = udp.read(); |
hudakz | 0:47d7b7b2bba3 | 100 | pc.putc(c); |
hudakz | 0:47d7b7b2bba3 | 101 | len++; |
hudakz | 0:47d7b7b2bba3 | 102 | } while (udp.available() > 0); |
hudakz | 0:47d7b7b2bba3 | 103 | |
hudakz | 0:47d7b7b2bba3 | 104 | pc.printf("', %d bytes\r\n", len); |
hudakz | 0:47d7b7b2bba3 | 105 | |
hudakz | 0:47d7b7b2bba3 | 106 | //finish reading this packet: |
hudakz | 0:47d7b7b2bba3 | 107 | udp.flush(); |
hudakz | 1:f6b6e2c173b9 | 108 | next = time(NULL) + 5; |
hudakz | 0:47d7b7b2bba3 | 109 | } |
hudakz | 0:47d7b7b2bba3 | 110 | } |
hudakz | 0:47d7b7b2bba3 | 111 | } |