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@11:3fb19220d9ec, 2016-12-29 (annotated)
- Committer:
- cassyarduino
- Date:
- Thu Dec 29 13:07:19 2016 +0100
- Revision:
- 11:3fb19220d9ec
- Parent:
- 6:f092f8b320ae
- Child:
- 12:aef29fbc4a62
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 | 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 | 0:e3fb1267e3c3 | 40 | LogObject.begin(9600); |
cassyarduino | 0:e3fb1267e3c3 | 41 | #endif |
cassyarduino | 0:e3fb1267e3c3 | 42 | |
cassyarduino | 0:e3fb1267e3c3 | 43 | uint8_t mac[6] = {0x00,0x01,0x02,0x03,0x04,0x05}; |
cassyarduino | 0:e3fb1267e3c3 | 44 | Ethernet.begin(mac); |
cassyarduino | 0:e3fb1267e3c3 | 45 | |
cassyarduino | 0:e3fb1267e3c3 | 46 | #if ACTLOGLEVEL>=LOG_INFO |
cassyarduino | 0:e3fb1267e3c3 | 47 | LogObject.uart_send_str(F("localIP: ")); |
cassyarduino | 0:e3fb1267e3c3 | 48 | LogObject.println(Ethernet.localIP()); |
cassyarduino | 0:e3fb1267e3c3 | 49 | LogObject.uart_send_str(F("subnetMask: ")); |
cassyarduino | 0:e3fb1267e3c3 | 50 | LogObject.println(Ethernet.subnetMask()); |
cassyarduino | 0:e3fb1267e3c3 | 51 | LogObject.uart_send_str(F("gatewayIP: ")); |
cassyarduino | 0:e3fb1267e3c3 | 52 | LogObject.println(Ethernet.gatewayIP()); |
cassyarduino | 0:e3fb1267e3c3 | 53 | LogObject.uart_send_str(F("dnsServerIP: ")); |
cassyarduino | 0:e3fb1267e3c3 | 54 | LogObject.println(Ethernet.dnsServerIP()); |
cassyarduino | 0:e3fb1267e3c3 | 55 | #endif |
cassyarduino | 0:e3fb1267e3c3 | 56 | |
cassyarduino | 0:e3fb1267e3c3 | 57 | next = 0; |
cassyarduino | 11:3fb19220d9ec | 58 | #if defined(ARDUINO) |
cassyarduino | 0:e3fb1267e3c3 | 59 | } |
cassyarduino | 0:e3fb1267e3c3 | 60 | |
cassyarduino | 0:e3fb1267e3c3 | 61 | void loop() { |
cassyarduino | 11:3fb19220d9ec | 62 | #endif |
cassyarduino | 0:e3fb1267e3c3 | 63 | |
cassyarduino | 11:3fb19220d9ec | 64 | #if defined(__MBED__) |
cassyarduino | 11:3fb19220d9ec | 65 | while(true) { |
cassyarduino | 11:3fb19220d9ec | 66 | #endif |
cassyarduino | 0:e3fb1267e3c3 | 67 | if (((signed long)(millis() - next)) > 0) |
cassyarduino | 0:e3fb1267e3c3 | 68 | { |
cassyarduino | 0:e3fb1267e3c3 | 69 | next = millis() + 5000; |
cassyarduino | 0:e3fb1267e3c3 | 70 | #if ACTLOGLEVEL>=LOG_INFO |
cassyarduino | 0:e3fb1267e3c3 | 71 | LogObject.uart_send_strln(F("Client connect")); |
cassyarduino | 0:e3fb1267e3c3 | 72 | #endif |
cassyarduino | 0:e3fb1267e3c3 | 73 | // replace hostname with name of machine running tcpserver.pl |
cassyarduino | 0:e3fb1267e3c3 | 74 | // if (client.connect("server.local",5000)) |
cassyarduino | 0:e3fb1267e3c3 | 75 | if (client.connect(IPAddress(192,168,0,1),5000)) |
cassyarduino | 0:e3fb1267e3c3 | 76 | { |
cassyarduino | 0:e3fb1267e3c3 | 77 | #if ACTLOGLEVEL>=LOG_INFO |
cassyarduino | 0:e3fb1267e3c3 | 78 | LogObject.uart_send_strln(F("Client connected")); |
cassyarduino | 0:e3fb1267e3c3 | 79 | #endif |
cassyarduino | 0:e3fb1267e3c3 | 80 | client.println(F("DATA from Client")); |
cassyarduino | 6:f092f8b320ae | 81 | int size; |
cassyarduino | 6:f092f8b320ae | 82 | while ((client.available()==0) && (millis()<next)) |
cassyarduino | 0:e3fb1267e3c3 | 83 | { |
cassyarduino | 6:f092f8b320ae | 84 | #if defined(ESP8266) |
cassyarduino | 6:f092f8b320ae | 85 | wdt_reset(); |
cassyarduino | 6:f092f8b320ae | 86 | #endif |
cassyarduino | 0:e3fb1267e3c3 | 87 | } |
cassyarduino | 0:e3fb1267e3c3 | 88 | while((size = client.available()) > 0) |
cassyarduino | 0:e3fb1267e3c3 | 89 | { |
cassyarduino | 0:e3fb1267e3c3 | 90 | uint8_t* msg = (uint8_t*)malloc(size); |
cassyarduino | 0:e3fb1267e3c3 | 91 | size = client.read(msg,size); |
cassyarduino | 0:e3fb1267e3c3 | 92 | #if ACTLOGLEVEL>=LOG_INFO |
cassyarduino | 0:e3fb1267e3c3 | 93 | LogObject.write(msg,size); |
cassyarduino | 0:e3fb1267e3c3 | 94 | #endif |
cassyarduino | 0:e3fb1267e3c3 | 95 | free(msg); |
cassyarduino | 0:e3fb1267e3c3 | 96 | } |
cassyarduino | 0:e3fb1267e3c3 | 97 | //disconnect client |
cassyarduino | 0:e3fb1267e3c3 | 98 | #if ACTLOGLEVEL>=LOG_INFO |
cassyarduino | 0:e3fb1267e3c3 | 99 | LogObject.uart_send_strln(F("Client disconnect")); |
cassyarduino | 0:e3fb1267e3c3 | 100 | #endif |
cassyarduino | 0:e3fb1267e3c3 | 101 | client.stop(); |
cassyarduino | 0:e3fb1267e3c3 | 102 | } |
cassyarduino | 0:e3fb1267e3c3 | 103 | else |
cassyarduino | 0:e3fb1267e3c3 | 104 | #if ACTLOGLEVEL>=LOG_INFO |
cassyarduino | 0:e3fb1267e3c3 | 105 | LogObject.uart_send_strln(F("Client connect failed")); |
cassyarduino | 0:e3fb1267e3c3 | 106 | #endif |
cassyarduino | 0:e3fb1267e3c3 | 107 | } |
cassyarduino | 0:e3fb1267e3c3 | 108 | } |
cassyarduino | 11:3fb19220d9ec | 109 | #if defined(__MBED__) |
cassyarduino | 11:3fb19220d9ec | 110 | } |
cassyarduino | 11:3fb19220d9ec | 111 | #endif |