Skip to content

Commit 77891b2

Browse files
committed
Added Thingspeak client to fetch data from a channel
1 parent 631cb53 commit 77891b2

File tree

2 files changed

+195
-0
lines changed

2 files changed

+195
-0
lines changed

ThingspeakClient.cpp

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
#include "ThingspeakClient.h"
2+
3+
4+
ThingspeakClient::ThingspeakClient() {
5+
6+
}
7+
8+
void ThingspeakClient::getLastChannelItem(String channelId, String readApiKey) {
9+
JsonStreamingParser parser;
10+
parser.setListener(this);
11+
WiFiClient client;
12+
13+
// http://api.thingspeak.com/channels/CHANNEL_ID/feeds.json?results=2&api_key=API_KEY
14+
const char host[] = "api.thingspeak.com";
15+
String url = "/channels/" + channelId +"/feeds.json?results=1&api_key=" + readApiKey;
16+
17+
const int httpPort = 80;
18+
if (!client.connect(host, httpPort)) {
19+
Serial.println("connection failed");
20+
return;
21+
}
22+
23+
24+
Serial.print("Requesting URL: ");
25+
Serial.println(url);
26+
27+
// This will send the request to the server
28+
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
29+
"Host: " + host + "\r\n" +
30+
"Connection: close\r\n\r\n");
31+
while(!client.available()) {
32+
Serial.println(".");
33+
delay(1000);
34+
}
35+
36+
int pos = 0;
37+
boolean isBody = false;
38+
char c;
39+
40+
int size = 0;
41+
client.setNoDelay(false);
42+
while(client.connected()) {
43+
while((size = client.available()) > 0) {
44+
c = client.read();
45+
if (c == '{' || c == '[') {
46+
isBody = true;
47+
}
48+
if (isBody) {
49+
parser.parse(c);
50+
}
51+
}
52+
}
53+
}
54+
55+
void ThingspeakClient::whitespace(char c) {
56+
57+
}
58+
59+
void ThingspeakClient::startDocument() {
60+
61+
}
62+
63+
void ThingspeakClient::key(String key) {
64+
if (key == "channel") {
65+
isHeader = true;
66+
} else if (key == "feeds") {
67+
isHeader = false;
68+
}
69+
currentKey = key;
70+
}
71+
72+
void ThingspeakClient::value(String value) {
73+
//Serial.println(currentKey +": " + value);
74+
75+
for (int i = 1; i < 9; i++) {
76+
String fieldKey = "field" + String(i);
77+
78+
if (currentKey == fieldKey) {
79+
if (isHeader) {
80+
fieldLabels[i-1] = value;
81+
} else {
82+
lastFields[i-1] = value;
83+
Serial.println(fieldKey + ": " + value);
84+
}
85+
86+
}
87+
}
88+
89+
90+
}
91+
92+
93+
String ThingspeakClient::getFieldLabel(int index) {
94+
return fieldLabels[index];
95+
}
96+
97+
String ThingspeakClient::getFieldValue(int index) {
98+
return lastFields[index];
99+
}
100+
101+
String ThingspeakClient::getCreatedAt() {
102+
return createdAt;
103+
}
104+
105+
void ThingspeakClient::endArray() {
106+
107+
}
108+
109+
void ThingspeakClient::endObject() {
110+
111+
}
112+
113+
void ThingspeakClient::endDocument() {
114+
115+
}
116+
117+
void ThingspeakClient::startArray() {
118+
119+
}
120+
121+
void ThingspeakClient::startObject() {
122+
123+
}

ThingspeakClient.h

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/**The MIT License (MIT)
2+
3+
Copyright (c) 2015 by Daniel Eichhorn
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
22+
23+
See more at http://blog.squix.ch
24+
*/
25+
26+
#pragma once
27+
28+
#include <JsonListener.h>
29+
#include <JsonStreamingParser.h>
30+
#include <ESP8266WiFi.h>
31+
#include <WiFiClient.h>
32+
33+
#define MAX_FORECAST_PERIODS 7
34+
35+
class ThingspeakClient: public JsonListener {
36+
private:
37+
// Thingspeak has a maximum of 8 fields
38+
String lastFields[8];
39+
String fieldLabels[8];
40+
String createdAt;
41+
boolean isHeader = true;
42+
String currentKey = "";
43+
44+
public:
45+
ThingspeakClient();
46+
47+
void getLastChannelItem(String channelId, String readApiKey);
48+
49+
String getFieldLabel(int index);
50+
51+
String getFieldValue(int index);
52+
53+
String getCreatedAt();
54+
55+
virtual void whitespace(char c);
56+
57+
virtual void startDocument();
58+
59+
virtual void key(String key);
60+
61+
virtual void value(String value);
62+
63+
virtual void endArray();
64+
65+
virtual void endObject();
66+
67+
virtual void endDocument();
68+
69+
virtual void startArray();
70+
71+
virtual void startObject();
72+
};

0 commit comments

Comments
 (0)