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/
examples/UdpClient/UdpClient.ino@11:3fb19220d9ec, 2016-12-29 (annotated)
- Committer:
- cassyarduino
- Date:
- Thu Dec 29 13:07:19 2016 +0100
- Revision:
- 11:3fb19220d9ec
- Parent:
- 6:f092f8b320ae
- Child:
- 12:aef29fbc4a62
Changes
Who changed what in which revision?
User | Revision | Line number | New 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 | 0:e3fb1267e3c3 | 40 | LogObject.begin(9600); |
cassyarduino | 0:e3fb1267e3c3 | 41 | #endif |
cassyarduino | 0:e3fb1267e3c3 | 42 | |
cassyarduino | 0:e3fb1267e3c3 | 43 | uint8_t mac[6] = {0x00,0x01,0x02,0x03,0x04,0x05}; |
cassyarduino | 0:e3fb1267e3c3 | 44 | |
cassyarduino | 0:e3fb1267e3c3 | 45 | Ethernet.begin(mac,IPAddress(192,168,0,6)); |
cassyarduino | 0:e3fb1267e3c3 | 46 | |
cassyarduino | 0:e3fb1267e3c3 | 47 | next = millis()+5000; |
cassyarduino | 11:3fb19220d9ec | 48 | #if defined(ARDUINO) |
cassyarduino | 0:e3fb1267e3c3 | 49 | } |
cassyarduino | 0:e3fb1267e3c3 | 50 | |
cassyarduino | 0:e3fb1267e3c3 | 51 | void loop() { |
cassyarduino | 11:3fb19220d9ec | 52 | #endif |
cassyarduino | 0:e3fb1267e3c3 | 53 | |
cassyarduino | 11:3fb19220d9ec | 54 | #if defined(__MBED__) |
cassyarduino | 11:3fb19220d9ec | 55 | while(true) { |
cassyarduino | 11:3fb19220d9ec | 56 | #endif |
cassyarduino | 0:e3fb1267e3c3 | 57 | int success; |
cassyarduino | 0:e3fb1267e3c3 | 58 | int len = 0; |
cassyarduino | 0:e3fb1267e3c3 | 59 | |
cassyarduino | 6:f092f8b320ae | 60 | if (millis()>next) |
cassyarduino | 0:e3fb1267e3c3 | 61 | { |
cassyarduino | 0:e3fb1267e3c3 | 62 | do |
cassyarduino | 0:e3fb1267e3c3 | 63 | { |
cassyarduino | 0:e3fb1267e3c3 | 64 | success = udp.beginPacket(IPAddress(192,168,0,1),5000); |
cassyarduino | 0:e3fb1267e3c3 | 65 | #if ACTLOGLEVEL>=LOG_INFO |
cassyarduino | 0:e3fb1267e3c3 | 66 | LogObject.uart_send_str(F("beginPacket: ")); |
cassyarduino | 0:e3fb1267e3c3 | 67 | LogObject.uart_send_strln(success ? "success" : "failed"); |
cassyarduino | 0:e3fb1267e3c3 | 68 | #endif |
cassyarduino | 0:e3fb1267e3c3 | 69 | //beginPacket fails if remote ethaddr is unknown. In this case an |
cassyarduino | 0:e3fb1267e3c3 | 70 | //arp-request is send out first and beginPacket succeeds as soon |
cassyarduino | 0:e3fb1267e3c3 | 71 | //the arp-response is received. |
cassyarduino | 6:f092f8b320ae | 72 | #if defined(ESP8266) |
cassyarduino | 6:f092f8b320ae | 73 | wdt_reset(); |
cassyarduino | 6:f092f8b320ae | 74 | #endif |
cassyarduino | 0:e3fb1267e3c3 | 75 | } |
cassyarduino | 6:f092f8b320ae | 76 | while (!success && (millis()<next)); |
cassyarduino | 6:f092f8b320ae | 77 | if (success) |
cassyarduino | 0:e3fb1267e3c3 | 78 | { |
cassyarduino | 6:f092f8b320ae | 79 | success = udp.write("hello world from arduino"); |
cassyarduino | 6:f092f8b320ae | 80 | #if ACTLOGLEVEL>=LOG_INFO |
cassyarduino | 6:f092f8b320ae | 81 | LogObject.uart_send_str(F("bytes written: ")); |
cassyarduino | 6:f092f8b320ae | 82 | LogObject.uart_send_decln(success); |
cassyarduino | 6:f092f8b320ae | 83 | #endif |
cassyarduino | 6:f092f8b320ae | 84 | success = udp.endPacket(); |
cassyarduino | 6:f092f8b320ae | 85 | #if ACTLOGLEVEL>=LOG_INFO |
cassyarduino | 6:f092f8b320ae | 86 | LogObject.uart_send_str(F("endPacket: ")); |
cassyarduino | 6:f092f8b320ae | 87 | LogObject.uart_send_strln(success ? "success" : "failed"); |
cassyarduino | 6:f092f8b320ae | 88 | #endif |
cassyarduino | 6:f092f8b320ae | 89 | do |
cassyarduino | 6:f092f8b320ae | 90 | { |
cassyarduino | 0:e3fb1267e3c3 | 91 | //check for new udp-packet: |
cassyarduino | 0:e3fb1267e3c3 | 92 | success = udp.parsePacket(); |
cassyarduino | 6:f092f8b320ae | 93 | #if defined(ESP8266) |
cassyarduino | 6:f092f8b320ae | 94 | wdt_reset(); |
cassyarduino | 6:f092f8b320ae | 95 | #endif |
cassyarduino | 6:f092f8b320ae | 96 | } |
cassyarduino | 6:f092f8b320ae | 97 | while (!success && (millis()<next)); |
cassyarduino | 6:f092f8b320ae | 98 | if (success) |
cassyarduino | 6:f092f8b320ae | 99 | { |
cassyarduino | 0:e3fb1267e3c3 | 100 | #if ACTLOGLEVEL>=LOG_INFO |
cassyarduino | 6:f092f8b320ae | 101 | LogObject.uart_send_str(F("received: '")); |
cassyarduino | 0:e3fb1267e3c3 | 102 | #endif |
cassyarduino | 6:f092f8b320ae | 103 | do |
cassyarduino | 6:f092f8b320ae | 104 | { |
cassyarduino | 6:f092f8b320ae | 105 | int c = udp.read(); |
cassyarduino | 6:f092f8b320ae | 106 | #if ACTLOGLEVEL>=LOG_INFO |
cassyarduino | 6:f092f8b320ae | 107 | LogObject.write(c); |
cassyarduino | 6:f092f8b320ae | 108 | #endif |
cassyarduino | 6:f092f8b320ae | 109 | len++; |
cassyarduino | 6:f092f8b320ae | 110 | } |
cassyarduino | 6:f092f8b320ae | 111 | while ((success = udp.available())>0); |
cassyarduino | 6:f092f8b320ae | 112 | #if ACTLOGLEVEL>=LOG_INFO |
cassyarduino | 6:f092f8b320ae | 113 | LogObject.uart_send_str(F("', ")); |
cassyarduino | 6:f092f8b320ae | 114 | LogObject.uart_send_dec(len); |
cassyarduino | 6:f092f8b320ae | 115 | LogObject.uart_send_strln(F(" bytes")); |
cassyarduino | 6:f092f8b320ae | 116 | #endif |
cassyarduino | 6:f092f8b320ae | 117 | //finish reading this packet: |
cassyarduino | 6:f092f8b320ae | 118 | udp.flush(); |
cassyarduino | 6:f092f8b320ae | 119 | } |
cassyarduino | 0:e3fb1267e3c3 | 120 | } |
cassyarduino | 0:e3fb1267e3c3 | 121 | udp.stop(); |
cassyarduino | 0:e3fb1267e3c3 | 122 | next = millis()+5000; |
cassyarduino | 0:e3fb1267e3c3 | 123 | } |
cassyarduino | 0:e3fb1267e3c3 | 124 | } |
cassyarduino | 11:3fb19220d9ec | 125 | #if defined(__MBED__) |
cassyarduino | 11:3fb19220d9ec | 126 | } |
cassyarduino | 11:3fb19220d9ec | 127 | #endif |