-
Notifications
You must be signed in to change notification settings - Fork 27
/
sendData.cpp
864 lines (771 loc) · 34.5 KB
/
sendData.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
#include "sendData.h"
dataSendHandler data_sender; // Global data sender
MQTTClient mqttClient(512);
// POST Timers
Ticker cloudTargetTicker;
Ticker localTargetTicker;
Ticker brewersFriendTicker;
Ticker brewfatherTicker;
Ticker grainfatherTicker;
Ticker brewStatusTicker;
//Ticker taplistioTicker; // Now inside dataSendHandler object
Ticker gSheetsTicker;
Ticker mqttTicker;
// POST Semaphores
bool send_cloudTarget = false;
bool send_localTarget = false;
bool send_brewersFriend = false;
bool send_brewfather = false;
bool send_grainfather = false;
bool send_brewStatus = false;
//bool send_taplistio = false; // Now inside dataSendHandler object
bool send_gSheets = false;
bool send_mqtt = false;
bool send_lock = false;
dataSendHandler::dataSendHandler() {}
void dataSendHandler::init()
{
init_mqtt();
// Set up timers
// DEBUG:
cloudTargetTicker.once(10, [](){send_cloudTarget = true;}); // Schedule first send to Cloud Target
localTargetTicker.once(20, [](){send_localTarget = true;}); // Schedule first send to Local Target
// localTargetTicker.once(5, [](){send_localTarget = true;}); // Schedule first send to Local Target
// DEBUG^
brewStatusTicker.once(30, [](){send_brewStatus = true;}); // Schedule first send to Brew Status
brewfatherTicker.once(40, [](){send_brewfather = true;}); // Schedule first send to Brewfather
brewersFriendTicker.once(50, [](){send_brewersFriend = true;}); // Schedule first send to Brewer's Friend
mqttTicker.once(60, [](){send_mqtt = true;}); // Schedule first send to MQTT
gSheetsTicker.once(70, [](){send_gSheets = true;}); // Schedule first send to Google Sheets
grainfatherTicker.once(80, [](){send_grainfather = true;}); // Schedule first send to Grainfather
taplistioTicker.once(90, [](){data_sender.send_taplistio = true;}); // Schedule first send to Taplist.io
}
bool dataSendHandler::send_to_localTarget()
{
bool result = true;
if (send_localTarget && ! send_lock)
{
// Local Target
send_localTarget = false;
send_lock = true;
// tilt_scanner.deinit();
if (WiFiClass::status() == WL_CONNECTED && strlen(config.localTargetURL) >= LOCALTARGET_MIN_URL_LENGTH)
{
Log.verbose(F("Calling send to Local Target.\r\n"));
DynamicJsonDocument doc(TILT_ALL_DATA_SIZE + 128);
char tilt_data[TILT_ALL_DATA_SIZE + 128];
tilt_scanner.tilt_to_json_string(tilt_data, true);
doc["mdns_id"] = config.mdnsID;
doc["tilts"] = serialized(tilt_data);
serializeJson(doc, tilt_data);
if (send_to_url(config.localTargetURL, "", tilt_data, "application/json"))
{
Log.notice(F("Completed send to Local Target.\r\n"));
}
else
{
result = false; // There was an error with the previous send
Log.verbose(F("Error sending to Local Target.\r\n"));
}
}
localTargetTicker.once(config.localTargetPushEvery, [](){send_localTarget = true;}); // Set up subsequent send to localTarget
// tilt_scanner.init();
send_lock = false;
}
return result;
}
bool send_to_bf_and_bf()
{
bool retval = false;
if (send_brewersFriend && ! send_lock)
{
send_lock = true;
// Brewer's Friend
send_brewersFriend = false;
if (WiFiClass::status() == WL_CONNECTED && strlen(config.brewersFriendKey) > BREWERS_FRIEND_MIN_KEY_LENGTH)
{
Log.verbose(F("Calling send to Brewer's Friend.\r\n"));
retval = data_sender.send_to_bf_and_bf(BF_MEANS_BREWERS_FRIEND);
if (retval)
{
Log.notice(F("Completed send to Brewer's Friend.\r\n"));
}
else
{
Log.verbose(F("Error sending to Brewer's Friend.\r\n"));
}
}
brewersFriendTicker.once(BREWERS_FRIEND_DELAY, [](){send_brewersFriend = true;}); // Set up subsequent send to Brewer's Friend
send_lock = false;
}
if (send_brewfather && ! send_lock)
{
send_lock = true;
// Brewfather
send_brewfather = false;
if (WiFiClass::status() == WL_CONNECTED && strlen(config.brewfatherKey) > BREWFATHER_MIN_KEY_LENGTH)
{
Log.verbose(F("Calling send to Brewfather.\r\n"));
retval = data_sender.send_to_bf_and_bf(BF_MEANS_BREWFATHER);
if (retval)
{
Log.notice(F("Completed send to Brewfather.\r\n"));
}
else
{
Log.verbose(F("Error sending to Brewfather.\r\n"));
}
}
brewfatherTicker.once(BREWFATHER_DELAY, [](){send_brewfather = true;}); // Set up subsequent send to Brewfather
send_lock = false;
}
return retval;
}
void send_to_cloud()
{
if (send_cloudTarget && ! send_lock) {
send_lock = true;
send_cloudTarget = false;
addTiltToParse();
cloudTargetTicker.once(CLOUD_DELAY, [](){send_cloudTarget = true;}); // Set up subsequent send to localTarget
}
send_lock = false;
}
bool dataSendHandler::send_to_bf_and_bf(const uint8_t which_bf)
{
// This function combines the data formatting for both "BF"s - Brewers
// Friend & Brewfather. Once the data is formatted, it is dispatched
// to send_to_url to be sent out.
bool result = true;
StaticJsonDocument<BF_SIZE> j;
char url[128];
// As this function is being used for both Brewer's Friend and Brewfather,
// let's determine which we want and set up the URL/API key accordingly.
if (which_bf == BF_MEANS_BREWFATHER)
{
if (strlen(config.brewfatherKey) <= BREWFATHER_MIN_KEY_LENGTH)
{
Log.verbose(F("Brewfather key not populated. Returning.\r\n"));
return false;
}
strcpy(url, "http://log.brewfather.net/stream?id=");
strcat(url, config.brewfatherKey);
}
else if (which_bf == BF_MEANS_BREWERS_FRIEND)
{
if (strlen(config.brewersFriendKey) <= BREWERS_FRIEND_MIN_KEY_LENGTH)
{
Log.verbose(F("Brewer's Friend key not populated. Returning.\r\n"));
return false;
}
strcpy(url, "http://log.brewersfriend.com/stream/");
strcat(url, config.brewersFriendKey);
}
else
{
Log.error(F("Invalid value of which_bf passed to send_to_bf_and_bf.\r\n"));
return false;
}
// Loop through each of the tilt colors cached by tilt_scanner, sending
// data for each of the active tilts
for (uint8_t i = 0; i < TILT_COLORS; i++)
{
if (tilt_scanner.tilt(i)->is_loaded())
{
Log.verbose(F("Tilt loaded with color name: %s\r\n"), tilt_color_names[i]);
j["name"] = tilt_color_names[i];
j["temp"] = tilt_scanner.tilt(i)->converted_temp(true); // Always in Fahrenheit
j["temp_unit"] = "F";
j["gravity"] = tilt_scanner.tilt(i)->converted_gravity(false);
j["gravity_unit"] = "G";
j["device_source"] = "TiltBridge";
char payload_string[BF_SIZE];
serializeJson(j, payload_string);
if (!send_to_url(url, "", payload_string, "application/json"))
result = false; // There was an error with the previous send
}
}
return result;
}
bool dataSendHandler::send_to_grainfather()
{
bool result = true;
StaticJsonDocument<GF_SIZE> j;
if (send_grainfather && ! send_lock)
{
// Brew Status
send_grainfather = false;
send_lock = true;
if (WiFiClass::status() == WL_CONNECTED)
{
Log.verbose(F("Calling send to Grainfather.\r\n"));
// Loop through each of the tilt colors cached by tilt_scanner, sending
// data for each of the active tilts
for (uint8_t i = 0; i < TILT_COLORS; i++)
{
if (strlen(config.grainfatherURL[i].link) == 0) {
continue;
}
if (tilt_scanner.tilt(i)->is_loaded())
{
Log.verbose(F("Tilt loaded with color name: %s\r\n"), tilt_color_names[i]);
j["Temp"] = tilt_scanner.tilt(i)->converted_temp(true); // Always in Fahrenheit
j["Unit"] = "F";
j["SG"] = tilt_scanner.tilt(i)->converted_gravity(false);
char payload_string[GF_SIZE];
serializeJson(j, payload_string);
if (!http_send_json(config.grainfatherURL[i].link, payload_string))
{
result = false; // There was an error with the previous send
}
}
}
}
grainfatherTicker.once(GRAINFATHER_DELAY, [](){send_grainfather = true;}); // Set up subsequent send to Grainfather
send_lock = false;
}
return result;
}
bool dataSendHandler::send_to_brewstatus()
{
bool result = true;
const int payload_size = 512;
char payload[payload_size];
if (send_brewStatus && ! send_lock)
{
// Brew Status
send_brewStatus = false;
send_lock = true;
if (WiFiClass::status() == WL_CONNECTED && strlen(config.brewstatusURL) > BREWSTATUS_MIN_URL_LENGTH)
{
Log.verbose(F("Calling send to Brew Status.\r\n"));
// The payload should look like this when sent to Brewstatus:
// ('Request payload:', 'SG=1.019&Temp=71.0&Color=ORANGE&Timepoint=43984.33630927084&Beer=Beer&Comment=Comment')
// BrewStatus ignores Beer, so we just set this to Undefined.
// BrewStatus will record Comment if it set, but just leave it blank.
// The Timepoint is Google Sheets time, which is fractional days since 12/30/1899
// Using https://www.timeanddate.com/date/durationresult.html?m1=12&d1=30&y1=1899&m2=1&d2=1&y2=1970 gives
// us 25,569 days from the start of Google Sheets time to the start of the Unix epoch.
// BrewStatus wants local time, so we allow the user to specify a time offset.
// Loop through each of the tilt colors cached by tilt_scanner, sending data for each of the active tilts
for (uint8_t i = 0; i < TILT_COLORS; i++)
{
if (tilt_scanner.tilt(i)->is_loaded())
{
snprintf(payload, payload_size, "SG=%s&Temp=%s&Color=%s&Timepoint=%.11f&Beer=Undefined&Comment=",
tilt_scanner.tilt(i)->converted_gravity(false).c_str(),
tilt_scanner.tilt(i)->converted_temp(true).c_str(), // Only sending Fahrenheit numbers since we don't send units
tilt_color_names[i],
((double)std::time(0) + (config.TZoffset * 3600.0)) / 86400.0 + 25569.0);
if (send_to_url(config.brewstatusURL, "", payload, "application/x-www-form-urlencoded"))
{
Log.notice(F("Completed send to Brew Status.\r\n"));
}
else
{
result = false;
Log.verbose(F("Error sending to Brew Status.\r\n"));
}
}
}
}
brewStatusTicker.once(config.brewstatusPushEvery, [](){send_brewStatus = true;}); // Set up subsequent send to Brew Status
send_lock = false;
}
return result;
}
bool dataSendHandler::http_send_json(const char * url, const char * payload)
{
int httpResponseCode;
StaticJsonDocument<GF_SIZE> retval;
http.setFollowRedirects(HTTPC_FORCE_FOLLOW_REDIRECTS);
http.setConnectTimeout(6000);
http.setReuse(false);
secureClient.setInsecure();
http.addHeader(F("Content-Type"), F("application/json"));
http.addHeader(F("Accept"), F("application/json"));
httpResponseCode = http.POST(payload);
if (httpResponseCode >= 400) {
Log.error(F("HTTP error %d: %s, %s.\r\n"), httpResponseCode, http.errorToString(httpResponseCode).c_str(), http.getString().c_str());
return false;
}
if (!http.begin(secureClient, url)) {
Log.error(F("Unable to create secure connection to %s.\r\n"), url);
return false;
}
deserializeJson(retval, http.getString().c_str());
http.end();
retval.clear();
return true;
}
// bool dataSendHandler::send_to_taplistio()
// {
// StaticJsonDocument<192> j;
// char payload_string[192];
// int httpResponseCode;
// bool result = true;
// // See if it's our time to send.
// if (!send_taplistio) {
// return false;
// } else if (send_lock) {
// return false;
// }
// // Attempt to send.
// send_taplistio = false;
// send_lock = true;
// if (WiFiClass::status() != WL_CONNECTED) {
// Log.verbose(F("taplist.io: Wifi not connected, skipping send.\r\n"));
// taplistioTicker.once(config.taplistioPushEvery, [](){send_taplistio = true;});
// send_lock = false;
// return false;
// }
// char userAgent[128];
// snprintf(userAgent, sizeof(userAgent),
// "tiltbridge/%s (branch %s; build %s)",
// version(),
// branch(),
// build()
// );
// for (uint8_t i = 0; i < TILT_COLORS; i++) {
// if (!tilt_scanner.tilt(i)->is_loaded()) {
// continue;
// }
// j["Color"] = tilt_color_names[i];
// j["Temp"] = tilt_scanner.tilt(i)->converted_temp(false);
// j["SG"] = tilt_scanner.tilt(i)->converted_gravity(false);
// j["temperature_unit"] = "F";
// j["gravity_unit"] = "G";
// serializeJson(j, payload_string);
// http.setFollowRedirects(HTTPC_FORCE_FOLLOW_REDIRECTS);
// http.setConnectTimeout(6000);
// http.setReuse(false);
// secureClient.setInsecure();
// Log.verbose(F("taplist.io: Sending %s Tilt to %s\r\n"), tilt_color_names[i], config.taplistioURL);
// if (!http.begin(secureClient, config.taplistioURL)) {
// Log.error(F("taplist.io: Unable to create secure connection to %s\r\n"), config.taplistioURL);
// result = false;
// break;
// }
// http.addHeader(F("Content-Type"), F("application/json"));
// http.setUserAgent(userAgent);
// httpResponseCode = http.POST(payload_string);
// if (httpResponseCode < HTTP_CODE_OK || httpResponseCode > HTTP_CODE_NO_CONTENT) {
// Log.error(F("taplist.io: send %s Tilt failed (%d): %s. Response:\r\n%s\r\n"),
// tilt_color_names[i],
// httpResponseCode,
// http.errorToString(httpResponseCode).c_str(),
// http.getString().c_str());
// result = false;
// } else {
// Log.verbose(F("taplist.io: %s Tilt: success!\r\n"), tilt_color_names[i]);
// }
// }
// taplistioTicker.once(config.taplistioPushEvery, [](){send_taplistio = true;});
// send_lock = false;
// return result;
// }
bool dataSendHandler::send_to_google()
{
bool result = true;
if (send_gSheets && !send_lock) {
// Google Sheets
send_gSheets = false;
send_lock = true;
//tilt_scanner.deinit();
StaticJsonDocument<GSHEETS_JSON> payload;
char payload_string[GSHEETS_JSON];
StaticJsonDocument<GSHEETS_JSON> retval;
int httpResponseCode;
int numSent = 0;
#if (ARDUINO_LOG_LEVEL == 6)
char buff[1024] = "";
#endif
// The google sheets handler only fires if we have both a Google Scripts URL to post to, and an email address.
if (strlen(config.scriptsURL) >= GSCRIPTS_MIN_URL_LENGTH && strlen(config.scriptsEmail) >= GSCRIPTS_MIN_EMAIL_LENGTH) {
Log.verbose(F("Checking for any pending Google Sheets pushes.\r\n"));
// Log.verbose(F("Executing on core %i.\r\n"), xPortGetCoreID());
printMem();
for (uint8_t i = 0; i < TILT_COLORS; i++) {
// Loop through each of the tilt colors and check if it is both available and has active data
if (tilt_scanner.tilt(i)->is_loaded()) {
// Check if there is a google sheet name associated with the specific Tilt
if (strlen(config.gsheets_config[i].name) > 0) {
// If there's a sheet name saved, then we should send the data
if (numSent == 0)
Log.notice(F("Beginning GSheets check-in.\r\n"));
payload["Beer"] = config.gsheets_config[i].name;
payload["Temp"] = tilt_scanner.tilt(i)->converted_temp(true); // Always send in Fahrenheit
payload["SG"] = tilt_scanner.tilt(i)->converted_gravity(false);
payload["Color"] = tilt_color_names[i];
payload["Comment"] = "";
payload["Email"] = config.scriptsEmail; // The gmail email address associated with the script on google
payload["tzOffset"] = config.TZoffset;
serializeJson(payload, payload_string);
payload.clear();
http.setFollowRedirects(HTTPC_FORCE_FOLLOW_REDIRECTS); // Follow the 301
http.setConnectTimeout(6000); // Set 6 second timeout
http.setReuse(false);
secureClient.setInsecure(); // Ignore SHA fingerprint
if (!http.begin(secureClient, config.scriptsURL)) { // Connect secure
Log.error(F("Unable to create secure connection to %s.\r\n"), config.scriptsURL);
result = false;
} else {
// Failed to open a connection
Log.verbose(F("Created secure connection to %s.\r\n"), config.scriptsURL);
Log.verbose(F("Sending the following payload to Google Sheets (%s):\r\n\t\t%s\r\n"), tilt_color_names[i], payload_string);
http.addHeader(F("Content-Type"), F("application/json")); // Specify content-type header
httpResponseCode = http.POST(payload_string); // Send the payload
if (httpResponseCode == HTTP_CODE_OK) { // HTTP_CODE_OK = 200
// POST success
#if (ARDUINO_LOG_LEVEL == 6)
// We need to use a buffer in order to be able to use the response twice
strlcpy(buff, http.getString().c_str(), 1024);
Log.verbose(F("HTTP Response: 200\r\nFull Response:\r\n\t%s\r\n"), buff);
deserializeJson(retval, buff);
// deserializeJson(retval, http.getString().c_str());
#else
deserializeJson(retval, http.getString().c_str());
#endif
if(strcmp(config.gsheets_config[i].link, retval["doclongurl"].as<const char *>()) != 0) {
Log.verbose(F("Storing new doclongurl: %s.\r\n"), retval["doclongurl"].as<const char *>());
strlcpy(config.gsheets_config[i].link, retval["doclongurl"].as<const char *>(), 255);
config.save();
}
retval.clear();
numSent++;
} else {
// Post generated an error (response code != 200)
Log.error(F("Google send to %s Tilt failed (%d): %s. Response:\r\n%s\r\n"),
tilt_color_names[i],
httpResponseCode,
http.errorToString(httpResponseCode).c_str(),
http.getString().c_str());
result = false;
} // Response code != 200
} // Good connection
http.end();
delay(100); // Give garbage collection time to run
} // Check we have a sheet name for the color
} // Check scanner is loaded for color
} // Loop through colors
Log.notice(F("Submitted %l sheet%s to Google.\r\n"), numSent, (numSent== 1) ? "" : "s");
}
gSheetsTicker.once(GSCRIPTS_DELAY, [](){send_gSheets = true;}); // Set up subsequent send to Google Sheets
//tilt_scanner.init();
send_lock = false;
}
return result;
}
void dataSendHandler::init_mqtt()
{
LCBUrl url;
if (strcmp(config.mqttBrokerHost, "") != 0 || strlen(config.mqttBrokerHost) != 0)
{
if (url.isMDNS(config.mqttBrokerHost)) {
Log.verbose(F("Initializing connection to MQTTBroker: %s (%s) on port: %d\r\n"),
config.mqttBrokerHost,
url.getIP(config.mqttBrokerHost).toString().c_str(),
config.mqttBrokerPort);
} else {
Log.verbose(F("Initializing connection to MQTTBroker: %s on port: %d\r\n"),
config.mqttBrokerHost,
config.mqttBrokerPort);
}
if (mqtt_alreadyinit) {
mqttClient.disconnect();
delay(250);
if (url.isMDNS(config.mqttBrokerHost)) {
mqttClient.setHost(url.getIP(config.mqttBrokerHost), config.mqttBrokerPort);
} else {
mqttClient.setHost(config.mqttBrokerHost, config.mqttBrokerPort);
}
} else {
if (url.isMDNS(config.mqttBrokerHost)) {
mqttClient.begin(url.getIP(config.mqttBrokerHost), config.mqttBrokerPort, mqClient);
} else {
mqttClient.begin(config.mqttBrokerHost, config.mqttBrokerPort, mqClient);
}
}
mqtt_alreadyinit = true;
mqttClient.setKeepAlive(config.mqttPushEvery);
}
}
void dataSendHandler::connect_mqtt()
{
if (strlen(config.mqttUsername) > 1)
{
mqttClient.connect(config.mdnsID, config.mqttUsername, config.mqttPassword);
}
else
{
mqttClient.connect(config.mdnsID);
}
}
bool dataSendHandler::send_to_url(const char *url, const char *apiKey, const char *dataToSend, const char *contentType, bool checkBody, const char* bodyCheck)
{
// This handles the generic act of sending data to an endpoint
bool retVal = false;
if (strlen(dataToSend) > 5 && strlen(url) > 8)
{
LCBUrl lcburl;
lcburl.setUrl(url);
bool validTarget = false;
if (lcburl.isMDNS(lcburl.getHost().c_str()))
{
// Make sure we can resolve the address
if (lcburl.getIP(lcburl.getHost().c_str()) != INADDR_NONE)
validTarget = true;
}
else if (lcburl.isValidIP(lcburl.getIP(lcburl.getHost().c_str()).toString().c_str()))
// We were passed an IP Address
validTarget = true;
else
{
// If it's not mDNS all we care about is that it's http
if (lcburl.getScheme() == "http")
validTarget = true;
}
if (validTarget)
{
if (lcburl.isMDNS(lcburl.getHost().c_str()))
// Use the IP address we resolved (necessary for mDNS)
Log.verbose(F("Connecting to: %s at %s on port %l\r\n"),
lcburl.getHost().c_str(),
lcburl.getIP(lcburl.getHost().c_str() ).toString().c_str(),
lcburl.getPort());
else
Log.verbose(F("Connecting to: %s on port %l\r\n"),
lcburl.getHost().c_str(),
lcburl.getPort());
if (client.connect(lcburl.getIP(lcburl.getHost().c_str()), lcburl.getPort()))
{
Log.verbose(F("Connected to: %s.\r\n"), lcburl.getHost().c_str());
// Open POST connection
if (lcburl.getAfterPath().length() > 0)
{
Log.verbose(F("POST /%s%s HTTP/1.1\r\n"),
lcburl.getPath().c_str(),
lcburl.getAfterPath().c_str());
}
else
{
Log.verbose(F("POST /%s HTTP/1.1\r\n"), lcburl.getPath().c_str());
}
client.print(F("POST /"));
client.print(lcburl.getPath().c_str());
if (lcburl.getAfterPath().length() > 0)
{
client.print(lcburl.getAfterPath().c_str());
}
client.println(F(" HTTP/1.1"));
// Begin headers
//
// Host
Log.verbose(F("Host: %s:%l\r\n"), lcburl.getHost().c_str(), lcburl.getPort());
client.print(F("Host: "));
client.print(lcburl.getHost().c_str());
client.print(F(":"));
client.println(lcburl.getPort());
//
Log.verbose(F("Connection: close\r\n"));
client.println(F("Connection: close"));
// Content
Log.verbose(F("Content-Length: %l\r\n"), strlen(dataToSend));
client.print(F("Content-Length: "));
client.println(strlen(dataToSend));
// Content Type
Log.verbose(F("Content-Type: %s\r\n"), contentType);
client.print(F("Content-Type: "));
client.println(contentType);
// API Key
if (strlen(apiKey) > 2)
{
Log.verbose(F("X-API-KEY: %s\r\n"), apiKey);
client.print(F("X-API-KEY: "));
client.println(apiKey);
}
// Terminate headers with a blank line
Log.verbose(F("End headers.\r\n"));
client.println();
//
// End Headers
// Post JSON
client.println(dataToSend);
// Check the HTTP status (should be "HTTP/1.1 200 OK")
char status[32] = {0};
client.readBytesUntil('\r', status, sizeof(status));
client.stop();
Log.verbose(F("Status: %s\r\n"), status);
if (strcmp(status + 9, "200 OK") == 0)
{
if (checkBody == true) // We can do additional checks here
{
// Check body
String response = String(status);
if (response.indexOf(bodyCheck) >= 0)
{
Log.verbose(F("Response body ok.\r\n"));
retVal = true;
}
else
{
Log.error(F("Unexpected body content: %s\r\n"), response.c_str());
retVal = false;
}
}
else
{
Log.verbose(F("Post to %s was successful.\r\n"), lcburl.getHost().c_str());
retVal = true;
}
}
else
{
Log.error(F("Unexpected status: %s\r\n"), status);
retVal = false;
}
}
else
{
Log.warning(F("Connection failed, Host: %s, Port: %l (Err: %d)\r\n"),
lcburl.getHost().c_str(), lcburl.getPort(), client.connected());
retVal = false;
}
}
else
{
Log.error(F("Invalid target: %s.\r\n"), url);
}
}
else
{
Log.notice(F("No URL provided, or no data to send.\r\n"));
retVal = false;
}
return retVal;
}
bool dataSendHandler::send_to_mqtt()
{
// TODO: (JSON) Come back and tighten this up
bool result = false;
StaticJsonDocument<512> payload;
mqttClient.loop();
if (send_mqtt && ! send_lock)
{
// MQTT
send_mqtt = false;
send_lock = true;
if (strcmp(config.mqttBrokerHost, "") != 0 || strlen(config.mqttBrokerHost) != 0)
{
Log.verbose(F("Publishing available results to MQTT Broker.\r\n"));
// Function sends three payloads with the first two designed to
// support autodiscovery and configuration on Home Assistant.
// General payload formatted as json when sent to mqTT:
//{"Color":"Black","SG":"1.0180","Temp":"73.0","fermunits":"SG","tempunits":"F","timeStamp":1608745710}
//
// Loop through each of the tilt colors cached by tilt_scanner,
// sending data for each of the active tilts
for (uint8_t i = 0; i < TILT_COLORS; i++)
{
if (tilt_scanner.tilt(i)->is_loaded())
{
char tilt_topic[50] = {'\0'};
snprintf(tilt_topic, 50, "%s/tilt_%s",
config.mqttTopic,
tilt_color_names[i]);
for (uint8_t j = 0; j < 3; j++)
{
char m_topic[90] = {'\0'};
char tilt_name[15] = {'\0'};
char tilt_sensor_name[35] = {'\0'};
char uniq_id[30] = {'\0'};
char unit[10] = {'\0'};
bool retain = false;
strcat(tilt_name, "Tilt ");
strcat(tilt_name, tilt_color_names[i]);
if (j < 2 )
{
JsonObject device = payload.createNestedObject("device");
device["identifiers"] = tilt_color_names[i];
device["name"] = tilt_name;
}
switch (j)
{
case 0: //Home Assistant Config Topic for Temperature
sprintf(m_topic, "homeassistant/sensor/%s_tilt_%s/temperature/config",
config.mqttTopic,
tilt_color_names[i]);
payload["dev_cla"] = "temperature";
strcat(unit, "\u00b0");
strcat(unit, config.tempUnit);
payload["unit_of_meas"] = unit;
payload["ic"] = "mdi:thermometer";
payload["stat_t"] = tilt_topic;
strcat(tilt_sensor_name, "Tilt Temperature - ");
strcat(tilt_sensor_name, tilt_color_names[i]);
payload["name"] = tilt_sensor_name;
payload["val_tpl"] = "{{value_json.Temp}}";
snprintf(uniq_id, 30, "tiltbridge_tilt%sT",
tilt_color_names[i]);
payload["uniq_id"] = uniq_id;
retain = true;
break;
case 1: //Home Assistant Config Topic for Sp Gravity
sprintf(m_topic, "homeassistant/sensor/%s_tilt_%sG/sp_gravity/config",
config.mqttTopic,
tilt_color_names[i]);
//payload["dev_cla"] = "None";
payload["unit_of_meas"] = "SG";
//payload["ic"] = "";
payload["stat_t"] = tilt_topic;
strcat(tilt_sensor_name, "Tilt Specific Gravity - ");
strcat(tilt_sensor_name, tilt_color_names[i]);
payload["name"] = tilt_sensor_name;
payload["val_tpl"] = "{{value_json.SG}}";
snprintf(uniq_id, 30, "tiltbridge_tilt%sG",
tilt_color_names[i]);
payload["uniq_id"] = uniq_id;
retain = true;
break;
case 2: //General payload with sensor data
strcat(m_topic, tilt_topic);
char current_grav[8] = {'\0'};
char current_temp[5] = {'\0'};
strcpy(current_grav, tilt_scanner.tilt(i)->converted_gravity(false).c_str());
strcpy(current_temp, tilt_scanner.tilt(i)->converted_temp(false).c_str());
payload["Color"] = tilt_color_names[i];
payload["timeStamp"] = (int)std::time(0);
payload["fermunits"] = "SG";
payload["SG"] = current_grav;
payload["Temp"] = current_temp;
payload["tempunits"] = config.tempUnit;
retain = false;
break;
}
char payload_string[320] = {'\0'};
serializeJson(payload, payload_string);
Log.verbose(F("Topic: %s\r\n"), m_topic);
Log.verbose(F("Message: %s\r\n"), payload_string);
if (!mqttClient.connected() && j == 0)
{
Log.warning(F("MQTT disconnected. Attempting to reconnect to MQTT Broker\r\n"));
connect_mqtt();
}
result = mqttClient.publish(m_topic, payload_string, retain, 0);
delay(10);
payload.clear();
}
}
}
if (result) {
Log.notice(F("Completed publish to MQTT Broker.\r\n"));
} else {
result = false; // There was an error with the previous send
Log.verbose(F("Error publishing to MQTT Broker.\r\n"));
}
}
mqttTicker.once(config.mqttPushEvery, [](){send_mqtt = true;}); // Set up subsequent send to MQTT
send_lock = false;
}
return result;
}