forked from mlukasek/M5_NightscoutMon
-
Notifications
You must be signed in to change notification settings - Fork 2
/
M5_NightscoutMon.ino
1290 lines (991 loc) · 47.8 KB
/
M5_NightscoutMon.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
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEScan.h>
#include <BLEAdvertisedDevice.h>
/* M5Stack Nightscout monitor
Copyright (C) 2019 Johan Degraeve
Connects to xdripswift via Bluetooth. Receives readings and slope. Shows both on screen.
Settings can be configured via xdripswift.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
Original code by Martin Lukasek <[email protected]>
This software uses some 3rd party libraries:
IniFile by Steve Marple <[email protected]> (GNU LGPL v2.1)
ArduinoJson by Benoit BLANCHON (MIT License)
IoT Icon Set by Artur Funk (GPL v3)
Additions to the code:
Peter Leimbach (Nightscout token)
*/
#include <M5Stack.h>
#include <Preferences.h>
#include <WiFi.h>
#include <WiFiMulti.h>
#include <HTTPClient.h>
#include "time.h"
// #include <util/eu_dst.h>
#define ARDUINOJSON_USE_LONG_LONG 1
#include <ArduinoJson.h>
#include "Free_Fonts.h"
#include "IniFile.h"
#include "M5NSconfig.h"
#include <cstring>
#include <string>
// needed to act as BLE server
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>
#include <BLE2902.h>
// if debugLogging then there's more logging, haha
const bool debugLogging = true;
extern const unsigned char wifi2_icon16x16[];
Preferences preferences;
tConfig cfg;
const char * ntpServer = "pool.ntp.org"; // "time.nist.gov", "time.google.com"
struct err_log_item {
struct tm err_time;
int err_code;
} err_log[10];
int err_log_ptr = 0;
int err_log_count = 0;
int icon_xpos[1] = {144};
int icon_ypos[1] = {0};
#ifndef min
#define min(a,b) (((a) < (b)) ? (a) : (b))
#endif
WiFiMulti WiFiMulti;
unsigned long msCount;
unsigned long msStart;
static uint8_t lcdBrightness = 10;
static char *iniFilename = "/M5NS.INI";
// color to use for text, also for slope arrow
static uint16_t textColor = TFT_WHITE;
// color to use for background
static uint16_t backGroundColor = TFT_BLACK;
// rotation to use
static uint8_t rotation = 1;// 1 = horizontal, normal; 2 = 90 clockwise, 3 = upside down, 4 = 270 clockwise or 90 anti-clockwise
// brightness
static uint8_t brightness = 100;// from 0 to 100
// milliseconds since start of last call to connectToWiFiIfNightScoutUrlExists from within nightscout check
unsigned long milliSecondsSinceLastCallToWifiConnectFromWithinNightScoutcheck = 0;
const unsigned long minimumTimeBetweenTwoCallsToWifiConnectFromWithinNightScoutcheck = 60000;
//timestamp of latest nightscout reading, initially 0
unsigned long timeStampLatestBgReadingInSecondsUTC = 0;
// to temporary store the latest shown string, each refresh, it will be checked if it has changed and if not then no redraw, otherwise the screen flickers annoyingly
const int senssgvStringLength = 30;
char previousSensSgvStr[senssgvStringLength];
DynamicJsonDocument JSONdoc(16384);
struct NSinfo {
uint64_t rawtime = 0;
time_t sensTime = 0; /// time in seconds
struct tm sensTm;
char sensDir[32];
float sensSgvMgDl = 0;
float sensSgv = 0;
int arrowAngle = 180;
} ns;
int previousArrowAngle = 180;
// if it's not an M5StackC, then it's a normal M5Stack
bool isM5StickC = false;
//////// BLE PROPERTIES ///////
// used for creating random password
char *letters = "abcdefghijklmnopqrstuvwxyz0123456789";
BLEServer *pServer = NULL;
BLECharacteristic * pRxTxCharacteristic;
BLEService *pService;
bool bleDeviceConnected = false;
uint8_t txValue = 0;
// device name that BLE client will see when scanning
const String BLE_DeviceName = "M5Stack";
// service and characteristic uuid for ble
// characteristic will be used for read and write
const String BLE_SERVICE_UUID = "AF6E5F78-706A-43FB-B1F4-C27D7D5C762F";
const String BLE_CHARACTERISTIC_UUID = "6D810E9F-0983-4030-BDA7-C7C9A6A19C1C";
// is BLE communication needed or not ? always true. Could be used in future, for instance by setting to false in the config file, ble would never start up and so save energy
const bool useBLE = true;
// maximum number of bytes to send in one BLE packet
const int maxBytesInOneBLEPacket = 20;
// will be set to true if we find a blepassword in the ini file
bool useConfiguredBlePassword = false;
// is authentication done or not, in case not authenticated, we won't accept any reading or anything else
bool bleAuthenticated = false;
// will be set to false as soon as BLE setup is fully finished
bool initialBLEStartUpOnGoing = true;
///// as we may work without wifi, we need to be able to set the time, we can use BLE client for that. Following variable tell us if we've already retrieved the time from the server
///// and also the moment when retrieved is noted as the number of milliseconds since start
///// we retrieve the local time, so we don't need to care about timezone, daylight savings etc.
// time difference in seconds, between local time and utc time - to get UTC time, do localTimeStampInSecondsRetrievedFromBLEClient - diffBetweenLocalTimeAndUTCTime
unsigned long diffBetweenLocalTimeAndUTCTime = 0;
// local time, in seconds since 1.1.1970 retrieved from client - this value remains fixed once retrieved. Value 0 means not yet retrieved.
unsigned long localTimeStampInSecondsRetrievedFromBLEClient = 0;
// time in milliseconds since start of the sketch, when timeStampInSecondsRetrievedFromBLEClient was received
unsigned long milliSecondsSinceRetrievalTimeStampInSecondsRetrievedFromBLEClient = 0;
// will hold value received from BLE client, defined here once to avoid heap fragmentation
byte rxValueAsByteArray[maxBytesInOneBLEPacket];
// when receiving wifi names and passwords, xdrip will split longer strings in multiple packets. The first packet will have as fourth byte (ie index 3),
// the number of the wifi and/or password being changed, this needs to be stored as a global variable, because only the first packet has that information
int indexForWifiNamesAndPasswords = 0;
////// NightScout properties
char NSurl[128];
/////// FUNCTIONS
// local time in seconds since 1.1.1970 , if return value is 0, then failed
unsigned long getLocalTimeInSeconds() {
// Start by trying timestamp received from BLE client, if so calculate it and return if not get it from ble server
// it is only after having passed here two times, that this can succeed, if localTimeStampInSecondsRetrievedFromBLEClient = 0, and ble is connected then we'll get the time
if (localTimeStampInSecondsRetrievedFromBLEClient > 0L) {
return localTimeStampInSecondsRetrievedFromBLEClient + (millis() - milliSecondsSinceRetrievalTimeStampInSecondsRetrievedFromBLEClient)/1000;
}
Serial.println("localTimeStampInSecondsRetrievedFromBLEClient <= 0");
/// using ble, only if connected and authenticated, ask client to send time
if (bleDeviceConnected && bleAuthenticated && pRxTxCharacteristic != NULL) {
Serial.println(F("Sending opcode readTimeStampRx to client"));
sendTextToBLEClient("", 0x11, 0);
}
/// next try using standard Arduino methods, ie ntp server
tm dateandtime;
if (getLocalTime(&dateandtime)) {
if (debugLogging) {
Serial.println(F("getLocalTime is true"));
}
time_t utcTimeInSeconds = mktime(&dateandtime);
if (debugLogging) {Serial.print("in getLocalTimeInSeconds, utcTimeInSeconds = ");Serial.println(utcTimeInSeconds);}
// if diffBetweenLocalTimeAndUTCTime still 0, then set it now, based on time_zone and dst in config
if (diffBetweenLocalTimeAndUTCTime == 0) {
// we assume here time_zone and dst are correctly set, which should be the case as getLocalTime was working ok
diffBetweenLocalTimeAndUTCTime = (unsigned long)((long)cfg.timeZone + (long)cfg.dst);
}
return (unsigned long) utcTimeInSeconds + diffBetweenLocalTimeAndUTCTime;
} else {
if (debugLogging) {Serial.println("getLocalTime is false");}
}
/// try setting up ntp , this will only work if wifi is on, after that, no matter if succeeded or not we will continue trying to get time from ble client
/// using ntp
if((WiFiMulti.run() == WL_CONNECTED)) {
if (debugLogging) {Serial.println("calling configtime");}
configTime(cfg.timeZone, cfg.dst, ntpServer, "time.nist.gov", "time.google.com");
}
return 0;
}
// utc time in seconds since 1.1.1970 , if return value is 0, then failed
unsigned long getUTCTimeInSeconds() {
unsigned long localTimeInSeconds = getLocalTimeInSeconds();
if (localTimeInSeconds > 0) {
return localTimeInSeconds - diffBetweenLocalTimeAndUTCTime;
} else {
return 0;
}
}
void setPageIconPos() {
// wifi icon, top left
icon_xpos[0] = 0;
icon_ypos[0] = 0;
}
void drawIcon(int16_t x, int16_t y, const uint8_t *bitmap, uint16_t color) {
int16_t w = 16;
int16_t h = 16;
int32_t i, j, byteWidth = (w + 7) / 8;
for (j = 0; j < h; j++) {
for (i = 0; i < w; i++) {
if (pgm_read_byte(bitmap + j * byteWidth + i / 8) & (128 >> (i & 7))) {
M5.Lcd.drawPixel(x + i, y + j, color);
} else {
M5.Lcd.drawPixel(x + i, y + j, backGroundColor);
}
}
}
}
void connectToWiFiIfNightScoutUrlExists() {
// check if nightscouturl exists, otherwise don't even try to connect
if ( sizeOfStringInCharArray(cfg.url, 64) == 0) {
return;
}
if((WiFiMulti.run() == WL_CONNECTED)) {
return;
}
WiFi.mode(WIFI_STA);
WiFi.disconnect();
delay(100);
Serial.println(F("WiFi connect start"));
// We start by connecting to a WiFi network
for(int i=0; i<=9; i++) {
if((cfg.wlanssid[i][0]!=0) && (cfg.wlanpass[i][0]!=0)) {
if (debugLogging) {
Serial.print(F("Adding access point "));Serial.print(cfg.wlanssid[i]);Serial.print(F(" with password "));Serial.println(cfg.wlanpass[i]);
}
WiFiMulti.addAP(cfg.wlanssid[i], cfg.wlanpass[i]);
}
}
Serial.println(F("Wait for WiFi... "));
if (WiFiMulti.run() != WL_CONNECTED) {
Serial.println(F("Wifi not connected"));
yield();
return;
}
Serial.println(F(""));
Serial.println(F("WiFi connected to SSID ")); Serial.println(WiFi.SSID());
Serial.println(F(""));
Serial.println(F("Connection done"));
}
// the setup routine runs once when M5Stack starts up
void setup() {
// initialize previousSensSgvStr
strcpy(previousSensSgvStr, "");
// initialize the M5Stack object
M5.begin();
// set rotation
M5.Lcd.setRotation(rotation);
// set brightness
M5.Lcd.setBrightness(brightness);
// set text color foreground and backGroundColor
M5.Lcd.setTextColor(textColor, backGroundColor);
// prevent button A "ghost" random presses
Wire.begin();
SD.begin();
// Lcd display
M5.Lcd.setBrightness(100);
M5.Lcd.fillScreen(backGroundColor);
M5.Lcd.setCursor(0, 0);//(0, 0, 1) for mini
M5.Lcd.setTextSize(2);// 1 for mini
yield();
Serial.print(F("Free Heap: ")); Serial.println(ESP.getFreeHeap());
uint8_t cardType = SD.cardType();
if(cardType == CARD_NONE){
Serial.println(F("No SD card attached"));
M5.Lcd.println(F("No SD card attached"));
} else {
readConfiguration(iniFilename, &cfg);
lcdBrightness = cfg.brightness1;
M5.Lcd.setBrightness(lcdBrightness);
if (sizeOfStringInCharArray(cfg.blepassword, 64) >0) {
useConfiguredBlePassword = true;
}
yield();
preferences.begin("M5StackNS", false);
if(preferences.getBool("SoftReset", false)) {
// no startup sound after soft reset and remove the SoftReset key
preferences.remove("SoftReset");
}
preferences.end();
delay(1000);
M5.Lcd.fillScreen(backGroundColor);
M5.Lcd.setBrightness(lcdBrightness);
connectToWiFiIfNightScoutUrlExists();
yield();// seems to be to let the board to things in the background, probably related to calling connectToWiFiIfNightScoutUrlExists
M5.Lcd.setBrightness(lcdBrightness);
M5.Lcd.fillScreen(backGroundColor);
setPageIconPos();
// stat startup time
msStart = millis();
// update glycemia now
msCount = msStart-16000L;
configureTargetServerAndUrl(cfg.url, cfg.token);
}
if (useBLE) {
Serial.println(F("Bluetooth is on, open the xdrip app on iOS device and scan for M5Stack"));
M5.Lcd.println(F("Bluetooth is on, open the xdrip app on iOS device and scan for M5Stack"));
setupBLE();
}
}
int readNightscout() {
int err=0;
if((WiFiMulti.run() == WL_CONNECTED)) {
Serial.println(F("In readNightscout"));
HTTPClient http;
char tmpstr[32];/// DELETE THIS OR CHECK FIRST IF DETAILED DEBUGGING IS ENABLED WITH EXTRA FLAG, TO AVOID THAT THIS GETS ALLOCATED EACH TIME AGAIN AND AGAIN
M5.Lcd.fillRect(icon_xpos[0], icon_ypos[0], 16, 16, backGroundColor);
drawIcon(icon_xpos[0], icon_ypos[0], (uint8_t*)wifi2_icon16x16, TFT_BLUE);
Serial.print(F("JSON query NSurl = \'"));Serial.print(NSurl);Serial.print(F("\'\n"));
http.begin(NSurl); //HTTP
Serial.print(F("[HTTP] GET...\n"));
// start connection and send HTTP header
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
Serial.printf("[HTTP] GET... code: %d\n", httpCode);
// file found at server
if(httpCode == HTTP_CODE_OK) {
String json = http.getString();
Serial.print(F("Free Heap = ")); Serial.println(ESP.getFreeHeap());
auto JSONerr = deserializeJson(JSONdoc, json);
Serial.println(F("JSON deserialized OK"));
JsonArray arr=JSONdoc.as<JsonArray>();
Serial.print(F("JSON array size = ")); Serial.println(arr.size());
if (JSONerr || arr.size()==0) { //Check for errors in parsing
if(JSONerr) {
err=1001; // "JSON parsing failed"
} else {
err=1002; // "No data from Nightscout"
}
} else {
JsonObject obj;
int sgvindex = 0;
do {
obj=JSONdoc[sgvindex].as<JsonObject>();
sgvindex++;
} while ((!obj.containsKey(F("sgv"))) && (sgvindex<(arr.size()-1)));
sgvindex--;
if(sgvindex<0 || sgvindex>(arr.size()-1))
sgvindex=0;
ns.rawtime = JSONdoc[sgvindex][F("date")].as<long long>(); // sensTime is time in milliseconds since 1970, something like 1555229938118
strlcpy(ns.sensDir, JSONdoc[sgvindex][F("direction")] | "N/A", 32);
ns.sensSgv = JSONdoc[sgvindex][F("sgv")]; // get value of sensor measurement
ns.sensTime = ns.rawtime / 1000; // no milliseconds, since 2000 would be - 946684800, but ok
timeStampLatestBgReadingInSecondsUTC = ns.sensTime;
ns.sensSgvMgDl = ns.sensSgv;
// internally we work in mmol/L
ns.sensSgv/=18.0;
localtime_r(&ns.sensTime, &ns.sensTm);
setNsArrowAngle();
// screen should be updated
updateGlycemia();
Serial.print(F("sensTime = "));
Serial.print(ns.sensTime);
sprintf(tmpstr, " (JSON %lld)", (long long) ns.rawtime);
Serial.print(tmpstr);
sprintf(tmpstr, " = %s", ctime(&ns.sensTime));
Serial.print(tmpstr);
Serial.print(F("sensSgv = "));
Serial.println(ns.sensSgv);
Serial.print(F("sensDir = "));
Serial.println(ns.sensDir);
// Serial.print(ns.sensTm.tm_year+1900); Serial.print(F(" / ")); Serial.print(ns.sensTm.tm_mon+1); Serial.print(F(" / ")); Serial.println(ns.sensTm.tm_mday);
Serial.print(F("Sensor time: ")); Serial.print(ns.sensTm.tm_hour); Serial.print(F(":")); Serial.print(ns.sensTm.tm_min); Serial.print(F(":")); Serial.print(ns.sensTm.tm_sec); Serial.print(F(" DST ")); Serial.println(ns.sensTm.tm_isdst);
}
} else {
err=httpCode;
}
} else {
Serial.print(F("httpCode = "));Serial.print(httpCode);Serial.print(F(", errorToString = "));Serial.println(http.errorToString(httpCode));
err=httpCode;
}
http.end();
if(err!=0)
return err;
} else {
if (millis() - milliSecondsSinceLastCallToWifiConnectFromWithinNightScoutcheck > minimumTimeBetweenTwoCallsToWifiConnectFromWithinNightScoutcheck) {
connectToWiFiIfNightScoutUrlExists();
yield();
milliSecondsSinceLastCallToWifiConnectFromWithinNightScoutcheck = millis();
}
}
M5.Lcd.fillRect(icon_xpos[0], icon_ypos[0], 16, 16, backGroundColor);
return err;
}
void updateGlycemia() {
char tmpstr[255];// MAKE GLOBAL AND AVOID RECREATION each TIME ???
M5.Lcd.setTextDatum(TL_DATUM);
M5.Lcd.setTextSize(1);
M5.Lcd.setCursor(0, 0);
if (M5.Lcd.width() >= 320){
M5.Lcd.setFreeFont(FSSB24);
} else {
M5.Lcd.setFreeFont(FSSB18);
}
sprintf(tmpstr, "Glyk: %4.1f %s", ns.sensSgv, ns.sensDir);
Serial.println(tmpstr);
char sensSgvStr[senssgvStringLength];
strcpy(sensSgvStr, "---");
struct tm timeinfo;
// if we can't get timeinfo then skip it all
unsigned long utcTimeInSeconds = getUTCTimeInSeconds();
if (utcTimeInSeconds > 0L) {
Serial.print(F("timeStampLatestBgReadingInSecondsUTC = ")); Serial.println(timeStampLatestBgReadingInSecondsUTC);
Serial.print(F("in updateGlycemia utcTimeInSeconds = ")); Serial.println(utcTimeInSeconds);
if (utcTimeInSeconds > timeStampLatestBgReadingInSecondsUTC + (5 * 60 + 10) || ns.sensSgvMgDl == 0) {
Serial.println(F("utcTimeInSeconds > timeStampLatestBgReadingInSecondsUTC + (5 * 60 + 10) or ns.sensSgvMgDl == 0, not showing value"));
// latest nightscout reading is more than 5 minutes old, don't show the value - value is "---"
// send heartbeat message - this will wake up xDrip4iOS, will fetch reading (either from NS or Libreview) and send back to M5Stack
sendTextToBLEClient("", 0x21, 0);
} else {
if( cfg.show_mgdl == 0 ) {
if(ns.sensSgvMgDl<100) {
sprintf(sensSgvStr, "%2.0f", ns.sensSgvMgDl);
} else {
sprintf(sensSgvStr, "%3.0f", ns.sensSgvMgDl);
}
} else {
Serial.println("before checking ns.sensSgv");
if(ns.sensSgv<10) {
Serial.println("ns.sensSgv < 10");
sprintf(sensSgvStr, "%3.1f", ns.sensSgv);
} else {
Serial.println("ns.sensSgv >= 10");
sprintf(sensSgvStr, "%4.1f", ns.sensSgv);
if (M5.Lcd.width() >= 320){
M5.Lcd.setFreeFont(FSSB18);
} else {
M5.Lcd.setFreeFont(FSSB12);
}
}
}
}
} else {
Serial.println(F("could not get local time info"));
}
boolean previousEqualToNew = true;
// check if the string to show is new
if (strlen(sensSgvStr) != strlen(previousSensSgvStr)) {
previousEqualToNew = false;
} else {
if (strcmp(previousSensSgvStr, sensSgvStr) != 0) {
previousEqualToNew = false;
}
}
if (previousArrowAngle != ns.arrowAngle) {
previousEqualToNew = false;
previousArrowAngle = ns.arrowAngle;
}
// if strings is new, then display the new string and copy to previousSensSgvStr
if (!previousEqualToNew) {
M5.Lcd.fillRect(0, 0, M5.Lcd.width(), M5.Lcd.height(), backGroundColor);
if (isM5StickC) {
M5.Lcd.setTextSize(1);
} else {
M5.Lcd.setTextSize(4);
}
M5.Lcd.setTextDatum(MC_DATUM);
M5.Lcd.drawString(sensSgvStr, M5.Lcd.width()/2, M5.Lcd.height()/2, GFXFF);
/// draw arrow
int ay=0;
if(ns.arrowAngle>=45)
ay=4;
else if(ns.arrowAngle>-45)
ay=18;
else
ay=30;
if (strcmp(sensSgvStr, "---") != 0) {
if(ns.arrowAngle!=180) {
if (isM5StickC) {
drawArrow(M5.Lcd.width() - 40, 40, 10, ns.arrowAngle+85, 30, 30, textColor);//(int x, int y, int asize, int aangle, int pwidth, int plength, uint16_t color){
} else {
drawArrow( M5.Lcd.width() - 40, ay, 10, ns.arrowAngle+85, 28, 28, textColor);
}
}
}
// copy sensSgvStr to previousSensSgvStr
for (int i = 0; i < senssgvStringLength; i++) {
previousSensSgvStr[i] = sensSgvStr[i];
}
}
}
// the loop routine runs over and over again forever
void loop(){
//Serial.println(F("in loop"));
delay(20);
unsigned long utcTimeInSeconds = getUTCTimeInSeconds();
// updateglycemia and call readNightScout every 120 seconds, or if latest reading is more than 2 minutes old, then check every 15 seconds, or if utcTimeInSeconds is still 0
// call to updateGlycemia here is only needed to make sure that if there's no recent reading, younger than 5 minutes, to make sure --- is shown
// if readNightScout results in a new reading, then this will also call updateGlyecemia
if((millis()-msCount>120000L) || ((millis()-msCount>5000) && utcTimeInSeconds == 0L) || ((millis()-msCount>15000L) && utcTimeInSeconds > 0L && (utcTimeInSeconds-timeStampLatestBgReadingInSecondsUTC>120L))) {
updateGlycemia();
readNightscout();
msCount = millis();
} else {
if((cfg.restart_at_logged_errors>0) && (err_log_count>=cfg.restart_at_logged_errors)) {
preferences.begin("M5StackNS", false);
preferences.putBool("SoftReset", true);
preferences.end();
ESP.restart();
}
}
M5.update();
}
//////// helper functions
void configureTargetServerAndUrl(char *url, char *token) {
if(strncmp(url, "http", 4))
strcpy(NSurl,"https://");
else
strcpy(NSurl,"");
strcat(NSurl,url);
strcat(NSurl,"/api/v1/entries.json");
if ((token!=NULL) && (strlen(token)>0)){
strcat(NSurl,"?token=");
strcat(NSurl,token);
}
}
void setNsArrowAngle() {
previousArrowAngle = ns.arrowAngle;
ns.arrowAngle = 180;
if(strcmp(ns.sensDir,"DoubleDown")==0)
ns.arrowAngle = 90;
else
if(strcmp(ns.sensDir,"SingleDown")==0)
ns.arrowAngle = 75;
else
if(strcmp(ns.sensDir,"FortyFiveDown")==0)
ns.arrowAngle = 45;
else
if(strcmp(ns.sensDir,"Flat")==0)
ns.arrowAngle = 0;
else
if(strcmp(ns.sensDir,"FortyFiveUp")==0)
ns.arrowAngle = -45;
else
if(strcmp(ns.sensDir,"SingleUp")==0)
ns.arrowAngle = -75;
else
if(strcmp(ns.sensDir,"DoubleUp")==0)
ns.arrowAngle = -90;
else
if(strcmp(ns.sensDir,"NONE")==0)
ns.arrowAngle = 180;
else
if(strcmp(ns.sensDir,"NOT COMPUTABLE")==0)
ns.arrowAngle = 180;
}
void drawArrow(int x, int y, int asize, int aangle, int pwidth, int plength, uint16_t color){
float dx = (asize-10)*cos(aangle-90)*PI/180+x; // calculate X position
float dy = (asize-10)*sin(aangle-90)*PI/180+y; // calculate Y position
float x1 = 0; float y1 = plength;
float x2 = pwidth/2; float y2 = pwidth/2;
float x3 = -pwidth/2; float y3 = pwidth/2;
float angle = aangle*PI/180-135;
float xx1 = x1*cos(angle)-y1*sin(angle)+dx;
float yy1 = y1*cos(angle)+x1*sin(angle)+dy;
float xx2 = x2*cos(angle)-y2*sin(angle)+dx;
float yy2 = y2*cos(angle)+x2*sin(angle)+dy;
float xx3 = x3*cos(angle)-y3*sin(angle)+dx;
float yy3 = y3*cos(angle)+x3*sin(angle)+dy;
M5.Lcd.fillTriangle(xx1,yy1,xx3,yy3,xx2,yy2, color);
M5.Lcd.drawLine(x, y, xx1, yy1, color);
M5.Lcd.drawLine(x+1, y, xx1+1, yy1, color);
M5.Lcd.drawLine(x, y+1, xx1, yy1+1, color);
M5.Lcd.drawLine(x-1, y, xx1-1, yy1, color);
M5.Lcd.drawLine(x, y-1, xx1, yy1-1, color);
M5.Lcd.drawLine(x+2, y, xx1+2, yy1, color);
M5.Lcd.drawLine(x, y+2, xx1, yy1+2, color);
M5.Lcd.drawLine(x-2, y, xx1-2, yy1, color);
M5.Lcd.drawLine(x, y-2, xx1, yy1-2, color);
}
void byteArrayToHexString(byte array[], unsigned int len, char buffer[])
{
for (unsigned int i = 0; i < len; i++)
{
byte nib1 = (array[i] >> 4) & 0x0F;
byte nib2 = (array[i] >> 0) & 0x0F;
buffer[i*2+0] = nib1 < 0xA ? '0' + nib1 : 'A' + nib1 - 0xA;
buffer[i*2+1] = nib2 < 0xA ? '0' + nib2 : 'A' + nib2 - 0xA;
}
buffer[len*2] = '\0';
}
// returns index of first occurrence of value '0', which can be considered as end of string
// maxsize must be set to size of the array, for some reason getting the size of stringtext by call .lenght() or sizeof doesn't work. To avoid run-time errors, the maxsize must be passed as parameter
int sizeOfStringInCharArray(char stringtext[], int arraysize) {
int returnValue = 0;
while (uint8_t(stringtext[returnValue]) != 0 && returnValue < arraysize) {
returnValue = returnValue + 1;
}
return returnValue;
}
// sends opCode and textToSend to characteristic. Possibly split in multiple packets
// first byte = opcode, second byte = packet number, third byte = number of packets in total
void sendTextToBLEClient(char * textToSend, uint8_t opCode, int maximumSizeOfTextToSend) {
int sizeOfTextToSend = sizeOfStringInCharArray(textToSend, maximumSizeOfTextToSend);
// if sizeOfTextToSend = 0, then it means there's just an opcode being sent
if (sizeOfTextToSend == 0) {
uint8_t dataToSend[3];
dataToSend[0] = opCode;
dataToSend[1] = 0x01;
dataToSend[2] = 0x01;
pRxTxCharacteristic->setValue(dataToSend, 3);
pRxTxCharacteristic->notify();
return;
}
// 20 (maxBytesInOneBLEPacket) bytes per packet. First byte = opcode, second byte is packet number, third byte = total number of packets
// text may be longer than maximum ble packet size, need to split up, this variable tells us how many characters are already sent
int charactersSent = 0;
// number of packets is total size / (maxBytesInOneBLEPacket - 3) , possibly + 1
int totalNumberOfPacketsToSent = sizeOfTextToSend/(maxBytesInOneBLEPacket - 3);
if (sizeOfTextToSend > totalNumberOfPacketsToSent * (maxBytesInOneBLEPacket - 3)) {
totalNumberOfPacketsToSent++;
}
// number of the next packet to send
int numberOfNextPacketToSend = 1;
// send them one by one
while (charactersSent < sizeOfTextToSend) {
// calculate size of packet to send
int sizeOfNextPacketToSend = maxBytesInOneBLEPacket;
if (numberOfNextPacketToSend == totalNumberOfPacketsToSent) {
sizeOfNextPacketToSend = 3 + (sizeOfTextToSend - charactersSent);//First byte = opcode, second byte is packet number, third byte = total number of packets, rest is content
}
// craete and populate dataToSend
uint8_t dataToSend[sizeOfNextPacketToSend];
dataToSend[0] = opCode;
dataToSend[1] = numberOfNextPacketToSend;
dataToSend[2] = totalNumberOfPacketsToSent;
for (int i = 0; i < sizeOfNextPacketToSend - 3; i++) {
dataToSend[i + 3] = uint8_t(textToSend[charactersSent + i]);
}
// send the data
if (debugLogging) {
Serial.print(F("sending packet "));Serial.print(numberOfNextPacketToSend);Serial.print(F(" for text "));Serial.print(textToSend);Serial.print(F(" with opcode "));Serial.print(opCode);Serial.println(F(" to client"));
}
pRxTxCharacteristic->setValue(dataToSend, sizeOfNextPacketToSend);
pRxTxCharacteristic->notify();
// increase charactersSent
charactersSent = charactersSent + (sizeOfNextPacketToSend - 3);
// increase numberOfNextPacketToSend
numberOfNextPacketToSend++;
}
}
///////// BLE declarations
class BLECharacteristicCallBack: public BLECharacteristicCallbacks {
void onWrite(BLECharacteristic *pCharacteristic) {
std::string rxValue = pCharacteristic->getValue();
if (rxValue.length() > 0) {
for (int i = 0; i < maxBytesInOneBLEPacket;i++) {
rxValueAsByteArray[i] = 0x00;
}
memcpy(rxValueAsByteArray, rxValue.c_str(), rxValue.length());
// only for logging
if (debugLogging) {
char rxValueAsHexString[rxValue.length() * 2] = "";
byteArrayToHexString(rxValueAsByteArray, rxValue.length(), rxValueAsHexString);
Serial.print(F("Received Value from BLE client : "));Serial.println(rxValueAsHexString);
}
// see what server has been sending
byte opCode = rxValueAsByteArray[0];
switch (opCode) {
/// codes for writing from client to server
case 0x01:{
Serial.println(F("received opcode for writeNightScoutUrlTx"));
// if it's a long url, we will receive multiple packets, the packet number is the second byte, starting at 1, the total number of
// packets is byte 2, then the contents start as of the 4th byte
// there might still be packets coming
std::strcpy (cfg.url + (rxValueAsByteArray[1] - 1) * (maxBytesInOneBLEPacket - 3), rxValue.c_str() + 3);
if (rxValueAsByteArray[1] == rxValueAsByteArray[2]) {
if (debugLogging) {Serial.print("received all packets, url = ");Serial.println(cfg.url);}
configureTargetServerAndUrl(cfg.url, cfg.token);
connectToWiFiIfNightScoutUrlExists();
}
}
break;
case 0x02:{
Serial.println(F("received opcode for writeNightScoutAPIKeyTx"));
std::strcpy (cfg.token + (rxValueAsByteArray[1] - 1) * (maxBytesInOneBLEPacket - 3), rxValue.c_str() + 3);
if (rxValueAsByteArray[1] == rxValueAsByteArray[2]) {
if (debugLogging) {Serial.print("received all packets, token = ");Serial.println(cfg.token);}
configureTargetServerAndUrl(cfg.url, cfg.token);
}
}
break;
case 0x03:{
char * unitismgdl = new char[6];// value is literally "true" or "false"
Serial.println(F("received opcode for writemgdlTx"));
std::strcpy (unitismgdl, rxValue.c_str() + 3);// starts at postion 4, because split in packets is used
if (strcmp(unitismgdl, "true") == 0) {
cfg.show_mgdl = 0;
} else {
cfg.show_mgdl = 1;
}
updateGlycemia();
delete[] unitismgdl;
}
break;
case 0x04:
Serial.println(F("received opcode for writebrightness1Tx"));
break;
case 0x05:
Serial.println(F("received opcode for writebrightness2Tx"));
break;
case 0x06:
Serial.println(F("received opcode for writebrightness3Tx"));
break;
case 0x07: {
Serial.println(F("received opcode for writeWlanSSIDTx"));
if (rxValueAsByteArray[1] == 1) {// it's the first packet, the first byte is the number of the wifi name
//0 is ascii code 48, that means for instance if rxValueAsByteArray[3] = 49, then the actual number is 1
// but from that we need to substract further one, because indexing starts at 0
indexForWifiNamesAndPasswords = rxValueAsByteArray[3] - 48 - 1;
std::strcpy (cfg.wlanssid[indexForWifiNamesAndPasswords], rxValue.c_str() + 4);
} else if (rxValueAsByteArray[1] == 2) {// second packet
std::strcpy (cfg.wlanssid[indexForWifiNamesAndPasswords] + maxBytesInOneBLEPacket - 4, rxValue.c_str() + 3);
} else {// third or fourth, ... packet
std::strcpy (cfg.wlanssid[indexForWifiNamesAndPasswords] + (maxBytesInOneBLEPacket - 4) + (rxValueAsByteArray[1] - 2) * (maxBytesInOneBLEPacket - 3), rxValue.c_str() + 3);
}
if (rxValueAsByteArray[1] == rxValueAsByteArray[2]) {
// all packets received
if (debugLogging) {Serial.print("received all packets, cfg.wlanssid = ");Serial.println(cfg.wlanssid[indexForWifiNamesAndPasswords]);}
}
}
break;
case 0x08:{
Serial.println(F("received opcode for writeWlanPassTx"));
if (rxValueAsByteArray[1] == 1) {// it's the first packet, the first byte is the number of the password
//0 is ascii code 48, that means for instance if rxValueAsByteArray[3] = 49, then the actual number is 1
// but from that we need to substract further one, because indexing starts at 0
indexForWifiNamesAndPasswords = rxValueAsByteArray[3] - 48 - 1;
std::strcpy (cfg.wlanpass[indexForWifiNamesAndPasswords], rxValue.c_str() + 4);
} else if (rxValueAsByteArray[1] == 2) {// second packet
std::strcpy (cfg.wlanpass[indexForWifiNamesAndPasswords] + maxBytesInOneBLEPacket - 4, rxValue.c_str() + 3);
} else {// third or fourth, ... packet
std::strcpy (cfg.wlanpass[indexForWifiNamesAndPasswords] + (maxBytesInOneBLEPacket - 4) + (rxValueAsByteArray[1] - 2) * (maxBytesInOneBLEPacket - 3), rxValue.c_str() + 3);
}
if (rxValueAsByteArray[1] == rxValueAsByteArray[2]) {
// all packets received
if (debugLogging) {Serial.print("received all packets, cfg.wlanssid = ");Serial.println(cfg.wlanpass[indexForWifiNamesAndPasswords]);}
}
}
break;
case 0x09: {
Serial.println(F("received opcode for readBlePassWordTx"));
// client is requesting the blepassword - if cfg.blepassword has length > 0, then it means it should already be known by the app, we won't send it.
// this is to prevent that some other app requests it
if (sizeOfStringInCharArray(cfg.blepassword, 64) == 0) {// here in this case useConfiguredBlePassword must be false
Serial.println(F("current blepassword has length 0, creating a new one and will send this to client"));
for(int i = 0; i<10 ; i++) {
cfg.blepassword[i] = letters[random(0, 36)];
}
sendTextToBLEClient(cfg.blepassword, 0x0E, 64);
// considering this case as authenticated
bleAuthenticated = true;
} else if (useConfiguredBlePassword) {
Serial.println(F("'Error - 1 - User should set password in settings', sending 0x0D to client"));
sendTextToBLEClient("", 0x0D, 0);
} else { // useConfiguredBlePassword is false and cfg.blepassword already exists, means it's already randomly generated
Serial.println(F("blepassword has length > 0, 'Error - 2 - Password already known, user should reset M5Stack', sending 0x0F to client"));
sendTextToBLEClient("", 0x0F, 0);
}
}
break;
case 0x0A: {//authenticateTx
Serial.println(F("received opcode for authenticateTx"));
// client is trying authentication, in case cfg.blepassword is currently 0, then we're in a situation where there's no password in the config, a new random password hasn't been
// generated yet, the app still has an old stored temporary password, but which needs to be renewed
if (sizeOfStringInCharArray(cfg.blepassword, 64) == 0) {
Serial.println(F("blepassword has length 0, creating a new one and will send this to client"));
for(int i = 0; i<10; i++) {
cfg.blepassword[i] = letters[random(0, 36)];
}
sendTextToBLEClient(cfg.blepassword, 0x0E, 64);
// considering this case as authenticated
bleAuthenticated = true;
} else {
// verify the password
bool passwordMatch = false;
if ((sizeOfStringInCharArray(cfg.blepassword, 64) + 1) == rxValue.length()) {// rxValue is opCode + password, meaning should be 1 longer than actual password, if that's not the case then password doesn't match
Serial.println(F("password length correct"));
passwordMatch = true;