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@6:f092f8b320ae, 2016-12-27 (annotated)
- Committer:
- cassyarduino
- Date:
- Tue Dec 27 13:45:01 2016 +0100
- Revision:
- 6:f092f8b320ae
- Parent:
- 0:e3fb1267e3c3
- Child:
- 11:3fb19220d9ec
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 | 0:e3fb1267e3c3 | 19 | #include <UIPEthernet.h> |
cassyarduino | 0:e3fb1267e3c3 | 20 | #include "utility/logging.h" |
cassyarduino | 0:e3fb1267e3c3 | 21 | |
cassyarduino | 0:e3fb1267e3c3 | 22 | EthernetUDP udp; |
cassyarduino | 0:e3fb1267e3c3 | 23 | unsigned long next; |
cassyarduino | 0:e3fb1267e3c3 | 24 | |
cassyarduino | 0:e3fb1267e3c3 | 25 | void setup() { |
cassyarduino | 0:e3fb1267e3c3 | 26 | |
cassyarduino | 0:e3fb1267e3c3 | 27 | #if ACTLOGLEVEL>LOG_NONE |
cassyarduino | 0:e3fb1267e3c3 | 28 | LogObject.begin(9600); |
cassyarduino | 0:e3fb1267e3c3 | 29 | #endif |
cassyarduino | 0:e3fb1267e3c3 | 30 | |
cassyarduino | 0:e3fb1267e3c3 | 31 | uint8_t mac[6] = {0x00,0x01,0x02,0x03,0x04,0x05}; |
cassyarduino | 0:e3fb1267e3c3 | 32 | |
cassyarduino | 0:e3fb1267e3c3 | 33 | Ethernet.begin(mac,IPAddress(192,168,0,6)); |
cassyarduino | 0:e3fb1267e3c3 | 34 | |
cassyarduino | 0:e3fb1267e3c3 | 35 | next = millis()+5000; |
cassyarduino | 0:e3fb1267e3c3 | 36 | } |
cassyarduino | 0:e3fb1267e3c3 | 37 | |
cassyarduino | 0:e3fb1267e3c3 | 38 | void loop() { |
cassyarduino | 0:e3fb1267e3c3 | 39 | |
cassyarduino | 0:e3fb1267e3c3 | 40 | int success; |
cassyarduino | 0:e3fb1267e3c3 | 41 | int len = 0; |
cassyarduino | 0:e3fb1267e3c3 | 42 | |
cassyarduino | 6:f092f8b320ae | 43 | if (millis()>next) |
cassyarduino | 0:e3fb1267e3c3 | 44 | { |
cassyarduino | 0:e3fb1267e3c3 | 45 | do |
cassyarduino | 0:e3fb1267e3c3 | 46 | { |
cassyarduino | 0:e3fb1267e3c3 | 47 | success = udp.beginPacket(IPAddress(192,168,0,1),5000); |
cassyarduino | 0:e3fb1267e3c3 | 48 | #if ACTLOGLEVEL>=LOG_INFO |
cassyarduino | 0:e3fb1267e3c3 | 49 | LogObject.uart_send_str(F("beginPacket: ")); |
cassyarduino | 0:e3fb1267e3c3 | 50 | LogObject.uart_send_strln(success ? "success" : "failed"); |
cassyarduino | 0:e3fb1267e3c3 | 51 | #endif |
cassyarduino | 0:e3fb1267e3c3 | 52 | //beginPacket fails if remote ethaddr is unknown. In this case an |
cassyarduino | 0:e3fb1267e3c3 | 53 | //arp-request is send out first and beginPacket succeeds as soon |
cassyarduino | 0:e3fb1267e3c3 | 54 | //the arp-response is received. |
cassyarduino | 6:f092f8b320ae | 55 | #if defined(ESP8266) |
cassyarduino | 6:f092f8b320ae | 56 | wdt_reset(); |
cassyarduino | 6:f092f8b320ae | 57 | #endif |
cassyarduino | 0:e3fb1267e3c3 | 58 | } |
cassyarduino | 6:f092f8b320ae | 59 | while (!success && (millis()<next)); |
cassyarduino | 6:f092f8b320ae | 60 | if (success) |
cassyarduino | 0:e3fb1267e3c3 | 61 | { |
cassyarduino | 6:f092f8b320ae | 62 | success = udp.write("hello world from arduino"); |
cassyarduino | 6:f092f8b320ae | 63 | #if ACTLOGLEVEL>=LOG_INFO |
cassyarduino | 6:f092f8b320ae | 64 | LogObject.uart_send_str(F("bytes written: ")); |
cassyarduino | 6:f092f8b320ae | 65 | LogObject.uart_send_decln(success); |
cassyarduino | 6:f092f8b320ae | 66 | #endif |
cassyarduino | 6:f092f8b320ae | 67 | success = udp.endPacket(); |
cassyarduino | 6:f092f8b320ae | 68 | #if ACTLOGLEVEL>=LOG_INFO |
cassyarduino | 6:f092f8b320ae | 69 | LogObject.uart_send_str(F("endPacket: ")); |
cassyarduino | 6:f092f8b320ae | 70 | LogObject.uart_send_strln(success ? "success" : "failed"); |
cassyarduino | 6:f092f8b320ae | 71 | #endif |
cassyarduino | 6:f092f8b320ae | 72 | do |
cassyarduino | 6:f092f8b320ae | 73 | { |
cassyarduino | 0:e3fb1267e3c3 | 74 | //check for new udp-packet: |
cassyarduino | 0:e3fb1267e3c3 | 75 | success = udp.parsePacket(); |
cassyarduino | 6:f092f8b320ae | 76 | #if defined(ESP8266) |
cassyarduino | 6:f092f8b320ae | 77 | wdt_reset(); |
cassyarduino | 6:f092f8b320ae | 78 | #endif |
cassyarduino | 6:f092f8b320ae | 79 | } |
cassyarduino | 6:f092f8b320ae | 80 | while (!success && (millis()<next)); |
cassyarduino | 6:f092f8b320ae | 81 | if (success) |
cassyarduino | 6:f092f8b320ae | 82 | { |
cassyarduino | 0:e3fb1267e3c3 | 83 | #if ACTLOGLEVEL>=LOG_INFO |
cassyarduino | 6:f092f8b320ae | 84 | LogObject.uart_send_str(F("received: '")); |
cassyarduino | 0:e3fb1267e3c3 | 85 | #endif |
cassyarduino | 6:f092f8b320ae | 86 | do |
cassyarduino | 6:f092f8b320ae | 87 | { |
cassyarduino | 6:f092f8b320ae | 88 | int c = udp.read(); |
cassyarduino | 6:f092f8b320ae | 89 | #if ACTLOGLEVEL>=LOG_INFO |
cassyarduino | 6:f092f8b320ae | 90 | LogObject.write(c); |
cassyarduino | 6:f092f8b320ae | 91 | #endif |
cassyarduino | 6:f092f8b320ae | 92 | len++; |
cassyarduino | 6:f092f8b320ae | 93 | } |
cassyarduino | 6:f092f8b320ae | 94 | while ((success = udp.available())>0); |
cassyarduino | 6:f092f8b320ae | 95 | #if ACTLOGLEVEL>=LOG_INFO |
cassyarduino | 6:f092f8b320ae | 96 | LogObject.uart_send_str(F("', ")); |
cassyarduino | 6:f092f8b320ae | 97 | LogObject.uart_send_dec(len); |
cassyarduino | 6:f092f8b320ae | 98 | LogObject.uart_send_strln(F(" bytes")); |
cassyarduino | 6:f092f8b320ae | 99 | #endif |
cassyarduino | 6:f092f8b320ae | 100 | //finish reading this packet: |
cassyarduino | 6:f092f8b320ae | 101 | udp.flush(); |
cassyarduino | 6:f092f8b320ae | 102 | } |
cassyarduino | 0:e3fb1267e3c3 | 103 | } |
cassyarduino | 0:e3fb1267e3c3 | 104 | udp.stop(); |
cassyarduino | 0:e3fb1267e3c3 | 105 | next = millis()+5000; |
cassyarduino | 0:e3fb1267e3c3 | 106 | } |
cassyarduino | 0:e3fb1267e3c3 | 107 | } |