Skip to content

WiFiClient example fix #7711

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Feb 6, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
128 changes: 71 additions & 57 deletions libraries/WiFi/examples/WiFiClient/WiFiClient.ino
Original file line number Diff line number Diff line change
@@ -1,29 +1,46 @@
/*
* 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.
*
Go to thingspeak.com and create an account if you don't have one already.
After logging in, click on the "New Channel" button to create a new channel for your data. This is where your data will be stored and displayed.
Fill in the Name, Description, and other fields for your channel as desired, then click the "Save Channel" button.
Take note of the "Write API Key" located in the "API keys" tab, this is the key you will use to send data to your channel.
Replace the channelID from tab "Channel Settings" and privateKey with "Read API Keys" from "API Keys" tab.
Replace the host variable with the thingspeak server hostname "api.thingspeak.com"
Upload the sketch to your ESP32 board and make sure that the board is connected to the internet. The ESP32 should now send data to your Thingspeak channel at the intervals specified by the loop function.
Go to the channel view page on thingspeak and check the "Field1" for the new incoming data.
You can use the data visualization and analysis tools provided by Thingspeak to display and process your data in various ways.
Please note, that Thingspeak accepts only integer values.

You can later check the values at https://thingspeak.com/channels/2005329
Please note that this public channel can be accessed by anyone and it is possible that more people will write their values.
*/

#include <WiFi.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 = "api.thingspeak.com";
const int httpPort = 80;
const String channelID = "2005329";
const String writeApiKey = "V6YOTILH9I7D51F9";
const String readApiKey = "34W6LGLIFXD56MPM";

// The default example accepts one data filed named "field1"
// For your own server you can ofcourse create more of them.
int field1 = 0;

int numberOfResults = 3; // Number of results to be read
int fieldNumber = 1; // Field number which will be read out

void setup()
{
Serial.begin(115200);
delay(10);
while(!Serial){delay(100);}

// We start by connecting to a WiFi network

Serial.println();
Serial.println();
Serial.println("******************************************************");
Serial.print("Connecting to ");
Serial.println(ssid);

Expand All @@ -40,55 +57,52 @@ void setup()
Serial.println(WiFi.localIP());
}

int value = 0;

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

Serial.print("connecting to ");
Serial.println(host);

// Use WiFiClient class to create TCP connections
WiFiClient client;
const int httpPort = 80;
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
return;
void readResponse(WiFiClient *client){
unsigned long timeout = millis();
while(client->available() == 0){
if(millis() - timeout > 5000){
Serial.println(">>> Client Timeout !");
client->stop();
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");
unsigned long timeout = millis();
while (client.available() == 0) {
if (millis() - timeout > 5000) {
Serial.println(">>> Client Timeout !");
client.stop();
return;
}
}

// Read all the lines of the reply from server and print them to Serial
while(client.available()) {
String line = client.readStringUntil('\r');
Serial.print(line);
}
// Read all the lines of the reply from server and print them to Serial
while(client->available()) {
String line = client->readStringUntil('\r');
Serial.print(line);
}

Serial.println();
Serial.println("closing connection");
Serial.printf("\nClosing connection\n\n");
}

void loop(){
WiFiClient client;
String footer = String(" HTTP/1.1\r\n") + "Host: " + String(host) + "\r\n" + "Connection: close\r\n\r\n";

// WRITE --------------------------------------------------------------------------------------------
if (!client.connect(host, httpPort)) {
return;
}

client.print("GET /update?api_key=" + writeApiKey + "&field1=" + field1 + footer);
readResponse(&client);

// READ --------------------------------------------------------------------------------------------

String readRequest = "GET /channels/" + channelID + "/fields/" + fieldNumber + ".json?results=" + numberOfResults + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n";

if (!client.connect(host, httpPort)) {
return;
}

client.print(readRequest);
readResponse(&client);

// -------------------------------------------------------------------------------------------------

++field1;
delay(10000);
}