Skip to content

Commit

Permalink
WiFi: improve WiFiClient(Basic) examples (#5197)
Browse files Browse the repository at this point in the history
WiFiClient no longer depends on now-defunct data.sparkfun.com
service, but uses a TCP "quote of the day" service instead.

fixes #4088
  • Loading branch information
yoursunny authored and devyte committed Oct 11, 2018
1 parent a1e59e9 commit d17ffc2
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 45 deletions.
56 changes: 21 additions & 35 deletions libraries/ESP8266WiFi/examples/WiFiClient/WiFiClient.ino
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
/*
This sketch sends data via HTTP GET requests to data.sparkfun.com service.
You need to get streamId and privateKey at data.sparkfun.com and paste them
below. Or just customize this script to talk to other HTTP servers.
This sketch establishes a TCP connection to a "quote of the day" service.
It sends a "hello" message, and then prints received data.
*/

#include <ESP8266WiFi.h>

const char* ssid = "your-ssid";
const char* password = "your-password";

const char* host = "data.sparkfun.com";
const char* streamId = "....................";
const char* privateKey = "....................";
const char* host = "djxmmx.net";
const uint16_t port = 17;

void setup() {
Serial.begin(115200);
Expand Down Expand Up @@ -43,54 +39,44 @@ void setup() {
Serial.println(WiFi.localIP());
}

int value = 0;

void loop() {
delay(5000);
++value;

Serial.print("connecting to ");
Serial.println(host);
Serial.print(host);
Serial.print(':');
Serial.println(port);

// Use WiFiClient class to create TCP connections
WiFiClient client;
const int httpPort = 80;
if (!client.connect(host, httpPort)) {
if (!client.connect(host, port)) {
Serial.println("connection failed");
delay(5000);
return;
}

// We now create a URI for the request
String url = "/input/";
url += streamId;
url += "?private_key=";
url += privateKey;
url += "&value=";
url += value;

Serial.print("Requesting URL: ");
Serial.println(url);

// This will send the request to the server
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
// This will send a string to the server
Serial.println("sending data to server");
client.println("hello from ESP8266");
unsigned long timeout = millis();
while (client.available() == 0) {
if (millis() - timeout > 5000) {
Serial.println(">>> Client Timeout !");
client.stop();
delay(60000);
return;
}
}

// Read all the lines of the reply from server and print them to Serial
while (client.available() || client.connected()) {
String line = client.readStringUntil('\r');
Serial.print(line);
Serial.println("receiving from remote server");
while (client.available()) {
char ch = static_cast<char>(client.read());
Serial.print(ch);
}

// Close the connection
Serial.println();
Serial.println("closing connection");
}
client.stop();

delay(300000); // execute once every 5 minutes, don't flood remote service
}
25 changes: 15 additions & 10 deletions libraries/ESP8266WiFi/examples/WiFiClientBasic/WiFiClientBasic.ino
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
/*
This sketch sends a message to a TCP server
This sketch sends a string to a TCP server, and prints a one-line response.
You must run a TCP server in your local network.
For example, on Linux you can use this command: nc -v -l 3000
*/

#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>

const char* ssid = "your-ssid";
const char* password = "your-password";

const char* host = "192.168.1.1";
const uint16_t port = 3000;

ESP8266WiFiMulti WiFiMulti;

void setup() {
Expand All @@ -14,7 +21,7 @@ void setup() {

// We start by connecting to a WiFi network
WiFi.mode(WIFI_STA);
WiFiMulti.addAP("SSID", "passpasspass");
WiFiMulti.addAP(ssid, password);

Serial.println();
Serial.println();
Expand All @@ -35,13 +42,10 @@ void setup() {


void loop() {
const uint16_t port = 80;
const char * host = "192.168.1.1"; // ip or dns



Serial.print("connecting to ");
Serial.println(host);
Serial.print(host);
Serial.print(':');
Serial.println(port);

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

// This will send the request to the server
client.println("Send this data to server");
client.println("hello from ESP8266");

//read back one line from server
Serial.println("receiving from remote server");
String line = client.readStringUntil('\r');
Serial.println(line);

Expand Down

3 comments on commit d17ffc2

@JAndrassy
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WiFiClient.ino is traditionally a HTTP GET example in all Arduino networking libraries. Here it was changed to almost same as the WiFiClientBasic example

@d-a-v
Copy link
Collaborator

@d-a-v d-a-v commented on d17ffc2 Sep 19, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you import a traditional example called that name and rename the current one to something like TCPClient.ino ?

@JAndrassy
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.