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:
Sat Jan 14 19:51:58 2017 +0100
Revision:
20:fe5026169ec6
Parent:
12:aef29fbc4a62
Child:
33:7ba5d53df0f2
Changes

Who changed what in which revision?

UserRevisionLine numberNew contents of line
cassyarduino 0:e3fb1267e3c3 1 /*
cassyarduino 0:e3fb1267e3c3 2 * UIPEthernet TcpClient 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 TcpClient example gets its local ip-address via dhcp and sets
cassyarduino 0:e3fb1267e3c3 12 * up a tcp socket-connection to 192.168.0.1 port 5000 every 5 Seconds.
cassyarduino 0:e3fb1267e3c3 13 * After sending a message it waits for a response. After receiving the
cassyarduino 0:e3fb1267e3c3 14 * response the client disconnects and tries to reconnect after 5 seconds.
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 EthernetClient client;
cassyarduino 0:e3fb1267e3c3 31 signed 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 12:aef29fbc4a62 40 #if defined(ARDUINO)
cassyarduino 12:aef29fbc4a62 41 LogObject.begin(9600);
cassyarduino 12:aef29fbc4a62 42 #endif
cassyarduino 12:aef29fbc4a62 43 #if defined(__MBED__)
cassyarduino 12:aef29fbc4a62 44 Serial LogObject(SERIAL_TX,SERIAL_RX);
cassyarduino 12:aef29fbc4a62 45 #endif
cassyarduino 0:e3fb1267e3c3 46 #endif
cassyarduino 0:e3fb1267e3c3 47
cassyarduino 0:e3fb1267e3c3 48 uint8_t mac[6] = {0x00,0x01,0x02,0x03,0x04,0x05};
cassyarduino 0:e3fb1267e3c3 49 Ethernet.begin(mac);
cassyarduino 0:e3fb1267e3c3 50
cassyarduino 0:e3fb1267e3c3 51 #if ACTLOGLEVEL>=LOG_INFO
cassyarduino 0:e3fb1267e3c3 52 LogObject.uart_send_str(F("localIP: "));
cassyarduino 12:aef29fbc4a62 53 #if defined(ARDUINO)
cassyarduino 12:aef29fbc4a62 54 LogObject.println(Ethernet.localIP());
cassyarduino 12:aef29fbc4a62 55 #endif
cassyarduino 12:aef29fbc4a62 56 #if defined(__MBED__)
cassyarduino 12:aef29fbc4a62 57 LogObject.printf("%d.%d.%d.%d",Ethernet.localIP()[0],Ethernet.localIP()[1],Ethernet.localIP()[2],Ethernet.localIP()[3]);
cassyarduino 12:aef29fbc4a62 58 #endif
cassyarduino 0:e3fb1267e3c3 59 LogObject.uart_send_str(F("subnetMask: "));
cassyarduino 12:aef29fbc4a62 60 #if defined(ARDUINO)
cassyarduino 12:aef29fbc4a62 61 LogObject.println(Ethernet.subnetMask());
cassyarduino 12:aef29fbc4a62 62 #endif
cassyarduino 12:aef29fbc4a62 63 #if defined(__MBED__)
cassyarduino 12:aef29fbc4a62 64 LogObject.printf("%d.%d.%d.%d",Ethernet.subnetMask()[0],Ethernet.subnetMask()[1],Ethernet.subnetMask()[2],Ethernet.subnetMask()[3]);
cassyarduino 12:aef29fbc4a62 65 #endif
cassyarduino 0:e3fb1267e3c3 66 LogObject.uart_send_str(F("gatewayIP: "));
cassyarduino 12:aef29fbc4a62 67 #if defined(ARDUINO)
cassyarduino 12:aef29fbc4a62 68 LogObject.println(Ethernet.gatewayIP());
cassyarduino 12:aef29fbc4a62 69 #endif
cassyarduino 12:aef29fbc4a62 70 #if defined(__MBED__)
cassyarduino 12:aef29fbc4a62 71 LogObject.printf("%d.%d.%d.%d",Ethernet.gatewayIP()[0],Ethernet.gatewayIP()[1],Ethernet.gatewayIP()[2],Ethernet.gatewayIP()[3]);
cassyarduino 12:aef29fbc4a62 72 #endif
cassyarduino 0:e3fb1267e3c3 73 LogObject.uart_send_str(F("dnsServerIP: "));
cassyarduino 12:aef29fbc4a62 74 #if defined(ARDUINO)
cassyarduino 12:aef29fbc4a62 75 LogObject.println(Ethernet.dnsServerIP());
cassyarduino 12:aef29fbc4a62 76 #endif
cassyarduino 12:aef29fbc4a62 77 #if defined(__MBED__)
cassyarduino 12:aef29fbc4a62 78 LogObject.printf("%d.%d.%d.%d",Ethernet.dnsServerIP()[0],Ethernet.dnsServerIP()[1],Ethernet.dnsServerIP()[2],Ethernet.dnsServerIP()[3]);
cassyarduino 12:aef29fbc4a62 79 #endif
cassyarduino 0:e3fb1267e3c3 80 #endif
cassyarduino 0:e3fb1267e3c3 81
cassyarduino 0:e3fb1267e3c3 82 next = 0;
cassyarduino 11:3fb19220d9ec 83 #if defined(ARDUINO)
cassyarduino 0:e3fb1267e3c3 84 }
cassyarduino 0:e3fb1267e3c3 85
cassyarduino 0:e3fb1267e3c3 86 void loop() {
cassyarduino 11:3fb19220d9ec 87 #endif
cassyarduino 0:e3fb1267e3c3 88
cassyarduino 11:3fb19220d9ec 89 #if defined(__MBED__)
cassyarduino 11:3fb19220d9ec 90 while(true) {
cassyarduino 11:3fb19220d9ec 91 #endif
cassyarduino 0:e3fb1267e3c3 92 if (((signed long)(millis() - next)) > 0)
cassyarduino 0:e3fb1267e3c3 93 {
cassyarduino 0:e3fb1267e3c3 94 next = millis() + 5000;
cassyarduino 0:e3fb1267e3c3 95 #if ACTLOGLEVEL>=LOG_INFO
cassyarduino 0:e3fb1267e3c3 96 LogObject.uart_send_strln(F("Client connect"));
cassyarduino 0:e3fb1267e3c3 97 #endif
cassyarduino 0:e3fb1267e3c3 98 // replace hostname with name of machine running tcpserver.pl
cassyarduino 0:e3fb1267e3c3 99 // if (client.connect("server.local",5000))
cassyarduino 0:e3fb1267e3c3 100 if (client.connect(IPAddress(192,168,0,1),5000))
cassyarduino 0:e3fb1267e3c3 101 {
cassyarduino 0:e3fb1267e3c3 102 #if ACTLOGLEVEL>=LOG_INFO
cassyarduino 0:e3fb1267e3c3 103 LogObject.uart_send_strln(F("Client connected"));
cassyarduino 0:e3fb1267e3c3 104 #endif
cassyarduino 0:e3fb1267e3c3 105 client.println(F("DATA from Client"));
cassyarduino 6:f092f8b320ae 106 int size;
cassyarduino 6:f092f8b320ae 107 while ((client.available()==0) && (millis()<next))
cassyarduino 0:e3fb1267e3c3 108 {
cassyarduino 6:f092f8b320ae 109 #if defined(ESP8266)
cassyarduino 6:f092f8b320ae 110 wdt_reset();
cassyarduino 6:f092f8b320ae 111 #endif
cassyarduino 0:e3fb1267e3c3 112 }
cassyarduino 0:e3fb1267e3c3 113 while((size = client.available()) > 0)
cassyarduino 0:e3fb1267e3c3 114 {
cassyarduino 12:aef29fbc4a62 115 uint8_t* msg = (uint8_t*)malloc(size+1);
cassyarduino 12:aef29fbc4a62 116 memset(msg, 0, size+1);
cassyarduino 0:e3fb1267e3c3 117 size = client.read(msg,size);
cassyarduino 0:e3fb1267e3c3 118 #if ACTLOGLEVEL>=LOG_INFO
cassyarduino 12:aef29fbc4a62 119 #if defined(ARDUINO)
cassyarduino 12:aef29fbc4a62 120 LogObject.write(msg,size);
cassyarduino 12:aef29fbc4a62 121 #endif
cassyarduino 12:aef29fbc4a62 122 #if defined(__MBED__)
cassyarduino 12:aef29fbc4a62 123 LogObject.uart_send_str(msg);
cassyarduino 12:aef29fbc4a62 124 #endif
cassyarduino 0:e3fb1267e3c3 125 #endif
cassyarduino 0:e3fb1267e3c3 126 free(msg);
cassyarduino 0:e3fb1267e3c3 127 }
cassyarduino 0:e3fb1267e3c3 128 //disconnect client
cassyarduino 0:e3fb1267e3c3 129 #if ACTLOGLEVEL>=LOG_INFO
cassyarduino 0:e3fb1267e3c3 130 LogObject.uart_send_strln(F("Client disconnect"));
cassyarduino 0:e3fb1267e3c3 131 #endif
cassyarduino 0:e3fb1267e3c3 132 client.stop();
cassyarduino 0:e3fb1267e3c3 133 }
cassyarduino 0:e3fb1267e3c3 134 else
cassyarduino 20:fe5026169ec6 135 {
cassyarduino 0:e3fb1267e3c3 136 #if ACTLOGLEVEL>=LOG_INFO
cassyarduino 0:e3fb1267e3c3 137 LogObject.uart_send_strln(F("Client connect failed"));
cassyarduino 0:e3fb1267e3c3 138 #endif
cassyarduino 20:fe5026169ec6 139 }
cassyarduino 0:e3fb1267e3c3 140 }
cassyarduino 0:e3fb1267e3c3 141 }
cassyarduino 11:3fb19220d9ec 142 #if defined(__MBED__)
cassyarduino 11:3fb19220d9ec 143 }
cassyarduino 11:3fb19220d9ec 144 #endif