File tree Expand file tree Collapse file tree 1 file changed +68
-0
lines changed
libraries/ESP8266WiFi/examples/WiFiClientBasic Expand file tree Collapse file tree 1 file changed +68
-0
lines changed Original file line number Diff line number Diff line change
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
+
You can’t perform that action at this time.
0 commit comments