[ESP32 아두이노] Arduino - TCP Client

ESPRESSIF|2021. 1. 3. 18:56

Simple TCP/IP Client example based on Arduino 

 

Reference : 

 

WiFi.begin

www.arduino.cc/en/Reference/WiFiBegin

 

WiFiClient  :  WiFi Client class 

www.arduino.cc/en/Reference/WiFiClient

 

client.connect(host, port)

www.arduino.cc/en/Reference/ClientConnect

 

client.print

www.arduino.cc/en/Reference/ClientPrint

 

client.stop

www.arduino.cc/en/Reference/ClientStop

 

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
#include <WiFi.h>
 
const char* ssid = "yourNetworkName";
const char* password =  "yourNetworkPass";
 
const uint16_t port = 8090;
const char * host = "192.168.1.83";
 
void setup()
{
 
  Serial.begin(115200);
 
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.println("...");
  }
 
  Serial.print("WiFi connected with IP: ");
  Serial.println(WiFi.localIP());
 
}
 
void loop()
{
    WiFiClient client;
 
    if (!client.connect(host, port)) {
 
        Serial.println("Connection to host failed");
 
        delay(1000);
        return;
    }
 
    Serial.println("Connected to server successful!");
 
    client.print("Hello from ESP32!");
 
    Serial.println("Disconnecting...");
    client.stop();
 
    delay(10000);
}

cs

WiFiClient

github.com/espressif/arduino-esp32/blob/409926ef778c8d0beabb53c09d6515d94b3fe00b/libraries/WiFi/src/WiFiClient.h

 

espressif/arduino-esp32

Arduino core for the ESP32. Contribute to espressif/arduino-esp32 development by creating an account on GitHub.

github.com