From 5ed6741e4a7d6a56b76dbdebd9be4f5131e32cf2 Mon Sep 17 00:00:00 2001 From: Filip Date: Wed, 29 Jan 2020 20:04:49 +0100 Subject: [PATCH] first commit --- WiFiClient_test_esp.ino | 87 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 WiFiClient_test_esp.ino diff --git a/WiFiClient_test_esp.ino b/WiFiClient_test_esp.ino new file mode 100644 index 0000000..33182ed --- /dev/null +++ b/WiFiClient_test_esp.ino @@ -0,0 +1,87 @@ +/* + * This sketch sends a message to a TCP server + * https://github.com/wemos/Arduino_ESP32/blob/master/docs/arduino-ide/boards_manager.md + */ + +#include +#include + +WiFiMulti WiFiMulti; + +void setup() +{ + Serial.begin(115200); + delay(10); + + // We start by connecting to a WiFi network + WiFiMulti.addAP("ssid", "key"); + + Serial.println(); + Serial.println(); + Serial.print("Waiting 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 + const uint16_t port = 443; + const char * host = "192.168.2.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("Waiting 5 seconds before retrying..."); + delay(5000); + return; + } + + // This will send a request to the server + //uncomment this line to send an arbitrary string to the server + //client.print("Send this data to the server"); + //uncomment this line to send a basic document request to the server + client.print("GET /index.html HTTP/1.1\n\n"); + + int maxloops = 0; + + //wait for the server's reply to become available + while (!client.available() && maxloops < 1000) + { + maxloops++; + delay(1); //delay 1 msec + } + if (client.available() > 0) + { + //read back one line from the server + String line = client.readStringUntil('\r'); + Serial.println(line); + } + else + { + Serial.println("client.available() timed out "); + } + + Serial.println("Closing connection."); + client.stop(); + + Serial.println("Waiting 5 seconds before restarting..."); + delay(5000); +}