Skip to content

Commit d17ffc2

Browse files
yoursunnydevyte
authored andcommitted
WiFi: improve WiFiClient(Basic) examples (#5197)
WiFiClient no longer depends on now-defunct data.sparkfun.com service, but uses a TCP "quote of the day" service instead. fixes #4088
1 parent a1e59e9 commit d17ffc2

File tree

2 files changed

+36
-45
lines changed

2 files changed

+36
-45
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
11
/*
2-
This sketch sends data via HTTP GET requests to data.sparkfun.com service.
3-
4-
You need to get streamId and privateKey at data.sparkfun.com and paste them
5-
below. Or just customize this script to talk to other HTTP servers.
6-
2+
This sketch establishes a TCP connection to a "quote of the day" service.
3+
It sends a "hello" message, and then prints received data.
74
*/
85

96
#include <ESP8266WiFi.h>
107

118
const char* ssid = "your-ssid";
129
const char* password = "your-password";
1310

14-
const char* host = "data.sparkfun.com";
15-
const char* streamId = "....................";
16-
const char* privateKey = "....................";
11+
const char* host = "djxmmx.net";
12+
const uint16_t port = 17;
1713

1814
void setup() {
1915
Serial.begin(115200);
@@ -43,54 +39,44 @@ void setup() {
4339
Serial.println(WiFi.localIP());
4440
}
4541

46-
int value = 0;
47-
4842
void loop() {
49-
delay(5000);
50-
++value;
51-
5243
Serial.print("connecting to ");
53-
Serial.println(host);
44+
Serial.print(host);
45+
Serial.print(':');
46+
Serial.println(port);
5447

5548
// Use WiFiClient class to create TCP connections
5649
WiFiClient client;
57-
const int httpPort = 80;
58-
if (!client.connect(host, httpPort)) {
50+
if (!client.connect(host, port)) {
5951
Serial.println("connection failed");
52+
delay(5000);
6053
return;
6154
}
6255

63-
// We now create a URI for the request
64-
String url = "/input/";
65-
url += streamId;
66-
url += "?private_key=";
67-
url += privateKey;
68-
url += "&value=";
69-
url += value;
70-
71-
Serial.print("Requesting URL: ");
72-
Serial.println(url);
73-
74-
// This will send the request to the server
75-
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
76-
"Host: " + host + "\r\n" +
77-
"Connection: close\r\n\r\n");
56+
// This will send a string to the server
57+
Serial.println("sending data to server");
58+
client.println("hello from ESP8266");
7859
unsigned long timeout = millis();
7960
while (client.available() == 0) {
8061
if (millis() - timeout > 5000) {
8162
Serial.println(">>> Client Timeout !");
8263
client.stop();
64+
delay(60000);
8365
return;
8466
}
8567
}
8668

8769
// Read all the lines of the reply from server and print them to Serial
88-
while (client.available() || client.connected()) {
89-
String line = client.readStringUntil('\r');
90-
Serial.print(line);
70+
Serial.println("receiving from remote server");
71+
while (client.available()) {
72+
char ch = static_cast<char>(client.read());
73+
Serial.print(ch);
9174
}
9275

76+
// Close the connection
9377
Serial.println();
9478
Serial.println("closing connection");
95-
}
79+
client.stop();
9680

81+
delay(300000); // execute once every 5 minutes, don't flood remote service
82+
}

libraries/ESP8266WiFi/examples/WiFiClientBasic/WiFiClientBasic.ino

+15-10
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
/*
2-
This sketch sends a message to a TCP server
3-
2+
This sketch sends a string to a TCP server, and prints a one-line response.
3+
You must run a TCP server in your local network.
4+
For example, on Linux you can use this command: nc -v -l 3000
45
*/
56

67
#include <ESP8266WiFi.h>
78
#include <ESP8266WiFiMulti.h>
89

10+
const char* ssid = "your-ssid";
11+
const char* password = "your-password";
12+
13+
const char* host = "192.168.1.1";
14+
const uint16_t port = 3000;
15+
916
ESP8266WiFiMulti WiFiMulti;
1017

1118
void setup() {
@@ -14,7 +21,7 @@ void setup() {
1421

1522
// We start by connecting to a WiFi network
1623
WiFi.mode(WIFI_STA);
17-
WiFiMulti.addAP("SSID", "passpasspass");
24+
WiFiMulti.addAP(ssid, password);
1825

1926
Serial.println();
2027
Serial.println();
@@ -35,13 +42,10 @@ void setup() {
3542

3643

3744
void loop() {
38-
const uint16_t port = 80;
39-
const char * host = "192.168.1.1"; // ip or dns
40-
41-
42-
4345
Serial.print("connecting to ");
44-
Serial.println(host);
46+
Serial.print(host);
47+
Serial.print(':');
48+
Serial.println(port);
4549

4650
// Use WiFiClient class to create TCP connections
4751
WiFiClient client;
@@ -54,9 +58,10 @@ void loop() {
5458
}
5559

5660
// This will send the request to the server
57-
client.println("Send this data to server");
61+
client.println("hello from ESP8266");
5862

5963
//read back one line from server
64+
Serial.println("receiving from remote server");
6065
String line = client.readStringUntil('\r');
6166
Serial.println(line);
6267

0 commit comments

Comments
 (0)