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/UdpServer/UdpServer.ino@0:e3fb1267e3c3, 2016-12-21 (annotated)
- Committer:
- cassyarduino
- Date:
- Wed Dec 21 16:58:10 2016 +0100
- Revision:
- 0:e3fb1267e3c3
- Child:
- 11:3fb19220d9ec
initial release
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
cassyarduino | 0:e3fb1267e3c3 | 1 | /* |
cassyarduino | 0:e3fb1267e3c3 | 2 | * UIPEthernet UdpServer 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 UdpServer example sets up a udp-server at 192.168.0.6 on port 5000. |
cassyarduino | 0:e3fb1267e3c3 | 12 | * send packet via upd to test |
cassyarduino | 0:e3fb1267e3c3 | 13 | * |
cassyarduino | 0:e3fb1267e3c3 | 14 | * Copyright (C) 2013 by Norbert Truchsess (norbert.truchsess@t-online.de) |
cassyarduino | 0:e3fb1267e3c3 | 15 | */ |
cassyarduino | 0:e3fb1267e3c3 | 16 | |
cassyarduino | 0:e3fb1267e3c3 | 17 | #include <UIPEthernet.h> |
cassyarduino | 0:e3fb1267e3c3 | 18 | #include "utility/logging.h" |
cassyarduino | 0:e3fb1267e3c3 | 19 | |
cassyarduino | 0:e3fb1267e3c3 | 20 | EthernetUDP udp; |
cassyarduino | 0:e3fb1267e3c3 | 21 | |
cassyarduino | 0:e3fb1267e3c3 | 22 | void setup() { |
cassyarduino | 0:e3fb1267e3c3 | 23 | |
cassyarduino | 0:e3fb1267e3c3 | 24 | #if ACTLOGLEVEL>LOG_NONE |
cassyarduino | 0:e3fb1267e3c3 | 25 | LogObject.begin(9600); |
cassyarduino | 0:e3fb1267e3c3 | 26 | #endif |
cassyarduino | 0:e3fb1267e3c3 | 27 | |
cassyarduino | 0:e3fb1267e3c3 | 28 | uint8_t mac[6] = {0x00,0x01,0x02,0x03,0x04,0x05}; |
cassyarduino | 0:e3fb1267e3c3 | 29 | |
cassyarduino | 0:e3fb1267e3c3 | 30 | Ethernet.begin(mac,IPAddress(192,168,0,6)); |
cassyarduino | 0:e3fb1267e3c3 | 31 | |
cassyarduino | 0:e3fb1267e3c3 | 32 | int success = udp.begin(5000); |
cassyarduino | 0:e3fb1267e3c3 | 33 | |
cassyarduino | 0:e3fb1267e3c3 | 34 | #if ACTLOGLEVEL>=LOG_INFO |
cassyarduino | 0:e3fb1267e3c3 | 35 | LogObject.uart_send_str(F("initialize: ")); |
cassyarduino | 0:e3fb1267e3c3 | 36 | LogObject.uart_send_strln(success ? "success" : "failed"); |
cassyarduino | 0:e3fb1267e3c3 | 37 | #endif |
cassyarduino | 0:e3fb1267e3c3 | 38 | |
cassyarduino | 0:e3fb1267e3c3 | 39 | } |
cassyarduino | 0:e3fb1267e3c3 | 40 | |
cassyarduino | 0:e3fb1267e3c3 | 41 | void loop() { |
cassyarduino | 0:e3fb1267e3c3 | 42 | |
cassyarduino | 0:e3fb1267e3c3 | 43 | //check for new udp-packet: |
cassyarduino | 0:e3fb1267e3c3 | 44 | int size = udp.parsePacket(); |
cassyarduino | 0:e3fb1267e3c3 | 45 | if (size > 0) { |
cassyarduino | 0:e3fb1267e3c3 | 46 | do |
cassyarduino | 0:e3fb1267e3c3 | 47 | { |
cassyarduino | 0:e3fb1267e3c3 | 48 | char* msg = (char*)malloc(size+1); |
cassyarduino | 0:e3fb1267e3c3 | 49 | int len = udp.read(msg,size+1); |
cassyarduino | 0:e3fb1267e3c3 | 50 | msg[len]=0; |
cassyarduino | 0:e3fb1267e3c3 | 51 | #if ACTLOGLEVEL>=LOG_INFO |
cassyarduino | 0:e3fb1267e3c3 | 52 | LogObject.uart_send_str(F("received: '")); |
cassyarduino | 0:e3fb1267e3c3 | 53 | LogObject.uart_send_str(msg); |
cassyarduino | 0:e3fb1267e3c3 | 54 | #endif |
cassyarduino | 0:e3fb1267e3c3 | 55 | free(msg); |
cassyarduino | 0:e3fb1267e3c3 | 56 | } |
cassyarduino | 0:e3fb1267e3c3 | 57 | while ((size = udp.available())>0); |
cassyarduino | 0:e3fb1267e3c3 | 58 | //finish reading this packet: |
cassyarduino | 0:e3fb1267e3c3 | 59 | udp.flush(); |
cassyarduino | 0:e3fb1267e3c3 | 60 | #if ACTLOGLEVEL>=LOG_INFO |
cassyarduino | 0:e3fb1267e3c3 | 61 | LogObject.uart_send_strln(F("'")); |
cassyarduino | 0:e3fb1267e3c3 | 62 | #endif |
cassyarduino | 0:e3fb1267e3c3 | 63 | int success; |
cassyarduino | 0:e3fb1267e3c3 | 64 | do |
cassyarduino | 0:e3fb1267e3c3 | 65 | { |
cassyarduino | 0:e3fb1267e3c3 | 66 | #if ACTLOGLEVEL>=LOG_INFO |
cassyarduino | 0:e3fb1267e3c3 | 67 | LogObject.uart_send_str(F("remote ip: ")); |
cassyarduino | 0:e3fb1267e3c3 | 68 | LogObject.println(udp.remoteIP()); |
cassyarduino | 0:e3fb1267e3c3 | 69 | LogObject.uart_send_str(F("remote port: ")); |
cassyarduino | 0:e3fb1267e3c3 | 70 | LogObject.println(udp.remotePort()); |
cassyarduino | 0:e3fb1267e3c3 | 71 | #endif |
cassyarduino | 0:e3fb1267e3c3 | 72 | //send new packet back to ip/port of client. This also |
cassyarduino | 0:e3fb1267e3c3 | 73 | //configures the current connection to ignore packets from |
cassyarduino | 0:e3fb1267e3c3 | 74 | //other clients! |
cassyarduino | 0:e3fb1267e3c3 | 75 | success = udp.beginPacket(udp.remoteIP(),udp.remotePort()); |
cassyarduino | 0:e3fb1267e3c3 | 76 | #if ACTLOGLEVEL>=LOG_INFO |
cassyarduino | 0:e3fb1267e3c3 | 77 | LogObject.uart_send_str(F("beginPacket: ")); |
cassyarduino | 0:e3fb1267e3c3 | 78 | LogObject.uart_send_strln(success ? "success" : "failed"); |
cassyarduino | 0:e3fb1267e3c3 | 79 | #endif |
cassyarduino | 0:e3fb1267e3c3 | 80 | //beginPacket fails if remote ethaddr is unknown. In this case an |
cassyarduino | 0:e3fb1267e3c3 | 81 | //arp-request is send out first and beginPacket succeeds as soon |
cassyarduino | 0:e3fb1267e3c3 | 82 | //the arp-response is received. |
cassyarduino | 0:e3fb1267e3c3 | 83 | } |
cassyarduino | 0:e3fb1267e3c3 | 84 | while (!success); |
cassyarduino | 0:e3fb1267e3c3 | 85 | |
cassyarduino | 0:e3fb1267e3c3 | 86 | success = udp.println(F("hello world from arduino")); |
cassyarduino | 0:e3fb1267e3c3 | 87 | |
cassyarduino | 0:e3fb1267e3c3 | 88 | #if ACTLOGLEVEL>=LOG_INFO |
cassyarduino | 0:e3fb1267e3c3 | 89 | LogObject.uart_send_str(F("bytes written: ")); |
cassyarduino | 0:e3fb1267e3c3 | 90 | LogObject.uart_send_decln(success); |
cassyarduino | 0:e3fb1267e3c3 | 91 | #endif |
cassyarduino | 0:e3fb1267e3c3 | 92 | |
cassyarduino | 0:e3fb1267e3c3 | 93 | success = udp.endPacket(); |
cassyarduino | 0:e3fb1267e3c3 | 94 | |
cassyarduino | 0:e3fb1267e3c3 | 95 | #if ACTLOGLEVEL>=LOG_INFO |
cassyarduino | 0:e3fb1267e3c3 | 96 | LogObject.uart_send_str(F("endPacket: ")); |
cassyarduino | 0:e3fb1267e3c3 | 97 | LogObject.uart_send_strln(success ? "success" : "failed"); |
cassyarduino | 0:e3fb1267e3c3 | 98 | #endif |
cassyarduino | 0:e3fb1267e3c3 | 99 | |
cassyarduino | 0:e3fb1267e3c3 | 100 | udp.stop(); |
cassyarduino | 0:e3fb1267e3c3 | 101 | //restart with new connection to receive packets from other clients |
cassyarduino | 0:e3fb1267e3c3 | 102 | #if ACTLOGLEVEL>=LOG_INFO |
cassyarduino | 0:e3fb1267e3c3 | 103 | LogObject.uart_send_str(F("restart connection: ")); |
cassyarduino | 0:e3fb1267e3c3 | 104 | LogObject.uart_send_strln(udp.begin(5000) ? "success" : "failed"); |
cassyarduino | 0:e3fb1267e3c3 | 105 | #endif |
cassyarduino | 0:e3fb1267e3c3 | 106 | } |
cassyarduino | 0:e3fb1267e3c3 | 107 | } |