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:
Thu Dec 29 13:07:19 2016 +0100
Revision:
11:3fb19220d9ec
Parent:
0:e3fb1267e3c3
Child:
12:aef29fbc4a62
Changes

Who changed what in which revision?

UserRevisionLine numberNew 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 0:e3fb1267e3c3 37 LogObject.begin(9600);
cassyarduino 0:e3fb1267e3c3 38 #endif
cassyarduino 0:e3fb1267e3c3 39
cassyarduino 0:e3fb1267e3c3 40 uint8_t mac[6] = {0x00,0x01,0x02,0x03,0x04,0x05};
cassyarduino 0:e3fb1267e3c3 41
cassyarduino 0:e3fb1267e3c3 42 Ethernet.begin(mac,IPAddress(192,168,0,6));
cassyarduino 0:e3fb1267e3c3 43
cassyarduino 0:e3fb1267e3c3 44 int success = udp.begin(5000);
cassyarduino 0:e3fb1267e3c3 45
cassyarduino 0:e3fb1267e3c3 46 #if ACTLOGLEVEL>=LOG_INFO
cassyarduino 0:e3fb1267e3c3 47 LogObject.uart_send_str(F("initialize: "));
cassyarduino 0:e3fb1267e3c3 48 LogObject.uart_send_strln(success ? "success" : "failed");
cassyarduino 0:e3fb1267e3c3 49 #endif
cassyarduino 11:3fb19220d9ec 50 #if defined(ARDUINO)
cassyarduino 0:e3fb1267e3c3 51 }
cassyarduino 0:e3fb1267e3c3 52
cassyarduino 0:e3fb1267e3c3 53 void loop() {
cassyarduino 11:3fb19220d9ec 54 #endif
cassyarduino 0:e3fb1267e3c3 55
cassyarduino 11:3fb19220d9ec 56 #if defined(__MBED__)
cassyarduino 11:3fb19220d9ec 57 while(true) {
cassyarduino 11:3fb19220d9ec 58 #endif
cassyarduino 0:e3fb1267e3c3 59 //check for new udp-packet:
cassyarduino 0:e3fb1267e3c3 60 int size = udp.parsePacket();
cassyarduino 0:e3fb1267e3c3 61 if (size > 0) {
cassyarduino 0:e3fb1267e3c3 62 do
cassyarduino 0:e3fb1267e3c3 63 {
cassyarduino 0:e3fb1267e3c3 64 char* msg = (char*)malloc(size+1);
cassyarduino 0:e3fb1267e3c3 65 int len = udp.read(msg,size+1);
cassyarduino 0:e3fb1267e3c3 66 msg[len]=0;
cassyarduino 0:e3fb1267e3c3 67 #if ACTLOGLEVEL>=LOG_INFO
cassyarduino 0:e3fb1267e3c3 68 LogObject.uart_send_str(F("received: '"));
cassyarduino 0:e3fb1267e3c3 69 LogObject.uart_send_str(msg);
cassyarduino 0:e3fb1267e3c3 70 #endif
cassyarduino 0:e3fb1267e3c3 71 free(msg);
cassyarduino 0:e3fb1267e3c3 72 }
cassyarduino 0:e3fb1267e3c3 73 while ((size = udp.available())>0);
cassyarduino 0:e3fb1267e3c3 74 //finish reading this packet:
cassyarduino 0:e3fb1267e3c3 75 udp.flush();
cassyarduino 0:e3fb1267e3c3 76 #if ACTLOGLEVEL>=LOG_INFO
cassyarduino 0:e3fb1267e3c3 77 LogObject.uart_send_strln(F("'"));
cassyarduino 0:e3fb1267e3c3 78 #endif
cassyarduino 0:e3fb1267e3c3 79 int success;
cassyarduino 0:e3fb1267e3c3 80 do
cassyarduino 0:e3fb1267e3c3 81 {
cassyarduino 0:e3fb1267e3c3 82 #if ACTLOGLEVEL>=LOG_INFO
cassyarduino 0:e3fb1267e3c3 83 LogObject.uart_send_str(F("remote ip: "));
cassyarduino 0:e3fb1267e3c3 84 LogObject.println(udp.remoteIP());
cassyarduino 0:e3fb1267e3c3 85 LogObject.uart_send_str(F("remote port: "));
cassyarduino 0:e3fb1267e3c3 86 LogObject.println(udp.remotePort());
cassyarduino 0:e3fb1267e3c3 87 #endif
cassyarduino 0:e3fb1267e3c3 88 //send new packet back to ip/port of client. This also
cassyarduino 0:e3fb1267e3c3 89 //configures the current connection to ignore packets from
cassyarduino 0:e3fb1267e3c3 90 //other clients!
cassyarduino 0:e3fb1267e3c3 91 success = udp.beginPacket(udp.remoteIP(),udp.remotePort());
cassyarduino 0:e3fb1267e3c3 92 #if ACTLOGLEVEL>=LOG_INFO
cassyarduino 0:e3fb1267e3c3 93 LogObject.uart_send_str(F("beginPacket: "));
cassyarduino 0:e3fb1267e3c3 94 LogObject.uart_send_strln(success ? "success" : "failed");
cassyarduino 0:e3fb1267e3c3 95 #endif
cassyarduino 0:e3fb1267e3c3 96 //beginPacket fails if remote ethaddr is unknown. In this case an
cassyarduino 0:e3fb1267e3c3 97 //arp-request is send out first and beginPacket succeeds as soon
cassyarduino 0:e3fb1267e3c3 98 //the arp-response is received.
cassyarduino 0:e3fb1267e3c3 99 }
cassyarduino 0:e3fb1267e3c3 100 while (!success);
cassyarduino 0:e3fb1267e3c3 101
cassyarduino 0:e3fb1267e3c3 102 success = udp.println(F("hello world from arduino"));
cassyarduino 0:e3fb1267e3c3 103
cassyarduino 0:e3fb1267e3c3 104 #if ACTLOGLEVEL>=LOG_INFO
cassyarduino 0:e3fb1267e3c3 105 LogObject.uart_send_str(F("bytes written: "));
cassyarduino 0:e3fb1267e3c3 106 LogObject.uart_send_decln(success);
cassyarduino 0:e3fb1267e3c3 107 #endif
cassyarduino 0:e3fb1267e3c3 108
cassyarduino 0:e3fb1267e3c3 109 success = udp.endPacket();
cassyarduino 0:e3fb1267e3c3 110
cassyarduino 0:e3fb1267e3c3 111 #if ACTLOGLEVEL>=LOG_INFO
cassyarduino 0:e3fb1267e3c3 112 LogObject.uart_send_str(F("endPacket: "));
cassyarduino 0:e3fb1267e3c3 113 LogObject.uart_send_strln(success ? "success" : "failed");
cassyarduino 0:e3fb1267e3c3 114 #endif
cassyarduino 0:e3fb1267e3c3 115
cassyarduino 0:e3fb1267e3c3 116 udp.stop();
cassyarduino 0:e3fb1267e3c3 117 //restart with new connection to receive packets from other clients
cassyarduino 0:e3fb1267e3c3 118 #if ACTLOGLEVEL>=LOG_INFO
cassyarduino 0:e3fb1267e3c3 119 LogObject.uart_send_str(F("restart connection: "));
cassyarduino 0:e3fb1267e3c3 120 LogObject.uart_send_strln(udp.begin(5000) ? "success" : "failed");
cassyarduino 0:e3fb1267e3c3 121 #endif
cassyarduino 0:e3fb1267e3c3 122 }
cassyarduino 0:e3fb1267e3c3 123 }
cassyarduino 11:3fb19220d9ec 124 #if defined(__MBED__)
cassyarduino 11:3fb19220d9ec 125 }
cassyarduino 11:3fb19220d9ec 126 #endif