-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSensorBoxTB.cpp
More file actions
312 lines (257 loc) · 8.17 KB
/
Copy pathSensorBoxTB.cpp
File metadata and controls
312 lines (257 loc) · 8.17 KB
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 "SensorBoxTB.h"
#if defined(__AVR_ATmega328P__) || defined(__AVR_ATmega168__)
#ifndef HAVE_HWSERIAL1
#include "SoftwareSerial.h"
SoftwareSerial Serial1(12, 13); // RX, TX
#endif
#endif
String token_tb;
#if defined(ESP8266) || defined(ESP32)
void writeStringEEPROM(int address, String word) {
for (int i = 0; i < word.length(); ++i) {
EEPROM.write(address + i, word[i]);
}
EEPROM.write(address + word.length(), '\0');
EEPROM.commit();
}
String readStringEEPROM(int address) {
String word;
char readChar = ' ';
int i = address;
while (readChar != '\0') {
readChar = char(EEPROM.read(i));
delay(10);
i++;
if (readChar != '\0') {
word += readChar;
}
}
return word;
}
bool provisionRequestSent = false;
volatile bool provisionResponseProcessed = false;
void processProvisionResponse(const Provision_Data &data) {
int jsonSize = measureJson(data) + 1;
char buffer[jsonSize];
serializeJson(data, buffer, jsonSize);
Serial.println(buffer);
if (strncmp(data["status"], "SUCCESS", strlen("SUCCESS")) != 0) {
provisionResponseProcessed = true;
Serial.println("[SensorBoxTB] Failed to provision device.");
while(1);
}
if (strncmp(data["credentialsType"], "ACCESS_TOKEN", strlen("ACCESS_TOKEN")) == 0) {
token_tb= data["credentialsValue"].as<String>();
const char* rcv_token = data["credentialsValue"];
writeStringEEPROM(0, "token");
writeStringEEPROM(6, rcv_token);
}
provisionResponseProcessed = true;
}
const Provision_Callback provisionCallback = processProvisionResponse;
void clearSavedToken() {
EEPROM.begin(64);
EEPROM.write(0, '\0');
EEPROM.commit();
}
#endif
void SensorBox::initComms() {
#if defined(__AVR_ATmega328P__) || defined(__AVR_ATmega168__)
Serial1.begin(WIFI_SHIELD_BAUD_RATE);
WiFi.init(&Serial1);
if (WiFi.status() == WL_NO_SHIELD) {
#if SERIAL_DEBUG
Serial.println(F("[SensorBoxTB] WiFi shield not present"));
#endif
return;
}
#endif
#ifdef ESP32
if (_username) {
WiFi.disconnect(true);
WiFi.mode(WIFI_STA);
esp_wifi_sta_wpa2_ent_set_identity((uint8_t *)_username, strlen(_username));
esp_wifi_sta_wpa2_ent_set_username((uint8_t *)_username, strlen(_username));
esp_wifi_sta_wpa2_ent_set_password((uint8_t *)_password, strlen(_password));
esp_wpa2_config_t config = WPA2_CONFIG_INIT_DEFAULT();
esp_wifi_sta_wpa2_ent_enable(&config);
}
#endif
#if defined(ESP8266) || defined(ESP32)
EEPROM.begin(64);
//Serial.print("address 0: "); Serial.println(EEPROM.readString(0));
if (!_token && readStringEEPROM(0) == "token") {
Serial.println("[SensorBoxTB] Found token in the EEPROM! Using it to connect...");
token_tb = readStringEEPROM(6);
Serial.print("[SensorBoxTB] EEPROM token: "); Serial.println(readStringEEPROM(6));
} else if (_token) {
token_tb= _token;
}
#else
token_tb= _token;
#endif
while (!connected()) {
connectWiFi();
if (WiFi.status() == WL_CONNECTED && strlen(token_tb.c_str()) > 0) {
Serial.print("[SensorBoxTB] Using token: "); Serial.println(token_tb);
connectThingsBoard();
}
#if defined(ESP8266) || defined(ESP32)
else if (_provisionDeviceKey) {
while (!provisionResponseProcessed) {
if (!provisionRequestSent) {
connectThingsBoardProvision();
if (Provision_Subscribe(provisionCallback)) {
if (sendProvisionRequest(_deviceName, _provisionDeviceKey, _provisionDeviceSecret)) {
provisionRequestSent = true;
Serial.println("[SensorBoxTB] Provision request was sent!");
}
}
}
ThingsBoard::loop();
}
if (connected()) {
disconnect();
}
ThingsBoard::loop();
}
#endif
}
}
void SensorBox::connectWiFi() {
if (WiFi.status() != WL_CONNECTED) {
subscribedRPC = false;
#if SERIAL_DEBUG
Serial.print(F("[SensorBoxTB] Connecting to WiFi"));
#endif
if (WiFi.status() != WL_CONNECTED) {
#ifdef ESP32
if (_username) {
WiFi.begin(_ap_name);
} else {
WiFi.begin(_ap_name, _password);
}
#else
WiFi.begin(_ap_name, _password);
#endif
for (int i = 0; i <= 10 && ( WiFi.status() == WL_DISCONNECTED || WiFi.status() == WL_IDLE_STATUS ); i++) {
#if SERIAL_DEBUG
Serial.print(".");
#endif
delay(1000);
}
}
Serial.print("\n");
#if SERIAL_DEBUG
if (WiFi.status() == WL_CONNECTED) {
Serial.println(F("[SensorBoxTB] Connected to WiFi!"));
} else {
Serial.println(F("[SensorBoxTB] Not connected to WiFi."));
}
#endif
}
}
#if defined(ESP8266) || defined(ESP32)
boolean SensorBox::connectThingsBoardProvision() {
if (!connected() && WiFi.status() == WL_CONNECTED) {
subscribedRPC = false;
#if SERIAL_DEBUG
Serial.println(F("[SensorBoxTB] Connecting to ThingsBoard Provison node ..."));
#endif
if ( !connect(_server_ip, "provision") ) {
#if SERIAL_DEBUG
Serial.print(F("[SensorBoxTB] Failed to connect to ThingsBoard Provision node."));
#endif
return false;
}
#if SERIAL_DEBUG
Serial.println(F("[SensorBoxTB] Connected successfully to ThingsBoard Provsion node!"));
#endif
}
return true;
}
#endif
boolean SensorBox::connectThingsBoard() {
if (!connected() && WiFi.status() == WL_CONNECTED) {
subscribedRPC = false;
#if SERIAL_DEBUG
Serial.println(F("[SensorBoxTB] Connecting to ThingsBoard node ..."));
#endif
if ( !connect(_server_ip, token_tb.c_str()) ) {
#if SERIAL_DEBUG
Serial.println(F("[SensorBoxTB] Failed to connect to ThingsBoard node."));
#endif
return false;
}
#if SERIAL_DEBUG
Serial.println(F("[SensorBoxTB] Connected successfully to ThingsBoard node!"));
#endif
}
return true;
}
#if defined(ESP8266) || defined(ESP32)
boolean SensorBox::subscribeActuatorsCmds(RPC_Callback *callbacks, size_t callbacks_size) {
if(callbacks && !subscribedRPC && connected()) {
if (!_callbacks) {
_callbacks = callbacks;
_callbacks_size = callbacks_size;
}
#if SERIAL_DEBUG
Serial.print(F("[SensorBoxTB] Subscribing to ")); Serial.print(callbacks_size); Serial.println(F(" actuators commands ..."));
#endif
if (!RPC_Subscribe(callbacks, callbacks_size)) {
#if SERIAL_DEBUG
Serial.print(F("[SensorBoxTB] Failed to subscribe to Actuators."));
#endif
return false;
}
subscribedRPC = true;
#if SERIAL_DEBUG
Serial.println(F("[SensorBoxTB] Subscribed successfully to Actuators!"));
#endif
}
return true;
}
#endif
boolean SensorBox::sendDeviceDetails(DeviceDetails *details, size_t details_size) {
#if SERIAL_DEBUG
Serial.println(F("[SensorBoxTB] Sending device's details ..."));
#endif
for (size_t i = 0; i < details_size; ++i) {
sendAttributeString(details[i].m_name, details[i].m_units);
#if SERIAL_DEBUG
Serial.print(F("[SensorBoxTB] - Sent ")); Serial.print(details[i].m_name); Serial.println(F(" details."));
#endif
}
return true;
}
#if defined(ESP8266) || defined(ESP32)
void SensorBox::checkFirmwareUpdates(const char* currFwTitle, const char* currFwVersion) {
Serial.println("[SensorBoxTB] Checking Firwmare Updates...");
Firmware_OTA_Subscribe();
if (Firmware_Update(currFwTitle, currFwVersion)) {
Serial.println("[SensorBoxTB] Update done, reboot now");
#if defined(ESP8266)
ESP.restart();
#elif defined(ESP32)
esp_restart();
#endif
}
else {
Serial.println("[SensorBoxTB] No new firmware");
}
Firmware_OTA_Unsubscribe();
}
#endif
unsigned long last_time_tb_loop;
void SensorBox::loop() {
if ( millis() - last_time_tb_loop > 5000 ) {
connectWiFi();
connectThingsBoard();
#if defined(ESP8266) || defined(ESP32)
subscribeActuatorsCmds(_callbacks, _callbacks_size);
#endif
last_time_tb_loop = millis();
}
ThingsBoard::loop();
}