-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBastWAN_Minimal_LoRa.ino
1131 lines (1084 loc) · 33.8 KB
/
BastWAN_Minimal_LoRa.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
// Uncomment the next line if uploading to Pavel
//#define Pavel 1
// Comment out this lime if you need the silent version:
// only incoming packets will be displayed in the serial monitor.
// If something doesn't work you won't know it though, unless
// you plug in an OLED.
#define NEED_DEBUG 1
// Uncomment this next line if you want to use a BME680
// #define NEED_BME 1
// Uncomment this next line if you want to use pins 5 & 6 for Gnd/Vcc
// Particularly useful on a breadboard as they are next to SDA/SCL
// #define NEED_SIDE_I2C 1
// Uncomment this next line if you want to use a DHTxx
//#define NEED_DHT
// Uncomment this next line if you want to use an SSD1306 OLED
#define NEED_SSD1306 1
// Uncomment this next line if you want to use an HDC1080
#define NEED_HDC1080 1
// Uncomment this next line if you want to use a CCS811
#define NEED_CCS811 1
// Uncomment this next line if you want to use an SGP30
//#define NEED_SGP30 1
// Uncomment this next line if you want to use an EEPROM
// #define NEED_EEPROM
// #define NEED_SHATEST
#include <SPI.h>
#include <Wire.h>
#include <LoRa.h>
// Click here to get the library: http://librarymanager/All#LoRa
#include <LoRandom.h>
// Click here to get the library: http://librarymanager/All#LoRandom
#include "aes.c"
#include "sha2.c"
/*
NOTE!
Add:
namespace std _GLIBCXX_VISIBILITY(default) {
_GLIBCXX_BEGIN_NAMESPACE_VERSION
void __throw_length_error(char const*) {}
void __throw_bad_alloc() {}
void __throw_out_of_range(char const*) {}
void __throw_logic_error(char const*) {}
}
to ~/Library/Arduino15/packages/arduino/tools/arm-none-eabi-gcc/4.8.3-2014q1/arm-none-eabi/include/c++/4.8.3/bits/basic_string.h
Add:
-D_GLIBCXX_USE_C99
to compiler.c.flags & compiler.cpp.flags.
Or the code won't compile.
*/
/*
If you're planning to usee an EEPROM, you need
to define the buffer lengths in SparkFun_External_EEPROM.h
Around line 56:
#elif defined(_VARIANT_ELECTRONICCATS_BASTWAN_)
#define I2C_BUFFER_LENGTH_RX SERIAL_BUFFER_SIZE
#define I2C_BUFFER_LENGTH_TX SERIAL_BUFFER_SIZE
*/
#include "ArduinoJson.h"
// Click here to get the library: http://librarymanager/All#ArduinoJson
#ifdef NEED_SSD1306
#include "SSD1306Ascii.h"
// Click here to get the library: http://librarymanager/All#SSD1306Ascii
#include "SSD1306AsciiWire.h"
#define I2C_ADDRESS 0x3C
#define RST_PIN -1
#define OLED_FORMAT &Adafruit128x32
SSD1306AsciiWire oled;
#endif // NEED_SSD1306
#ifdef NEED_SGP30
#include "sensirion_common.h"
#include "sgp30.h"
// https://github.com/Seeed-Studio/SGP30_Gas_Sensor
uint16_t tvoc_co2[2] = {0};
void displaySGP30();
#endif // NEED_SGP30
#ifdef NEED_HDC1080
#include <ClosedCube_HDC1080.h>
// Click here to get the library: http://librarymanager/All#ClosedCube_HDC1080
ClosedCube_HDC1080 hdc1080;
#define hdc1080_waitout 30000
double lastReading = 0;
float temp_hum_val[2] = {0};
#define PING_DELAY 300000 // 5 minutes
#ifdef NEED_CCS811 // Linked to NEED_HDC1080
#include <SparkFunCCS811.h>
// Click here to get the library: http://librarymanager/All#SparkFunCCS811
#define CCS811_ADDR 0x5A // Alternate I2C Address
uint16_t tvoc_co2[2] = {0};
#define PIN_NOT_WAKE 5
#define PIN_NOT_INT 6
CCS811 myCCS811(CCS811_ADDR);
#endif // NEED_CCS811
#endif // NEED_HDC1080
#ifdef NEED_BME
#include "ClosedCube_BME680.h"
// Click here to get the library: http://librarymanager/All#ClosedCube_BME680
ClosedCube_BME680 bme680;
double lastReading = 0;
#define PING_DELAY 300000 // 5 minutes
#endif // NEED_BME
#ifdef NEED_EEPROM
#include "SparkFun_External_EEPROM.h"
// Click here to get the library: http://librarymanager/All#SparkFun_External_EEPROM
ExternalEEPROM myMem;
#endif // NEED_EEPROM
#ifdef NEED_DHT
#include "DHT.h"
// Click here to get the library: http://librarymanager/All#DHT_sensor_library
#define DHTPIN 9 // what pin we're connected to
#define DHTTYPE DHT22 // DHT 22 (AM2302)
DHT dht(DHTPIN, DHTTYPE);
#define PING_DELAY 300000 // 5 minutes
double lastReading = 0;
float temp_hum_val[2] = {0};
void displayDHT();
#endif // NEED_DHT
#include "helper.h"
#include "haversine.h"
#include "SerialCommands.h"
/*
Welcome to role-assigned values: each machine will have a specific role,
and code will be compiled and run depending on who it is for.
Pavel:
- Outdoors (WHEN IT'S NOT RAINING) device.
- BME680 inside
OR
- DHT22 inside
- Possibly an OV5208 camera soon
*/
void setup() {
// ---- HOUSEKEEPING ----
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, 1); // Turn on Blue LED
SerialUSB.begin(115200);
delay(3000);
#ifdef NEED_DEBUG
SerialUSB.println("\n\nBastWAN at your service!");
#endif // NEED_DEBUG
#ifdef NEED_SIDE_I2C
// this has to happen first, if the I2C bus is powered by 5/6
#ifdef NEED_DEBUG
SerialUSB.println(" - Set up I2C");
#endif // NEED_DEBUG
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
digitalWrite(5, LOW); // Keyboard Featherwing I2C GND
digitalWrite(6, HIGH); // Keyboard Featherwing I2C VCC
// And obviously we can't display on the OLED yet...
#endif // NEED_SIDE_I2C
Wire.begin(SDA, SCL);
Wire.setClock(100000);
#ifdef NEED_SSD1306
SerialUSB.println("Setting up OLED");
// Initialising the UI will init the display too.
oled.begin(OLED_FORMAT, I2C_ADDRESS);
oled.setFont(System5x7);
#if INCLUDE_SCROLLING == 0
#error INCLUDE_SCROLLING must be non-zero. Edit SSD1306Ascii.h
#endif // INCLUDE_SCROLLING
// Set auto scrolling at end of window.
oled.setScrollMode(SCROLL_MODE_AUTO);
oled.println("BastWAN Minimal LoRa");
#endif // NEED_SSD1306
#ifdef NEED_EEPROM
#ifdef NEED_DEBUG
SerialUSB.println(" - Start EEPROM");
#endif // NEED_DEBUG
#ifdef NEED_SSD1306
oled.println(" . Start EEPROM");
#endif // NEED_SSD1306
if (myMem.begin() == false) {
#ifdef NEED_DEBUG
SerialUSB.println(" No memory detected. Freezing.");
#endif // NEED_DEBUG
#ifdef NEED_SSD1306
oled.println("No memory detected.");
oled.println("Freezing...");
#endif // NEED_SSD1306
while (1)
;
}
uint32_t myLen = myMem.length(), index = 0;
#ifdef NEED_DEBUG
SerialUSB.println("Memory detected!");
SerialUSB.print("Mem size in bytes: ");
SerialUSB.println(myLen);
#ifdef NEED_SSD1306
oled.println("Memory detected!");
oled.println("Size in bytes: ");
oled.println(myLen);
#endif // NEED_SSD1306
#endif // NEED_DEBUG
memset(msgBuf, 0, BUFF_LENGTH);
myMem.read(0, msgBuf, 32);
myMem.read(32, msgBuf + 32, 32);
myMem.read(64, msgBuf + 64, 32);
// Let's limit the JSON string size to 96 for now.
#ifdef NEED_DEBUG
hexDump(msgBuf, 96);
#endif // NEED_DEBUG
StaticJsonDocument<200> doc;
DeserializationError error = deserializeJson(doc, msgBuf);
if (error) {
#ifdef NEED_DEBUG
SerialUSB.println(F("\ndeserializeJson() failed!"));
#endif // NEED_DEBUG
#ifdef NEED_SSD1306
oled.println("JSON prefs fail.");
#endif // NEED_SSD1306
savePrefs();
}
myFreq = doc["myFreq"];
mySF = doc["mySF"] = mySF;
myBW = doc["myBW"];
myCR = doc["myCR"];
const char *x = doc["deviceName"];
SerialUSB.println("setDeviceName in NEED_EEPROM");
setDeviceName(x);
#ifdef NEED_DEBUG
SerialUSB.print("FQ: "); SerialUSB.println(myFreq / 1e6);
SerialUSB.print("SF: "); SerialUSB.println(mySF);
SerialUSB.print("BW: "); SerialUSB.println(myBW);
SerialUSB.print("Device Name: "); SerialUSB.println(deviceName);
#endif // NEED_DEBUG
#ifdef NEED_SSD1306
oled.println("JSON prefs fail.");
oled.print("SF: "); oled.println(SF);
oled.print("BW: "); oled.println(myBW);
#endif // NEED_SSD1306
#endif // NEED_EEPROM
#ifdef NEED_BME
// ---- BME STUFF ----
#ifdef NEED_DEBUG
SerialUSB.println(" - ClosedCube BME680 ([T]emperature, [P]ressure, [H]umidity)");
#endif // NEED_DEBUG
#ifdef NEED_SSD1306
oled.println("ClosedCube BME680");
#endif // NEED_SSD1306
bme680.init(0x77); // I2C address: 0x76 or 0x77
bme680.reset();
#ifdef NEED_DEBUG
SerialUSB.print("Chip ID=0x");
SerialUSB.println(bme680.getChipID(), HEX);
#endif // NEED_DEBUG
// oversampling: humidity = x1, temperature = x2, pressure = x16
bme680.setOversampling(BME680_OVERSAMPLING_X1, BME680_OVERSAMPLING_X2, BME680_OVERSAMPLING_X16);
bme680.setIIRFilter(BME680_FILTER_3);
bme680.setForcedMode();
#endif // NEED_BME
#ifdef NEED_SSD1306
oled.println("LoRa Setup");
#endif // NEED_SSD1306
pinMode(RFM_TCXO, OUTPUT);
pinMode(RFM_SWITCH, OUTPUT);
pinMode(LED_BUILTIN, OUTPUT);
LoRa.setPins(SS, RFM_RST, RFM_DIO0);
if (!LoRa.begin(myFreq)) {
#ifdef NEED_DEBUG
SerialUSB.println("Starting LoRa failed!\nNow that's disappointing...");
#endif // NEED_DEBUG
#ifdef NEED_SSD1306
oled.println("LoRa init failed!");
oled.println("Freezing...");
#endif // NEED_SSD1306
while (1);
}
#ifdef NEED_SSD1306
oled.println(" . Random");
#endif // NEED_SSD1306
stockUpRandom();
// first fill a 256-byte array with random bytes
#ifdef NEED_SSD1306
oled.println(" . Set SF");
#endif // NEED_SSD1306
LoRa.setSpreadingFactor(mySF);
#ifdef NEED_SSD1306
oled.println(" . Set BW");
#endif // NEED_SSD1306
LoRa.setSignalBandwidth(BWs[myBW] * 1e3);
#ifdef NEED_SSD1306
oled.println(" . Set C/R");
#endif // NEED_SSD1306
LoRa.setCodingRate4(myCR);
#ifdef NEED_SSD1306
oled.println(" . Set Preamble");
#endif // NEED_SSD1306
LoRa.setPreambleLength(8);
#ifdef NEED_SSD1306
oled.println(" . Set Tx Power");
#endif // NEED_SSD1306
LoRa.setTxPower(TxPower, PA_OUTPUT_PA_BOOST_PIN);
digitalWrite(RFM_SWITCH, HIGH);
#ifdef NEED_SSD1306
oled.println(" . Set PA_BOOST");
#endif // NEED_SSD1306
if (PA_BOOST) LoRa.setTxPower(TxPower, PA_OUTPUT_PA_BOOST_PIN);
else LoRa.setTxPower(TxPower, 0); // NOT RECOMMENDED!
#ifdef NEED_SSD1306
oled.println(" . Set PA_CONFIG");
#endif // NEED_SSD1306
LoRa.writeRegister(REG_PA_CONFIG, 0b11111111); // That's for the transceiver
// 0B 1111 1111
// 1 PA_BOOST pin. Maximum power of +20 dBm
// 111 MaxPower 10.8+0.6*MaxPower [dBm] = 15
// 1111 OutputPower Pout=17-(15-OutputPower) if PaSelect = 1 --> 17
#ifdef NEED_SSD1306
oled.println(" . Set PA_DAC");
#endif // NEED_SSD1306
LoRa.writeRegister(REG_PA_DAC, PA_DAC_HIGH); // That's for the transceiver
// 0B 1000 0111
// 00000 RESERVED
// 111 +20dBm on PA_BOOST when OutputPower=1111
// LoRa.writeRegister(REG_LNA, 00); // TURN OFF LNA FOR TRANSMIT
#ifdef NEED_SSD1306
oled.println(" . Set REG_OCP");
#endif // NEED_SSD1306
if (OCP_ON) LoRa.writeRegister(REG_OCP, 0b00111111); // OCP Max 240
else LoRa.writeRegister(REG_OCP, 0b00011111); // NO OCP
// 0b 0010 0011
// 001 G1 = highest gain
// 00 Default LNA current
// 0 Reserved
// 11 Boost on, 150% LNA current
LoRa.receive();
#ifdef NEED_SSD1306
oled.println(" . Set REG_LNA");
#endif // NEED_SSD1306
LoRa.writeRegister(REG_LNA, 0x23); // TURN ON LNA FOR RECEIVE
#ifdef Pavel
setDeviceName("Pavel");
#else
setDeviceName("SensorNode 1");
#endif // Pavel
pingFrequency = 3000019;
needPing = true;
#ifdef NEED_SSD1306
oled.println("Device name:"); oled.println(deviceName);
#endif // NEED_SSD1306
#ifdef NEED_DHT
#ifdef NEED_SSD1306
oled.println("DHT");
#endif // NEED_SSD1306
dht.begin();
#endif // NEED_DHT
#ifdef NEED_SGP30
#ifdef NEED_SSD1306
oled.println("SGP30");
#endif // NEED_SSD1306
s16 err;
u16 scaled_ethanol_signal, scaled_h2_signal;
while (sgp_probe() != STATUS_OK) {
Serial.println("SGP failed");
digitalWrite(LED_BUILTIN, 0);
delay(800);
digitalWrite(LED_BUILTIN, 1);
delay(800);
}
/*Read H2 and Ethanol signal in the way of blocking*/
err = sgp_measure_signals_blocking_read(&scaled_ethanol_signal, &scaled_h2_signal);
if (err == STATUS_OK) {
Serial.println("get ram signal!");
} else {
Serial.println("Error reading signals");
#ifdef NEED_SSD1306
oled.println("Error reading signals");
#endif // NEED_SSD1306
}
err = sgp_iaq_init();
#endif // NEED_SGP30
#ifdef NEED_HDC1080
#ifdef NEED_SSD1306
oled.println("HDC1080");
#endif // NEED_SSD1306
SerialUSB.println("HDC1080:");
hdc1080.begin(0x40); // I2C address
SerialUSB.print("Manufacturer ID=0x");
SerialUSB.println(hdc1080.readManufacturerId(), HEX); // 0x5449 ID of Texas Instruments
SerialUSB.print("Device ID=0x");
SerialUSB.println(hdc1080.readDeviceId(), HEX); // 0x1050 ID of the device
Serial.print("Device Serial Number=");
HDC1080_SerialNumber sernum = hdc1080.readSerialNumber();
char format[12];
sprintf(format, "%02X-%04X-%04X", sernum.serialFirst, sernum.serialMid, sernum.serialLast);
Serial.println(format);
HDC1080_Registers reg = hdc1080.readRegister();
SerialUSB.print("Battery: 0x");
SerialUSB.println(reg.BatteryStatus, HEX);
SerialUSB.print("Heater: 0x");
SerialUSB.println(reg.Heater, HEX);
SerialUSB.print("HumidityMeasurementResolution: 0x");
SerialUSB.println(reg.HumidityMeasurementResolution, HEX);
SerialUSB.print("TemperatureMeasurementResolution: 0x");
SerialUSB.println(reg.TemperatureMeasurementResolution, HEX);
#ifdef NEED_CCS811 // Linked to NEED_HDC1080
#ifdef NEED_SSD1306
oled.println("CCS811");
#endif // NEED_SSD1306
SerialUSB.println("CCS811:");
// This begins the CCS811 sensor and prints error status of .beginWithStatus()
CCS811Core::CCS811_Status_e returnCode = myCCS811.beginWithStatus();
Serial.print("CCS811 begin exited with: ");
// Pass the error code to a function to print the results
Serial.println(myCCS811.statusString(returnCode));
// This sets the mode to 60 second reads, and prints returned error status.
returnCode = myCCS811.setDriveMode(2);
Serial.print("Mode request exited with: ");
Serial.println(myCCS811.statusString(returnCode));
// Configure and enable the interrupt line,
// then print error status
pinMode(PIN_NOT_INT, INPUT_PULLUP);
returnCode = myCCS811.enableInterrupts();
Serial.print("Interrupt configuration exited with: ");
Serial.println(myCCS811.statusString(returnCode));
// Configure the wake line
pinMode(PIN_NOT_WAKE, OUTPUT);
digitalWrite(PIN_NOT_WAKE, 1); // Start asleep
#endif // NEED_CCS811
#endif // NEED_HDC1080
#ifdef NEED_SSD1306
oled.println("Sets");
#endif // NEED_SSD1306
DeserializationError error = deserializeJson(sets, "{\"freq\":[868,868.125,868.125],\"sf\":[12,9,9],\"bw\":[9,8,6]}");
if (error) {
#ifdef NEED_SSD1306
oled.println("ndeserializeJson failed");
#endif // NEED_SSD1306
#ifdef NEED_DEBUG
SerialUSB.println(F("\ndeserializeJson() in Sets failed!"));
hexDump(msgBuf, BUFF_LENGTH);
#endif // NEED_DEBUG
} else {
setsFQ = sets["freq"];
setsSF = sets["sf"];
setsBW = sets["bw"];
uint8_t i, j = setsFQ.size();
#ifdef NEED_DEBUG
SerialUSB.println("\n\n" + String(j) + " Sets:");
#endif // NEED_DEBUG
for (i = 0; i < j; i++) {
float F = setsFQ[i];
int S = setsSF[i];
int B = setsBW[i];
#ifdef NEED_DEBUG
sprintf((char*)msgBuf, " . Freq: %3.3f MHz, SF %d, BW %d: %3.2f", F, S, B, BWs[B]);
SerialUSB.println((char*)msgBuf);
#endif // NEED_DEBUG
#ifdef NEED_SSD1306
oled.print("Freq["); oled.print(i); oled.print("]: "); oled.println(String(F, 3) + " MHz");
oled.print("SF["); oled.print(i); oled.print("]: "); oled.println(S);
oled.print("BW["); oled.print(i); oled.print("]: "); oled.print(B); oled.print(" ie "); oled.println(BWs[B]);
#endif // NEED_SSD1306
}
}
#ifdef NEED_DEBUG
SerialUSB.println("Setup done...");
#endif // NEED_DEBUG
#ifdef NEED_BME
displayBME680();
lastReading = millis();
#endif // NEED_BME
#ifdef NEED_DHT
displayDHT();
lastReading = millis();
#endif // NEED_DHT
#ifdef NEED_SGP30
displaySGP30();
#endif // NEED_SGP30
#ifdef NEED_HDC1080
displayHDC1080();
lastReading = millis();
#endif // NEED_HDC1080
#ifdef NEED_SHATEST
shaTest();
#endif // NEED_SHATEST
digitalWrite(LED_BUILTIN, 0); // Turn off blue LED
}
void loop() {
double t0 = millis();
#ifdef NEED_BME
if (t0 - lastReading >= PING_DELAY) {
displayBME680();
lastReading = millis();
}
#endif // NEED_BME
#ifdef NEED_DHT
if (t0 - lastReading >= PING_DELAY) {
displayDHT();
lastReading = millis();
}
#endif // NEED_DHT
#ifdef NEED_SGP30
if (t0 - lastReading >= PING_DELAY) {
displaySGP30();
lastReading = millis();
}
#endif // NEED_SGP30
#ifdef NEED_HDC1080
if (t0 - lastReading >= PING_DELAY) {
displayHDC1080();
lastReading = millis();
}
#endif // NEED_HDC1080
// Uncomment if you have a battery plugged in.
// if (millis() - batteryUpdateDelay > 10000) {
// getBattery();
// batteryUpdateDelay = millis();
// }
int packetSize = LoRa.parsePacket();
if (packetSize) {
digitalWrite(LED_BUILTIN, 1); // Turn on Blue LED
#ifdef NEED_SSD1306
oled.print("Incoming! ");
#endif // NEED_SSD1306
memset(msgBuf, 0xFF, BUFF_LENGTH);
int ix = 0;
while (LoRa.available()) {
char c = (char)LoRa.read();
delay(10);
msgBuf[ix++] = c;
} msgBuf[ix] = 0;
digitalWrite(LED_BUILTIN, 0); // Turn off Blue LED
int rssi = LoRa.packetRssi();
#ifdef NEED_SSD1306
oled.print("RSSI: ");
oled.println(rssi);
#endif // NEED_SSD1306
#ifdef NEED_DEBUG
SerialUSB.println("Received packet: ");
if (NEED_DEBUG > 0) hexDump(msgBuf, ix);
#endif // NEED_DEBUG
if (needEncryption) {
#ifdef NEED_DEBUG
SerialUSB.println("\n . Decrypting...");
#endif // NEED_DEBUG
packetSize = decryptECB(msgBuf, ix);
if (packetSize > -1) {
memset(msgBuf, 0, BUFF_LENGTH);
memcpy(msgBuf, encBuf, packetSize);
} else {
SerialUSB.println("Error while decrypting");
return;
}
}
StaticJsonDocument<200> doc;
DeserializationError error = deserializeJson(doc, msgBuf);
if (error) {
#ifdef NEED_DEBUG
SerialUSB.print(F("deserializeJson() failed: "));
SerialUSB.println(error.f_str());
#endif // NEED_DEBUG
#ifdef NEED_SSD1306
oled.print("deserializeJson failed");
#endif // NEED_SSD1306
return;
}
// DISPLAY HERE JSON PACKET
// IF NEED_DEBUG IS NOT DEFINED
#ifndef NEED_DEBUG
doc["rssi"] = rssi;
serializeJson(doc, SerialUSB); SerialUSB.println("");
#endif // NEED_DEBUG
// Print 4-byte ID
const char *myID = doc["UUID"];
// Print sender
const char *from = doc["from"];
// Print command
const char *cmd = doc["cmd"];
// Do we have a message?
#ifdef NEED_SSD1306
oled.print("from ");
oled.print(from);
oled.print(": ");
oled.println(cmd);
#endif // NEED_SSD1306
if (strcmp(cmd, "msg") == 0) {
const char *msg = doc["msg"];
#ifdef NEED_SSD1306
oled.println(msg);
#endif // NEED_SSD1306
}
JsonVariant mydata = doc["V"];
if (!mydata.isNull()) {
uint16_t tvoc = mydata.as<uint16_t>();
mydata = doc["C"];
uint16_t co2 = mydata.as<uint16_t>();
char buff[32];
sprintf(buff, "H: %2.2f%% T: %2.2f *C\n", tvoc, co2);
#ifdef NEED_DEBUG
SerialUSB.print(buff);
#endif // NEED_DEBUG
#ifdef NEED_SSD1306
oled.print(buff);
#endif // NEED_SSD1306
}
bool hasLatLong = true;
float tLat, tLong, tDistance;
mydata = doc["lat"];
if (mydata.isNull()) {
// we don't have
hasLatLong = false;
} else {
tLat = mydata.as<float>();
mydata = doc["long"];
if (mydata.isNull()) {
// we don't have
hasLatLong = false;
} else {
tLong = mydata.as<float>();
// we now have both values AND hasLatLong = true
// Display distance
tDistance = haversine(homeLatitude, homeLongitude, tLat, tLong);
}
}
#ifdef NEED_DEBUG
SerialUSB.print("ID: ");
SerialUSB.println(myID);
SerialUSB.print("Sender: ");
SerialUSB.println(from);
SerialUSB.print("Command: ");
SerialUSB.println(cmd);
if (strcmp(cmd, "msg") == 0) {
const char *msg = doc["msg"];
SerialUSB.print("Message: ");
SerialUSB.println(msg);
}
if (hasLatLong) {
SerialUSB.print("Distance: ");
if (tDistance >= 1000.0) {
SerialUSB.print(tDistance / 1000.0);
SerialUSB.println(" km");
} else {
SerialUSB.print(tDistance);
SerialUSB.println(" m");
}
}
SerialUSB.print("RSSI: ");
SerialUSB.println(rssi);
#endif // NEED_DEBUG
if (strcmp(cmd, "ping") == 0 && pongBack) {
// if it's a PING, and we are set to respond:
LoRa.idle();
#ifdef NEED_DEBUG
SerialUSB.println("Pong back:");
#endif // NEED_DEBUG
#ifdef NEED_SSD1306
oled.print("PONG back! ");
#endif // NEED_SSD1306
// we cannot pong back right away – the message would be lost
// if there are other devices on the same network
uint16_t dl = getRamdom16() % 2800 + 3300;
#ifdef NEED_DEBUG
SerialUSB.println("Delaying " + String(dl) + " millis...");
#endif // NEED_DEBUG
delay(dl);
sendPong((char*)myID, rssi);
LoRa.receive();
} else if (strcmp(cmd, "pong") == 0) {
int rcvRSSI = doc["rcvRSSI"];
#ifdef NEED_DEBUG
SerialUSB.print("rcvRSSI: ");
SerialUSB.println(rcvRSSI);
#endif // NEED_DEBUG
#ifdef NEED_SSD1306
oled.print("rcvRSSI: ");
oled.println(rcvRSSI);
#endif // NEED_SSD1306
mydata = doc["T"];
if (!mydata.isNull()) {
float tp = mydata.as<float>();
mydata = doc["H"];
float hm = mydata.as<float>();
#ifdef NEED_DEBUG
SerialUSB.print("Humidity: ");
SerialUSB.print(hm);
SerialUSB.print("% Temperature: ");
SerialUSB.println(tp);
#endif // NEED_DEBUG
#ifdef NEED_SSD1306
oled.print("H: ");
oled.print(hm);
oled.print("% ");
oled.print("T: ");
oled.print(tp);
oled.println(" C");
#endif // NEED_SSD1306
}
mydata = doc["V"];
if (!mydata.isNull()) {
float tVoc = mydata.as<float>();
mydata = doc["C"];
float cO2 = mydata.as<float>();
#ifdef NEED_DEBUG
SerialUSB.print("tVoc: ");
SerialUSB.println(tVoc);
SerialUSB.print("% CO2: ");
SerialUSB.println(cO2);
#endif // NEED_DEBUG
#ifdef NEED_SSD1306
oled.print("tVoc: ");
oled.print(tVoc);
oled.print(" ");
oled.print("co2: ");
oled.println(cO2);
#endif // NEED_SSD1306
}
} else if (strcmp(cmd, "freq") == 0) {
// Do we have a frequency change request?
if (strcmp(from, "BastMobile") != 0) return;
// Not for you, brah
mydata = doc["freq"];
if (mydata.isNull()) {
#ifdef NEED_DEBUG
SerialUSB.println("mydata (doc['freq']) is null!");
#endif
} else {
uint32_t fq = mydata.as<float>() * 1e6;
#ifdef NEED_DEBUG
SerialUSB.println("mydata (doc['freq']) = " + String(fq, 3));
#endif
if (fq < 862e6 || fq > 1020e6) {
#ifdef NEED_DEBUG
SerialUSB.println("Requested frequency (" + String(fq) + ") is invalid!");
#endif // NEED_DEBUG
} else {
myFreq = fq;
LoRa.idle();
LoRa.setFrequency(myFreq);
delay(100);
LoRa.receive();
#ifdef NEED_DEBUG
SerialUSB.println("Frequency set to " + String(myFreq / 1e6, 3) + " MHz");
#endif // NEED_DEBUG
#ifdef NEED_SSD1306
oled.print("New freq: ");
oled.println(String(myFreq / 1e6, 3) + " MHz");
#endif // NEED_SSD1306
savePrefs();
}
}
} else if (strcmp(cmd, "bw") == 0) {
// Do we have a bandwidth change request?
/*
Note on SF / BW pairs:
Unless you are sending very small packets, all pairs might not work.
Here is a table based on empirical results of pairs that work.
BW|SF|Y/N
--|--|---
|9 | Y
6 |10| N
|11| N
|12| N
--|--|---
|9 | Y
7 |10| Y
|11| N
|12| N
--|--|---
|9 | Y
8 |10| Y
|11| Y
|12| N
--|--|---
|9 | Y
9 |10| Y
|11| Y
|12| Y
*/
if (strcmp(from, "BastMobile") != 0) return;
// Not for you, brah
mydata = doc["bw"];
if (mydata.isNull()) {
#ifdef NEED_DEBUG
SerialUSB.println("mydata (doc['bw']) is null!");
#endif
} else {
int bw = mydata.as<int>();
#ifdef NEED_DEBUG
SerialUSB.println("mydata (doc['bw']) = " + String(bw));
#endif
if (bw < 0 || bw > 9) {
#ifdef NEED_DEBUG
SerialUSB.println("Requested bandwidth (" + String(bw) + ") is invalid!");
#endif // NEED_DEBUG
} else {
myBW = bw;
LoRa.idle();
LoRa.setSignalBandwidth(BWs[myBW] * 1e3);
delay(100);
LoRa.receive();
#ifdef NEED_DEBUG
SerialUSB.println("Bandwidth set to " + String(BWs[myBW], 3) + " KHz");
#endif // NEED_DEBUG
#ifdef NEED_SSD1306
oled.print("New BW: ");
oled.println(String(BWs[myBW], 3) + " KHz");
#endif // NEED_SSD1306
savePrefs();
}
}
} else if (strcmp(cmd, "sf") == 0) {
// Do we have a spreading factor change request?
if (strcmp(from, "BastMobile") != 0) return;
// Not for you, brah
mydata = doc["sf"];
if (mydata.isNull()) {
#ifdef NEED_DEBUG
SerialUSB.println("mydata (doc['sf']) is null!");
#endif
} else {
int sf = mydata.as<int>();
#ifdef NEED_DEBUG
SerialUSB.println("mydata (doc['sf']) = " + String(sf));
#endif
if (sf < 7 || sf > 12) {
#ifdef NEED_DEBUG
SerialUSB.println("Requested SF (" + String(sf) + ") is invalid!");
#endif // NEED_DEBUG
} else {
mySF = sf;
LoRa.idle();
LoRa.setSpreadingFactor(mySF);
delay(100);
LoRa.receive();
#ifdef NEED_DEBUG
SerialUSB.println("SF set to " + String(sf));
#endif // NEED_DEBUG
#ifdef NEED_SSD1306
oled.print("New SF: ");
oled.println(mySF);
#endif // NEED_SSD1306
savePrefs();
}
}
} else if (strcmp(cmd, "switch") == 0) {
// Do we have a spreading factor change request?
if (strcmp(from, "BastMobile") != 0) return;
// Not for you, brah
mydata = doc["set"];
if (mydata.isNull()) {
#ifdef NEED_DEBUG
SerialUSB.println("mydata (doc['set']) is null!");
#endif
} else {
int setNum = mydata.as<int>();
#ifdef NEED_DEBUG
SerialUSB.println("Switching to set #" + String(setNum));
#endif
float F = setsFQ[0];
int S = setsSF[0];
int B = setsBW[0];
#ifdef NEED_DEBUG
sprintf((char*)msgBuf, " . Freq: %3.3f MHz, SF %d, BW %d: %3.2f", F, S, B, BWs[B]);
SerialUSB.println((char*)msgBuf);
#endif // NEED_DEBUG
myFreq = F * 1e6;
mySF = S;
myBW = B;
LoRa.idle();
LoRa.setFrequency(myFreq);
delay(10);
LoRa.setSpreadingFactor(mySF);
delay(10);
LoRa.setSignalBandwidth(BWs[myBW] * 1e3);
delay(10);
LoRa.receive();
sendPong((char*)myID, rssi);
#ifdef NEED_SSD1306
oled.print("New freq: ");
oled.println(String(myFreq / 1e6, 3) + " MHz");
oled.print("New SF: ");
oled.println(mySF);
oled.print("New BW: ");
oled.println(String(BWs[myBW], 3) + " KHz");
#endif // NEED_SSD1306
}
}
}
if (SerialUSB.available()) {
// When the BastMobile is connected via USB to a computer,
// you can make changes to settings via Serial,
// like in BastWAN_Minimal_LoRa
handleSerial();
}
if (needPing) {
double t0 = millis();
if (t0 - lastAutoPing > pingFrequency) {
#ifdef NEED_SSD1306
oled.println("Auto PING!");
#endif // NEED_SSD1306
sendPing();
// lastAutoPing = millis();
// done in sendPing
}
}
}
#ifdef NEED_BME
void displayBME680() {
#ifdef NEED_SSD1306
oled.println("displayBME680");
#endif // NEED_SSD1306
#ifdef NEED_DEBUG
SerialUSB.println("BME680");
#endif // NEED_DEBUG
ClosedCube_BME680_Status status = bme680.readStatus();
if (status.newDataFlag) {
double temp = bme680.readTemperature();
double pres = bme680.readPressure();
double hum = bme680.readHumidity();
// save the values in the same global array, so that they can be sent in packets
temp_hum_val[0] = (float)hum;
temp_hum_val[1] = (float)temp;
#ifdef NEED_SSD1306
displayHT();
#endif // NEED_SSD1306
#ifdef NEED_DEBUG
sprintf((char*)msgBuf, "result: T = % f C, RH = % f % %, P = % d hPa\n", temp, hum, pres);
SerialUSB.println((char*)msgBuf);
#endif // NEED_DEBUG
lastReading = millis();
}
}
#endif // NEED_DEBUG
#ifdef NEED_DHT
void displayDHT() {
#ifdef NEED_SSD1306
oled.println("displayDHT");
#endif // NEED_SSD1306
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (it's a very slow sensor)
// if (dht.readTempAndHumidity(temp_hum_val)) {
dht.readTempAndHumidity(temp_hum_val);
SerialUSB.print("Humidity: ");
SerialUSB.print(temp_hum_val[0]);
SerialUSB.print(" % \t");
SerialUSB.print("Temperature: ");
SerialUSB.print(temp_hum_val[1]);
SerialUSB.println(" *C");
#ifdef NEED_SSD1306
displayHT();
#endif // NEED_SSD1306
// } else {
// SerialUSB.println("Failed to get temperature and humidity value.");
// }
}
#endif // NEED_DHT
#ifdef NEED_HDC1080
void displayHDC1080() {
char buff[48];
temp_hum_val[0] = hdc1080.readHumidity();
temp_hum_val[1] = hdc1080.readTemperature();
sprintf(buff, "Temp: %2.2f C, Humidity: %2.2f%%\n", temp_hum_val[1], temp_hum_val[0]);
Serial.print(buff);
#ifdef NEED_CCS811 // Linked to NEED_HDC1080
// Look for interrupt request from CCS811
if (digitalRead(PIN_NOT_INT) == 0) {
// Wake up the CCS811 logic engine
digitalWrite(PIN_NOT_WAKE, 0);
// Need to wait at least 50 µs
delay(1);
// Interrupt signal caught, so cause the CCS811 to run its algorithm
myCCS811.readAlgorithmResults(); // Calling this function updates the global tVOC and CO2 variables
tvoc_co2[0] = myCCS811.getTVOC();
tvoc_co2[1] = myCCS811.getCO2();