-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathThingSpeak.cpp
222 lines (170 loc) · 6.35 KB
/
ThingSpeak.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*
ThingSpeak.cpp Espress Connection for esp8266
Copyright (c) 2016 David Paiva (david@nailbuster.com). All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "ThingSpeak.h"
#include "globals.h"
#include <myWebServer.h>
#include <ArduinoJson.h>
#include <FS.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266HTTPClient.h>
ThingSpeakClass ThingSpeak;
ThingSpeakClass::ThingSpeakClass()
{
lastThingSpeak = millis();
lastTalkBack = millis();
ThingEnabled = false; //defaults;
TalkBackEnabled = false;
thingInterval = 15;
talkBackInterval = 15; //in seconds
}
void ThingSpeakClass::begin() //loads settings from json file....
{
String values = "";
File f = SPIFFS.open("/cloudgen.json", "r");
if (!f) {
DebugPrintln("thingspeak config not found");
}
else { //file exists;
values = f.readStringUntil('\n'); //read json
f.close();
DynamicJsonBuffer jsonBuffer;
JsonObject& root = jsonBuffer.parseObject(values); //parse weburl
if (!root.success())
{
DebugPrintln("parseObject() thingspeak failed");
return;
}
if (root["spkurl"].asString() != "") { //verify good json info
thingSpeakURL = root["spkurl"].asString();
thingWriteKey = root["spkwkey"].asString();
thingInterval = String(root["spkint"].asString()).toInt();
TalkBackID = root["tkbid"].asString();
TalkBackKey = root["tkbkey"].asString();
talkBackInterval = String(root["tkbint"].asString()).toInt();
if (String(root["status"].asString()).toInt() == 1) ThingEnabled = true; else ThingEnabled = false;
if (String(root["tbstatus"].asString()).toInt() == 1) TalkBackEnabled = true; else TalkBackEnabled = false;
DebugPrintln("ThingSpeak Starting....");
}
} //file exists;
}
void ThingSpeakClass::handle()
{
if (ThingEnabled == false) return;
unsigned long curTime = millis();
//manage timers manually....more stable this way!
if (curTime - lastTalkBack >= talkBackInterval * 1000) {
ProcessTalkBacks();
lastTalkBack = millis();
}
if (curTime - lastThingSpeak >= thingInterval * 1000) {
SendThingSpeakValues();
lastThingSpeak = millis();
}
}
void ThingSpeakClass::ProcessTalkBacks()
{
String msgStr = "";
if (TalkBackEnabled == false) return;
if (thingSpeakURL == "") return;
msgStr = getThingSpeak(TalkBackID, TalkBackKey);
if (msgStr.charAt(0) == '$') { //process talkback as a command
if ((HMGlobal.getValue(msgStr, 0) == "$ALARM"))
{
HMGlobal.ConfigAlarms(msgStr);
}
if ((HMGlobal.getValue(msgStr, 0) == "$SETPOINT"))
{
HMGlobal.SetTemp(String(HMGlobal.getValue(msgStr,1)).toInt());
}
}
}
String ThingSpeakClass::getThingSpeak(String talkBackID, String talkApiKey) //talkback message processing
{ //TalkBack Function from thingspeak
String pageLength;
String CommandString = "";
String HTMLResult;
bool GoodResult = false;
DebugPrintln("talkback checking...");
if (TalkBackEnabled == false) return "";
if (thingSpeakURL == "") return "";
if (WiFi.status() != WL_CONNECTED) return ""; //check to make sure we are connected...
String url = "/talkbacks/" + talkBackID + "/commands/execute?api_key=" + talkApiKey;
HTTPClient http;
http.begin("http://"+ thingSpeakURL + url);
int httpCode = http.GET();
// httpCode will be negative on error
if (httpCode > 0) {
// HTTP header has been send and Server response header has been handled
//DebugPrintln("[HTTP] GET... code: " + String(httpCode));
// file found at server
if (httpCode == HTTP_CODE_OK) {
CommandString = http.getString();
}
}
else {
DebugPrintln("[HTTP] GET... failed, error: " + http.errorToString(httpCode));
}
http.end();
CommandString.replace("\n", "");
if (CommandString!="") DebugPrintln("Got talkback result : " + CommandString);
return CommandString;
}
void ThingSpeakClass::SendThingSpeakValues()
{
// if (ThingEnabled) DebugPrintln("thingspeak enabled"); else DebugPrintln("thingspeak disabled");
if (ThingEnabled == false) return;
if (thingSpeakURL == "") return;
DebugPrintln(thingSpeakURL);
if (WiFi.status() != WL_CONNECTED) return; //check to make sure we are connected...
String postStr = "api_key=" + thingWriteKey;
if (HMGlobal.hmProbeTemp[0] != "U") postStr += "&field1=" + HMGlobal.hmProbeTemp[0];
if (HMGlobal.hmProbeTemp[1] != "U") postStr += "&field2=" + HMGlobal.hmProbeTemp[1];
if (HMGlobal.hmProbeTemp[2] != "U") postStr += "&field3=" + HMGlobal.hmProbeTemp[2];
if (HMGlobal.hmProbeTemp[3] != "U") postStr += "&field4=" + HMGlobal.hmProbeTemp[3];
if (HMGlobal.hmFanMovAvg != "U") postStr += "&field5=" + HMGlobal.hmFanMovAvg;
if (HMGlobal.hmFan != "U") postStr += "&field6=" + HMGlobal.hmFan;
if (HMGlobal.hmSetPoint != "U") postStr += "&field7=" + HMGlobal.hmSetPoint;
if (HMGlobal.hmLidOpenCountdown != "U") postStr += "&field8=" + HMGlobal.hmLidOpenCountdown;
if (inAlarm) //if alarm was triggered we send on next msg
{
postStr += "&status=" + MyWebServer.urlencode(MyWebServer.CurTimeString() + " " + AlarmInfo);
AlarmInfo = "";
inAlarm = false;
}
HTTPClient http;
DebugPrintln("http://" + thingSpeakURL + "/update");
http.begin("http://" + thingSpeakURL + "/update");
int httpCode = http.POST(postStr);
// httpCode will be negative on error
if (httpCode > 0) {
if (httpCode == HTTP_CODE_OK) {
}
}
else {
DebugPrintln("[HTTP] POST... failed, error: " + http.errorToString(httpCode));
}
http.end();
DebugPrintln("sending thingspeak stuffs");
}
void ThingSpeakClass::SendAlarm(String AlarmMsg)
{
if (ThingEnabled == false) return;
if (thingSpeakURL == "") return;
inAlarm = true; //next message send we will send alarm info.
AlarmInfo = AlarmMsg;
DebugPrintln(thingSpeakURL);
}