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/TcpClient/TcpClient.ino@6:f092f8b320ae, 2016-12-27 (annotated)
- Committer:
- cassyarduino
- Date:
- Tue Dec 27 13:45:01 2016 +0100
- Revision:
- 6:f092f8b320ae
- Parent:
- 3:6b1c9bd7773a
- 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 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 | 0:e3fb1267e3c3 | 19 | #include <UIPEthernet.h> |
cassyarduino | 0:e3fb1267e3c3 | 20 | #include "utility/logging.h" |
cassyarduino | 0:e3fb1267e3c3 | 21 | |
cassyarduino | 0:e3fb1267e3c3 | 22 | EthernetClient client; |
cassyarduino | 0:e3fb1267e3c3 | 23 | signed 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 | Ethernet.begin(mac); |
cassyarduino | 0:e3fb1267e3c3 | 33 | |
cassyarduino | 0:e3fb1267e3c3 | 34 | #if ACTLOGLEVEL>=LOG_INFO |
cassyarduino | 0:e3fb1267e3c3 | 35 | LogObject.uart_send_str(F("localIP: ")); |
cassyarduino | 0:e3fb1267e3c3 | 36 | LogObject.println(Ethernet.localIP()); |
cassyarduino | 0:e3fb1267e3c3 | 37 | LogObject.uart_send_str(F("subnetMask: ")); |
cassyarduino | 0:e3fb1267e3c3 | 38 | LogObject.println(Ethernet.subnetMask()); |
cassyarduino | 0:e3fb1267e3c3 | 39 | LogObject.uart_send_str(F("gatewayIP: ")); |
cassyarduino | 0:e3fb1267e3c3 | 40 | LogObject.println(Ethernet.gatewayIP()); |
cassyarduino | 0:e3fb1267e3c3 | 41 | LogObject.uart_send_str(F("dnsServerIP: ")); |
cassyarduino | 0:e3fb1267e3c3 | 42 | LogObject.println(Ethernet.dnsServerIP()); |
cassyarduino | 0:e3fb1267e3c3 | 43 | #endif |
cassyarduino | 0:e3fb1267e3c3 | 44 | |
cassyarduino | 0:e3fb1267e3c3 | 45 | next = 0; |
cassyarduino | 0:e3fb1267e3c3 | 46 | } |
cassyarduino | 0:e3fb1267e3c3 | 47 | |
cassyarduino | 0:e3fb1267e3c3 | 48 | void loop() { |
cassyarduino | 0:e3fb1267e3c3 | 49 | |
cassyarduino | 0:e3fb1267e3c3 | 50 | if (((signed long)(millis() - next)) > 0) |
cassyarduino | 0:e3fb1267e3c3 | 51 | { |
cassyarduino | 0:e3fb1267e3c3 | 52 | next = millis() + 5000; |
cassyarduino | 0:e3fb1267e3c3 | 53 | #if ACTLOGLEVEL>=LOG_INFO |
cassyarduino | 0:e3fb1267e3c3 | 54 | LogObject.uart_send_strln(F("Client connect")); |
cassyarduino | 0:e3fb1267e3c3 | 55 | #endif |
cassyarduino | 0:e3fb1267e3c3 | 56 | // replace hostname with name of machine running tcpserver.pl |
cassyarduino | 0:e3fb1267e3c3 | 57 | // if (client.connect("server.local",5000)) |
cassyarduino | 0:e3fb1267e3c3 | 58 | if (client.connect(IPAddress(192,168,0,1),5000)) |
cassyarduino | 0:e3fb1267e3c3 | 59 | { |
cassyarduino | 0:e3fb1267e3c3 | 60 | #if ACTLOGLEVEL>=LOG_INFO |
cassyarduino | 0:e3fb1267e3c3 | 61 | LogObject.uart_send_strln(F("Client connected")); |
cassyarduino | 0:e3fb1267e3c3 | 62 | #endif |
cassyarduino | 0:e3fb1267e3c3 | 63 | client.println(F("DATA from Client")); |
cassyarduino | 6:f092f8b320ae | 64 | int size; |
cassyarduino | 6:f092f8b320ae | 65 | while ((client.available()==0) && (millis()<next)) |
cassyarduino | 0:e3fb1267e3c3 | 66 | { |
cassyarduino | 6:f092f8b320ae | 67 | #if defined(ESP8266) |
cassyarduino | 6:f092f8b320ae | 68 | wdt_reset(); |
cassyarduino | 6:f092f8b320ae | 69 | #endif |
cassyarduino | 0:e3fb1267e3c3 | 70 | } |
cassyarduino | 0:e3fb1267e3c3 | 71 | while((size = client.available()) > 0) |
cassyarduino | 0:e3fb1267e3c3 | 72 | { |
cassyarduino | 0:e3fb1267e3c3 | 73 | uint8_t* msg = (uint8_t*)malloc(size); |
cassyarduino | 0:e3fb1267e3c3 | 74 | size = client.read(msg,size); |
cassyarduino | 0:e3fb1267e3c3 | 75 | #if ACTLOGLEVEL>=LOG_INFO |
cassyarduino | 0:e3fb1267e3c3 | 76 | LogObject.write(msg,size); |
cassyarduino | 0:e3fb1267e3c3 | 77 | #endif |
cassyarduino | 0:e3fb1267e3c3 | 78 | free(msg); |
cassyarduino | 0:e3fb1267e3c3 | 79 | } |
cassyarduino | 0:e3fb1267e3c3 | 80 | //disconnect client |
cassyarduino | 0:e3fb1267e3c3 | 81 | #if ACTLOGLEVEL>=LOG_INFO |
cassyarduino | 0:e3fb1267e3c3 | 82 | LogObject.uart_send_strln(F("Client disconnect")); |
cassyarduino | 0:e3fb1267e3c3 | 83 | #endif |
cassyarduino | 0:e3fb1267e3c3 | 84 | client.stop(); |
cassyarduino | 0:e3fb1267e3c3 | 85 | } |
cassyarduino | 0:e3fb1267e3c3 | 86 | else |
cassyarduino | 0:e3fb1267e3c3 | 87 | #if ACTLOGLEVEL>=LOG_INFO |
cassyarduino | 0:e3fb1267e3c3 | 88 | LogObject.uart_send_strln(F("Client connect failed")); |
cassyarduino | 0:e3fb1267e3c3 | 89 | #endif |
cassyarduino | 0:e3fb1267e3c3 | 90 | } |
cassyarduino | 0:e3fb1267e3c3 | 91 | } |