-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArduinoAQIData.cpp
312 lines (242 loc) · 8.04 KB
/
ArduinoAQIData.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#include "ArduinoAQIData.h"
ArduinoAQIData::ArduinoAQIData() {
EEPROM.begin(512);
}
void ArduinoAQIData::begin() {
Credentials myCredentials = getSavedCredentials();
_wifiManager.setSavedSsid(myCredentials.ssid);
_wifiManager.setSavedPassword(myCredentials.password);
_wifiManager.setDebugOutput(true);
_wifiManager.setConfigPortalTimeout(300);
_wifiManager.setSaveConfigCallback([]() -> void {
Serial.println("WiFi config saved");
});
_wifiManager.autoConnect(ACCESS_POINT, ACCESS_POINT_PASSWORD);
_onWifiConnect();
}
bool ArduinoAQIData::isConnected() {
return WiFi.status() == WL_CONNECTED;
}
void ArduinoAQIData::setAPCallback( void (*func)(WiFiManager* myWiFiManager) ) {
_wifiManager.setAPCallback(func);
}
bool ArduinoAQIData::write(float number1, float number2, float number3, float number4) {
if (!isConnected()) {
Serial.println("No wifi connection, try reconnecting.");
_connectionAttempts = 0;
_reconnectWifi();
return false;
}
if (!_isRegistered) {
Serial.println("Not registered, cancel data send and load config…");
_loadThingspeakConfig();
return false;
}
Serial.println("Sending data…");
ThingSpeak.setField(1, number1);
ThingSpeak.setField(2, number2);
ThingSpeak.setField(3, number3);
ThingSpeak.setField(4, number4);
int x = ThingSpeak.writeFields(_thingspeakChannelId, _thingspeakWriteKey.c_str());
if (x != OK_SUCCESS) {
Serial.println("Error code: " + String(x));
}
if (x == ERR_CONNECT_FAILED) {
Serial.println("ThingSpeak connection error!");
_connectionAttempts = 0;
_reconnectWifi();
return false;
}
if (x == ERR_BADAPIKEY || x == ERR_BADURL) {
Serial.println("Wrong API key or bad endpoint. Reload config…");
_loadThingspeakConfig();
return false;
}
if (x == ERR_NOT_INSERTED) {
Serial.println("ThingSpeak write failed! Probably reached rate limit.");
}
return x == OK_SUCCESS;
}
void ArduinoAQIData::restart() {
// @see
// https://github.com/esp8266/Arduino/issues/1722
Serial.println("ESP.restart does not work the first time after serial flashing.");
ESP.restart();
}
void ArduinoAQIData::disconnectAndRestart(bool clearCredentials) {
if (isConnected()) {
WiFi.disconnect(clearCredentials);
// wait for disconnect
int disconnectAttempts = 0;
while(1) {
disconnectAttempts++;
if (WiFi.status() == WL_DISCONNECTED) {
break;
}
if (disconnectAttempts >= MAX_DISCONNECT_ATTEMPTS) {
break;
}
delay(1000);
}
}
restart();
}
void ArduinoAQIData::resetWifi() {
// clear credentials and restart
_clearEEPROMCredentials();
disconnectAndRestart(true);
}
String ArduinoAQIData::getMacAddress() {
String myMac;
byte mac[6];
WiFi.macAddress(mac);
myMac = String(mac[0], HEX) + ":";
myMac += String(mac[1], HEX) + ":";
myMac += String(mac[2], HEX) + ":";
myMac += String(mac[3], HEX) + ":";
myMac += String(mac[4], HEX) + ":";
myMac += String(mac[5], HEX);
return myMac;
}
Credentials ArduinoAQIData::getSavedCredentials() {
char ssid[WIFI_VARIABLE_LENGTH];
char password[WIFI_VARIABLE_LENGTH];
EEPROM.get(0, ssid);
EEPROM.get(WIFI_VARIABLE_LENGTH, password);
String mySsid = String(ssid);
String myPassword = String(password);
if (_isNull(0)) mySsid = "";
if (_isNull(WIFI_VARIABLE_LENGTH)) myPassword = "";
return {
ssid: mySsid,
password: myPassword
};
}
void ArduinoAQIData::_clearEEPROMCredentials() {
for (int i = 0; i < WIFI_VARIABLE_LENGTH * 2; i++) {
EEPROM.write(i, 255);
}
EEPROM.commit();
}
void ArduinoAQIData::_reconnectWifi() {
_connectionAttempts++;
if (isConnected()) {
Serial.println("Already connected, something is wonky. Try restarting…");
restart();
return;
}
// only try a few times, otherwise reset button is blocked
if (_connectionAttempts > MAX_CONNECTION_ATTEMPTS) {
Serial.println("Max connection attempts reached. Try restarting…");
restart();
return;
}
// retrieve saved credentials
Credentials myCredentials = getSavedCredentials();
if (myCredentials.ssid == "") {
Serial.println("No EEPROM-stored credentials. Back to config…");
resetWifi();
return;
}
// attempt connection
Serial.println("Attempting to reconnect to " + myCredentials.ssid + "… (" + _connectionAttempts + ")");
WiFi.disconnect();
_wifiManager.connectWifi(myCredentials.ssid, myCredentials.password);
// success
_onWifiConnect();
}
void ArduinoAQIData::_saveWifiCredentials() {
// only save validated credentials
if (!isConnected()) return;
char mySsid[WIFI_VARIABLE_LENGTH];
char myPassword[WIFI_VARIABLE_LENGTH];
WiFi.SSID().toCharArray(mySsid, WIFI_VARIABLE_LENGTH);
WiFi.psk().toCharArray(myPassword, WIFI_VARIABLE_LENGTH);
EEPROM.put(0, mySsid);
EEPROM.put(WIFI_VARIABLE_LENGTH, myPassword);
EEPROM.commit();
_wifiManager.setSavedSsid(WiFi.SSID());
_wifiManager.setSavedPassword(WiFi.psk());
}
void ArduinoAQIData::_onWifiConnect () {
Serial.println("_onWifiConnect");
// no connection, retry
if (!isConnected()) {
_reconnectWifi();
return;
}
// successfully joined as client
_connectionAttempts = 0;
Serial.println("MAC Address: " + getMacAddress());
Serial.println("Connected to network: " + WiFi.SSID());
// save credentials to EEPROM
_saveWifiCredentials();
// load ThingSpeak config
_loadThingspeakConfig();
}
void ArduinoAQIData::_loadThingspeakConfig() {
_isRegistered = false;
ThingSpeak.begin(_client);
if (!_fetchThingspeakConfig()) {
_setDefaultThingspeakConfig();
}
}
bool ArduinoAQIData::_fetchThingspeakConfig() {
if (THINGSPEAK_REGISTRY_CHANNEL_NUMBER == 0) {
Serial.println("No ThingSpeak registration channel configured");
return false;
}
String json = ThingSpeak.readRaw(THINGSPEAK_REGISTRY_CHANNEL_NUMBER, String("/feeds/?results=8000"), THINGSPEAK_REGISTRY_API_KEY);
int statusCode = ThingSpeak.getLastReadStatus();
if (statusCode == OK_SUCCESS) {
if (_setThingspeakConfig(json)) {
_isRegistered = true;
Serial.println("ThingSpeak registration complete.");
return true;
} else {
Serial.println("ThingSpeak registration failed!");
}
} else {
Serial.println("Failed to load ThingSpeak config from channel " + String(THINGSPEAK_REGISTRY_CHANNEL_NUMBER) +
"! Error code: " + String(statusCode));
}
return false;
}
void ArduinoAQIData::_setDefaultThingspeakConfig() {
Serial.println("Using default ThingSpeak channel " + String(THINGSPEAK_DEFAULT_DATA_CHANNEL_NUMBER));
_thingspeakChannelId = THINGSPEAK_DEFAULT_DATA_CHANNEL_NUMBER;
_thingspeakWriteKey = THINGSPEAK_DEFAULT_DATA_WRITE_API_KEY;
_isRegistered = true;
}
bool ArduinoAQIData::_setThingspeakConfig(String json) {
int from = json.indexOf("[");
if (from == -1) return false;
int to = json.indexOf("]");
if (to == -1) return false;
// try parsing the JSON
JSONVar feeds = JSON.parse(json.substring(from, to + 1));
if (JSON.typeof(feeds) != "array") return false;
numRegisteredDevices = feeds.length();
Serial.println("Number of registered devices: " + String(numRegisteredDevices));
// iterate our ThingSpeak registration channel and match MAC addresses
for (int i = 0; i < feeds.length(); i++) {
JSONVar feed = feeds[i];
if (feed.hasOwnProperty("field1") && feed.hasOwnProperty("field2") && feed.hasOwnProperty("field3")) {
const char* macAddress = feed["field1"];
if (String(macAddress) == getMacAddress()) {
// match found, set configuration
const char* channelId = feed["field2"];
_thingspeakChannelId = atoi(channelId);
const char* writeKey = feed["field3"];
_thingspeakWriteKey = String(writeKey);
break;
}
}
}
return _thingspeakChannelId > 0;
}
bool ArduinoAQIData::_isNull(int loc) {
// hack to check if first byte is 255 (assumed to be null)
int firstChar = EEPROM.read(loc);
return firstChar == 255;
}