-
Notifications
You must be signed in to change notification settings - Fork 0
/
mySQMPRO-026-wifi.ino
1586 lines (1485 loc) · 42.7 KB
/
mySQMPRO-026-wifi.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
// mySQMPRO a DIY Arduino Nano Project
// Sky Quality Meter, Cloud Sensor, Rain Sensor, Barometric Sensor, GPS, Lux Sensor
// (c) Copyright Robert Brown 2014-2019. All Rights Reserved.
// This project is protected by International Copyright Law.
// https://sourceforge.net/projects/arduinomysqmskyqualitymeter/files/mySQMPRO/ Original Source
// Permission is granted for personal and Academic/Educational use only.
// Software distributed under MIT License https://opensource.org/licenses/MIT
//
// THis fork which extent the code with WIfi based up on the NodeMCU 12E
// Created By Chris ALberts
//
//
// REQUIRES LCD1602, LCD1604, LCD2004 or OLED I2C DISPLAY
// PCB (Version 5) can be ordered online at https://aisler.net/p/JHYFZBXW
// CONTRIBUTIONS
// If you wish to make a contribution in thanks for this project, please use PayPal and send the amount
// to user [email protected] (Robert Brown). All contributions are gratefully accepted.
// based loosely on code from http://forum.arduino.cc/index.php?PHPSESSID=knhnejk2g00u2smd8ve8tn5l82&topic=21536.45
// and http://stargazerslounge.com/topic/183600-arduino-sky-quality-meter-working/
// Acknowledgements: madepablo, Martin Nawrath (Frequency Counter Library), Corpze
//
// ------------------------------------------------------------------------------------------------------
// HARDWARE MAPPINGS
// Connection for TSL237 Sensor
// VDD (pin2) to +5V (Note: Use a 0.1uf ceramic capacitor mounted close to the sensor across VDD and GND)
// GND (pin1) to GND
// VOUT (pin3) to Arduino Pin D5
// Connection for NEO-6M-GPS
// GPS-TXD to Arduino Pin D4
// GPS-RXD to Arduino Pin D3
// GND to GND
// VCC to +5V
// Connection for LCD2004 or LCD1604 OR LCD1602 I2C display
// VCC to +5V
// GND to GND
// SDA to A4
// SCL to A5
// Connection for MLX90614 IR Sensor - be careful what I2C connector you connect this to
// Connecting to 5V will destroy this sensor
// VCC 3.3V
// GND GND
// SDA A4
// SCL A5
// Connection for TSL2561 Sensor - be careful what I2C connector you connect this to
// Connecting to 5V will destroy this sensor
// VCC 3.3V
// GND GND
// SDA A4
// SCL A5
// If not fitted, then GL5528 LDR will be used for lux
// Connection for GL5528 LDR
// 5V - GL5528 - Arduino pin A0 - 10K Pulldown resistor - GND
// Connection for Rain Sensor
// AO to Arduino pin A1
// DO to Arduino pin D2
// GND GND
// VCC 5V
// Connection for BME280 Barometric Sensor, uses I2C interface - make sure you purchase the 5V version
// VCC 5V
// GND GND
// SDA A4
// SCL A5
// HARDWARE MAPPINGS END
// ------------------------------------------------------------------------------------------------------
// CONFIG SECTION
// to use TSL2561 sensor for determining lux, uncomment the next line
#define TSL2561SENSOR 1
// To enable GPS, uncomment the next line
//#define useGPSNEO 1
// To enable the rain sensor, uncomment the next line
#define RAINSENSOR 1
// To enable the MLX90614 ir sensor uncomment the next line
#define MLX90614SENSOR 1
// To enable the BME280 humdity, ambient temp, barmetric pressure sensor uncomment the next line
#define BME280SENSOR 1
// YOU MUST CHOOSE ONE OF THE FOLLOWING SENSORS, not both, recommended is TSL237
//#define TSL235 1
#define TSL237 2
// To enable the LCD DISPLAY uncomment the next line
#define LCDDISPLAY 1
// to enable LCD1602 or LCD1604 or LCD2004, only uncomment one or the other below - you must have only ONE uncommented
//#define LCD1602 1
//#define LCD1604 2
#define LCD2004 3
// To enable the OLED display (SSD1306 chip) uncomment the next line
//#define OLEDDISPLAY 1
#define LCDADDRESS 0x27 // some LCD displays maybe at 0x3F, use I2Cscanner to find the correct address
#define OLEDADDRESS 0x3C // address of OLED, do not change
// do not change
#ifdef LCDDISPLAY
#ifdef OLEDDISPLAY
#halt //Error - you can only have one LCD type defined - either LCDDISPLAY or OLEDDISPLAY.
#endif
#endif
// do not change
#ifdef LCDDISPLAY
#ifdef LCD1602
#ifdef LCD1604
#halt //Error - you can only have one LCD type defined, one of LCD1602, LCD1604 or LCD2004.
#endif
#endif
#endif
// do not change
#ifdef LCDDISPLAY
#ifdef LCD1602
#ifdef LCD2004
#halt //Error - you can only have one LCD type defined, one of LCD1602, LCD1604 or LCD2004.
#endif
#endif
#endif
// do not change
#ifdef LCDDISPLAY
#ifdef LCD1604
#ifdef LCD2004
#halt //Error - you can only have one LCD type defined, one of LCD1602, LCD1604 or LCD2004.
#endif
#endif
#endif
// do not change
#ifdef LCDDISPLAY
#ifdef LCD1602
#ifdef LCD2004
#ifdef LCD1604
#halt //Error - you can only have one LCD type defined, one of LCD1602, LCD1604 or LCD2004.
#endif
#endif
#endif
#endif
// do not change
#ifdef LCDDISPLAY
#ifndef LCD2004
#ifndef LCD1604
#ifndef LCD1602
#halt //Error - you can must define either LCD1602 or LCD2004 or LCD1604.
#endif
#endif
#endif
#endif
// do not change
#ifndef TSL235
#ifndef TSL237
#halt Error - you must define ONE TSL SENSOR TYPE
#endif
#endif
// YOU MUST CHOOSE ONE OF THE FOLLOWING METHODS TO CALCULATE THE SQM VALUE
//#define OLDSQMMETHOD 1
//#define NEWSQMMETHOD 2
//#define NEWSQMMETHODCORRECTED 3
#define IRRADIANCEMETHOD 4
// do not change
#ifdef LCDDISPLAY
#ifdef LCD1602
#ifdef LCD2004
#halt // Error, you cannot have both LCD1602 and LCD2004 uncommented at the same time
#endif
#endif
#endif
// do not change
#ifdef LCDDISPLAY
#ifdef LCD1602
#ifdef LCD1604
#halt // Error, you cannot have both LCD1602 and LCD1604 uncommented at the same time
#endif
#endif
#endif
// do not change
#ifdef LCDDISPLAY
#ifdef LCD1604
#ifdef LCD2004
#halt // Error, you cannot have both LCD1604 and LCD2004 uncommented at the same time
#endif
#endif
#endif
// do not change
#ifdef LCDDISPLAY
#ifndef LCD1602
#ifndef LCD2004
#ifndef LCD1604
#halt // Error, you must have either LCD1602 OR LCD1604 OR LCD2004 uncommented
#endif
#endif
#endif
#endif
// do not change
#ifndef OLDSQMMETHOD
#ifndef NEWSQMMETHOD
#ifndef NEWSQMMETHODCORRECTED
#ifndef IRRADIANCEMETHOD
#halt Error - you must define ONE sqm method above
#endif
#endif
#endif
#endif
// do not change
#ifdef OLDSQMMETHOD
#ifdef NEWSQMMETHOD
#halt Error - cannot have more than one SQM method defined
#else
#ifdef NEWSQMMETHODCORRECTED
#halt Error - cannot have more than one SQM method defined
#else
#ifdef IRRADIANCEMETHOD
#halt Error - cannot have more than one SQM method defined
#endif
#endif
#endif
#endif
// do not change
#ifdef NEWSQMMETHOD
#ifdef OLDSQMMETHOD
#halt Error - cannot have more than one SQM method defined
#else
#ifdef NEWSQMMETHODCORRECTED
#halt Error - cannot have more than one SQM method defined
#else
#ifdef IRRADIANCEMETHOD
#halt Error - cannot have more than one SQM method defined
#endif
#endif
#endif
#endif
// do not change
#ifdef NEWSQMMETHODCORRECTED
#ifdef OLDSQMMETHOD
#halt Error - cannot have more than one SQM method defined
#else
#ifdef NEWSQMMETHOD
#halt Error - cannot have more than one SQM method defined
#else
#ifdef IRRADIANCEMETHOD
#halt Error - cannot have more than one SQM method defined
#endif
#endif
#endif
#endif
// do not change
#ifdef IRRADIANCEMETHOD
#ifdef OLDSQMMETHOD
#halt Error - cannot have more than one SQM method defined
#else
#ifdef NEWSQMMETHOD
#halt Error - cannot have more than one SQM method defined
#else
#ifdef NEWSQMMETHODCORRECTED
#halt Error - cannot have more than one SQM method defined
#endif
#endif
#endif
#endif
// do not change
#ifdef OLDSQMMETHOD
#define sqm_limit 21.83 // mag limit for earth is 21.95
#endif
#ifdef NEWSQMMETHOD
#define sqm_limit 23.09 // mag limit for earth is 21.95
#endif
#ifdef NEWSQMMETHODCORRECTED
//#define sqm_limit 21.95 // mag limit for earth is 21.95
#define sqm_limit 20 // mostaccurate within 18-22 mpsas
#endif
#ifdef IRRADIANCEMETHOD
#define sqm_limit 21.95 // mag limit for earth is 21.95
#endif
#include <SoftwareSerial.h> // Chris
#include <ArduinoJson.h> // Chris
#include <Arduino.h>
#include <myQueue.h> // By Steven de Salas
#include <FreqCounter.h> // required for frequency measurement of TLS sensor, (c) Martin Nawrath
#include <Math.h> // required for log()
#ifdef LCDDISPLAY
#include <LCD.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h> // needed for LCD, see https://bitbucket.org/fmalpartida/new-liquidcrystal/downloads
#endif
#ifdef OLEDDISPLAY
#include <Wire.h> // needed for I2C, installed when installing the Arduino IDE
#include <mySSD1306AsciiUC.h> // oled
#include <mySSD1306AsciiWireUC.h> // oled
#endif
#ifdef useGPSNEO
#include <myAdafruit_GPS.h> // reguired for GPS, (c) Adafruit
#endif
#ifdef MLX90614SENSOR
#include <myMLX90614AF.h> // IR sensor, (c) Adafruit
#endif
#ifdef BME280SENSOR
#include <mybme280.h>
#endif
#ifdef TSL2561SENSOR
#include <myTSL2561AF.h>
#endif
// Chris
// Connect the TX line from the ESP module to the Arduino's pin 2
// and the RX line from the ESP module to the Arduino's pin 3
// Emulate EspSerial on pins 2/3 if not present
SoftwareSerial s(8, 9); // RX, TX
// ------------------------------------------------------------------------------------------------------
// GLOBAL DEFINES
#define rs_AO A1 // the rain sensor analog port pin, voltage
#define rs_DO 2 // the rain sensor digital port pin, raining=yes/no
#define BME280_I2CADDR 0x76
#define LCDADDRESS 0x27
#define OLEDADDRESS 0x3C // address of OLED
#define GPSRXPin 4
#define GPSTXPin 3 // the gps transmits to Arduino to pin D4, Arduino transmits to GPS using Pin3 to RXD
#define SENSORSAMPLETIME 1500 // time between updates of sensors
#define SQMSAMPLETIME 5000 // an SQM sample is taken every 5s (cannot be less than 5s)
#define LCDUPDATETIME 4000 // LCD screen updated every 4.5s
#define buf_size 20 // size of temporary buffers for string conversions
#define timebuf_size 12
#define datebuf_size 12
#define SerialPortSpeed 9600 // Note that to talk to APT as a Sky Meter needs to be 115200
#define GPSPortSpeed 9600 // The speed at which the controller talks to a GPS unit
#define LDRCutoff1 275 // boundary between black and dark, used to determine Gate time
#define LDRCutoff2 500 // boundary between dark and light
#define blackperiod 2000 // 2s Gate time, at very dark sites
#define darkperiod 1000 // 1s Gate time, at dark sites
#define lightperiod 100 // 100ms Gate time for day light
#define MAXCOMMAND 8 // size of receive buffer, :xxxx#
#define SKYCLEAR 0
#define SKYPCLOUDY 1
#define SKYCLOUDY 2
#define SKYUNKNOWN 3
// ------------------------------------------------------------------------------------------------------
// PROGRAM VARIABLES
char programName[] = "MYSQMPRO and Wifi"; // Chris Program title and version information
byte programVersion = 26;
int setpoint1; // setpoint values used to determine sky state
int setpoint2;
int skystate;
long sensorsampletimer; // used to determine when a sample sensor reading is taken
bool sensorsamplerequest; // determines which sensors to read
long sqmsampletimer; // used to determine when a sample sqm reading is taken
long lcdsampletimer;
long now;
double bme280humidity;
double bme280temperature;
unsigned long int bme280pressure;
float dewpoint;
float mlx90614ambient;
float mlx90614object;
double lux;
bool raining;
int rainVout;
int volts;
#ifdef useGPSNEO
SoftwareSerial ss(GPSRXPin, GPSTXPin); // define software serial port for GPS
Adafruit_GPS GPS(&ss); // create GPS object
char mytime[timebuf_size]; // holds formatted time string from GPS data
char mydate[datebuf_size]; // holds formatted date string from GPS data
boolean usingInterrupt = true;
void useInterrupt(boolean); // Func prototype keeps Arduino 0023 happy
#endif
#ifdef LCDDISPLAY
LiquidCrystal_I2C lcd(LCDADDRESS, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
int lcdpage; // which page to display
#endif
#ifdef OLEDDISPLAY
SSD1306AsciiWire myoled;
int lcdpage; // which page to display
#endif
// TSL237 Sensor
#define mySQMSensor 5 // TSL237 OUT to digital pin 5 (cannot change)
float mySQMreading; // the SQM value, sky magnitude
double frequency; // measured TSL237 frequency which is dependent on light
double irradiance;
double nelm;
#ifdef TSL2561SENSOR
TSL2561 myTSL2561(TSL2561_ADDR_FLOAT);
#endif
// LDR GL5528
int LDRpin = A0; // Light Dependant Resistor is on A0
short period; // gate time - specifies duration time for frequency measurement
// IR Sensor, used as a cloud sensor, values help define sky state - cloudy, partly cloudy, clear
#ifdef MLX90614SENSOR
Adafruit_MLX90614 mymlx90614 = Adafruit_MLX90614();
#endif
// Serial command interface
Queue<String> queue(10); // receive serial queue of commands
char line[MAXCOMMAND];
int eoc; // end of command
int idx; // index into command string
#ifdef BME280SENSOR
bme280 mybme280;
#endif
// ------------------------------------------------------------------------------------------------------
// METHODS START
// calculates dew point
// input: humidity [%RH], temperature in C
// output: dew point in C
void calc_dewpoint(float t, float h)
{
float logEx;
logEx = 0.66077 + 7.5 * t / (237.3 + t) + (log10(h) - 2);
dewpoint = (logEx - 0.66077) * 237.3 / (0.66077 + 7.5 - logEx);
}
void getskystate()
{
#ifdef useGPSNEO
GPS.pause(true);
#endif
#ifdef MLX90614SENSOR
float TempDiff = mlx90614ambient - mlx90614object;
// object temp is IR temp of sky which at night time will be a lot less than ambient temp
// so TempDiff is basically ambient + abs(object)
// setpoint 1 is set for clear skies
// setpoint 2 is set for cloudy skies
// setpoint2 should be lower than setpoint1
// For clear, Object will be very low, so TempDiff is largest
// For cloudy, Object is closer to ambient, so TempDiff will be lowest
// Readings are only valid at night when dark and sensor is pointed to sky
// During the day readings are meaningless
if ( TempDiff > setpoint1 )
skystate = SKYCLEAR; // clear
else if ( (TempDiff > setpoint2) && (TempDiff < setpoint1) )
skystate = SKYPCLOUDY; // partly cloudy
else if (TempDiff < setpoint2)
skystate = SKYCLOUDY; // cloudy
else
skystate = SKYUNKNOWN; // unknown
#ifdef useGPSNEO
GPS.pause(false);
#endif
#endif
}
void getraining()
{
#ifdef RAINSENSOR
if ( !digitalRead( rs_DO ) )
raining = true;
else
raining = false;
rainVout = analogRead( rs_AO );
#endif
}
// convert string to int
int decstr2int(String line)
{
int ret = 0;
ret = line.toInt();
return ret;
}
void processCommand()
{
int len;
int cmdval;
String replystr = "";
String mycmd = "";
String param = "";
#ifdef useGPSNEO
GPS.pause(true);
#endif
replystr = queue.pop();
len = replystr.length();
if ( len == 2 )
{
mycmd = replystr; // a valid command with no parameters, ie, :01#
}
else if ( len > 2 )
{
mycmd = replystr.substring(0, 2); // this command has parameters
param = replystr.substring(2, len);
}
else return;
cmdval = decstr2int(mycmd);
#ifdef DEBUG
Serial.print("replystr = "); Serial.println(replystr);
Serial.print("len = "); Serial.println(len);
Serial.print("mycmd = "); Serial.println(mycmd);
Serial.print("param = "); Serial.println(param);
Serial.print("cmdval = "); Serial.println(cmdval);
#endif
switch ( cmdval )
{
case 1: // if the command is GR "GetReading"
replystr = "A" + String(mySQMreading) + "#";
Serial.print(replystr);
break;
case 2: // if the command is GF "Get Frequency"
replystr = "B" + String(frequency) + "#";
Serial.print(replystr);
break;
case 3: // if the command is GI "Get Irradiance"
replystr = "C" + String(irradiance) + "#";
Serial.print(replystr);
break;
case 4: // if the command is GV "Get Firmware Version"
replystr = "D" + String(programVersion) + "#";
Serial.print(replystr);
break;
case 5: // if the command is GZ "Get Firmware Filename"
replystr = "E" + String(programName) + "#";
Serial.print(replystr);
break;
case 6: // if the command is GL "Get LDR value"
{
int LDRval = analogRead(LDRpin); // Read the analogue pin
replystr = "F" + String(LDRval) + "#";
Serial.print(replystr);
}
break;
case 7: // if the command is GP "Get period gate time"
replystr = "G" + String(period) + "#";
Serial.print(replystr);
break;
case 8: // if the command is GD "Get Date"
#ifdef useGPSNEO
replystr = "H" + String(GPS.day) + "/" + String(GPS.month) + "/20" + String(GPS.year) + "#";
#else
replystr = "H01/01/2001#";
#endif
Serial.print(replystr);
break;
case 9: // if the command is GT "Get Time"
#ifdef useGPSNEO
replystr = "I" + String(GPS.hour) + ":" + String(GPS.minute) + ":" + String(GPS.seconds) + "#";
#else
replystr = "I00:00:00#";
#endif
Serial.print(replystr);
break;
case 10: // if the command is GO "Get Longitude"
#ifdef useGPSNEO
replystr = "J" + String(GPS.longitude / 100) + String(GPS.lon) + "#";
#else
replystr = "J00#";
#endif
Serial.print(replystr);
break;
case 11: // if the command is GA "Get Latitude"
#ifdef useGPSNEO
replystr = "K" + String(GPS.latitude / 100) + String(GPS.lat) + "#";
#else
replystr = "K00#";
#endif
Serial.print(replystr);
break;
case 12: // if the command is GH "Get Altitude"
#ifdef useGPSNEO
replystr = "L" + String((int)GPS.altitude) + "#";
#else
replystr = "L0#";
#endif
Serial.print(replystr);
break;
case 13: // if the command is GS "Get Satelittes"
#ifdef useGPSNEO
replystr = "M" + String(GPS.satellites) + "#";
#else
replystr = "M0#";
#endif
Serial.print(replystr);
break;
case 16: // gps fix?
replystr = "N0#";
#ifdef useGPSNEO
if ( GPS.fix )
replystr = "N1#";
#endif
Serial.print(replystr);
break;
case 19: // get IR object temperature
replystr = "O" + String(mlx90614object) + "#";
Serial.print(replystr);
break;
case 20: // get IR ambient temperature
replystr = "P" + String(mlx90614ambient) + "#";
Serial.print(replystr);
break;
case 21: // return LUX value
replystr = "U" + String(lux) + "#";
Serial.print(replystr);
break;
case 23: // return raining
replystr = "R0#";
#ifdef RAINSENSOR
if ( raining == true )
replystr = "R1#";
#endif
Serial.print(replystr);
break;
case 24: // return rain voltage
replystr = "S0#";
#ifdef RAINSENSOR
replystr = "S" + String(rainVout) + "#";
#endif
Serial.print(replystr);
break;
case 25: // save setpoint1
setpoint1 = decstr2int(param);
break;
case 26: // save setpoint2
setpoint2 = decstr2int(param);
break;
case 27: // return sky state
replystr = "V" + String(skystate) + "#";
Serial.print(replystr);
break;
case 28: // return setpoint1
replystr = "W" + String(setpoint1) + "#";
Serial.print(replystr);
break;
case 29: // return setpoint2
replystr = "X" + String(setpoint2) + "#";
Serial.print(replystr);
break;
case 31: // if the command is GI "Get Nelm"
replystr = "Z" + String(nelm) + "#";
Serial.print(replystr);
break;
case 32: // Get Humidity BME280 sensor
replystr = "a" + String(bme280humidity, 2) + "#";
Serial.print(replystr);
break;
case 33: // Get pressure BME280 sensor
replystr = "e" + String(bme280pressure) + "#";
Serial.print(replystr);
break;
case 34: // Get temperature BME280 sensor
replystr = "c" + String(bme280temperature, 2) + "#";
Serial.print(replystr);
break;
case 35: // Get dewpoint
replystr = "d" + String(dewpoint, 2) + "#";
Serial.print(replystr);
break;
}
#ifdef useGPSNEO
GPS.pause(false);
#endif
}
// calculate LUX reading from LDR value
void getlux()
{
#ifdef useGPSNEO
GPS.pause(true);
#endif
#ifdef TSL2561SENSOR
uint32_t lum = myTSL2561.getFullLuminosity();
uint16_t ir, full;
ir = lum >> 16;
full = lum & 0xFFFF;
lux = myTSL2561.calculateLux(full, ir);
#else
// portions of code from https://www.digikey.com/en/maker/projects/design-a-luxmeter-with-an-ldr-and-an-arduino/623aeee0f93e427bb57e02c4592567d1
int ldrRawData;
float resistorVoltage, ldrVoltage, ldrResistance;
ldrRawData = analogRead(LDRpin); // Read the analogue pin, returns value 0-1023
resistorVoltage = (float)ldrRawData / 1023 * 5;
ldrVoltage = 5.0 - resistorVoltage;
ldrResistance = ldrVoltage / resistorVoltage * 10000; // Resistor is 10 kohm
lux = (double)12518931 * pow(ldrResistance, -1.405);
#endif
#ifdef useGPSNEO
GPS.pause(false);
#endif
}
#ifdef useGPSNEO
// Interrupt is called once a millisecond, looks for any new GPS data, and stores it
SIGNAL(TIMER0_COMPA_vect)
{
char c = GPS.read();
}
#endif
#ifdef OLEDDISPLAY
void showpageoled()
{
String tempStr;
char tempstr[buf_size];
char sqmstr[] = "SQM";
#ifdef useGPSNEO
GPS.pause(true);
#endif
// can only be called if displayenabled is true
myoled.clear();
switch ( lcdpage )
{
case 1:
myoled.set2X();
myoled.println(sqmstr);
myoled.println(mySQMreading);
lcdpage = 2;
break;
case 2:
#ifdef useGPSNEO
myoled.set1X();
// date and time
myoled.print("DATE: ");
myoled.print(GPS.day, DEC);
myoled.print('/');
myoled.print(GPS.month, DEC);
myoled.print("/20");
myoled.println(GPS.year, DEC);
myoled.print("TIME: ");
myoled.print(GPS.hour, DEC);
myoled.print(':');
myoled.print(GPS.minute, DEC);
myoled.print(':');
myoled.println(GPS.seconds, DEC);
// latitude and longitude
myoled.print("LAT : ");
myoled.print(GPS.latitude / 100);
if ( GPS.lat == 'N' )
myoled.println(" N");
else
myoled.println(" S");
myoled.print("LON : ");
myoled.print(GPS.longitude / 100);
if ( GPS.lon == 'W' )
myoled.println(" W");
else
myoled.println(" E");
// satellites
myoled.print("SAT : ");
myoled.println( GPS.satellites );
myoled.print("ALT : ");
myoled.print((int)GPS.altitude);
myoled.println(" M");
#else
myoled.print("GPS NOT ENABLED");
#endif
lcdpage = 3;
break;
case 3:
myoled.set1X();
myoled.print("NELM = ");
myoled.println(nelm);
// ir sensor
myoled.print("IR AMBIENT = ");
myoled.println(mlx90614ambient);
myoled.print("IR OBJECT = ");
myoled.println(mlx90614object);
// LUX
memset(tempstr, 0, buf_size);
myoled.print("LUX = ");
myoled.println(lux);
// raining
myoled.print("RAINING = ");
if ( raining == false )
myoled.println("NO");
else
myoled.println("YES");
// raining Vout, resolution accuracy is 4.9 mV
volts = map(rainVout, 0, 1023, 0, 5000 );
myoled.print("RAIN VOUT = ");
myoled.print( volts);
myoled.println(" mV");
// setpoint1 and 2
myoled.print("SP1 = ");
myoled.println(setpoint1);
myoled.print("SP2 = ");
myoled.println(setpoint2);
lcdpage = 4;
break;
case 4:
// sky state
switch ( skystate )
{
case SKYCLEAR: myoled.println("CLEAR"); break;
case SKYPCLOUDY: myoled.println("PARTLY CLOUDY"); break;
case SKYCLOUDY: myoled.println("CLOUDY"); break;
default: myoled.println("UNKNOWN"); break;
}
// bme280 data, ambient, humidity, dewpoint, barometric pressure
myoled.print("HUMIDITY : ");
myoled.println(bme280humidity);
myoled.print("AMBIENT : ");
myoled.println(bme280temperature);
myoled.print("DEWPOINT : ");
myoled.println(dewpoint);
myoled.print("HPA : ");
myoled.println(bme280pressure);
lcdpage = 1;
break;
}
#ifdef useGPSNEO
GPS.pause(false);
#endif
}
#endif
#ifdef LCDDISPLAY
#ifdef LCD2004
void showpage2004()
{
#ifdef useGPSNEO
GPS.pause(true);
#endif
switch ( lcdpage )
{
case 1:
lcd.print("SQM : ");
lcd.print(mySQMreading);
lcd.setCursor(0, 1);
lcd.print("NELM: ");
lcd.print(nelm);
#ifdef useGPSNEO
lcd.setCursor(0, 2);
lcd.print("Date: ");
lcd.print(GPS.day, DEC);
lcd.print('/');
lcd.print(GPS.month, DEC);
lcd.print("/20");
lcd.print(GPS.year, DEC);
lcd.setCursor(0, 3);
lcd.print("Time: ");
lcd.print(GPS.hour, DEC);
lcd.print(':');
lcd.print(GPS.minute, DEC);
lcd.print(':');
lcd.print(GPS.seconds, DEC);
#endif
lcdpage = 2;
break;
case 2:
#ifdef useGPSNEO
// latitude and longitude
lcd.print("Lat : ");
lcd.print(GPS.latitude / 100);
if ( GPS.lat == 'N' )
lcd.print(" N");
else
lcd.print(" S");
lcd.setCursor(0, 1);
lcd.print("Lon : ");
lcd.print(GPS.longitude / 100);
if ( GPS.lon == 'W' )
lcd.print(" W");
else
lcd.print(" E");
lcd.setCursor(0, 2);
// satellites
lcd.print("Sat : ");
lcd.print( GPS.satellites );
lcd.setCursor(0, 3);
lcd.print("Alt : ");
lcd.print((int)GPS.altitude);
lcd.print("m");
#else
lcd.print("GPS not enabled");
#endif
lcdpage = 3;
break;
case 3:
lcd.print("IRAmb : ");
lcd.print(mlx90614ambient);
lcd.print("c");
lcd.setCursor(0, 1);
lcd.print("IRObj : ");
lcd.print(mlx90614object);
lcd.print("c");
lcd.setCursor(0, 2);
// raining
lcd.print("Rain? : ");
if ( raining == false ) {
lcd.print("NO");
}
else {
lcd.print("YES");
}
lcd.setCursor(0, 3);
volts = map(rainVout, 0, 1023, 0, 5000 );
lcd.print("RVout : ");
lcd.print(volts);
lcd.print("mV");
lcdpage = 4;
break;
case 4:
// LUX
lcd.print("Lux : " );
lcd.print(lux);
lcd.setCursor(0, 1);
lcd.print("SKY : ");
// Sky state
switch ( skystate )
{
case SKYCLEAR: lcd.print("CLEAR"); break;
case SKYPCLOUDY: lcd.print("PART CLOUDY"); break;
case SKYCLOUDY: lcd.print("CLOUDY"); break;
default: lcd.print("UNKNOWN"); break;
}
// setpoint1 and 2
lcd.setCursor(0, 2);
lcd.print("SP1 : ");
lcd.print(setpoint1);
lcd.setCursor(0, 3);
lcd.print("SP2 : ");
lcd.print(setpoint2);
lcdpage = 5;
break;
case 5:
// bme280 data, ambient, humidity, dewpoint, barometric pressure
lcd.print("Hum: ");
lcd.print(bme280humidity);
lcd.print("%");
lcd.setCursor(0, 1);
lcd.print("Amb: ");
lcd.print(bme280temperature);
lcd.print("c");
lcd.setCursor(0, 2);
lcd.print("Dew: ");
lcd.print(dewpoint);
lcd.print("c");
lcd.setCursor(0, 3);
lcd.print("hPa: ");
lcd.print(bme280pressure);
lcdpage = 1;
break;
}
#ifdef useGPSNEO
GPS.pause(false);
#endif
}
#endif
#endif
#ifdef LCDDISPLAY
#ifdef LCD1604
void showpage1604()
{
#ifdef useGPSNEO
GPS.pause(true);
#endif
switch ( lcdpage )
{
case 1: