-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConnectedStatusMonitor.ino
1472 lines (1241 loc) · 54.9 KB
/
ConnectedStatusMonitor.ino
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
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
SENSOR STATUS DISPLAY
/~https://github.com/Andy4495/ConnectedStatusMonitor
Status display for various sensor data readings downloaded from ThingSpeak IoT Platform.
Designed specifically for use with TM4C129 Connected LaunchPad
and Kentec Touch Display BoosterPack (SPI)
Initial web connection code based on "Web client" example sketch by David A. Mellis.
NTP functions are from the Arduino Time library
07/10/2018 - A.T. - Initial updates
08/17/2018 - A.T. - Functional display code for weather station, workshop and slim temps, and garage door status.
08/19/2018 - A.T. - Add state machine to turn on display depending on state of light sensor on pin A13
08/20/2018 - A.T. - Add NTP time and date to status display
08/22/2018 - A.T. - Add automatic DST support. Other display cleanup.
08/23/2018 - A.T. - Fix DST to switch at 2:00 AM (instead of midnight)
08/24/2018 - A.T. - Minor updates: display time before sensor readings (since it is faster),
use #define for DST effective hour, flash LED1 while display backlight off,
display welcome message in larger (scaled) font,
enable ethernet link and activity LEDs.
08/24/2018 - A.T. - Ethernet LEDs disabled by default, turned on with PUSH1 at reset.
Use "time.nist.gov" instead of specific IP for time server.
10/02/2018 - A.T. - Replace Workshop display with Pond sensor. Update thresholds for value colors.
10/10/2018 - A.T. - Change pond battery threshold (using 2AAs instead of LiPo).
01/30/2019 - A.T. - Change pond battery threshold (using 3xAAs through a TPS715A33 3.3V regulator).
- Fix display of negative temps.
- Increased time to wait for ThingSpeak response.
11/04/2019 - A.T. - Add Workshop temp display.
- Remove pond battery display (since it is AC powered)
- Add battery level warning for Workshop sensor
11/07/2019 - A.T. - Add support for VFD display
- Moved light sensor to pin 68/A19
11/11/2019 - A.T. - Update VFD message text.
11/14/2019 - A.T. - Adjust VFD display timing.
Change lipo low batt level to 3.8V (lasts about 2.5 days at this point)
11/21/2019 - A.T. - Turn sensor 4 and 5 voltage reading red if less than 2 days remaining.
(Sensors 4 and 5 now send charge time remaining instead of millis).
12/01/2019 - A.T. - Add support for displaying Pressure history on SparkFun OLED (parallel mode).
-- Query ThingSpeak for pressure data even when display is off
- Don't display time if no response/invalid response from time server
- Updated low battery thresholds
- Remove check for PUSH1 -- ethernet status LEDs always disabled.
01/19/2020 - A.T. - Fix a case where invalid time needs to be cleared from display.
01/21/2020 - A.T. - Change LiPo low battery threshold to 3.75V
02/13/2020 - A.T. - Update to use new Futaba VFD library naming.
04/27/2020 - A.T. - Change lipo time remaining threshold to zero, since the value does not seem
to correlate with reality in this application. Tweaked the lipo battery voltage threshold.
09/01/2020 - A.T. - Add support for turtle pond temperature. Rename "Pond" to "Fish" to distinguish the two sensors.
09/14/2020 - A.T. - Change "Gargoyle" to "Sensor5"
02/03/2021 - A.T. - Add a check for zero pressure value.
06/19/2022 - A.T. - Update to ArduinoJson v6. See https://arduinojson.org/v6/doc/upgrade/
06/19/2022 - A.T. - Fix some compiler warnings.
01/18/2023 - A.T. - Rename Slim sensor to Sensor3
03/26/2024 - Andy4495 - Rename to Large Pond and Small Pond
01-Apr-2024 - Andy4495 - Add GitHub URL for modified Kentec_35_SPI library
- Update to support ArduinoJson library version 7
*** IMPORTANT ***
This sketch uses a modified version of Energia's Kentec_35_SPI library so that it works with
this hardware configuration and TM4C129 LaunchPad: /~https://github.com/Andy4495/Kentec_35_SPI
* ***************
This sketch uses a modified version of SparkFun's MicroOLED library so that it compiles with Energia
and works with MSP and TM4C129 processors: /~https://github.com/Andy4495/SparkFun_Micro_OLED_Arduino_Library
* ***************
*** Future improvements:
Color a sensor value (or the label) yellow or red if an update has not been received for more than X minutes
Add a pixel to y-coordinates to allow for hanging comma space (otherwise comma touches next line)
Update lux string to include commas
Deal with JSON parse failure -- maybe just display last good value (i.e., no display indication of bad JSON)
*/
#include <ArduinoJson.h> // From https://arduinojson.org/, version 7.x
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
#include <TimeLib.h> // From /~https://github.com/PaulStoffregen/Time, version 1.5
#include "dst.h"
#include "Screen_K35_SPI.h" // From /~https://github.com/Andy4495/Kentec_35_SPI
Screen_K35_SPI myScreen;
// ThingSpeakKeys.h is not included in the code distribution, since it contains private key info
// This file should #define the following:
// #define LAUNCHPAD_MAC {<comma-separated 8-byte MAC address of your ethernet card>}
// Plus #defines for each of your ThingSpeak feed keys and channel IDs
#include "ThingSpeakKeys.h"
#include "Coordinates.h"
// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
// byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte mac[] = LAUNCHPAD_MAC; // Defined in ThingSpeakKeys.h
const char* server = "api.thingspeak.com";
const char* timeServer = "time.nist.gov"; // Automatically resolves to nearest active server
// IPAddress timeServer(132.163.97.1); // time.nist.gov
// Use Standard Time for Time Zone. DST is corrected when printing.
// Time zones are #defined in dst.h
int timeZone = STANDARD_TZ; // Use standard time, DST correction is done later
time_t t;
uint16_t timeColor;
EthernetUDP Udp;
unsigned int localPort = 8888; // local port to listen for UDP packets
const int NTP_PACKET_SIZE = 48; // NTP time is in the first 48 bytes of message
byte packetBuffer[NTP_PACKET_SIZE]; //buffer to hold incoming & outgoing packets
char receiveBuffer[1024] = {};
char printBuffer[32] = {};
char prevPrintBuffer[32] = {};
// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;
Layout layout;
//----- Sensor Reading Global Storage -----//
#define TEMPSIZE 6
#define LUXSIZE 9
#define RHSIZE 5
#define PSIZE 6
#define GDSIZE 7
#define BATTSIZE 6
#define TADSIZE 20
#define TIMESIZE 21
char outdoorTemp[TEMPSIZE];
char prevOutdoorTemp[TEMPSIZE];
char outdoorLux[LUXSIZE];
char prevOutdoorLux[LUXSIZE];
char outdoorRH[RHSIZE];
char prevOutdoorRH[RHSIZE];
char outdoorP[PSIZE];
char prevOutdoorP[PSIZE];
char sensor3Temp[TEMPSIZE];
char prevSensor3Temp[TEMPSIZE];
char fishTemp[TEMPSIZE];
char prevFishTemp[TEMPSIZE];
char smallPondTemp[TEMPSIZE];
char prevSmallPondTemp[TEMPSIZE];
char sensor5Temp[TEMPSIZE];
char prevSensor5Temp[TEMPSIZE];
char workshopTemp[TEMPSIZE];
char prevWorkshopTemp[TEMPSIZE];
char garageDoor[GDSIZE];
char prevGarageDoor[GDSIZE];
char outdoorBatt[BATTSIZE];
char prevOutdoorBatt[BATTSIZE];
char sensor3Batt[BATTSIZE];
char prevSensor3Batt[BATTSIZE];
char sensor5Batt[BATTSIZE];
char prevSensor5Batt[BATTSIZE];
char timeAndDate[TADSIZE];
char prevTimeAndDate[TADSIZE];
char weatherTime[TIMESIZE];
char sensor3Time[TIMESIZE];
char sensor5Time[TIMESIZE];
char pondTime[TIMESIZE];
char workshopTime[TIMESIZE];
char garageTime[TIMESIZE];
#define LIGHT_SENSOR_PIN 68
#define LIGHT_SENSOR_ADC A19
#define LIGHT_SENSOR_THRESHOLD 2000 // Based on 10K resistor and cheap photoresistor voltage divider and 12-bit ADC (4096 max value)
#define LIGHTS_ON_SLEEP_TIME 30000
#define LIGHTS_OFF_SLEEP_TIME 5000
#define BACKLIGHT_PIN 40
#define SLEEPING_STATUS_LED PN_1 // Flash when display backlight is off to show the unit is active
#define LIPO_LO_BATT_LEVEL 3780 // There are still a few days left at this level.
#define LIPO_LO_TIME_REMAIN 0 // Value reported by Fuel Gauge drops too quickly. Setting to zero effectively disables this check.
// VFD support
#include <FutabaVFD162S.h>
#define VFD_CLOCK_PIN 74
#define VFD_DATA_PIN 73
#define VFD_RESET_PIN 72
#define VFD_BUFFER_EN 76 // Controls the EN pins on the CD40109 buffer
#define VFD_POWER_CONTROL 77 // HIGH == OFF
#define VFD_BRIGHTNESS 128
FutabaVFD162S vfd(VFD_CLOCK_PIN, VFD_DATA_PIN, VFD_RESET_PIN);
int vfd_loop_counter = 0; // Used for testing
// SparkFun OLED support
// This is a modified version of SparkFun's library so that it compiles with Energia and works with MSP and TM4C129 processors
#include <SFE_MicroOLED.h> // /~https://github.com/Andy4495/SparkFun_Micro_OLED_Arduino_Library
// MicroOLED(uint8_t rst, uint8_t dc, uint8_t cs, uint8_t wr, uint8_t rd,
// uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3,
// uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7)
MicroOLED oled(35, 50, 79, 48, 58,
12, 29, 49, 85, 86, 59, 51, 30);
/* Test data
long p_hist[] = { 2902, 2902, 2902, 2902, 2903, 2903, 2903, 2903, 2903, 2903, 2903, 2904, 2904, 2904, 2905, 2905,
2904, 2905, 2905, 2905, 2905, 2905, 2905, 2906, 2906, 2906, 2907, 2907, 2907, 2907, 2909, 2908,
2908, 2908, 2909, 2909, 2909, 2908, 2909, 2909, 2909, 2910, 2910, 2911, 2911, 2911, 2911, 2911,
2911, 2912, 2912, 2912, 2912, 2913, 2913, 2913, 2913, 2914, 2914, 2914, 2914, 2914, 2915, 2915
};
*/
// Circular buffer of Pressure readings.
// Initial value of 0xFEFE so only show partial display with limited data on startup
long p_hist[64];
unsigned int hist_position = 0; // Current position in p_hist circular buffer
unsigned int hist_num_entries = 0; // Number of valid entries in the history (used after reset until buffer is filled first time)
const unsigned int hist_len = 64;
long last_weather_entry_id = 0; // Used to track whether we received new data
unsigned long lastPressureQuery = 0; // Track times between readings when displays are off
#define PRESSURE_READINGS_TIME 120000UL // Time between readings: 2 minutes (2 min * 60 s/min * 1000 ms/s)
int statusLEDstate = 0;
enum {LIGHTS_OFF, LIGHTS_TURNED_ON, LIGHTS_ON, LIGHTS_TURNED_OFF};
int lightSensorState = LIGHTS_OFF;
void setup() {
vfdIOSetup();
pinMode(LIGHT_SENSOR_PIN, INPUT);
pinMode(SLEEPING_STATUS_LED, OUTPUT);
/// This is just for checking button out of reset
/// The rest of the time, this pin (85/PJ0) is used as data pin to OLED.
pinMode(PUSH1, INPUT_PULLUP);
// start the serial library:
Serial.begin(9600);
delay(1000);
Serial.println("Starting the Sensor Monitor...");
Serial.println("Initializing LCD...");
myScreen.begin();
Serial.println("...LCD Initialized");
myScreen.setPenSolid(true);
myScreen.setFontSolid(false);
myScreen.setFontSize(2);
myScreen.setOrientation(2);
displayStartup();
Serial.println("Initializing Ethernet...");
// start the Ethernet connection:
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP.");
myScreen.gText(24, 120, "Failed!", redColour, blackColour);
myScreen.gText(24, 160, "Program halted.", redColour, blackColour);
// no point in carrying on, so do nothing forevermore:
for (;;)
;
}
Serial.println("Ethernet initialized...");
// give the Ethernet shield a second to initialize:
delay(1000);
/* Disable this check since the I/O pin used for PUSH1 is used by the OLED
// Turn on Ethernet status LEDs if PUSH1 is pressed at reboot
// Otherwise, they default to off.
if (digitalRead(PUSH1) == LOW) {
Ethernet.enableLinkLed();
Ethernet.enableActivityLed();
Serial.println("Ethernet LINK and ACTIVITY LEDs enabled.");
}
else {
Serial.println("Ethernet status LEDs disabled by default.");
// Following code can be used to turn off Ethernet status LEDs (default)
// GPIODirModeSet(ACTIVITY_LED_BASE, ACTIVITY_LED_PIN, GPIO_DIR_MODE_IN);
// GPIODirModeSet(LINK_LED_BASE, LINK_LED_PIN, GPIO_DIR_MODE_IN);
}
*/
Serial.println("Connecting to NTP....");
// DisplayCharacterMap(); // For testing
// Initialze the "previous" strings to empty
prevOutdoorTemp[0] = 0;
prevOutdoorLux[0] = 0;
prevOutdoorRH[0] = 0;
prevOutdoorP[0] = 0;
prevSensor3Temp[0] = 0;
prevWorkshopTemp[0] = 0;
prevFishTemp[0] = 0;
prevSmallPondTemp[0] = 0;
prevGarageDoor[0] = 0;
prevOutdoorBatt[0] = 0;
prevSensor3Batt[0] = 0;
prevTimeAndDate[0] = 0;
Udp.begin(localPort);
Serial.println("Setting up NTP...");
setSyncProvider(getNtpTime);
Serial.println("Setting up OLED.");
oled.begin(); // Initialize the OLED
oled.clear(PAGE); // Clear the display's internal memory
oled.clear(ALL); // Clear the library's display buffer
oled.command(DISPLAYOFF); // Turn off the display until other displays are enabled
}
void loop()
{
int lightSensor;
lightSensor = analogRead(LIGHT_SENSOR_ADC);
Serial.print("Light Sensor: ");
Serial.println(lightSensor);
switch (lightSensorState) {
case LIGHTS_OFF:
if (lightSensor > LIGHT_SENSOR_THRESHOLD) {
lightSensorState = LIGHTS_TURNED_ON;
}
else {
if (millis() - lastPressureQuery > PRESSURE_READINGS_TIME) {
lastPressureQuery = millis();
getPressure();
}
statusLEDstate = ~statusLEDstate; // Flash the LED
digitalWrite(SLEEPING_STATUS_LED, statusLEDstate);
digitalWrite(BACKLIGHT_PIN, 0);
vfdOff();
oled.clear(PAGE);
oled.display();
oled.command(DISPLAYOFF);
delay(LIGHTS_OFF_SLEEP_TIME);
}
break;
case LIGHTS_TURNED_ON:
statusLEDstate = 0;
digitalWrite(SLEEPING_STATUS_LED, statusLEDstate);
myScreen.clear();
digitalWrite(BACKLIGHT_PIN, 1);
displayWelcome();
displayTitles();
vfdOn();
oled.command(DISPLAYON);
lightSensorState = LIGHTS_ON;
break;
case LIGHTS_ON:
if (lightSensor > LIGHT_SENSOR_THRESHOLD) {
getAndDisplayTime();
getAndDisplayWeather();
getAndDisplaySensor3();
getAndDisplaySensor5();
getAndDisplayPond();
getAndDisplayWorkshop();
getAndDisplayGarage();
Serial.println("Disconnecting. Waiting 30 seconds before next query. ");
displayOLED();
displayVFD();
// The VFD display messages take 30 seconds, so no need for separate delay.
// delay(LIGHTS_ON_SLEEP_TIME);
}
else
{
lightSensorState = LIGHTS_OFF;
}
break;
case LIGHTS_TURNED_OFF: // State not needed
Serial.println("Invalid State! Changing state to LIGHTS_OFF.");
lightSensorState = LIGHTS_OFF;
break;
default: // Should never get here
Serial.println("Reached 'default' case in switch statement! Changing state to LIGHTS_OFF.");
lightSensorState = LIGHTS_OFF;
break;
}
}
void GetThingSpeakChannel(EthernetClient* c, const char* chan, const char* key, int results)
{
const int BUF_SIZE = 256;
char buffer[BUF_SIZE];
snprintf(buffer, BUF_SIZE, "GET /channels/%s/feeds.json?api_key=%s&results=%d", chan, key, results);
/// Serial.println(buffer);
c->println(buffer);
c->println();
}
void GetThingSpeakField(EthernetClient* c, const char* chan, const char* key, const char* field, int results)
{
const int BUF_SIZE = 256;
char buffer[BUF_SIZE];
snprintf(buffer, BUF_SIZE, "GET /channels/%s/field/%s.json?api_key=%s&results=%d", chan, field, key, results);
/// Serial.println(buffer);
c->println(buffer);
c->println();
}
void getAndDisplayWeather() {
JsonDocument doc;
unsigned int i = 0;
signed char c;
uint16_t battColor;
// if you get a connection, report back via serial:
if (client.connect(server, 80)) {
Serial.println("connected");
}
else {
// if you didn't get a connection to the server:
Serial.println("connection failed");
}
// Make a HTTP request for Weather Station
GetThingSpeakChannel(&client, WEATHERSTATION_CHANNEL, WEATHERSTATION_KEY, 1);
// Need to check for connection and wait for characters
// Need to timeout after some time, but not too soon before receiving response
// Initially just use delay(), but replace with improved code using millis()
delay(750);
while (client.connected()) {
/// Add a timeout with millis()
c = client.read();
if (c != -1) receiveBuffer[i++] = c;
if (i > sizeof(receiveBuffer) - 2) break; // Leave a byte for the null terminator
}
receiveBuffer[i] = '\0';
Serial.print("JSON received size: ");
Serial.println(i);
Serial.println("JSON buffer: ");
Serial.println(receiveBuffer);
Serial.println("");
client.stop();
DeserializationError error = deserializeJson(doc, receiveBuffer);
/*
"Parsing Program" code generated from ArduinoJson Assistant at arduinojson.org:
JsonObject channel = root["channel"];
long channel_id = channel["id"]; // 379945
const char* channel_name = channel["name"]; // "Weather-Station"
const char* channel_description = channel["description"]; // "Outdoor weather station using MSP430 LaunchPad and SENSORS BoosterPack. "
const char* channel_latitude = channel["latitude"]; // "0.0"
const char* channel_longitude = channel["longitude"]; // "0.0"
const char* channel_field1 = channel["field1"]; // "Temp-BME280"
const char* channel_field2 = channel["field2"]; // "Temp-TMP007-Ext"
const char* channel_field3 = channel["field3"]; // "Temp-TMP007-Int"
const char* channel_field4 = channel["field4"]; // "Temp-MSP430"
const char* channel_field5 = channel["field5"]; // "RH-BME280"
const char* channel_field6 = channel["field6"]; // "Pressure-BME280"
const char* channel_field7 = channel["field7"]; // "Lux-OPT3001"
const char* channel_field8 = channel["field8"]; // "Batt-Vcc"
const char* channel_created_at = channel["created_at"]; // "2017-12-07T00:28:34Z"
const char* channel_updated_at = channel["updated_at"]; // "2018-06-10T22:26:22Z"
long channel_last_entry_id = channel["last_entry_id"]; // 90649
*/
if (!error) {
JsonObject feeds0 = doc["feeds"][0];
const char* feeds0_created_at = feeds0["created_at"]; // "2018-06-10T22:26:23Z"
long feeds0_entry_id = feeds0["entry_id"]; // 90649
long Tf = strtol(feeds0["field3"], NULL, 10);
long lux = strtol(feeds0["field7"], NULL, 10);
long rh = strtol(feeds0["field5"], NULL, 10);
long p = strtol(feeds0["field6"], NULL, 10);
long wBatt = strtol(feeds0["field8"], NULL, 10);
strncpy(weatherTime, feeds0_created_at, TIMESIZE - 1);
weatherTime[TIMESIZE - 1] = '\0'; // hard-code a null terminator at end of string
Serial.println("Parsed JSON: ");
Serial.print("Created at: ");
Serial.println(feeds0_created_at);
Serial.print("Entry ID: ");
Serial.println(feeds0_entry_id);
snprintf(outdoorTemp, TEMPSIZE, "%3li.%li", Tf / 10, abs(Tf) % 10);
snprintf(outdoorLux, LUXSIZE, "%8li", lux);
snprintf(outdoorRH, RHSIZE, "%2li.%li", rh / 10, rh % 10);
snprintf(outdoorP, PSIZE, "%2li.%02li", p / 100, p % 100);
snprintf(outdoorBatt, BATTSIZE, "%li.%03li", wBatt / 1000, wBatt % 1000);
if (wBatt < 2600) battColor = redColour;
else battColor = greenColour;
if (feeds0_entry_id != last_weather_entry_id) {
last_weather_entry_id = feeds0_entry_id;
if (p != 0) {
if (hist_num_entries < hist_len) // If we haven't filled the buffer the first time after reset
{
p_hist[hist_num_entries++] = p;
}
else // Once buffer has filled, then use a circular buffer and track current postion with wraparound
{
p_hist[hist_position++] = p;
if (hist_position >= hist_len) hist_position = 0; // Wrap around if needed.
}
}
}
}
else
{
Serial.print("JSON parse failed with code: ");
Serial.println(error.c_str());
snprintf(outdoorTemp, TEMPSIZE, " N/A");
snprintf(outdoorLux, LUXSIZE, " N/A");
snprintf(outdoorRH, RHSIZE, " N/A");
snprintf(outdoorP, PSIZE, " N/A");
snprintf(outdoorBatt, BATTSIZE, " N/A");
strcpy(weatherTime, " N/A");
battColor = whiteColour;
}
myScreen.gText(layout.WeatherTempValue.x, layout.WeatherTempValue.y, prevOutdoorTemp, blackColour);
myScreen.gText(layout.WeatherTempValue.x, layout.WeatherTempValue.y, outdoorTemp);
strncpy(prevOutdoorTemp, outdoorTemp, TEMPSIZE);
myScreen.gText(layout.WeatherLuxValue.x, layout.WeatherLuxValue.y, prevOutdoorLux, blackColour);
myScreen.gText(layout.WeatherLuxValue.x, layout.WeatherLuxValue.y, outdoorLux);
strncpy(prevOutdoorLux, outdoorLux, LUXSIZE);
myScreen.gText(layout.WeatherRHValue.x, layout.WeatherRHValue.y, prevOutdoorRH, blackColour);
myScreen.gText(layout.WeatherRHValue.x, layout.WeatherRHValue.y, outdoorRH);
strncpy(prevOutdoorRH, outdoorRH, RHSIZE);
myScreen.gText(layout.WeatherPValue.x, layout.WeatherPValue.y, prevOutdoorP, blackColour);
myScreen.gText(layout.WeatherPValue.x, layout.WeatherPValue.y, outdoorP);
strncpy(prevOutdoorP, outdoorP, PSIZE);
myScreen.gText(layout.BattOutdoorValue.x, layout.BattOutdoorValue.y, prevOutdoorBatt, blackColour);
myScreen.gText(layout.BattOutdoorValue.x, layout.BattOutdoorValue.y, outdoorBatt, battColor);
strncpy(prevOutdoorBatt, outdoorBatt, BATTSIZE);
} // getAndDisplayWeather()
void getPressure() {
JsonDocument doc;
unsigned int i = 0;
signed char c;
// if you get a connection, report back via serial:
if (client.connect(server, 80)) {
Serial.println("connected");
}
else {
// if you didn't get a connection to the server:
Serial.println("connection failed");
}
// Make a HTTP request for Weather Station
GetThingSpeakField(&client, WEATHERSTATION_CHANNEL, WEATHERSTATION_KEY, "field6", 1);
// Need to check for connection and wait for characters
// Need to timeout after some time, but not too soon before receiving response
// Initially just use delay(), but replace with improved code using millis()
delay(750);
while (client.connected()) {
/// Add a timeout with millis()
c = client.read();
if (c != -1) receiveBuffer[i++] = c;
if (i > sizeof(receiveBuffer) - 2) break; // Leave a byte for the null terminator
}
receiveBuffer[i] = '\0';
Serial.print("JSON received size: ");
Serial.println(i);
Serial.println("JSON received (field6): ");
Serial.println(receiveBuffer);
Serial.println("");
client.stop();
DeserializationError error = deserializeJson(doc, receiveBuffer);
/*
"Parsing Program" code generated from ArduinoJson Assistant at arduinojson.org:
JsonObject channel = root["channel"];
long channel_id = channel["id"]; // 379945
const char* channel_name = channel["name"]; // "Weather-Station"
const char* channel_description = channel["description"]; // "Outdoor weather station using MSP430 LaunchPad and SENSORS BoosterPack. "
const char* channel_latitude = channel["latitude"]; // "0.0"
const char* channel_longitude = channel["longitude"]; // "0.0"
const char* channel_field6 = channel["field6"]; // "Pressure-BME280"
const char* channel_created_at = channel["created_at"]; // "2017-12-07T00:28:34Z"
const char* channel_updated_at = channel["updated_at"]; // "2018-06-10T22:26:22Z"
long channel_last_entry_id = channel["last_entry_id"]; // 90649
*/
if (!error) {
JsonObject feeds0 = doc["feeds"][0];
const char* feeds0_created_at = feeds0["created_at"]; // "2018-06-10T22:26:23Z"
long feeds0_entry_id = feeds0["entry_id"]; // 90649
long p = strtol(feeds0["field6"], NULL, 10);
Serial.println("Parsed JSON: ");
Serial.print("Pressure: ");
Serial.println(p);
Serial.print("Created at: ");
Serial.println(feeds0_created_at);
Serial.print("Entry ID: ");
Serial.println(feeds0_entry_id);
if (feeds0_entry_id != last_weather_entry_id) {
last_weather_entry_id = feeds0_entry_id;
if (p != 0 ) {
if (hist_num_entries < hist_len) // If we haven't filled the buffer the first time after reset
{
p_hist[hist_num_entries++] = p;
}
else // Once buffer has filled, then use a circular buffer and track current postion with wraparound
{
p_hist[hist_position++] = p;
if (hist_position >= hist_len) hist_position = 0; // Wrap around if needed.
}
}
}
Serial.print("hist_num_entries: ");
Serial.println(hist_num_entries);
Serial.print("hist_position: ");
Serial.println(hist_position);
}
else
{
Serial.print("JSON parse failed with code: ");
Serial.println(error.c_str());
}
} // getPressure()
void getAndDisplaySensor3() {
JsonDocument doc;
unsigned int i = 0;
signed char c;
uint16_t tempColor, battColor;
// if you get a connection, report back via serial:
if (client.connect(server, 80)) {
Serial.println("connected");
}
else {
// if you didn't get a connection to the server:
Serial.println("connection failed");
}
// Make a HTTP request for Sensor3's sensor
GetThingSpeakChannel(&client, SENSOR3TEMP_CHANNEL, SENSOR3TEMP_KEY, 1);
// Need to check for connection and wait for characters
// Need to timeout after some time, but not too soon before receiving response
// Initially just use delay(), but replace with improved code using millis()
delay(750);
while (client.connected()) {
/// Add a timeout with millis()
c = client.read();
if (c != -1) receiveBuffer[i++] = c;
if (i > sizeof(receiveBuffer) - 2) break; // Leave a byte for the null terminator
}
receiveBuffer[i] = '\0';
Serial.println("JSON received: ");
Serial.println(receiveBuffer);
Serial.println("");
client.stop();
DeserializationError error = deserializeJson(doc, receiveBuffer);
/*
"Parsing Program" code generated from ArduinoJson Assistant at arduinojson.org:
JsonObject channel = root["channel"];
long channel_id = channel["id"]; // 412285
const char* channel_name = channel["name"]; // "Sensor3's Temp"
const char* channel_description = channel["description"]; // "Sensor3's temperature sensor. "
const char* channel_latitude = channel["latitude"]; // "0.0"
const char* channel_longitude = channel["longitude"]; // "0.0"
const char* channel_field1 = channel["field1"]; // "Temp"
const char* channel_field2 = channel["field2"]; // "Vcc"
const char* channel_field3 = channel["field3"]; // "Loops"
const char* channel_field4 = channel["field4"]; // "Millis"
const char* channel_field5 = channel["field5"]; // "RSSI"
const char* channel_field6 = channel["field6"]; // "LQI"
const char* channel_created_at = channel["created_at"]; // "2018-01-26T17:19:35Z"
const char* channel_updated_at = channel["updated_at"]; // "2018-08-17T02:18:28Z"
long channel_last_entry_id = channel["last_entry_id"]; // 165702
*/
if (!error) {
JsonObject feeds0 = doc["feeds"][0];
const char* feeds0_created_at = feeds0["created_at"]; // "2018-06-10T22:26:23Z"
long feeds0_entry_id = feeds0["entry_id"]; // 90649
long Tsensor3 = strtol(feeds0["field1"], NULL, 10);
long sBatt = strtol(feeds0["field2"], NULL, 10);
strncpy(sensor3Time, feeds0_created_at, TIMESIZE - 1);
sensor3Time[TIMESIZE - 1] = '\0'; // hard-code a null terminator at end of string
Serial.println("Parsed JSON: ");
Serial.print("Created at: ");
Serial.println(feeds0_created_at);
Serial.print("Entry ID: ");
Serial.println(feeds0_entry_id);
tempColor = whiteColour;
if (sBatt < 2300) battColor = redColour;
else battColor = greenColour;
snprintf(sensor3Temp, TEMPSIZE, "%3li.%li", Tsensor3 / 10, abs(Tsensor3) % 10);
snprintf(sensor3Batt, BATTSIZE, "%li.%03li", sBatt / 1000, sBatt % 1000);
}
else
{
Serial.print("JSON parse failed with code: ");
Serial.println(error.c_str());
snprintf(sensor3Temp, TEMPSIZE, " N/A");
snprintf(sensor3Batt, BATTSIZE, " N/A");
strcpy(sensor3Time, " N/A");
battColor = whiteColour;
tempColor = whiteColour;
}
myScreen.gText(layout.Sensor3TempValue.x, layout.Sensor3TempValue.y, prevSensor3Temp, blackColour);
myScreen.gText(layout.Sensor3TempValue.x, layout.Sensor3TempValue.y, sensor3Temp, tempColor);
strncpy(prevSensor3Temp, sensor3Temp, TEMPSIZE);
myScreen.gText(layout.BattSensor3Value.x, layout.BattSensor3Value.y, prevSensor3Batt, blackColour);
myScreen.gText(layout.BattSensor3Value.x, layout.BattSensor3Value.y, sensor3Batt, battColor);
strncpy(prevSensor3Batt, sensor3Batt, BATTSIZE);
} // getAndDisplaySensor3()
void getAndDisplaySensor5() {
JsonDocument doc;
unsigned int i = 0;
signed char c;
uint16_t tempColor, battColor;
// if you get a connection, report back via serial:
if (client.connect(server, 80)) {
Serial.println("connected");
}
else {
// if you didn't get a connection to the server:
Serial.println("connection failed");
}
// Make a HTTP request for Sensor3's sensor
GetThingSpeakChannel(&client, TEMP5_CHANNEL, TEMP5_KEY, 1);
// Need to check for connection and wait for characters
// Need to timeout after some time, but not too soon before receiving response
// Initially just use delay(), but replace with improved code using millis()
delay(750);
while (client.connected()) {
/// Add a timeout with millis()
c = client.read();
if (c != -1) receiveBuffer[i++] = c;
if (i > sizeof(receiveBuffer) - 2) break; // Leave a byte for the null terminator
}
receiveBuffer[i] = '\0';
Serial.println("JSON received: ");
Serial.println(receiveBuffer);
Serial.println("");
client.stop();
DeserializationError error = deserializeJson(doc, receiveBuffer);
/*
"Parsing Program" code generated from ArduinoJson Assistant at arduinojson.org:
JsonObject channel = root["channel"];
long channel_id = channel["id"]; // 412284
const char* channel_name = channel["name"]; // "Indoor Temp 5"
const char* channel_description = channel["description"]; // "Indoor Temp Sensor #5"
const char* channel_latitude = channel["latitude"]; // "0.0"
const char* channel_longitude = channel["longitude"]; // "0.0"
const char* channel_field1 = channel["field1"]; // "Temp"
const char* channel_field2 = channel["field2"]; // "Vcc"
const char* channel_field3 = channel["field3"]; // "Loops"
const char* channel_field4 = channel["field4"]; // "Millis"
const char* channel_field5 = channel["field5"]; // "RSSI"
const char* channel_field6 = channel["field6"]; // "LQI"
const char* channel_created_at = channel["created_at"]; // "2018-01-26T17:18:49Z"
const char* channel_updated_at = channel["updated_at"]; // "2018-09-18T08:44:13Z"
long channel_last_entry_id = channel["last_entry_id"]; // 243115
*/
if (!error) {
JsonObject feeds0 = doc["feeds"][0];
const char* feeds0_created_at = feeds0["created_at"]; // "2018-06-10T22:26:23Z"
long feeds0_entry_id = feeds0["entry_id"]; // 90649
long T5 = strtol(feeds0["field1"], NULL, 10);
long B5 = strtol(feeds0["field2"], NULL, 10);
unsigned long remain5 = strtoul(feeds0["field4"], NULL, 10);
strncpy(sensor5Time, feeds0_created_at, TIMESIZE - 1);
sensor5Time[TIMESIZE - 1] = '\0'; // hard-code a null terminator at end of string
Serial.println("Parsed JSON: ");
Serial.print("Created at: ");
Serial.println(feeds0_created_at);
Serial.print("Entry ID: ");
Serial.println(feeds0_entry_id);
if (T5 > 850) tempColor = redColour;
else tempColor = greenColour;
if ( (B5 < LIPO_LO_BATT_LEVEL) || (remain5 < LIPO_LO_TIME_REMAIN) ) battColor = redColour;
else battColor = greenColour;
snprintf(sensor5Temp, TEMPSIZE, "%3li.%li", T5 / 10, abs(T5) % 10);
snprintf(sensor5Batt, BATTSIZE, "%li.%03li", B5 / 1000, B5 % 1000);
}
else
{
Serial.print("JSON parse failed with code: ");
Serial.println(error.c_str());
snprintf(sensor5Temp, TEMPSIZE, " N/A");
snprintf(sensor5Batt, BATTSIZE, " N/A");
strcpy(sensor5Time, " N/A");
battColor = whiteColour;
tempColor = whiteColour;
}
myScreen.gText(layout.Sensor5TempValue.x, layout.Sensor5TempValue.y, prevSensor5Temp, blackColour);
myScreen.gText(layout.Sensor5TempValue.x, layout.Sensor5TempValue.y, sensor5Temp, tempColor);
strncpy(prevSensor5Temp, sensor5Temp, TEMPSIZE);
myScreen.gText(layout.BattSensor5Value.x, layout.BattSensor5Value.y, prevSensor5Batt, blackColour);
myScreen.gText(layout.BattSensor5Value.x, layout.BattSensor5Value.y, sensor5Batt, battColor);
strncpy(prevSensor5Batt, sensor5Batt, BATTSIZE);
} // getAndDisplaySensor5()
void getAndDisplayWorkshop() {
JsonDocument doc;
unsigned int i = 0;
signed char c;
uint16_t battColor;
// if you get a connection, report back via serial:
if (client.connect(server, 80)) {
Serial.println("connected");
}
else {
// if you didn't get a connection to the server:
Serial.println("connection failed");
}
// Make a HTTP request for Sensor3's sensor
GetThingSpeakChannel(&client, TEMP4_CHANNEL, TEMP4_KEY, 1);
// Need to check for connection and wait for characters
// Need to timeout after some time, but not too soon before receiving response
// Initially just use delay(), but replace with improved code using millis()
delay(750);
while (client.connected()) {
/// Add a timeout with millis()
c = client.read();
if (c != -1) receiveBuffer[i++] = c;
if (i > sizeof(receiveBuffer) - 2) break; // Leave a byte for the null terminator
}
receiveBuffer[i] = '\0';
Serial.println("JSON received: ");
Serial.println(receiveBuffer);
Serial.println("");
client.stop();
DeserializationError error = deserializeJson(doc, receiveBuffer);
if (!error) {
JsonObject feeds0 = doc["feeds"][0];
const char* feeds0_created_at = feeds0["created_at"]; // "2018-06-10T22:26:23Z"
long feeds0_entry_id = feeds0["entry_id"]; // 90649
long T4 = strtol(feeds0["field1"], NULL, 10);
long B4 = strtol(feeds0["field2"], NULL, 10);
long remain4 = strtol(feeds0["field4"], NULL, 10);
strncpy(workshopTime, feeds0_created_at, TIMESIZE - 1);
workshopTime[TIMESIZE - 1] = '\0'; // hard-code a null terminator at end of string
Serial.println("Parsed JSON: ");
Serial.print("Created at: ");
Serial.println(feeds0_created_at);
Serial.print("Entry ID: ");
Serial.println(feeds0_entry_id);
if ( (B4 < LIPO_LO_BATT_LEVEL) || (remain4 < LIPO_LO_TIME_REMAIN))
battColor = redColour;
else
battColor = blackColour;
snprintf(workshopTemp, TEMPSIZE, "%3li.%li", T4 / 10, abs(T4) % 10);
}
else
{
Serial.print("JSON parse failed with code: ");
Serial.println(error.c_str());
snprintf(workshopTemp, TEMPSIZE, " N/A");
strcpy(workshopTime, " N/A");
battColor = blackColour;
}
myScreen.gText(layout.WorkshopTempValue.x, layout.WorkshopTempValue.y, prevWorkshopTemp, blackColour);
myScreen.gText(layout.WorkshopTempValue.x, layout.WorkshopTempValue.y, workshopTemp, whiteColour);
myScreen.gText(layout.WorkshopLoBat.x, layout.WorkshopLoBat.y, WorkshopLoBat, battColor);
strncpy(prevWorkshopTemp, workshopTemp, TEMPSIZE);
} // getAndDisplayWorkshop()
void getAndDisplayPond() {
JsonDocument doc;
unsigned int i = 0;
signed char c;
// if you get a connection, report back via serial:
if (client.connect(server, 80)) {
Serial.println("connected");
}
else {
// if you didn't get a connection to the server:
Serial.println("connection failed");
}
// Make a HTTP request for Pond sensor
GetThingSpeakChannel(&client, POND_CHANNEL, POND_KEY, 1);
// Need to check for connection and wait for characters
// Need to timeout after some time, but not too soon before receiving response
// Initially just use delay(), but replace with improved code using millis()
delay(750);
while (client.connected()) {
/// Add a timeout with millis()
c = client.read();
if (c != -1) receiveBuffer[i++] = c;
if (i > sizeof(receiveBuffer) - 2) break; // Leave a byte for the null terminator
}
receiveBuffer[i] = '\0';
Serial.println("JSON received: ");
Serial.println(receiveBuffer);
Serial.println("");
client.stop();
DeserializationError error = deserializeJson(doc, receiveBuffer);
/*
"Parsing Program" code generated from ArduinoJson Assistant at arduinojson.org:
JsonObject channel = root["channel"];
long channel_id = channel["id"]; // 572681
const char* channel_name = channel["name"]; // "Pond Sensor"
const char* channel_description = channel["description"]; // "Various sensor readings at the pond. "
const char* channel_latitude = channel["latitude"]; // "0.0"
const char* channel_longitude = channel["longitude"]; // "0.0"
const char* channel_field1 = channel["field1"]; // "Small Pond"
const char* channel_field2 = channel["field2"]; // "Large Pond"
const char* channel_field3 = channel["field3"]; // "Vcc"
const char* channel_field4 = channel["field4"]; // "Loops"
const char* channel_field5 = channel["field5"]; // "WiFi RSSI"
const char* channel_field6 = channel["field6"]; // "Aerator Status"
const char* channel_created_at = channel["created_at"]; // "2018-09-09T19:02:08Z"
const char* channel_updated_at = channel["updated_at"]; // "2018-09-09T23:27:23Z"