Skip to content

Commit 053de36

Browse files
committed
Merge pull request #1144 from Links2004/master
add a simple TCP example
2 parents 14b70e9 + cedce24 commit 053de36

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* This sketch sends a message to a TCP server
3+
*
4+
*/
5+
6+
#include <ESP8266WiFi.h>
7+
#include <ESP8266WiFiMulti.h>
8+
9+
ESP8266WiFiMulti WiFiMulti;
10+
11+
void setup() {
12+
Serial.begin(115200);
13+
delay(10);
14+
15+
// We start by connecting to a WiFi network
16+
WiFiMulti.addAP("SSID", "passpasspass");
17+
18+
Serial.println();
19+
Serial.println();
20+
Serial.print("Wait for WiFi... ");
21+
22+
while(WiFiMulti.run() != WL_CONNECTED) {
23+
Serial.print(".");
24+
delay(500);
25+
}
26+
27+
Serial.println("");
28+
Serial.println("WiFi connected");
29+
Serial.println("IP address: ");
30+
Serial.println(WiFi.localIP());
31+
32+
delay(500);
33+
}
34+
35+
36+
void loop() {
37+
const uint16_t port = 80;
38+
const char * host = "192.168.1.1"; // ip or dns
39+
40+
41+
42+
Serial.print("connecting to ");
43+
Serial.println(host);
44+
45+
// Use WiFiClient class to create TCP connections
46+
WiFiClient client;
47+
48+
if (!client.connect(host, port)) {
49+
Serial.println("connection failed");
50+
Serial.println("wait 5 sec...");
51+
delay(5000);
52+
return;
53+
}
54+
55+
// This will send the request to the server
56+
client.print("Send this data to server");
57+
58+
//read back one line from server
59+
String line = client.readStringUntil('\r');
60+
client.println(line);
61+
62+
Serial.println("closing connection");
63+
client.stop();
64+
65+
Serial.println("wait 5 sec...");
66+
delay(5000);
67+
}
68+

0 commit comments

Comments
 (0)