arduino-esp32/libraries/WiFi/examples/WiFiClientBasic/WiFiClientBasic.ino

88 lines
1.9 KiB
Arduino
Raw Normal View History

2016-10-06 13:21:30 +02:00
/*
* This sketch sends a message to a TCP server
*
*/
#include <WiFi.h>
#include <WiFiMulti.h>
WiFiMulti 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("Waiting for WiFi... ");
2016-10-06 13:21:30 +02:00
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 = 1337;
const char * host = "192.168.1.10"; // ip or dns
2016-10-06 13:21:30 +02:00
Serial.print("Connecting to ");
2016-10-06 13:21:30 +02:00
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...");
2016-10-06 13:21:30 +02:00
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
2016-10-06 13:21:30 +02:00
String line = client.readStringUntil('\r');
Serial.println(line);
}
else
{
Serial.println("client.available() timed out ");
}
2016-10-06 13:21:30 +02:00
Serial.println("Closing connection.");
2016-10-06 13:21:30 +02:00
client.stop();
Serial.println("Waiting 5 seconds before restarting...");
2016-10-06 13:21:30 +02:00
delay(5000);
}