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

Dependencies:   UIPEthernet

main.cpp

Committer:
hudakz
Date:
2017-07-01
Revision:
1:f6b6e2c173b9
Parent:
0:47d7b7b2bba3
Child:
2:c69ad6e71f97

File content as of revision 1:f6b6e2c173b9:

/*
 * UIPEthernet UdpClient example.
 *
 * 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>
 *
 *      -----------------
 *
 * 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
 *
 */
#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);   // 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;

/**
 * @brief
 * @note
 * @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;

        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");

                //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();
            next = time(NULL) + 5;
        }
    }
}