Skip to content

Commit 2007329

Browse files
committed
Modified WiFiClient example to use thingspeak instead of non-functionig sparkfun
1 parent a95d838 commit 2007329

File tree

1 file changed

+71
-57
lines changed

1 file changed

+71
-57
lines changed
Lines changed: 71 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,46 @@
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+
Go to thingspeak.com and create an account if you don't have one already.
3+
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.
4+
Fill in the Name, Description, and other fields for your channel as desired, then click the "Save Channel" button.
5+
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.
6+
Replace the channelID from tab "Channel Settings" and privateKey with "Read API Keys" from "API Keys" tab.
7+
Replace the host variable with the thingspeak server hostname "api.thingspeak.com"
8+
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.
9+
Go to the channel view page on thingspeak and check the "Field1" for the new incoming data.
10+
You can use the data visualization and analysis tools provided by Thingspeak to display and process your data in various ways.
11+
Please note, that Thingspeak accepts only integer values.
12+
13+
You can later check the values at https://thingspeak.com/channels/2005329
14+
Please note that this public channel can be accessed by anyone and it is possible that more people will write their values.
715
*/
816

917
#include <WiFi.h>
1018

1119
const char* ssid = "your-ssid";
1220
const char* password = "your-password";
1321

14-
const char* host = "data.sparkfun.com";
15-
const char* streamId = "....................";
16-
const char* privateKey = "....................";
22+
const char* host = "api.thingspeak.com";
23+
const int httpPort = 80;
24+
const String channelID = "2005329";
25+
const String writeApiKey = "V6YOTILH9I7D51F9";
26+
const String readApiKey = "34W6LGLIFXD56MPM";
27+
28+
// The default example accepts one data filed named "field1"
29+
// For your own server you can ofcourse create more of them.
30+
int field1 = 0;
31+
32+
int numberOfResults = 3; // Number of results to be read
33+
int fieldNumber = 1; // Field number which will be read out
1734

1835
void setup()
1936
{
2037
Serial.begin(115200);
21-
delay(10);
38+
while(!Serial){delay(100);}
2239

2340
// We start by connecting to a WiFi network
2441

2542
Serial.println();
26-
Serial.println();
43+
Serial.println("******************************************************");
2744
Serial.print("Connecting to ");
2845
Serial.println(ssid);
2946

@@ -40,55 +57,52 @@ void setup()
4057
Serial.println(WiFi.localIP());
4158
}
4259

43-
int value = 0;
44-
45-
void loop()
46-
{
47-
delay(5000);
48-
++value;
49-
50-
Serial.print("connecting to ");
51-
Serial.println(host);
52-
53-
// Use WiFiClient class to create TCP connections
54-
WiFiClient client;
55-
const int httpPort = 80;
56-
if (!client.connect(host, httpPort)) {
57-
Serial.println("connection failed");
58-
return;
60+
void readResponse(WiFiClient *client){
61+
unsigned long timeout = millis();
62+
while(client->available() == 0){
63+
if(millis() - timeout > 5000){
64+
Serial.println(">>> Client Timeout !");
65+
client->stop();
66+
return;
5967
}
68+
}
6069

61-
// We now create a URI for the request
62-
String url = "/input/";
63-
url += streamId;
64-
url += "?private_key=";
65-
url += privateKey;
66-
url += "&value=";
67-
url += value;
68-
69-
Serial.print("Requesting URL: ");
70-
Serial.println(url);
71-
72-
// This will send the request to the server
73-
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
74-
"Host: " + host + "\r\n" +
75-
"Connection: close\r\n\r\n");
76-
unsigned long timeout = millis();
77-
while (client.available() == 0) {
78-
if (millis() - timeout > 5000) {
79-
Serial.println(">>> Client Timeout !");
80-
client.stop();
81-
return;
82-
}
83-
}
84-
85-
// Read all the lines of the reply from server and print them to Serial
86-
while(client.available()) {
87-
String line = client.readStringUntil('\r');
88-
Serial.print(line);
89-
}
70+
// Read all the lines of the reply from server and print them to Serial
71+
while(client->available()) {
72+
String line = client->readStringUntil('\r');
73+
Serial.print(line);
74+
}
9075

91-
Serial.println();
92-
Serial.println("closing connection");
76+
Serial.printf("\nClosing connection\n\n");
9377
}
9478

79+
void loop(){
80+
WiFiClient client;
81+
String footer = String(" HTTP/1.1\r\n") + "Host: " + String(host) + "\r\n" + "Connection: close\r\n\r\n";
82+
83+
// WRITE --------------------------------------------------------------------------------------------
84+
if (!client.connect(host, httpPort)) {
85+
return;
86+
}
87+
88+
client.print("GET /update?api_key=" + writeApiKey + "&field1=" + field1 + footer);
89+
readResponse(&client);
90+
91+
// READ --------------------------------------------------------------------------------------------
92+
93+
String readRequest = "GET /channels/" + channelID + "/fields/" + fieldNumber + ".json?results=" + numberOfResults + " HTTP/1.1\r\n" +
94+
"Host: " + host + "\r\n" +
95+
"Connection: close\r\n\r\n";
96+
97+
if (!client.connect(host, httpPort)) {
98+
return;
99+
}
100+
101+
client.print(readRequest);
102+
readResponse(&client);
103+
104+
// -------------------------------------------------------------------------------------------------
105+
106+
++field1;
107+
delay(10000);
108+
}

0 commit comments

Comments
 (0)