/
weatherradio.cpp
1562 lines (1380 loc) · 63.9 KB
/
weatherradio.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
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
/*
Weather Radio - a universal driver for weather stations that
transmit their raw sensor data as JSON documents.
Copyright (C) 2019-2022 Wolfgang Reissenberger <sterne-jaeger@openfuture.de>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Cloud coverage calculation taken from the INDI driver for the AAG Cloud Watcher
(Lunatico https://www.lunatico.es), implemented by Sergio Alonso (zerjioi@ugr.es)
*/
#include "weatherradio.h"
#include "weathercalculator.h"
#include <cstdlib>
#include <cstring>
#include <map>
#include <sys/stat.h>
#include "indistandardproperty.h"
#include "indilogger.h"
#include "connectionplugins/connectionserial.h"
#include "indicom.h"
#include "curl/curl.h"
#include "config.h"
const char *CALIBRATION_TAB = "Calibration";
/* Our weather station auto pointer */
std::unique_ptr<WeatherRadio> station_ptr(new WeatherRadio());
#define MAX_WEATHERBUFFER 512
#define ARDUINO_SETTLING_TIME 5
#define WIFI_DEVICE "WiFi"
#define WEATHER_TEMPERATURE "WEATHER_TEMPERATURE"
#define WEATHER_PRESSURE "WEATHER_PRESSURE"
#define WEATHER_HUMIDITY "WEATHER_HUMIDITY"
#define WEATHER_CLOUD_COVER "WEATHER_CLOUD_COVER"
#define WEATHER_SQM "WEATHER_SQM"
#define WEATHER_DEWPOINT "WEATHER_DEWPOINT"
#define WEATHER_SKY_TEMPERATURE "WEATHER_SKY_TEMPERATURE"
#define WEATHER_WIND_GUST "WEATHER_WIND_GUST"
#define WEATHER_WIND_SPEED "WEATHER_WIND_SPEED"
#define WEATHER_WIND_DIRECTION "WEATHER_WIND_DIRECTION"
#define WEATHER_RAIN_DROPS "WEATHER_RAIN_DROPS"
#define WEATHER_RAIN_VOLUME "WEATHER_RAIN_VOLUME"
#define WEATHER_WETNESS "WEATHER_WETNESS"
/**************************************************************************************
**
***************************************************************************************/
static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp)
{
strcpy(static_cast<char *>(userp), static_cast<char *>(contents));
return size * nmemb;
}
/**************************************************************************************
** Constructor
***************************************************************************************/
WeatherRadio::WeatherRadio()
{
setVersion(WEATHERRADIO_VERSION_MAJOR, WEATHERRADIO_VERSION_MINOR);
weatherCalculator = new WeatherCalculator();
// define weather radio commands
commands[CMD_VERSION] = "v";
commands[CMD_WEATHER] = "w";
commands[CMD_CONFIG] = "c";
commands[CMD_DURATION] = "t";
commands[CMD_WIFI_ON] = "s";
commands[CMD_WIFI_OFF] = "d";
commands[CMD_RESET] = "r";
}
/**************************************************************************************
** Initialize all properties & set default values.
**************************************************************************************/
bool WeatherRadio::initProperties()
{
INDI::Weather::initProperties();
addConfigurationControl();
IUFillNumber(&ttyTimeoutN[0], "TIMEOUT", "Timeout (s)", "%.f", 0, 60, 1, getTTYTimeout());
IUFillNumberVector(&ttyTimeoutNP, ttyTimeoutN, 1, getDeviceName(), "TTY_TIMEOUT", "TTY timeout", CONNECTION_TAB, IP_RW, 0,
IPS_OK);
// Firmware version
IUFillText(&FirmwareInfoT[0], "FIRMWARE_INFO", "Firmware Version", "<unknown version>");
IUFillTextVector(&FirmwareInfoTP, FirmwareInfoT, 1, getDeviceName(), "FIRMWARE", "Firmware", INFO_TAB, IP_RO, 60, IPS_OK);
// Reset Arduino
IUFillSwitch(&resetArduinoS[0], "RESET", "Reset", ISS_OFF);
IUFillSwitchVector(&resetArduinoSP, resetArduinoS, 1, getDeviceName(), "RESET_ARDUINO", "Arduino", INFO_TAB, IP_RW,
ISR_ATMOST1, 60, IPS_IDLE);
// refresh firmware configuration
IUFillSwitch(&refreshConfigS[0], "REFRESH", "Refresh", ISS_OFF);
IUFillSwitchVector(&refreshConfigSP, refreshConfigS, 1, getDeviceName(), "REFRESH_CONFIG", "Refresh", INFO_TAB, IP_RW,
ISR_ATMOST1, 60, IPS_IDLE);
// connect/disconnect WiFi
IUFillSwitch(&wifiConnectionS[0], "DISCONNECT", "Disconnect", ISS_OFF);
IUFillSwitch(&wifiConnectionS[1], "CONNECT", "Connect", ISS_OFF);
IUFillSwitchVector(&wifiConnectionSP, wifiConnectionS, 2, getDeviceName(), "WIFI", "WiFi", INFO_TAB, IP_RW, ISR_ATMOST1, 60,
IPS_IDLE);
// calibration parameters
IUFillNumber(&skyTemperatureCalibrationN[0], "K1", "K1", "%.2f", 0, 100, 1,
weatherCalculator->skyTemperatureCoefficients.k1);
IUFillNumber(&skyTemperatureCalibrationN[1], "K2", "K2", "%.2f", -200, 200, 1,
weatherCalculator->skyTemperatureCoefficients.k2);
IUFillNumber(&skyTemperatureCalibrationN[2], "K3", "K3", "%.2f", 0, 100, 1,
weatherCalculator->skyTemperatureCoefficients.k3);
IUFillNumber(&skyTemperatureCalibrationN[3], "K4", "K4", "%.2f", 0, 100, 1,
weatherCalculator->skyTemperatureCoefficients.k4);
IUFillNumber(&skyTemperatureCalibrationN[4], "K5", "K5", "%.2f", 0, 100, 1,
weatherCalculator->skyTemperatureCoefficients.k5);
IUFillNumber(&skyTemperatureCalibrationN[5], "T_CLEAR", "clear sky (°C)", "%.2f", -20, 20, 1,
weatherCalculator->skyTemperatureCoefficients.t_clear);
IUFillNumber(&skyTemperatureCalibrationN[6], "T_OVERCAST", "overcast sky (°C)", "%.2f", -20, 20, 1,
weatherCalculator->skyTemperatureCoefficients.t_overcast);
IUFillNumberVector(&skyTemperatureCalibrationNP, skyTemperatureCalibrationN, 7, getDeviceName(), "SKY_TEMP_CALIBRATION",
"Sky Temp calibr.", CALIBRATION_TAB, IP_RW, 0, IPS_OK);
// calibration parameters
IUFillNumber(&humidityCalibrationN[0], "FACTOR", "Factor", "%.3f", 0, 10, 0.1, 1.0);
IUFillNumber(&humidityCalibrationN[1], "SHIFT", "Shift", "%.3f", -100, 100, 1, 0.0);
IUFillNumberVector(&humidityCalibrationNP, humidityCalibrationN, 2, getDeviceName(), "HUMIDITY_CALIBRATION",
"Humidity calibr.", CALIBRATION_TAB, IP_RW, 0, IPS_OK);
IUFillNumber(&temperatureCalibrationN[0], "FACTOR", "Factor", "%.3f", 0, 10, 0.1, 1.0);
IUFillNumber(&temperatureCalibrationN[1], "SHIFT", "Shift", "%.3f", -100, 100, 1, 0.0);
IUFillNumberVector(&temperatureCalibrationNP, temperatureCalibrationN, 2, getDeviceName(), "TEMPERATURE_CALIBRATION",
"Temperature calibr.", CALIBRATION_TAB, IP_RW, 0, IPS_OK);
IUFillNumber(&sqmCalibrationN[0], "FACTOR", "Factor", "%.3f", 0, 10, 0.1, 1.0);
IUFillNumber(&sqmCalibrationN[1], "SHIFT", "Shift", "%.3f", -100, 100, 1, 0.0);
IUFillNumberVector(&sqmCalibrationNP, sqmCalibrationN, 2, getDeviceName(), "SQM_CALIBRATION", "SQM calibr.",
CALIBRATION_TAB, IP_RW, 0, IPS_OK);
IUFillNumber(&wetnessCalibrationN[0], "FACTOR", "Factor", "%.3f", 0, 10, 0.1, 1.0);
IUFillNumber(&wetnessCalibrationN[1], "SHIFT", "Shift", "%.3f", -100, 100, 1, 0.0);
IUFillNumberVector(&wetnessCalibrationNP, wetnessCalibrationN, 2, getDeviceName(), "WETNESS_CALIBRATION", "Wetness calibr.",
CALIBRATION_TAB, IP_RW, 0, IPS_OK);
IUFillNumber(&windDirectionCalibrationN[0], "OFFSET", "Offset", "%.3f", 0, 360, 1, 0.0);
IUFillNumberVector(&windDirectionCalibrationNP, windDirectionCalibrationN, 1, getDeviceName(), "WIND_DIRECTION_CALIBRATION",
"Wind direction", CALIBRATION_TAB, IP_RW, 0, IPS_OK);
addDebugControl();
setWeatherConnection(CONNECTION_SERIAL);
// define INDI config values for known sensors
deviceConfig["BME280"]["Temp"] = {"Temperature (°C)", TEMPERATURE_SENSOR, "%.1f", -100.0, 100.0, 1.0};
deviceConfig["BME280"]["Pres"] = {"Pressure (hPa)", PRESSURE_SENSOR, "%.1f", 500., 1100.0, 1.0};
deviceConfig["BME280"]["Hum"] = {"Humidity (%)", HUMIDITY_SENSOR, "%.1f", 0., 100.0, 1.0};
deviceConfig["DHT"]["Temp"] = {"Temperature (°C)", TEMPERATURE_SENSOR, "%.1f", -100.0, 100.0, 1.0};
deviceConfig["DHT"]["Hum"] = {"Humidity (%)", HUMIDITY_SENSOR, "%.1f", 0., 100.0, 1.0};
deviceConfig["MLX90614"]["T amb"] = {"Ambient Temp. (°C)", TEMPERATURE_SENSOR, "%.1f", -100.0, 100.0, 1.0};
deviceConfig["MLX90614"]["T obj"] = {"Sky Temp. (°C)", OBJECT_TEMPERATURE_SENSOR, "%.1f", -100.0, 100.0, 1.0};
deviceConfig["TSL237"]["Frequency"] = {"Frequency", INTERNAL_SENSOR, "%.0f", 0.0, 100000.0, 1.0};
deviceConfig["TSL237"]["SQM"] = {"SQM", SQM_SENSOR, "%.1f", 0.0, 25.0, 1.0};
deviceConfig["TSL2591"]["Lux"] = {"Luminance (Lux)", LUMINOSITY_SENSOR, "%.3f", 0.0, 1000.0, 1.0};
deviceConfig["TSL2591"]["Visible"] = {"Lightness (Vis)", INTERNAL_SENSOR, "%.1f", 0.0, 1000.0, 1.0};
deviceConfig["TSL2591"]["IR"] = {"Lightness (IR)", INTERNAL_SENSOR, "%.1f", 0.0, 1000.0, 1.0};
deviceConfig["TSL2591"]["Gain"] = {"Gain", INTERNAL_SENSOR, "%.0f", 0.0, 1000.0, 1.0};
deviceConfig["TSL2591"]["Timing"] = {"Timing", INTERNAL_SENSOR, "%.0f", 0.0, 1000.0, 1.0};
deviceConfig["Davis Anemometer"]["avg speed"] = {"Wind speed (avg, m/s)", WIND_SPEED_SENSOR, "%.1f", 0., 100.0, 1.0};
deviceConfig["Davis Anemometer"]["min speed"] = {"Wind speed (min, m/s)", INTERNAL_SENSOR, "%.1f", 0., 100.0, 1.0};
deviceConfig["Davis Anemometer"]["max speed"] = {"Wind speed (max, m/s)", WIND_GUST_SENSOR, "%.1f", 0., 100.0, 1.0};
deviceConfig["Davis Anemometer"]["direction"] = {"Wind direction (deg)", WIND_DIRECTION_SENSOR, "%.0f", 0., 360.0, 1.0};
deviceConfig["Davis Anemometer"]["rotations"] = {"Wind wheel rotations", INTERNAL_SENSOR, "%.0f", 0., 360.0, 1.0};
deviceConfig["RG11 Rain Sensor"]["drop freq"] = {"Rain drops (1/min)", RAIN_DROPS_SENSOR, "%.3f", 0., 200.0, 0.1};
deviceConfig["RG11 Rain Sensor"]["rain volume"] = {"Rain volume (mm)", RAIN_VOLUME_SENSOR, "%.3f", 0., 10000.0, 1.0};
deviceConfig["RG11 Rain Sensor"]["count"] = {"Event count", INTERNAL_SENSOR, "%.0f", 0., 1000000.0, 1.0};
deviceConfig["W174 Rain Sensor"]["rain volume"] = {"Rain volume (mm)", RAIN_VOLUME_SENSOR, "%.3f", 0., 10000.0, 1.0};
deviceConfig["W174 Rain Sensor"]["count"] = {"Event count", INTERNAL_SENSOR, "%.0f", 0., 1000000.0, 1.0};
deviceConfig["Water"]["wetness"] = {"Wetness (%)", WETNESS_SENSOR, "%.1f", 0., 100.0, 0.1};
LOG_DEBUG("Properties initialization finished successfully.");
return true;
}
/**************************************************************************************
** Update the INDI properties as a reaction on connect or disconnect.
***************************************************************************************/
bool WeatherRadio::updateProperties()
{
bool result = true;
// dynamically add weather parameters
if (isConnected())
{
// read the weather parameters for the first time so that #updateProperties() knows all sensors
updateWeather();
if (sensorRegistry.temperature.size() > 0)
{
addParameter(WEATHER_TEMPERATURE, "Temperature (°C)", -10, 40, 15);
setCriticalParameter(WEATHER_TEMPERATURE);
addSensorSelection(&temperatureSensorSP, sensorRegistry.temperature, "TEMPERATURE_SENSOR", "Temperature Sensor");
addSensorSelection(&ambientTemperatureSensorSP, sensorRegistry.temperature, "AMBIENT_TEMP_SENSOR", "Ambient Temp. Sensor");
defineProperty(&temperatureCalibrationNP);
defineProperty(&skyTemperatureCalibrationNP);
LOG_INFO("Temperature sensor selections added.");
}
if (sensorRegistry.pressure.size() > 0)
{
addParameter(WEATHER_PRESSURE, "Pressure (hPa)", 950, 1070, 15);
setCriticalParameter(WEATHER_PRESSURE);
addSensorSelection(&pressureSensorSP, sensorRegistry.pressure, "PRESSURE_SENSOR", "Pressure Sensor");
LOG_INFO("Pressure sensor selections added.");
}
if (sensorRegistry.humidity.size() > 0)
{
addParameter(WEATHER_HUMIDITY, "Humidity (%)", 0, 100, 15);
addParameter(WEATHER_DEWPOINT, "Dewpoint (°C)", -10, 30, 15);
setCriticalParameter(WEATHER_HUMIDITY);
addSensorSelection(&humiditySensorSP, sensorRegistry.humidity, "HUMIDITY_SENSOR", "Humidity Sensor");
defineProperty(&humidityCalibrationNP);
LOG_INFO("Humidity sensor selections added.");
}
if (sensorRegistry.luminosity.size() > 0 || sensorRegistry.sqm.size() > 0)
{
addParameter(WEATHER_SQM, "SQM", 10, 30, 15);
setCriticalParameter(WEATHER_SQM);
if (sensorRegistry.luminosity.size() > 0)
{
addSensorSelection(&luminositySensorSP, sensorRegistry.luminosity, "LUMINOSITY_SENSOR", "Luminosity Sensor");
LOG_INFO("Luminosity sensor selections added.");
}
if (sensorRegistry.sqm.size() > 0)
{
addSensorSelection(&sqmSensorSP, sensorRegistry.sqm, "SQM_SENSOR", "SQM Sensor");
defineProperty(&sqmCalibrationNP);
LOG_INFO("SQM sensor selections added.");
}
}
if (sensorRegistry.temp_object.size() > 0)
{
addParameter(WEATHER_CLOUD_COVER, "Clouds (%)", 0, 100, 50);
addParameter(WEATHER_SKY_TEMPERATURE, "Sky Temp (corr, °C)", -30, 20, 0);
setCriticalParameter(WEATHER_CLOUD_COVER);
addSensorSelection(&objectTemperatureSensorSP, sensorRegistry.temp_object, "OBJECT_TEMP_SENSOR", "Object Temp. Sensor");
LOG_INFO("Sky temperature sensor selections added.");
}
if (sensorRegistry.wind_gust.size() > 0)
{
addParameter(WEATHER_WIND_GUST, "Wind gust (m/s)", 0, 15, 50);
setCriticalParameter(WEATHER_WIND_GUST);
addSensorSelection(&windGustSensorSP, sensorRegistry.wind_gust, "WIND_GUST_SENSOR", "Wind Gust Sensor");
LOG_INFO("Wind gust sensor selections added.");
}
if (sensorRegistry.wind_speed.size() > 0)
{
addParameter(WEATHER_WIND_SPEED, "Wind speed (m/s)", 0, 10, 50);
setCriticalParameter(WEATHER_WIND_SPEED);
addSensorSelection(&windSpeedSensorSP, sensorRegistry.wind_speed, "WIND_SPEED_SENSOR", "Wind Speed Sensor");
LOG_INFO("Wind speed sensor selections added.");
}
if (sensorRegistry.wind_direction.size() > 0)
{
addParameter(WEATHER_WIND_DIRECTION, "Wind direction (deg)", 0, 360, 10);
addSensorSelection(&windDirectionSensorSP, sensorRegistry.wind_direction, "WIND_DIRECTION_SENSOR", "Wind Direction Sensor");
defineProperty(&windDirectionCalibrationNP);
LOG_INFO("Wind direction sensor selections added.");
}
if (sensorRegistry.rain_drops.size() > 0)
{
addParameter(WEATHER_RAIN_DROPS, "Rain drops (1/min)", 0, 1, 10);
setCriticalParameter(WEATHER_RAIN_DROPS);
addSensorSelection(&rainDropsSensorSP, sensorRegistry.rain_drops, "RAIN_DROPS_SENSOR", "Rain Drops Sensor");
// no calibration needed
LOG_INFO("Rain intensity sensor selection added.");
}
if (sensorRegistry.rain_volume.size() > 0)
{
addParameter(WEATHER_RAIN_VOLUME, "Rain volume (mm)", 0, 100000, 10);
addSensorSelection(&rainVolumeSensorSP, sensorRegistry.rain_volume, "RAIN_VOLUME_SENSOR", "Rain Volume Sensor");
// no calibration needed
LOG_INFO("Rain volume sensor selection added.");
}
if (sensorRegistry.wetness.size() > 0)
{
addParameter(WEATHER_WETNESS, "Wetness (%)", 0, 100, 10);
setCriticalParameter(WEATHER_WETNESS);
addSensorSelection(&wetnessSensorSP, sensorRegistry.wetness, "WETNESS_SENSOR", "Wetness Sensor");
defineProperty(&wetnessCalibrationNP);
LOG_INFO("Wetness sensor selection added.");
}
for (size_t i = 0; i < rawDevices.size(); i++)
defineProperty(&rawDevices[i]);
LOG_INFO("Raw sensors added.");
result = INDI::Weather::updateProperties();
defineProperty(&resetArduinoSP);
}
else
{
for (size_t i = 0; i < rawDevices.size(); i++)
deleteProperty(rawDevices[i].name);
deleteProperty(resetArduinoSP.name);
deleteProperty(wetnessSensorSP.name);
deleteProperty(wetnessCalibrationNP.name);
deleteProperty(rainVolumeSensorSP.name);
deleteProperty(rainDropsSensorSP.name);
deleteProperty(windDirectionCalibrationNP.name);
deleteProperty(sqmCalibrationNP.name);
deleteProperty(temperatureCalibrationNP.name);
deleteProperty(humidityCalibrationNP.name);
deleteProperty(skyTemperatureCalibrationNP.name);
deleteProperty(temperatureSensorSP.name);
deleteProperty(pressureSensorSP.name);
deleteProperty(humiditySensorSP.name);
deleteProperty(luminositySensorSP.name);
deleteProperty(sqmSensorSP.name);
deleteProperty(ambientTemperatureSensorSP.name);
deleteProperty(objectTemperatureSensorSP.name);
deleteProperty(windGustSensorSP.name);
deleteProperty(windSpeedSensorSP.name);
deleteProperty(windDirectionSensorSP.name);
deleteProperty(refreshConfigSP.name);
deleteProperty(wifiConnectionSP.name);
deleteProperty(FirmwareInfoTP.name);
deleteProperty(FirmwareConfigTP.name);
result = INDI::Weather::updateProperties();
// clear firmware configuration so that #handleFirmwareVersion() recongnizes an initialisation
FirmwareConfigTP.tp = nullptr;
free(FirmwareConfigT);
FirmwareConfigT = nullptr;
// clear version information
major_version = 0;
minor_version = 0;
LOG_DEBUG("Weather Radio properties removal completed.");
}
return result;
}
/**************************************************************************************
** Retrieve basic data after a successful connect.
***************************************************************************************/
IPState WeatherRadio::getBasicData()
{
FirmwareInfoTP.s = updateFirmwareVersion();
IDSetText(&FirmwareInfoTP, nullptr);
if (FirmwareInfoTP.s != IPS_OK)
{
LOG_ERROR("Failed to get firmware from device.");
return FirmwareInfoTP.s;
}
else
{
LOGF_INFO("Firmware version: %s", FirmwareInfoT[0].text);
char* dotpos = strchr(FirmwareInfoT[0].text, '.');
if (dotpos != nullptr)
{
major_version = atoi(FirmwareInfoT[0].text);
minor_version = atoi(dotpos + 1);
}
else
{
LOGF_ERROR("Version not in dot notation: %s", FirmwareInfoT[0].text);
}
}
defineProperty(&FirmwareInfoTP);
IPState result = updateFirmwareConfig();
return result;
}
/**************************************************************************************
** Update firmware configuration data
***************************************************************************************/
void WeatherRadio::updateConfigData()
{
FirmwareInfoTP.s = updateFirmwareVersion();
if (FirmwareInfoTP.s != IPS_OK)
LOG_ERROR("Failed to get firmware from device.");
FirmwareConfigTP.s = updateFirmwareConfig();
IDSetText(&FirmwareInfoTP, nullptr);
IDSetText(&FirmwareConfigTP, nullptr);
}
/**************************************************************************************
** Version of the Arduino firmware
***************************************************************************************/
IPState WeatherRadio::updateFirmwareVersion()
{
bool result = executeCommand(CMD_VERSION);
LOG_DEBUG(result ? "Firmware retrieved successfully." : "Request for firmware version failed!");
return (result ? IPS_OK : IPS_ALERT);
}
void WeatherRadio::handleFirmwareVersion(JsonValue value)
{
FirmwareInfoTP.s = IPS_IDLE;
JsonIterator docIter;
for (docIter = begin(value); docIter != end(value); ++docIter)
{
if (strcmp(docIter->key, "version") == 0)
{
IUSaveText(&FirmwareInfoT[0], docIter->value.toString());
FirmwareInfoTP.s = IPS_OK;
}
}
}
/**************************************************************************************
** Retrieve the configuration parameters from the firmware
***************************************************************************************/
IPState WeatherRadio::updateFirmwareConfig()
{
bool result = executeCommand(CMD_CONFIG);
LOG_DEBUG(result ? "Firmware configuration retrieved successfully." : "Request for firmware configuration failed!");
return (result ? IPS_OK : IPS_ALERT);
}
IPState WeatherRadio::handleFirmwareConfig(JsonValue jvalue)
{
FirmwareConfig config;
// are the INDI properties already initialized?
bool init = FirmwareConfigT == nullptr;
JsonIterator deviceIter;
for (deviceIter = begin(jvalue); deviceIter != end(jvalue); ++deviceIter)
{
char *device {new char[strlen(deviceIter->key) + 1] {0}};
strncpy(device, deviceIter->key, static_cast<size_t>(strlen(deviceIter->key)));
if (strcmp(device, WIFI_DEVICE) == 0)
hasWiFi = true;
JsonIterator configIter;
// read settings for the single device
for (configIter = begin(deviceIter->value); configIter != end(deviceIter->value); ++configIter)
{
char *name {new char[strlen(configIter->key) + 1] {0}};
// copy single setting
strncpy(name, configIter->key, static_cast<size_t>(strlen(configIter->key)));
std::string value;
double number;
switch (configIter->value.getTag())
{
case JSON_NUMBER:
number = configIter->value.toNumber();
if (trunc(number) == number)
value = std::to_string(int(number));
else
value = std::to_string(number);
break;
case JSON_TRUE:
value = "true";
break;
case JSON_FALSE:
value = "false";
break;
default:
value = strdup(configIter->value.toString());
break;
}
// add it to the configuration
config[std::string(device) + "::" + std::string(name)] = value;
}
}
// define the properties vector
if (init)
{
FirmwareConfigT = new IText[config.size()];
IUFillTextVector(&FirmwareConfigTP, FirmwareConfigT, static_cast<int>(config.size()), getDeviceName(), "FIRMWARE_CONFIGS",
"Firmware config", INFO_TAB, IP_RO, 60, IPS_OK);
}
// fill device cofigurations into INDI properties
size_t pos = 0;
for (std::map<std::string, std::string>::iterator it = config.begin(); it != config.end(); ++it)
{
if (init)
{
FirmwareConfigT[pos].text = nullptr; // seems like a bug in IUFillText that this is necessary
IUFillText(&FirmwareConfigT[pos++], it->first.c_str(), it->first.c_str(), it->second.c_str());
LOGF_INFO("Firmware config: %s = %s", it->first.c_str(), it->second.c_str());
}
else
{
// find the matching text property
for (int i = 0; i < FirmwareConfigTP.ntp; i++)
{
if (strcmp(FirmwareConfigT[i].name, it->first.c_str()) == 0)
{
IUSaveText(&FirmwareConfigT[i], it->second.c_str());
LOGF_INFO("Firmware config: %s = %s", it->first.c_str(), it->second.c_str());
}
}
}
}
if (init)
{
// firmware configuration properties
defineProperty(&FirmwareConfigTP);
// refresh button
defineProperty(&refreshConfigSP);
if (hasWiFi)
defineProperty(&wifiConnectionSP);
}
// update WiFi status
if (hasWiFi)
{
FirmwareConfig::iterator configIt = config.find(std::string(WIFI_DEVICE) + "::" + "connected");
bool connected = (configIt != config.end() && strcmp(configIt->second.c_str(), "true") == 0);
LOG_DEBUG("WiFi device detected.");
updateWiFiStatus(connected);
}
LOG_DEBUG("Firmware parsed successfully.");
return IPS_OK;
}
/**************************************************************************************
** Connect / disconnect the Arduino to WiFi.
***************************************************************************************/
bool WeatherRadio::connectWiFi(bool connect)
{
bool result;
if (connect)
{
result = executeCommand(CMD_WIFI_ON);
LOGF_INFO("Connecting WiFi %", result ? "succeeded." : "failed!");
}
else
{
result = executeCommand(CMD_WIFI_OFF);
LOGF_INFO("Disonnecting WiFi %", result ? "succeeded." : "failed!");
}
return result;
}
void WeatherRadio::updateWiFiStatus(bool connected)
{
wifiConnectionS[0].s = connected ? ISS_OFF : ISS_ON;
wifiConnectionS[1].s = connected ? ISS_ON : ISS_OFF;
wifiConnectionSP.s = IPS_OK;
IDSetSwitch(&wifiConnectionSP, nullptr);
LOGF_INFO("WiFi %s.", connected ? "connected" : "disconnected");
}
/**************************************************************************************
** Reset the Arduino.
***************************************************************************************/
bool WeatherRadio::resetArduino()
{
bool result = executeCommand(CMD_RESET);
return result;
}
/**************************************************************************************
** Create a selection of sensors for a certain weather property.
***************************************************************************************/
void WeatherRadio::addSensorSelection(ISwitchVectorProperty *sensor, std::vector<sensor_name> sensors, const char* name,
const char* label)
{
ISwitch *switches {new ISwitch[sensors.size()]};
for (size_t i = 0; i < sensors.size(); i++)
{
std::string name = canonicalName(sensors[i]).c_str();
IUFillSwitch(&switches[i], name.c_str(), name.c_str(), ISS_OFF);
}
IUFillSwitchVector(sensor, switches, static_cast<int>(sensors.size()), getDeviceName(), name, label, OPTIONS_TAB, IP_RW,
ISR_1OFMANY, 60, IPS_IDLE);
defineProperty(sensor);
}
/**************************************************************************************
** Define Basic properties to clients.
***************************************************************************************/
void WeatherRadio::ISGetProperties(const char *dev)
{
// Ask the default driver first to send properties.
INDI::Weather::ISGetProperties(dev);
}
/**************************************************************************************
** Process Text properties
***************************************************************************************/
bool WeatherRadio::ISNewText(const char *dev, const char *name, char *texts[], char *names[], int n)
{
if (dev != nullptr && strcmp(dev, getDeviceName()) == 0)
{
// grab host name and port for HTTP connection
if (strcmp(name, INDI::SP::DEVICE_ADDRESS) == 0)
{
for (int i = 0; i < n; i++)
{
if (strcmp(names[i], "ADDRESS") == 0)
strcpy(hostname, texts[i]);
else if (strcmp(names[i], "PORT") == 0)
strcpy(port, texts[i]);
}
// fall through to pass the text vector to the base class
}
}
return INDI::Weather::ISNewText(dev, name, texts, names, n);
}
/**************************************************************************************
** Process Number properties
***************************************************************************************/
bool WeatherRadio::ISNewNumber(const char *dev, const char *name, double values[], char *names[], int n)
{
if (dev != nullptr && strcmp(dev, getDeviceName()) == 0)
{
if (strcmp(name, ttyTimeoutNP.name) == 0)
{
IUUpdateNumber(&ttyTimeoutNP, values, names, n);
ttyTimeout = int(values[0]);
ttyTimeoutNP.s = IPS_OK;
IDSetNumber(&ttyTimeoutNP, nullptr);
return ttyTimeoutNP.s;
}
else if (strcmp(name, skyTemperatureCalibrationNP.name) == 0)
{
IUUpdateNumber(&skyTemperatureCalibrationNP, values, names, n);
if (n > 0) weatherCalculator->skyTemperatureCoefficients.k1 = values[0];
if (n > 1) weatherCalculator->skyTemperatureCoefficients.k2 = values[1];
if (n > 2) weatherCalculator->skyTemperatureCoefficients.k3 = values[2];
if (n > 3) weatherCalculator->skyTemperatureCoefficients.k4 = values[3];
if (n > 4) weatherCalculator->skyTemperatureCoefficients.k5 = values[4];
if (n > 5) weatherCalculator->skyTemperatureCoefficients.t_clear = values[5];
if (n > 6) weatherCalculator->skyTemperatureCoefficients.t_overcast = values[6];
skyTemperatureCalibrationNP.s = IPS_OK;
IDSetNumber(&skyTemperatureCalibrationNP, nullptr);
LOG_DEBUG("Cloud coverage value calibration updated.");
return skyTemperatureCalibrationNP.s;
}
else if (strcmp(name, humidityCalibrationNP.name) == 0)
{
IUUpdateNumber(&humidityCalibrationNP, values, names, n);
weatherCalculator->humidityCalibration.factor = values[0];
weatherCalculator->humidityCalibration.shift = values[1];
humidityCalibrationNP.s = IPS_OK;
IDSetNumber(&humidityCalibrationNP, nullptr);
LOG_DEBUG("Humidity value calibration updated.");
return humidityCalibrationNP.s;
}
else if (strcmp(name, temperatureCalibrationNP.name) == 0)
{
IUUpdateNumber(&temperatureCalibrationNP, values, names, n);
weatherCalculator->temperatureCalibration.factor = values[0];
weatherCalculator->temperatureCalibration.shift = values[1];
temperatureCalibrationNP.s = IPS_OK;
IDSetNumber(&temperatureCalibrationNP, nullptr);
LOG_DEBUG("Temperature value calibration updated.");
return temperatureCalibrationNP.s;
}
else if (strcmp(name, sqmCalibrationNP.name) == 0)
{
IUUpdateNumber(&sqmCalibrationNP, values, names, n);
weatherCalculator->sqmCalibration.factor = values[0];
weatherCalculator->sqmCalibration.shift = values[1];
sqmCalibrationNP.s = IPS_OK;
IDSetNumber(&sqmCalibrationNP, nullptr);
return sqmCalibrationNP.s;
LOG_DEBUG("SQM value calibration updated.");
}
else if (strcmp(name, windDirectionCalibrationNP.name) == 0)
{
IUUpdateNumber(&windDirectionCalibrationNP, values, names, n);
weatherCalculator->windDirectionOffset = values[0];
windDirectionCalibrationNP.s = IPS_OK;
IDSetNumber(&windDirectionCalibrationNP, nullptr);
return windDirectionCalibrationNP.s;
LOG_DEBUG("Wind direction value calibration updated.");
}
else if (strcmp(name, wetnessCalibrationNP.name) == 0)
{
IUUpdateNumber(&wetnessCalibrationNP, values, names, n);
weatherCalculator->wetnessCalibration.factor = values[0];
weatherCalculator->wetnessCalibration.shift = values[1];
wetnessCalibrationNP.s = IPS_OK;
IDSetNumber(&wetnessCalibrationNP, nullptr);
return wetnessCalibrationNP.s;
LOG_DEBUG("Wetness value calibration updated.");
}
else if (strcmp(name, "GEOGRAPHIC_COORD") == 0)
{
// update the weather if location (and especially the elevation) changes
if (INDI::Weather::ISNewNumber(dev, name, values, names, n))
return (updateWeather() == IPS_OK);
else
return false;
}
}
return INDI::Weather::ISNewNumber(dev, name, values, names, n);
}
/**************************************************************************************
** Process Switch properties
***************************************************************************************/
bool WeatherRadio::ISNewSwitch(const char *dev, const char *name, ISState *states, char *names[], int n)
{
if (dev != nullptr && strcmp(dev, getDeviceName()) == 0)
{
if (strcmp(name, refreshConfigSP.name) == 0)
{
// refresh config button pressed
IUUpdateSwitch(&refreshConfigSP, states, names, n);
updateConfigData();
refreshConfigSP.s = IPS_OK;
refreshConfigS[0].s = ISS_OFF;
IDSetSwitch(&refreshConfigSP, nullptr);
LOG_INFO("Firmware configuration data updated.");
return (refreshConfigSP.s == IPS_OK);
}
else if (strcmp(name, wifiConnectionSP.name) == 0)
{
// reconnect config button pressed
IUUpdateSwitch(&wifiConnectionSP, states, names, n);
int pressed = IUFindOnSwitchIndex(&wifiConnectionSP);
wifiConnectionSP.s = connectWiFi(pressed == 1) ? IPS_OK : IPS_ALERT;
IDSetSwitch(&wifiConnectionSP, nullptr);
LOGF_INFO("%s WiFi. Press \"Refresh\" to update the status.", pressed == 1 ? "Connecting" : "Disconnecting");
return (wifiConnectionSP.s == IPS_OK);
}
else if (strcmp(name, resetArduinoSP.name) == 0)
{
// reset Arduino button pressed
IUUpdateSwitch(&resetArduinoSP, states, names, n);
if (resetArduino())
resetArduinoSP.s = IPS_OK;
else
resetArduinoSP.s = IPS_ALERT;
resetArduinoS->s = ISS_OFF;
IDSetSwitch(&resetArduinoSP, nullptr);
LOG_INFO("Resetting Arduino. Press \"Refresh\" to update the status");
return (resetArduinoSP.s == IPS_OK);
}
else if (strcmp(name, temperatureSensorSP.name) == 0)
{
// temperature sensor selected
IUUpdateSwitch(&temperatureSensorSP, states, names, n);
const char *selected = IUFindOnSwitchName(states, names, n);
sensor_name sensor = updateSensorSelection(&temperatureSensorSP, selected);
currentSensors.temperature = sensor;
LOGF_DEBUG("Temperature sensor selected: %s", selected);
return (temperatureSensorSP.s == IPS_OK);
}
else if (strcmp(name, pressureSensorSP.name) == 0)
{
// pressure sensor selected
IUUpdateSwitch(&pressureSensorSP, states, names, n);
const char *selected = IUFindOnSwitchName(states, names, n);
sensor_name sensor = updateSensorSelection(&pressureSensorSP, selected);
currentSensors.pressure = sensor;
LOGF_DEBUG("Pressure sensor selected: %s", selected);
return (pressureSensorSP.s == IPS_OK);
}
else if (strcmp(name, humiditySensorSP.name) == 0)
{
// humidity sensor selected
IUUpdateSwitch(&humiditySensorSP, states, names, n);
const char *selected = IUFindOnSwitchName(states, names, n);
sensor_name sensor = updateSensorSelection(&humiditySensorSP, selected);
currentSensors.humidity = sensor;
LOGF_DEBUG("Humidity sensor selected: %s", selected);
return (humiditySensorSP.s == IPS_OK);
}
else if (strcmp(name, luminositySensorSP.name) == 0)
{
// luminosity sensor selected
IUUpdateSwitch(&luminositySensorSP, states, names, n);
const char *selected = IUFindOnSwitchName(states, names, n);
sensor_name sensor = updateSensorSelection(&luminositySensorSP, selected);
currentSensors.luminosity = sensor;
LOGF_DEBUG("Luminosity sensor selected: %s", selected);
return (luminositySensorSP.s == IPS_OK);
}
else if (strcmp(name, sqmSensorSP.name) == 0)
{
// SQM sensor selected
IUUpdateSwitch(&sqmSensorSP, states, names, n);
const char *selected = IUFindOnSwitchName(states, names, n);
sensor_name sensor = updateSensorSelection(&sqmSensorSP, selected);
currentSensors.sqm = sensor;
LOGF_DEBUG("SQM sensor selected: %s", selected);
return (sqmSensorSP.s == IPS_OK);
}
else if (strcmp(name, ambientTemperatureSensorSP.name) == 0)
{
// ambient temperature sensor selected
IUUpdateSwitch(&ambientTemperatureSensorSP, states, names, n);
const char *selected = IUFindOnSwitchName(states, names, n);
sensor_name sensor = updateSensorSelection(&ambientTemperatureSensorSP, selected);
currentSensors.temp_ambient = sensor;
LOGF_DEBUG("Ambient temperature sensor selected: %s", selected);
return (ambientTemperatureSensorSP.s == IPS_OK);
}
else if (strcmp(name, objectTemperatureSensorSP.name) == 0)
{
// object temperature sensor selected
IUUpdateSwitch(&objectTemperatureSensorSP, states, names, n);
const char *selected = IUFindOnSwitchName(states, names, n);
sensor_name sensor = updateSensorSelection(&objectTemperatureSensorSP, selected);
currentSensors.temp_object = sensor;
LOGF_DEBUG("Object temperature sensor selected: %s", selected);
return (objectTemperatureSensorSP.s == IPS_OK);
}
else if (strcmp(name, windGustSensorSP.name) == 0)
{
// wind gust sensor selected
IUUpdateSwitch(&windGustSensorSP, states, names, n);
const char *selected = IUFindOnSwitchName(states, names, n);
sensor_name sensor = updateSensorSelection(&windGustSensorSP, selected);
currentSensors.wind_gust = sensor;
LOGF_DEBUG("Wind gust sensor selected: %s", selected);
return (windGustSensorSP.s == IPS_OK);
}
else if (strcmp(name, windSpeedSensorSP.name) == 0)
{
// wind speed sensor selected
IUUpdateSwitch(&windSpeedSensorSP, states, names, n);
const char *selected = IUFindOnSwitchName(states, names, n);
sensor_name sensor = updateSensorSelection(&windSpeedSensorSP, selected);
currentSensors.wind_speed = sensor;
LOGF_DEBUG("Wind speed sensor selected: %s", selected);
return (windSpeedSensorSP.s == IPS_OK);
}
else if (strcmp(name, windDirectionSensorSP.name) == 0)
{
// wind direction sensor selected
IUUpdateSwitch(&windDirectionSensorSP, states, names, n);
const char *selected = IUFindOnSwitchName(states, names, n);
sensor_name sensor = updateSensorSelection(&windDirectionSensorSP, selected);
currentSensors.wind_direction = sensor;
LOGF_DEBUG("Wind direction sensor selected: %s", selected);
return (windDirectionSensorSP.s == IPS_OK);
}
else if (strcmp(name, rainDropsSensorSP.name) == 0)
{
// rain intensity sensor selected
IUUpdateSwitch(&rainDropsSensorSP, states, names, n);
const char *selected = IUFindOnSwitchName(states, names, n);
sensor_name sensor = updateSensorSelection(&rainDropsSensorSP, selected);
currentSensors.rain_drops = sensor;
LOGF_DEBUG("Rain intensity sensor selected: %s", selected);
return (rainDropsSensorSP.s == IPS_OK);
}
else if (strcmp(name, rainVolumeSensorSP.name) == 0)
{
// rain volume sensor selected
IUUpdateSwitch(&rainVolumeSensorSP, states, names, n);
const char *selected = IUFindOnSwitchName(states, names, n);
sensor_name sensor = updateSensorSelection(&rainVolumeSensorSP, selected);
currentSensors.rain_volume = sensor;
LOGF_DEBUG("Rain intensity sensor selected: %s", selected);
return (rainVolumeSensorSP.s == IPS_OK);
}
else if (strcmp(name, wetnessSensorSP.name) == 0)
{
// wetness sensor selected
IUUpdateSwitch(&wetnessSensorSP, states, names, n);
const char *selected = IUFindOnSwitchName(states, names, n);
sensor_name sensor = updateSensorSelection(&wetnessSensorSP, selected);
currentSensors.wetness = sensor;
LOGF_DEBUG("Wetness sensor selected: %s", selected);
return (wetnessSensorSP.s == IPS_OK);
}
}
return INDI::Weather::ISNewSwitch(dev, name, states, names, n);
}
/**************************************************************************************
** Manage BLOBs.
***************************************************************************************/
bool WeatherRadio::ISNewBLOB(const char *dev, const char *name, int sizes[], int blobsizes[], char *blobs[],
char *formats[], char *names[], int n)
{
return INDI::Weather::ISNewBLOB(dev, name, sizes, blobsizes, blobs, formats, names, n);
}
/**************************************************************************************
** Initialization when the driver gets connected.
***************************************************************************************/
bool WeatherRadio::Handshake()
{
// Load the configuration
loadConfig();
// Sleep for 5 seconds so that the serial connection of the Arduino has settled
// This seems to be necessary for some Arduinos, otherwise they run into a timeout
struct timespec request_delay = {ARDUINO_SETTLING_TIME, 0L};
DEBUGFDEVICE(getDeviceName(), INDI::Logger::DBG_SESSION, "Waiting for %d seconds the communication to Arduino to settle.",
ARDUINO_SETTLING_TIME);
nanosleep(&request_delay, nullptr);
// retrieve basic data to initialize the weather station
IPState result = getBasicData();
return result == IPS_OK;
}
/**************************************************************************************
** Read all weather sensor values.
***************************************************************************************/
IPState WeatherRadio::updateWeather()
{
bool result = executeCommand(CMD_WEATHER);
// result recieved
LOGF_DEBUG("Reading weather data from Arduino %s", result ? "succeeded." : "failed!");
return result == true ? IPS_OK : IPS_ALERT;
}