本项止转自github官方arduino-esp32 传在这里仅为阅读源码方便
projectuser
2019-07-05 50148ccffe21ff54262064ec9f2245900eaf18aa
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
 *  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... ");
 
    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("Waiting 5 seconds before retrying...");
        delay(5000);
        return;
    }
 
    // This will send a request to the server
    client.print("Send this data to the server");
 
    //read back one line from the server
    String line = client.readStringUntil('\r');
    client.println(line);
 
    Serial.println("Closing connection.");
    client.stop();
 
    Serial.println("Waiting 5 seconds before restarting...");
    delay(5000);
}