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@12:aef29fbc4a62, 2016-12-29 (annotated)
- Committer:
- cassyarduino
- Date:
- Thu Dec 29 14:09:13 2016 +0100
- Revision:
- 12:aef29fbc4a62
- Parent:
- 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 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 | 11:3fb19220d9ec | 17 | #if defined(__MBED__) |
cassyarduino | 11:3fb19220d9ec | 18 | #include <mbed.h> |
cassyarduino | 11:3fb19220d9ec | 19 | #include "mbed/millis.h" |
cassyarduino | 11:3fb19220d9ec | 20 | #define delay(x) wait_ms(x) |
cassyarduino | 11:3fb19220d9ec | 21 | #define PROGMEM |
cassyarduino | 11:3fb19220d9ec | 22 | #include "mbed/Print.h" |
cassyarduino | 11:3fb19220d9ec | 23 | #endif |
cassyarduino | 11:3fb19220d9ec | 24 | |
cassyarduino | 0:e3fb1267e3c3 | 25 | #include <UIPEthernet.h> |
cassyarduino | 0:e3fb1267e3c3 | 26 | #include "utility/logging.h" |
cassyarduino | 0:e3fb1267e3c3 | 27 | |
cassyarduino | 0:e3fb1267e3c3 | 28 | EthernetUDP udp; |
cassyarduino | 0:e3fb1267e3c3 | 29 | |
cassyarduino | 11:3fb19220d9ec | 30 | #if defined(ARDUINO) |
cassyarduino | 0:e3fb1267e3c3 | 31 | void setup() { |
cassyarduino | 11:3fb19220d9ec | 32 | #endif |
cassyarduino | 11:3fb19220d9ec | 33 | #if defined(__MBED__) |
cassyarduino | 11:3fb19220d9ec | 34 | int main() { |
cassyarduino | 11:3fb19220d9ec | 35 | #endif |
cassyarduino | 0:e3fb1267e3c3 | 36 | #if ACTLOGLEVEL>LOG_NONE |
cassyarduino | 12:aef29fbc4a62 | 37 | #if defined(ARDUINO) |
cassyarduino | 12:aef29fbc4a62 | 38 | LogObject.begin(9600); |
cassyarduino | 12:aef29fbc4a62 | 39 | #endif |
cassyarduino | 12:aef29fbc4a62 | 40 | #if defined(__MBED__) |
cassyarduino | 12:aef29fbc4a62 | 41 | Serial LogObject(SERIAL_TX,SERIAL_RX); |
cassyarduino | 12:aef29fbc4a62 | 42 | #endif |
cassyarduino | 0:e3fb1267e3c3 | 43 | #endif |
cassyarduino | 0:e3fb1267e3c3 | 44 | |
cassyarduino | 0:e3fb1267e3c3 | 45 | uint8_t mac[6] = {0x00,0x01,0x02,0x03,0x04,0x05}; |
cassyarduino | 0:e3fb1267e3c3 | 46 | |
cassyarduino | 0:e3fb1267e3c3 | 47 | Ethernet.begin(mac,IPAddress(192,168,0,6)); |
cassyarduino | 0:e3fb1267e3c3 | 48 | |
cassyarduino | 0:e3fb1267e3c3 | 49 | int success = udp.begin(5000); |
cassyarduino | 0:e3fb1267e3c3 | 50 | |
cassyarduino | 0:e3fb1267e3c3 | 51 | #if ACTLOGLEVEL>=LOG_INFO |
cassyarduino | 0:e3fb1267e3c3 | 52 | LogObject.uart_send_str(F("initialize: ")); |
cassyarduino | 0:e3fb1267e3c3 | 53 | LogObject.uart_send_strln(success ? "success" : "failed"); |
cassyarduino | 0:e3fb1267e3c3 | 54 | #endif |
cassyarduino | 11:3fb19220d9ec | 55 | #if defined(ARDUINO) |
cassyarduino | 0:e3fb1267e3c3 | 56 | } |
cassyarduino | 0:e3fb1267e3c3 | 57 | |
cassyarduino | 0:e3fb1267e3c3 | 58 | void loop() { |
cassyarduino | 11:3fb19220d9ec | 59 | #endif |
cassyarduino | 0:e3fb1267e3c3 | 60 | |
cassyarduino | 11:3fb19220d9ec | 61 | #if defined(__MBED__) |
cassyarduino | 11:3fb19220d9ec | 62 | while(true) { |
cassyarduino | 11:3fb19220d9ec | 63 | #endif |
cassyarduino | 0:e3fb1267e3c3 | 64 | //check for new udp-packet: |
cassyarduino | 0:e3fb1267e3c3 | 65 | int size = udp.parsePacket(); |
cassyarduino | 0:e3fb1267e3c3 | 66 | if (size > 0) { |
cassyarduino | 0:e3fb1267e3c3 | 67 | do |
cassyarduino | 0:e3fb1267e3c3 | 68 | { |
cassyarduino | 0:e3fb1267e3c3 | 69 | char* msg = (char*)malloc(size+1); |
cassyarduino | 0:e3fb1267e3c3 | 70 | int len = udp.read(msg,size+1); |
cassyarduino | 0:e3fb1267e3c3 | 71 | msg[len]=0; |
cassyarduino | 0:e3fb1267e3c3 | 72 | #if ACTLOGLEVEL>=LOG_INFO |
cassyarduino | 0:e3fb1267e3c3 | 73 | LogObject.uart_send_str(F("received: '")); |
cassyarduino | 0:e3fb1267e3c3 | 74 | LogObject.uart_send_str(msg); |
cassyarduino | 0:e3fb1267e3c3 | 75 | #endif |
cassyarduino | 0:e3fb1267e3c3 | 76 | free(msg); |
cassyarduino | 0:e3fb1267e3c3 | 77 | } |
cassyarduino | 0:e3fb1267e3c3 | 78 | while ((size = udp.available())>0); |
cassyarduino | 0:e3fb1267e3c3 | 79 | //finish reading this packet: |
cassyarduino | 0:e3fb1267e3c3 | 80 | udp.flush(); |
cassyarduino | 0:e3fb1267e3c3 | 81 | #if ACTLOGLEVEL>=LOG_INFO |
cassyarduino | 0:e3fb1267e3c3 | 82 | LogObject.uart_send_strln(F("'")); |
cassyarduino | 0:e3fb1267e3c3 | 83 | #endif |
cassyarduino | 0:e3fb1267e3c3 | 84 | int success; |
cassyarduino | 0:e3fb1267e3c3 | 85 | do |
cassyarduino | 0:e3fb1267e3c3 | 86 | { |
cassyarduino | 0:e3fb1267e3c3 | 87 | #if ACTLOGLEVEL>=LOG_INFO |
cassyarduino | 0:e3fb1267e3c3 | 88 | LogObject.uart_send_str(F("remote ip: ")); |
cassyarduino | 12:aef29fbc4a62 | 89 | #if defined(ARDUINO) |
cassyarduino | 12:aef29fbc4a62 | 90 | LogObject.println(udp.remoteIP()); |
cassyarduino | 12:aef29fbc4a62 | 91 | #endif |
cassyarduino | 12:aef29fbc4a62 | 92 | #if defined(__MBED__) |
cassyarduino | 12:aef29fbc4a62 | 93 | LogObject.printf("%d.%d.%d.%d",udp.remoteIP()[0],udp.remoteIP()[1],udp.remoteIP()[2],udp.remoteIP()[3]); |
cassyarduino | 12:aef29fbc4a62 | 94 | #endif |
cassyarduino | 0:e3fb1267e3c3 | 95 | LogObject.uart_send_str(F("remote port: ")); |
cassyarduino | 12:aef29fbc4a62 | 96 | LogObject.uart_send_decln(udp.remotePort()); |
cassyarduino | 0:e3fb1267e3c3 | 97 | #endif |
cassyarduino | 0:e3fb1267e3c3 | 98 | //send new packet back to ip/port of client. This also |
cassyarduino | 0:e3fb1267e3c3 | 99 | //configures the current connection to ignore packets from |
cassyarduino | 0:e3fb1267e3c3 | 100 | //other clients! |
cassyarduino | 0:e3fb1267e3c3 | 101 | success = udp.beginPacket(udp.remoteIP(),udp.remotePort()); |
cassyarduino | 0:e3fb1267e3c3 | 102 | #if ACTLOGLEVEL>=LOG_INFO |
cassyarduino | 0:e3fb1267e3c3 | 103 | LogObject.uart_send_str(F("beginPacket: ")); |
cassyarduino | 0:e3fb1267e3c3 | 104 | LogObject.uart_send_strln(success ? "success" : "failed"); |
cassyarduino | 0:e3fb1267e3c3 | 105 | #endif |
cassyarduino | 0:e3fb1267e3c3 | 106 | //beginPacket fails if remote ethaddr is unknown. In this case an |
cassyarduino | 0:e3fb1267e3c3 | 107 | //arp-request is send out first and beginPacket succeeds as soon |
cassyarduino | 0:e3fb1267e3c3 | 108 | //the arp-response is received. |
cassyarduino | 0:e3fb1267e3c3 | 109 | } |
cassyarduino | 0:e3fb1267e3c3 | 110 | while (!success); |
cassyarduino | 0:e3fb1267e3c3 | 111 | |
cassyarduino | 0:e3fb1267e3c3 | 112 | success = udp.println(F("hello world from arduino")); |
cassyarduino | 0:e3fb1267e3c3 | 113 | |
cassyarduino | 0:e3fb1267e3c3 | 114 | #if ACTLOGLEVEL>=LOG_INFO |
cassyarduino | 0:e3fb1267e3c3 | 115 | LogObject.uart_send_str(F("bytes written: ")); |
cassyarduino | 0:e3fb1267e3c3 | 116 | LogObject.uart_send_decln(success); |
cassyarduino | 0:e3fb1267e3c3 | 117 | #endif |
cassyarduino | 0:e3fb1267e3c3 | 118 | |
cassyarduino | 0:e3fb1267e3c3 | 119 | success = udp.endPacket(); |
cassyarduino | 0:e3fb1267e3c3 | 120 | |
cassyarduino | 0:e3fb1267e3c3 | 121 | #if ACTLOGLEVEL>=LOG_INFO |
cassyarduino | 0:e3fb1267e3c3 | 122 | LogObject.uart_send_str(F("endPacket: ")); |
cassyarduino | 0:e3fb1267e3c3 | 123 | LogObject.uart_send_strln(success ? "success" : "failed"); |
cassyarduino | 0:e3fb1267e3c3 | 124 | #endif |
cassyarduino | 0:e3fb1267e3c3 | 125 | |
cassyarduino | 0:e3fb1267e3c3 | 126 | udp.stop(); |
cassyarduino | 0:e3fb1267e3c3 | 127 | //restart with new connection to receive packets from other clients |
cassyarduino | 0:e3fb1267e3c3 | 128 | #if ACTLOGLEVEL>=LOG_INFO |
cassyarduino | 0:e3fb1267e3c3 | 129 | LogObject.uart_send_str(F("restart connection: ")); |
cassyarduino | 0:e3fb1267e3c3 | 130 | LogObject.uart_send_strln(udp.begin(5000) ? "success" : "failed"); |
cassyarduino | 0:e3fb1267e3c3 | 131 | #endif |
cassyarduino | 0:e3fb1267e3c3 | 132 | } |
cassyarduino | 0:e3fb1267e3c3 | 133 | } |
cassyarduino | 11:3fb19220d9ec | 134 | #if defined(__MBED__) |
cassyarduino | 11:3fb19220d9ec | 135 | } |
cassyarduino | 11:3fb19220d9ec | 136 | #endif |