From 8cec66b23ba3b78e9abec9cd918b1b9cced5266c Mon Sep 17 00:00:00 2001 From: Markus Sattler Date: Fri, 4 Dec 2015 18:16:05 +0100 Subject: [PATCH] add a simple TCP example --- .../examples/WiFiClientMin/WiFiClientMin.ino | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 libraries/ESP8266WiFi/examples/WiFiClientMin/WiFiClientMin.ino diff --git a/libraries/ESP8266WiFi/examples/WiFiClientMin/WiFiClientMin.ino b/libraries/ESP8266WiFi/examples/WiFiClientMin/WiFiClientMin.ino new file mode 100644 index 000000000..9386362ff --- /dev/null +++ b/libraries/ESP8266WiFi/examples/WiFiClientMin/WiFiClientMin.ino @@ -0,0 +1,68 @@ +/* + * This sketch sends a message to a TCP server + * + */ + +#include +#include + +ESP8266WiFiMulti WiFiMulti; + +void setup() { + Serial.begin(115200); + delay(10); + + // We start by connecting to a WiFi network + WiFiMulti.addAP("SSID", "passpasspass"); + + Serial.println(); + Serial.println(); + Serial.print("Wait for WiFi... "); + + while(WiFiMulti.run() != WL_CONNECTED) { + Serial.print("."); + delay(500); + } + + Serial.println(""); + Serial.println("WiFi connected"); + Serial.println("IP address: "); + Serial.println(WiFi.localIP()); + + delay(500); +} + + +void loop() { + const uint16_t port = 80; + const char * host = "192.168.1.1"; // ip or dns + + + + Serial.print("connecting to "); + Serial.println(host); + + // Use WiFiClient class to create TCP connections + WiFiClient client; + + if (!client.connect(host, port)) { + Serial.println("connection failed"); + Serial.println("wait 5 sec..."); + delay(5000); + return; + } + + // This will send the request to the server + client.print("Send this data to server"); + + //read back one line from server + String line = client.readStringUntil('\r'); + client.println(line); + + Serial.println("closing connection"); + client.stop(); + + Serial.println("wait 5 sec..."); + delay(5000); +} +