UIPEthernet library for Arduino IDE, Eclipse with arduino plugin and MBED/SMeshStudio (AVR,STM32F,ESP8266,Intel ARC32,Nordic nRF51,Teensy boards,Realtek Ameba(RTL8195A,RTL8710)), ENC28j60 network chip. Compatible with Wiznet W5100 Ethernet library API. Compiled and tested on Nucleo-F302R8. Master repository is: https://github.com/UIPEthernet/UIPEthernet/

Committer:
cassyarduino
Date:
Tue Jan 23 15:08:43 2018 +0100
Revision:
39:deeb00b81cc9
Parent:
12:aef29fbc4a62
Release: 2.0.4

Who changed what in which revision?

UserRevisionLine numberNew contents of line
cassyarduino 0:e3fb1267e3c3 1 /*
cassyarduino 0:e3fb1267e3c3 2 * UIPEthernet UdpClient example.
cassyarduino 0:e3fb1267e3c3 3 *
cassyarduino 0:e3fb1267e3c3 4 * UIPEthernet is a TCP/IP stack that can be used with a enc28j60 based
cassyarduino 0:e3fb1267e3c3 5 * Ethernet-shield.
cassyarduino 0:e3fb1267e3c3 6 *
cassyarduino 0:e3fb1267e3c3 7 * UIPEthernet uses the fine uIP stack by Adam Dunkels <adam@sics.se>
cassyarduino 0:e3fb1267e3c3 8 *
cassyarduino 0:e3fb1267e3c3 9 * -----------------
cassyarduino 0:e3fb1267e3c3 10 *
cassyarduino 0:e3fb1267e3c3 11 * This UdpClient example tries to send a packet via udp to 192.168.0.1
cassyarduino 0:e3fb1267e3c3 12 * on port 5000 every 5 seconds. After successfully sending the packet it
cassyarduino 0:e3fb1267e3c3 13 * waits for up to 5 seconds for a response on the local port that has been
cassyarduino 0:e3fb1267e3c3 14 * implicitly opened when sending the packet.
cassyarduino 0:e3fb1267e3c3 15 *
cassyarduino 0:e3fb1267e3c3 16 * Copyright (C) 2013 by Norbert Truchsess (norbert.truchsess@t-online.de)
cassyarduino 0:e3fb1267e3c3 17 */
cassyarduino 0:e3fb1267e3c3 18
cassyarduino 11:3fb19220d9ec 19 #if defined(__MBED__)
cassyarduino 11:3fb19220d9ec 20 #include <mbed.h>
cassyarduino 11:3fb19220d9ec 21 #include "mbed/millis.h"
cassyarduino 11:3fb19220d9ec 22 #define delay(x) wait_ms(x)
cassyarduino 11:3fb19220d9ec 23 #define PROGMEM
cassyarduino 11:3fb19220d9ec 24 #include "mbed/Print.h"
cassyarduino 11:3fb19220d9ec 25 #endif
cassyarduino 11:3fb19220d9ec 26
cassyarduino 0:e3fb1267e3c3 27 #include <UIPEthernet.h>
cassyarduino 0:e3fb1267e3c3 28 #include "utility/logging.h"
cassyarduino 0:e3fb1267e3c3 29
cassyarduino 0:e3fb1267e3c3 30 EthernetUDP udp;
cassyarduino 0:e3fb1267e3c3 31 unsigned long next;
cassyarduino 0:e3fb1267e3c3 32
cassyarduino 11:3fb19220d9ec 33 #if defined(ARDUINO)
cassyarduino 0:e3fb1267e3c3 34 void setup() {
cassyarduino 11:3fb19220d9ec 35 #endif
cassyarduino 11:3fb19220d9ec 36 #if defined(__MBED__)
cassyarduino 11:3fb19220d9ec 37 int main() {
cassyarduino 11:3fb19220d9ec 38 #endif
cassyarduino 0:e3fb1267e3c3 39 #if ACTLOGLEVEL>LOG_NONE
cassyarduino 12:aef29fbc4a62 40 #if defined(ARDUINO)
cassyarduino 12:aef29fbc4a62 41 LogObject.begin(9600);
cassyarduino 12:aef29fbc4a62 42 #endif
cassyarduino 12:aef29fbc4a62 43 #if defined(__MBED__)
cassyarduino 12:aef29fbc4a62 44 Serial LogObject(SERIAL_TX,SERIAL_RX);
cassyarduino 12:aef29fbc4a62 45 #endif
cassyarduino 0:e3fb1267e3c3 46 #endif
cassyarduino 0:e3fb1267e3c3 47
cassyarduino 0:e3fb1267e3c3 48 uint8_t mac[6] = {0x00,0x01,0x02,0x03,0x04,0x05};
cassyarduino 0:e3fb1267e3c3 49
cassyarduino 0:e3fb1267e3c3 50 Ethernet.begin(mac,IPAddress(192,168,0,6));
cassyarduino 0:e3fb1267e3c3 51
cassyarduino 0:e3fb1267e3c3 52 next = millis()+5000;
cassyarduino 11:3fb19220d9ec 53 #if defined(ARDUINO)
cassyarduino 0:e3fb1267e3c3 54 }
cassyarduino 0:e3fb1267e3c3 55
cassyarduino 0:e3fb1267e3c3 56 void loop() {
cassyarduino 11:3fb19220d9ec 57 #endif
cassyarduino 0:e3fb1267e3c3 58
cassyarduino 11:3fb19220d9ec 59 #if defined(__MBED__)
cassyarduino 11:3fb19220d9ec 60 while(true) {
cassyarduino 11:3fb19220d9ec 61 #endif
cassyarduino 0:e3fb1267e3c3 62 int success;
cassyarduino 0:e3fb1267e3c3 63 int len = 0;
cassyarduino 0:e3fb1267e3c3 64
cassyarduino 6:f092f8b320ae 65 if (millis()>next)
cassyarduino 0:e3fb1267e3c3 66 {
cassyarduino 0:e3fb1267e3c3 67 do
cassyarduino 0:e3fb1267e3c3 68 {
cassyarduino 0:e3fb1267e3c3 69 success = udp.beginPacket(IPAddress(192,168,0,1),5000);
cassyarduino 0:e3fb1267e3c3 70 #if ACTLOGLEVEL>=LOG_INFO
cassyarduino 0:e3fb1267e3c3 71 LogObject.uart_send_str(F("beginPacket: "));
cassyarduino 0:e3fb1267e3c3 72 LogObject.uart_send_strln(success ? "success" : "failed");
cassyarduino 0:e3fb1267e3c3 73 #endif
cassyarduino 0:e3fb1267e3c3 74 //beginPacket fails if remote ethaddr is unknown. In this case an
cassyarduino 0:e3fb1267e3c3 75 //arp-request is send out first and beginPacket succeeds as soon
cassyarduino 0:e3fb1267e3c3 76 //the arp-response is received.
cassyarduino 6:f092f8b320ae 77 #if defined(ESP8266)
cassyarduino 6:f092f8b320ae 78 wdt_reset();
cassyarduino 6:f092f8b320ae 79 #endif
cassyarduino 0:e3fb1267e3c3 80 }
cassyarduino 6:f092f8b320ae 81 while (!success && (millis()<next));
cassyarduino 6:f092f8b320ae 82 if (success)
cassyarduino 0:e3fb1267e3c3 83 {
cassyarduino 6:f092f8b320ae 84 success = udp.write("hello world from arduino");
cassyarduino 6:f092f8b320ae 85 #if ACTLOGLEVEL>=LOG_INFO
cassyarduino 6:f092f8b320ae 86 LogObject.uart_send_str(F("bytes written: "));
cassyarduino 6:f092f8b320ae 87 LogObject.uart_send_decln(success);
cassyarduino 6:f092f8b320ae 88 #endif
cassyarduino 6:f092f8b320ae 89 success = udp.endPacket();
cassyarduino 6:f092f8b320ae 90 #if ACTLOGLEVEL>=LOG_INFO
cassyarduino 6:f092f8b320ae 91 LogObject.uart_send_str(F("endPacket: "));
cassyarduino 6:f092f8b320ae 92 LogObject.uart_send_strln(success ? "success" : "failed");
cassyarduino 6:f092f8b320ae 93 #endif
cassyarduino 6:f092f8b320ae 94 do
cassyarduino 6:f092f8b320ae 95 {
cassyarduino 0:e3fb1267e3c3 96 //check for new udp-packet:
cassyarduino 0:e3fb1267e3c3 97 success = udp.parsePacket();
cassyarduino 6:f092f8b320ae 98 #if defined(ESP8266)
cassyarduino 6:f092f8b320ae 99 wdt_reset();
cassyarduino 6:f092f8b320ae 100 #endif
cassyarduino 6:f092f8b320ae 101 }
cassyarduino 6:f092f8b320ae 102 while (!success && (millis()<next));
cassyarduino 6:f092f8b320ae 103 if (success)
cassyarduino 6:f092f8b320ae 104 {
cassyarduino 0:e3fb1267e3c3 105 #if ACTLOGLEVEL>=LOG_INFO
cassyarduino 6:f092f8b320ae 106 LogObject.uart_send_str(F("received: '"));
cassyarduino 0:e3fb1267e3c3 107 #endif
cassyarduino 6:f092f8b320ae 108 do
cassyarduino 6:f092f8b320ae 109 {
cassyarduino 12:aef29fbc4a62 110 char c = udp.read();
cassyarduino 6:f092f8b320ae 111 #if ACTLOGLEVEL>=LOG_INFO
cassyarduino 12:aef29fbc4a62 112 #if defined(ARDUINO)
cassyarduino 12:aef29fbc4a62 113 LogObject.write(c);
cassyarduino 12:aef29fbc4a62 114 #endif
cassyarduino 12:aef29fbc4a62 115 #if defined(__MBED__)
cassyarduino 12:aef29fbc4a62 116 LogObject.printf("%c",&c);
cassyarduino 12:aef29fbc4a62 117 #endif
cassyarduino 6:f092f8b320ae 118 #endif
cassyarduino 6:f092f8b320ae 119 len++;
cassyarduino 6:f092f8b320ae 120 }
cassyarduino 6:f092f8b320ae 121 while ((success = udp.available())>0);
cassyarduino 6:f092f8b320ae 122 #if ACTLOGLEVEL>=LOG_INFO
cassyarduino 6:f092f8b320ae 123 LogObject.uart_send_str(F("', "));
cassyarduino 6:f092f8b320ae 124 LogObject.uart_send_dec(len);
cassyarduino 6:f092f8b320ae 125 LogObject.uart_send_strln(F(" bytes"));
cassyarduino 6:f092f8b320ae 126 #endif
cassyarduino 6:f092f8b320ae 127 //finish reading this packet:
cassyarduino 6:f092f8b320ae 128 udp.flush();
cassyarduino 6:f092f8b320ae 129 }
cassyarduino 0:e3fb1267e3c3 130 }
cassyarduino 0:e3fb1267e3c3 131 udp.stop();
cassyarduino 0:e3fb1267e3c3 132 next = millis()+5000;
cassyarduino 0:e3fb1267e3c3 133 }
cassyarduino 0:e3fb1267e3c3 134 }
cassyarduino 11:3fb19220d9ec 135 #if defined(__MBED__)
cassyarduino 11:3fb19220d9ec 136 }
cassyarduino 11:3fb19220d9ec 137 #endif