ReadPwsObs  BRIGADOON-005
Get Observations from a Personal Weather Station
index.php
Go to the documentation of this file.
1 <?php
2 
3  ###########################################################################################
4  # Set the username and password to use. The read_user should only be allowed to read data #
5  # from the database, while the write_user will probably need to read and write. #
6  ###########################################################################################
7  include '/usr/local/share/brigadoon/apache/db_passwords.php';
8 
9 
22  function DbConnect( $DbFunction, $ConnectionType = 'mysqli' )
23  {
24  global $db_password; global $db_username; global $log_file;
25 
26  /* Set the host as the localhost. The username and password for the database should be set only for localhost in the database
27  to lessen the chance of someone outside this system attempting to use them to access the database.
28  */
29  $host = 'localhost';
30 
31  /* Set the name of the local database to connect to. */
32  $db = 'WeatherStation';
33 
34  /* Attempt to open the connection based on the selected library.
35  */
36  if ( $ConnectionType == 'mysqli' )
37  {
38  // file_put_contents( $log_file, "mysqli Connection Attempt\n", FILE_APPEND );
39  return new mysqli( $host, $db_username, $db_password, $db ) or die ( 'Cannot Open Database' );
40  }
41  elseif ( $ConnectionType == 'pdo' )
42  {
43  // file_put_contents( $log_file, "pdo Connection Attempt\n", FILE_APPEND );
44  try
45  {
46  return new PDO( "mysql::host=$host;dbname=$db;", $db_username, $db_password );
47  }
48  catch (Exception $e)
49  {
50  header("HTTP/1.0 400 Unable to Open Database");
51  exit;
52  }
53  }
54  else
55  {
56  // file_put_contents( $log_file, "Unrecognised Driver", FILE_APPEND );
57  header("HTTP/1.0 400 Unrecognised Driver Type");
58  exit;
59  }
60  }
61 
69  function SaveObservation()
70  {
71  global $db_password; global $db_username; global $log_file;
72  ##################################################################################################################
73  # Used to Determine the Parameters being Passed to This Program #
74  ##################################################################################################################
75  $parameters = json_encode($_POST);
76 
77  ###############################
78  # Used for Debugging Purposes #
79  ###############################
80  // file_put_contents( $log_file, $parameters );
81 
82  ##################################################################################################################
83  # Read the Weather Station's Username and Password, the action to be carried out and the Date of the Observation #
84  # as these are mandatory parameters required to save the readings. This not determine if this is a valid message #
85  # but it does determine if it at least has the minimum parameters required. #
86  ##################################################################################################################
87  $readings = json_decode( $parameters, true );
88 
89  if ( $readings['PASSKEY'] )
90  {
91  $passkey = $readings['PASSKEY'];
92  }
93 
94  if ( $readings['dateutc'] )
95  {
96  $date_utc = $readings['dateutc'];
97  }
98 
99  ###################################################################
100  # If the Two (2) Mandatory Fields Are There, Process the Reading #
101  ###################################################################
102  if ( $passkey && $date_utc )
103  {
104 
105  ##########################################################
106  # Create a Database Connection #
107  ##########################################################
108  // file_put_contents( $log_file, "Attempt Open", FILE_APPEND );
109 
110  $connection = DbConnect( 'WRITE', 'pdo' );
111 
112  // file_put_contents( $log_file, "Opened", FILE_APPEND );
113  ##########################################################
114  # Extract Readings from the Parameters passed in POST #
115  ##########################################################
116  if ( $readings['stationtype'] != '' )
117  {
118  $software_type = $readings['stationtype'];
119  }
120 
121  if ( $readings['model'] != '' )
122  {
123  $model = $readings['model'];
124  }
125 
126  if ( $readings['freq'] != '' )
127  {
128  $freq = $readings['freq'];
129  }
130 
131  ##########################################################
132  # Extract the Indoor Temperature #
133  ##########################################################
134  if ( $readings['tempinf'] )
135  {
136  $indoor_temp = ($readings['tempinf'] - 32.0) * 0.555555556;
137  }
138 
139  ##########################################################
140  # Extract the Outdoor Temperature #
141  ##########################################################
142  if ($readings['tempf'] )
143  {
144  $outdoor_temp = ($readings['tempf'] - 32.0) * 0.555555556;
145  }
146 
147  ##########################################################
148  # Extract the Dew Point Temperature #
149  ##########################################################
150  if ($readings['dewptf'] )
151  {
152  $dew_point = ($readings['dewptf'] - 32.0) * 0.555555556;
153  }
154 
155  ##########################################################
156  # Extract the Wind Chill Temperature #
157  ##########################################################
158  if ( $readings['windchillf'] )
159  {
160  $windchill = ($readings['windchillf'] - 32.0) * 0.555555556;
161  }
162 
163  ##########################################################
164  # Extract the Indoor Relative Humidity #
165  ##########################################################
166  if ( $readings['humidityin'] )
167  {
168  $indoor_humidity = $readings['humidityin'];
169  }
170 
171  ##########################################################
172  # Extract the Outdoor Relative Humidity #
173  ##########################################################
174  if ( $readings['humidity'] )
175  {
176  $outdoor_humidity = $readings['humidity'];
177  }
178 
179  ##########################################################
180  # Extract the Wind Speed #
181  ##########################################################
182  if ( $readings['windspeedmph'] )
183  {
184  $wind_speed = $readings['windspeedmph'] * 1.609344;
185  }
186 
187  ##########################################################
188  # Extract the Wind Speed #
189  ##########################################################
190  if ( $readings['windspdmph_avg10m'] )
191  {
192  $wind_speed_avg10m = $readings['windspdmph_avg10m'] * 1.609344;
193  }
194 
195  ##########################################################
196  # Extract the Wind Gust Speed #
197  ##########################################################
198  if ( $readings['windgustmph'] )
199  {
200  $wind_gust = $readings['windgustmph'] * 1.609344;
201  }
202 
203  ##########################################################
204  # Extract the Max Daily Wind Gust Speed #
205  ##########################################################
206  if ( $readings['maxdailygust'] )
207  {
208  $wind_gust_max_daily = $readings['maxdailygust'] * 1.609344;
209  }
210 
211  ##########################################################
212  # Extract the Wind Direction #
213  ##########################################################
214  if ( $readings['winddir'] )
215  {
216  $wind_direction = $readings['winddir'];
217  }
218 
219  ##########################################################
220  # Extract the Wind Direction #
221  ##########################################################
222  if ( $readings['windspdmph_avg10m'] )
223  {
224  $wind_direction_avg10m = $readings['windspdmph_avg10m'];
225  }
226 
227  ##########################################################
228  # Extract the Local Barometric Pressure #
229  ##########################################################
230  if ( $readings['baromabsin'] )
231  {
232  $absolute_barometer = $readings['baromabsin'] * 33.8639;
233  }
234 
235  ##########################################################
236  # Extract the MSL Barometric Pressure #
237  ##########################################################
238  if ( $readings['baromrelin'] )
239  {
240  $relative_barometer = $readings['baromrelin'] * 33.8639;
241  }
242 
243  ##########################################################
244  # Extract the Rainfall Rate #
245  ##########################################################
246 
247  if ( $readings['rainratein'] )
248  {
249  $rain_rate = $readings['rainratein'] * 25.4;
250  }
251 
252  ##########################################################
253  # Extract the Rainfall Event #
254  ##########################################################
255  if( $readings['eventrainin'] )
256  {
257  $rain_event = $readings['eventrainin'] * 25.4;
258  }
259 
260  ##########################################################
261  # Extract the Hourly Rainfall #
262  ##########################################################
263  if ( $readings['hourlyrainin'] )
264  {
265  $rain_hourly = $readings['hourlyrainin'] * 25.4;
266  }
267 
268  ##########################################################
269  # Extract the Daily Rainfall #
270  ##########################################################
271  if ( $readings['dailyrainin'] )
272  {
273  $rain_daily = $readings['dailyrainin'] * 25.4;
274  }
275 
276  ##########################################################
277  # Extract the Weekly Rainfall #
278  ##########################################################
279  if ( $readings['weeklyrainin'] )
280  {
281  $rain_weekly = $readings['weeklyrainin'] * 25.4;
282  }
283 
284  ##########################################################
285  # Extract the Monthly Rainfall #
286  ##########################################################
287  if ( $readings['monthlyrainin'] )
288  {
289  $rain_monthly = $readings['monthlyrainin'] * 25.4;
290  }
291 
292  ##########################################################
293  # Extract the Yearly Rainfall #
294  ##########################################################
295  if ( $readings['yearlyrainin'] )
296  {
297  $rain_yearly = $readings['yearlyrainin'] * 25.4;
298  }
299 
300  ##########################################################
301  # Extract the Solar Radiation #
302  ##########################################################
303  if ( $readings['solarradiation'] )
304  {
305  $power = $readings['solarradiation'];
306  }
307 
308  ##########################################################
309  # Extract the UV Index #
310  ##########################################################
311  if ( $readings['uv'] != '' )
312  {
313  $uv_index = $readings['uv'];
314  }
315 
316  ##########################################################
317  # Extract the Extra Temp/Humidity Sensors #
318  ##########################################################
319 
320  if ( $readings['temp1f'] )
321  {
322  $temperature1 = ($readings['temp1f'] - 32.0) * 0.555555556;
323  }
324 
325  if ( $readings['temp2f'] )
326  {
327  $temperature2 = ($readings['temp2f'] - 32.0) * 0.555555556;
328  }
329 
330  if ( $readings['temp3f'] )
331  {
332  $temperature3 = ($readings['temp3f'] - 32.0) * 0.555555556;
333  }
334 
335  if ( $readings['temp4f'] )
336  {
337  $temperature4 = ($readings['temp4f'] - 32.0) * 0.555555556;
338  }
339 
340  if ( $readings['temp5f'] )
341  {
342  $temperature5 = ($readings['temp5f'] - 32.0) * 0.555555556;
343  }
344 
345  if ( $readings['temp6f'] )
346  {
347  $temperature6 = ($readings['temp6f'] - 32.0) * 0.555555556;
348  }
349 
350  if ( $readings['temp7f'] )
351  {
352  $temperature7 = ($readings['temp7f'] - 32.0) * 0.555555556;
353  }
354 
355  if ( $readings['temp8f'] )
356  {
357  $temperature8 = ($readings['temp8f'] - 32.0) * 0.555555556;
358  }
359 
360  if ( $readings['humidity1'] )
361  {
362  $humidity1 = $readings['humidity1'];
363  }
364 
365  if ( $readings['humidity2'] )
366  {
367  $humidity2 = $readings['humidity2'];
368  }
369 
370  if ( $readings['humidity3'] )
371  {
372  $humidity3 = $readings['humidity3'];
373  }
374 
375  if ( $readings['humidity4'] )
376  {
377  $humidity4 = $readings['humidity4'];
378  }
379 
380  if ( $readings['humidity5'] )
381  {
382  $humidity5 = $readings['humidity5'];
383  }
384 
385  if ( $readings['humidity6'] )
386  {
387  $humidity6 = $readings['humidity6'];
388  }
389 
390  if ( $readings['humidity7'] )
391  {
392  $humidity7 = $readings['humidity7'];
393  }
394 
395  if ( $readings['humidity8'] )
396  {
397  $humidity1 = $readings['humidity8'];
398  }
399 
400  ##########################################################
401  # Extract the Soil Moisture Readings #
402  ##########################################################
403 
404  if ( $readings['soilmoisture1'] )
405  {
406  $moisture_soil_1 = $readings['soilmoisture1'];
407  }
408 
409  if ( $readings['soilmoisture2'] )
410  {
411  $moisture_soil_2 = $readings['soilmoisture2'];
412  }
413 
414  if ( $readings['soilmoisture3'] )
415  {
416  $moisture_soil_3 = $readings['soilmoisture3'];
417  }
418 
419  if ( $readings['soilmoisture4'] )
420  {
421  $moisture_soil_4 = $readings['soilmoisture4'];
422  }
423 
424  if ( $readings['soilmoisture5'] )
425  {
426  $moisture_soil_5 = $readings['soilmoisture5'];
427  }
428 
429  if ( $readings['soilmoisture6'] )
430  {
431  $moisture_soil_6 = $readings['soilmoisture6'];
432  }
433 
434  if ( $readings['soilmoisture7'] )
435  {
436  $moisture_soil_7 = $readings['soilmoisture7'];
437  }
438 
439  if ( $readings['soilmoisture8'] )
440  {
441  $moisture_soil_8 = $readings['soilmoisture8'];
442  }
443 
444  ##########################################################
445  # Extract the Battery States #
446  ##########################################################
447 
448  if ( $readings['wh65batt'] )
449  {
450  $battery_wh65 = $readings['wh65batt'];
451  }
452 
453  if ( $readings['wh25batt'] )
454  {
455  $battery_wh25 = $readings['wh25batt'];
456  }
457 
458  if ( $readings['batt1'] )
459  {
460  $battery_1 = $readings['batt1'];
461  }
462 
463  if ( $readings['batt2'] )
464  {
465  $battery_2 = $readings['batt2'];
466  }
467 
468  if ( $readings['batt3'] )
469  {
470  $battery_3 = $readings['batt3'];
471  }
472 
473  if ( $readings['batt4'] )
474  {
475  $battery_4 = $readings['batt4'];
476  }
477 
478  if ( $readings['batt5'] )
479  {
480  $battery_5 = $readings['batt5'];
481  }
482 
483  if ( $readings['batt6'] )
484  {
485  $battery_6 = $readings['batt6'];
486  }
487 
488  if ( $readings['batt7'] )
489  {
490  $battery_7 = $readings['batt7'];
491  }
492 
493  if ( $readings['batt8'] )
494  {
495  $battery_8 = $readings['batt8'];
496  }
497 
498  ############################################################
499  # Old versions of the firmware may use "Siolbatt", while #
500  # later versions will use "soilbatt", so include both #
501  # just in case. There are so many models and versions that #
502  # it is simpler to try anything based on model numbers. #
503  ############################################################
504 
505  if ( $readings['Siolbatt1'] != '' )
506  {
507  $battery_moisture_soil_1 = $readings['Siolbatt1'];
508  }
509 
510  if ( $readings['Siolbatt2'] != '' )
511  {
512  $battery_moisture_soil_2 = $readings['Siolbatt2'];
513  }
514 
515  if ( $readings['Siolbatt3'] != '' )
516  {
517  $battery_moisture_soil_3 = $readings['Siolbatt3'];
518  }
519 
520  if ( $readings['Siolbatt4'] != '' )
521  {
522  $battery_moisture_soil_4 = $readings['Siolbatt4'];
523  }
524 
525  if ( $readings['Siolbatt5'] != '' )
526  {
527  $battery_moisture_soil_5 = $readings['Siolbatt5'];
528  }
529 
530  if ( $readings['Siolbatt6'] != '' )
531  {
532  $battery_moisture_soil_6 = $readings['Siolbatt6'];
533  }
534 
535  if ( $readings['Siolbatt7'] != '' )
536  {
537  $battery_moisture_soil_7 = $readings['Siolbatt7'];
538  }
539 
540  if ( $readings['Siolbatt8'] != '' )
541  {
542  $battery_moisture_soil_8 = $readings['Siolbatt8'];
543  }
544 
545  if ( $readings['soilbatt1'] != '' )
546  {
547  $battery_moisture_soil_1 = $readings['soilbatt1'];
548  }
549 
550  if ( $readings['soilbatt2'] != '' )
551  {
552  $battery_moisture_soil_2 = $readings['soilbatt2'];
553  }
554 
555  if ( $readings['soilbatt3'] != '' )
556  {
557  $battery_moisture_soil_3 = $readings['soilbatt3'];
558  }
559 
560  if ( $readings['soilbatt4'] != '' )
561  {
562  $battery_moisture_soil_4 = $readings['soilbatt4'];
563  }
564 
565  if ( $readings['soilbatt5'] != '' )
566  {
567  $battery_moisture_soil_5 = $readings['soilbatt5'];
568  }
569 
570  if ( $readings['soilbatt6'] != '' )
571  {
572  $battery_moisture_soil_6 = $readings['soilbatt6'];
573  }
574 
575  if ( $readings['soilbatt7'] != '' )
576  {
577  $battery_moisture_soil_7 = $readings['soilbatt7'];
578  }
579 
580  if ( $readings['soilbatt8'] != '' )
581  {
582  $battery_moisture_soil_8 = $readings['soilbatt8'];
583  }
584 
585  if ( $readings['pm25batt1'] != '' )
586  {
587  $option_pm25_battery_1 = $readings['pm25batt1'];
588  }
589 
590  if ( $readings['pm25batt2'] != '' )
591  {
592  $option_pm25_battery_2 = $readings['pm25batt2'];
593  }
594 
595  if ( $readings['pm25batt3'] != '' )
596  {
597  $option_pm25_battery_3 = $readings['pm25batt3'];
598  }
599 
600  if ( $readings['pm25batt4'] != '' )
601  {
602  $option_pm25_battery_4 = $readings['pm25batt4'];
603  }
604 
605  #########################################################################
606  # Process any PM2.5 Sensors that are connected to the weather station #
607  # (by radio). #
608  #########################################################################
609 
610  if ( $readings['pm25_ch1'] != '' )
611  {
612  $option_pm25_ch1 = $readings['pm25_ch1'];
613  }
614 
615  if ( $readings['pm25_avg_24h_ch1'] != '' )
616  {
617  $option_pm25_avg_24h_ch1 = $readings['pm25_avg_24h_ch1'];
618  }
619 
620  if ( $readings['pm25_ch2'] != '' )
621  {
622  $option_pm25_ch2 = $readings['pm25_ch2'];
623  }
624 
625  if ( $readings['pm25_avg_24h_ch2'] != '' )
626  {
627  $option_pm25_avg_24h_ch2 = $readings['pm25_avg_24h_ch2'];
628  }
629 
630  if ( $readings['pm25_ch3'] != '' )
631  {
632  $option_pm25_ch3 = $readings['pm25_ch3'];
633  }
634 
635  if ( $readings['pm25_avg_24h_ch3'] != '' )
636  {
637  $option_pm25_avg_24h_ch3 = $readings['pm25_avg_24h_ch3'];
638  }
639 
640  if ( $readings['pm25_ch4'] != '' )
641  {
642  $option_pm25_ch4 = $readings['pm25_ch4'];
643  }
644 
645  if ( $readings['pm25_avg_24h_ch4'] != '' )
646  {
647  $option_pm25_avg_24h_ch4 = $readings['pm25_avg_24h_ch4'];
648  }
649 
650  #########################################################################
651  # If Dew Point is not provided, estimate the Dew Point using the method #
652  # by Arden Buck in Journal of Applied Meteorology and Climatology. This #
653  # method is accurate for the temperature range -40C to +50C. #
654  #########################################################################
655  if ( !isset( $dewpoint ) )
656  {
657  if ( $outdoor_temp >= 0.0 )
658  {
659  $a = 6.1121; $b = 17.368; $c = 238.88; $d = 234.5;
660  }
661  else
662  {
663  $a = 6.1121; $b = 17.966; $c = 247.15; $d = 234.5;
664  }
665  $ym = log( ($outdoor_humidity/100.0) * exp( ($b-$outdoor_temp/$d)*($outdoor_temp/($c+$outdoor_temp) ) ) );
666  $dew_point = $c*$ym/($b-$ym);
667  }
668 
669  #########################################################################
670  # If Windchill Temperature is not provided, estimate the Windchill Temp #
671  # using the Australian Bureau Of Meteorology Apparent Temperature (AT) #
672  # method calculated for an adult walking outdoors in the shade. #
673  #########################################################################
674  if ( !isset( $windchill ) )
675  {
676  $v = $wind_speed * 0.2777778; $e = ($outdoor_humidity/100.0)*6.105*exp( (17.27*$outdoor_temp)/(237.7+$outdoor_temp) );
677  $windchill = $outdoor_temp + 0.33*$e - 0.7*$v - 4.0;
678  }
679 
680  ########################################################################
681  # Find the Station Index From the Ecowitt Passkey #
682  # The station index is the key identifying the station in all records. #
683  # Also determine if non-essential fields like the battery states and #
684  # the identification strings are required. If they are not required, #
685  # those fields are not sent, reducing the database storage required. #
686  # If the fields are not defined in the database are sent, it will #
687  # result in the entire observation being discarded. If the fields #
688  # exist in the database, but are not sent, they will be recorded as a #
689  # NULL in the database, but the other readings will be recorded as #
690  # normal. #
691  ########################################################################
692  // file_put_contents( $log_file, "Start Query", FILE_APPEND );
693 
694  $station_query = "select station_index, trim_readings from station where ecowitt_passkey= '$passkey';";
695  $result = $connection->query($station_query);
696  $row_count = $result->rowCount();
697  if ( $row_count != 1)
698  {
699  // file_put_contents( $log_file, "No matching passkey", FILE_APPEND );
700  exit;
701  }
702  foreach ($connection->query($station_query) as $row )
703  {
704  $station_index = $row['station_index'];
705  $trim_readings = $row['trim_readings'];
706  }
707 
708  ###############################################################
709  # Create the SQL INSERT Statement based on readings provided. #
710  ###############################################################
711 
712  $field_string = "station_index, date_utc";
713  $value_string = $station_index . ',\'' . $date_utc . '\'';
714 
715  $field_string_battery = "station_index, date_utc";
716  $value_string_battery = $station_index . ',\'' . $date_utc . '\'';
717 
718  $field_string_version = "station_index, date_utc";
719  $value_string_version = $station_index . ',\'' . $date_utc . '\'';
720 
721 
722  if ( isset($indoor_temp) )
723  {
724  $field_string .= ", temperature_internal";
725  $value_string .= ", " . $indoor_temp;
726  }
727 
728  if ( isset($outdoor_temp) )
729  {
730  $field_string .= ", temperature_external";
731  $value_string .= ", " . $outdoor_temp;
732  }
733 
734  if ( isset($dew_point) )
735  {
736  $field_string .= ", dew_point";
737  $value_string .= ", " . $dew_point;
738  }
739 
740  if ( isset($windchill) )
741  {
742  $field_string .= ", wind_chill_temp";
743  $value_string .= ", " . $windchill;
744  }
745 
746  if ( isset($outdoor_humidity) )
747  {
748  $field_string .= ", humidity_external";
749  $value_string .= ", " . $outdoor_humidity;
750  }
751 
752  if ( isset($indoor_humidity) )
753  {
754  $field_string .= ", humidity_internal";
755  $value_string .= ", " . $indoor_humidity;
756  }
757 
758  if ( isset($wind_speed) )
759  {
760  $field_string .= ", wind_speed";
761  $value_string .= ", " . $wind_speed;
762  }
763 
764  if ( isset($wind_speed_avg10m) )
765  {
766  $field_string .= ", wind_speed_avg10m";
767  $value_string .= ", " . $wind_speed_avg10m;
768  }
769 
770  if ( isset($wind_gust) )
771  {
772  $field_string .= ", wind_gust";
773  $value_string .= ", " . $wind_gust;
774  }
775 
776  if ( isset($wind_gust_max_daily) )
777  {
778  $field_string .= ", wind_gust_max_daily";
779  $value_string .= ", " . $wind_gust_max_daily;
780  }
781 
782  if ( isset($wind_direction) )
783  {
784  $field_string .= ", wind_direction";
785  $value_string .= ", " . $wind_direction;
786  }
787 
788  if ( isset($wind_direction_avg10m) )
789  {
790  $field_string .= ", wind_direction_avg10m";
791  $value_string .= ", " . $wind_direction_avg10m;
792  }
793 
794  if ( isset($absolute_barometer) )
795  {
796  $field_string .= ", barometer_absolute";
797  $value_string .= ", " . $absolute_barometer;
798  }
799 
800  if ( isset($relative_barometer) )
801  {
802  $field_string .= ", barometer_relative";
803  $value_string .= ", " . $relative_barometer;
804  }
805 
806  if ( isset($rain_rate) )
807  {
808  $field_string .= ", rainfall_rate";
809  $value_string .= ", " . $rain_rate;
810  }
811 
812  if ( isset($rain_event) )
813  {
814  $field_string .= ", rainfall_event";
815  $value_string .= ", " . $rain_event;
816  }
817 
818  if ( isset($rain_hourly) )
819  {
820  $field_string .= ", rainfall_hour";
821  $value_string .= ", " . $rain_hourly;
822  }
823 
824  if ( isset($rain_daily) )
825  {
826  $field_string .= ", rainfall_day";
827  $value_string .= ", " . $rain_daily;
828  }
829 
830  if ( isset($rain_weekly) )
831  {
832  $field_string .= ", rainfall_week";
833  $value_string .= ", " . $rain_weekly;
834  }
835 
836  if ( isset($rain_monthly) )
837  {
838  $field_string .= ", rainfall_month";
839  $value_string .= ", " . $rain_monthly;
840  }
841 
842  if ( isset($rain_yearly) )
843  {
844  $field_string .= ", rainfall_year";
845  $value_string .= ", " . $rain_yearly;
846  }
847 
848  if ( isset($power) )
849  {
850  $field_string .= ", power";
851  $value_string .= ", " . $power;
852  }
853 
854  if ( isset($uv_index) )
855  {
856  $field_string .= ", uv_index";
857  $value_string .= ", " . $uv_index;
858  }
859 
860  if ( isset($temperature1) )
861  {
862  $field_string .= ", temperature_1";
863  $value_string .= ", " . $temperature1;
864  }
865 
866  if ( isset($temperature2) )
867  {
868  $field_string .= ", temperature_2";
869  $value_string .= ", " . $temperature2;
870  }
871 
872  if ( isset($temperature3) )
873  {
874  $field_string .= ", temperature_3";
875  $value_string .= ", " . $temperature3;
876  }
877 
878  if ( isset($temperature4) )
879  {
880  $field_string .= ", temperature_4";
881  $value_string .= ", " . $temperature4;
882  }
883 
884  if ( isset($temperature5) )
885  {
886  $field_string .= ", temperature_5";
887  $value_string .= ", " . $temperature5;
888  }
889 
890  if ( isset($temperature6) )
891  {
892  $field_string .= ", temperature_6";
893  $value_string .= ", " . $temperature6;
894  }
895 
896  if ( isset($temperature7) )
897  {
898  $field_string .= ", temperature_7";
899  $value_string .= ", " . $temperature7;
900  }
901 
902  if ( isset($temperature8) )
903  {
904  $field_string .= ", temperature_8";
905  $value_string .= ", " . $temperature8;
906  }
907 
908  if ( isset($humidity1) )
909  {
910  $field_string .= ", humidity_1";
911  $value_string .= ", " . $humidity1;
912  }
913 
914  if ( isset($humidity2) )
915  {
916  $field_string .= ", humidity_2";
917  $value_string .= ", " . $humidity2;
918  }
919 
920  if ( isset($humidity3) )
921  {
922  $field_string .= ", humidity_3";
923  $value_string .= ", " . $humidity3;
924  }
925 
926  if ( isset($humidity4) )
927  {
928  $field_string .= ", humidity_4";
929  $value_string .= ", " . $humidity4;
930  }
931 
932  if ( isset($humidity5) )
933  {
934  $field_string .= ", humidity_5";
935  $value_string .= ", " . $humidity5;
936  }
937 
938  if ( isset($humidity6) )
939  {
940  $field_string .= ", humidity_6";
941  $value_string .= ", " . $humidity6;
942  }
943 
944  if ( isset($humidity7) )
945  {
946  $field_string .= ", humidity_7";
947  $value_string .= ", " . $humidity7;
948  }
949 
950  if ( isset($humidity8) )
951  {
952  $field_string .= ", humidity_8";
953  $value_string .= ", " . $humidity8;
954  }
955 
956  if ( isset( $moisture_soil_1 ) )
957  {
958  $field_string .= ", moisture_soil_1";
959  $value_string .= ", " . $moisture_soil_1;
960  }
961 
962  if ( isset( $moisture_soil_2 ) )
963  {
964  $field_string .= ", moisture_soil_2";
965  $value_string .= ", " . $moisture_soil_2;
966  }
967 
968  if ( isset( $moisture_soil_3 ) )
969  {
970  $field_string .= ", moisture_soil_3";
971  $value_string .= ", " . $soil_moisture3;
972  }
973 
974  if ( isset( $moisture_soil_4 ) )
975  {
976  $field_string .= ", moisture_soil_4";
977  $value_string .= ", " . $soil_moisture4;
978  }
979 
980  if ( isset( $moisture_soil_5 ) )
981  {
982  $field_string .= ", moisture_soil_5";
983  $value_string .= ", " . $soil_moisture5;
984  }
985 
986  if ( isset( $moisture_soil_6 ) )
987  {
988  $field_string .= ", moisture_soil_6";
989  $value_string .= ", " . $soil_moisture6;
990  }
991 
992  if ( isset( $moisture_soil_7 ) )
993  {
994  $field_string .= ", moisture_soil_7";
995  $value_string .= ", " . $soil_moisture7;
996  }
997 
998  if ( isset( $moisture_soil_8 ) )
999  {
1000  $field_string .= ", moisture_soil_8";
1001  $value_string .= ", " . $soil_moisture8;
1002  }
1003 
1004  if ( $option_pm25_ch1 != '' )
1005  {
1006  $field_string .= ", air_quality_P25_1";
1007  $value_string .= ", " . $option_pm25_ch1;
1008  }
1009 
1010  if ( $option_pm25_avg_24h_ch1 != '' )
1011  {
1012  $field_string .= ", air_quality_AQI_1";
1013  $value_string .= ", " . $option_pm25_avg_24h_ch1;
1014  }
1015 
1016  if ( $option_pm25_ch2 != '' )
1017  {
1018  $field_string .= ", air_quality_P25_2";
1019  $value_string .= ", " . $option_pm25_ch2;
1020  }
1021 
1022  if ( $option_pm25_avg_24h_ch2 != '' )
1023  {
1024  $field_string .= ", air_quality_AQI_2";
1025  $value_string .= ", " . $option_pm25_avg_24h_ch2;
1026  }
1027 
1028  if ( $option_pm25_ch3 != '' )
1029  {
1030  $field_string .= ", air_quality_P25_3";
1031  $value_string .= ", " . $option_pm25_ch3;
1032  }
1033 
1034  if ( $option_pm25_avg_24h_ch3 != '' )
1035  {
1036  $field_string .= ", air_quality_AQI_3";
1037  $value_string .= ", " . $option_pm25_avg_24h_ch3;
1038  }
1039 
1040  if ( $option_pm25_ch4 != '' )
1041  {
1042  $field_string .= ", air_quality_P25_4";
1043  $value_string .= ", " . $option_pm25_ch4;
1044  }
1045 
1046  if ( $option_pm25_avg_24h_ch4 != '' )
1047  {
1048  $field_string .= ", air_quality_AQI_4";
1049  $value_string .= ", " . $option_pm25_avg_24h_ch4;
1050  }
1051 
1052  // file_put_contents( $log_file, "Trim_Readings = $trim_readings", FILE_APPEND );
1053  if ( $trim_readings != 1 )
1054  {
1055 
1056  if ( $battery_1 != '' )
1057  {
1058  $field_string_battery .= ", battery_temp_humidity_1";
1059  $value_string_battery .= ", " . $battery_1;
1060  }
1061 
1062  if ( isset($battery_2) )
1063  {
1064  $field_string_battery .= ", battery_temp_humidity_2";
1065  $value_string_battery .= ", " . $battery_2;
1066  }
1067 
1068  if ( isset($battery_3) )
1069  {
1070  $field_string_battery .= ", battery_temp_humidity_3";
1071  $value_string_battery .= ", " . $battery_3;
1072  }
1073 
1074  if ( isset($battery_4) )
1075  {
1076  $field_string_battery .= ", battery_temp_humidity_4";
1077  $value_string_battery .= ", " . $battery_4;
1078  }
1079 
1080  if ( isset($battery_5) )
1081  {
1082  $field_string_battery .= ", battery_temp_humidity_5";
1083  $value_string_battery .= ", " . $battery_5;
1084  }
1085 
1086  if ( isset($battery_6) )
1087  {
1088  $field_string_battery .= ", battery_temp_humidity_6";
1089  $value_string_battery .= ", " . $battery_6;
1090  }
1091 
1092  if ( isset($battery_7) )
1093  {
1094  $field_string_battery .= ", battery_temp_humidity_7";
1095  $value_string_battery .= ", " . $battery_7;
1096  }
1097 
1098  if ( isset($battery_8) )
1099  {
1100  $field_string_battery .= ", battery_temp_humidity_8";
1101  $value_string_battery .= ", " . $battery_8;
1102  }
1103 
1104  if ( isset($battery_moisture_soil_1 ) )
1105  {
1106  $field_string_battery .= ", battery_moisture_soil_1";
1107  $value_string_battery .= ", " . $battery_moisture_soil_1;
1108  }
1109 
1110  if ( isset($battery_moisture_soil_2 ) )
1111  {
1112  $field_string_battery .= ", battery_moisture_soil_2";
1113  $value_string_battery .= ", " . $battery_moisture_soil_2;
1114  }
1115 
1116  if ( isset($battery_moisture_soil_3 ) )
1117  {
1118  $field_string_battery .= ", battery_moisture_soil_3";
1119  $value_string_battery .= ", " . $battery_moisture_soil_3;
1120  }
1121 
1122  if ( isset($battery_moisture_soil_4 ) )
1123  {
1124  $field_string_battery .= ", battery_moisture_soil_4";
1125  $value_string_battery .= ", " . $battery_moisture_soil_4;
1126  }
1127 
1128  if ( isset($battery_moisture_soil_5 ) )
1129  {
1130  $field_string_battery .= ", battery_moisture_soil_5";
1131  $value_string_battery .= ", " . $battery_moisture_soil_5;
1132  }
1133 
1134  if ( isset($battery_moisture_soil_6 ) )
1135  {
1136  $field_string_battery .= ", battery_moisture_soil_6";
1137  $value_string_battery .= ", " . $battery_moisture_soil_6;
1138  }
1139 
1140  if ( isset($battery_moisture_soil_7 ) )
1141  {
1142  $field_string_battery .= ", battery_moisture_soil_6";
1143  $value_string_battery .= ", " . $battery_moisture_soil_7;
1144  }
1145 
1146  if ( isset($battery_moisture_soil_8 ) )
1147  {
1148  $field_string_battery .= ", battery_moisture_soil_8";
1149  $value_string_battery .= ", " . $battery_moisture_soil_8;
1150  }
1151 
1152  if ( isset($option_pm25_battery_1 ) )
1153  {
1154  $field_string_battery .= ", option_pm25_battery_1";
1155  $value_string_battery .= ", " . $option_pm25_battery_1;
1156  }
1157 
1158  if ( isset($option_pm25_battery_2 ) )
1159  {
1160  $field_string_battery .= ", option_pm25_battery_2";
1161  $value_string_battery .= ", " . $option_pm25_battery_2;
1162  }
1163 
1164  if ( isset($option_pm25_battery_3 ) )
1165  {
1166  $field_string_battery .= ", option_pm25_battery_3";
1167  $value_string_battery .= ", " . $option_pm25_battery_3;
1168  }
1169 
1170  if ( isset($option_pm25_battery_4 ) )
1171  {
1172  $field_string_battery .= ", option_pm25_battery_4";
1173  $value_string_battery .= ", " . $option_pm25_battery_4;
1174  }
1175 
1176  if ( isset($battery_wh65) )
1177  {
1178  $field_string_battery .= ", battery_wh65";
1179  $value_string_battery .= ", " . $battery_wh65;
1180  }
1181 
1182  if ( isset($battery_wh25) )
1183  {
1184  $field_string_battery .= ", battery_wh25";
1185  $value_string_battery .= ", " . $battery_wh25;
1186  }
1187 
1188  if ( isset($software_type ) )
1189  {
1190  $field_string_version .= ", software_type";
1191  $value_string_version .= ", '" . $software_type . "'";
1192  }
1193 
1194  if ( isset( $model ) )
1195  {
1196  $field_string_version .= ", station_model";
1197  $value_string_version .= ", '" . $model . "'";
1198  }
1199 
1200  if ( isset( $freq ) )
1201  {
1202  $field_string_version .= ", frequency";
1203  $value_string_version .= ", '" . $freq . "'";
1204  }
1205  }
1206  $insert_query = "insert into WeatherStation.ws_obs( " . $field_string . " ) values( " . $value_string . " );";
1207  $insert_query_battery = "insert into WeatherStation.ws_battery( " . $field_string_battery . " ) values( " . $value_string_battery . " );";
1208  $insert_query_option = "insert into WeatherStation.ws_version( " . $field_string_version . " ) values( " . $value_string_version . " );";
1209 
1210  ###############################
1211  # Used for Debugging Purposes #
1212  ###############################
1213  $query = "\n\n$insert_query";
1214  // file_put_contents( $log_file, $query, FILE_APPEND );
1215 
1216  # Insert the Observation Into the database
1217  $statement = $connection->prepare( $insert_query );
1218  $statement->execute();
1219 
1220 
1221  if ( $trim_readings != 1 )
1222  {
1223  $statement_battery = $connection->prepare( $insert_query_battery );
1224  $statement_battery->execute();
1225 
1226  ###############################
1227  # Used for Debugging Purposes #
1228  ###############################
1229  $query_option = "\n\n$insert_query_battery";
1230  // file_put_contents( $log_file, $query_option, FILE_APPEND );
1231 
1232  ###############################
1233  # Used for Debugging Purposes #
1234  ###############################
1235  $query_option = "\n\n$insert_query_option";
1236  // file_put_contents( $log_file, $query_option, FILE_APPEND );
1237 
1238  $statement_option = $connection->prepare( $insert_query_option );
1239  $statement_option->execute();
1240  }
1241 
1242  $connection->commit();
1243  header( "HTTP/1.0 200 Success" );
1244  }
1245  }
1246 
1247 
1248 
1249  SaveObservation();
1250 ?>
1251 
1252 
$db_password
$log_file
$db_username
SaveObservation()
Save the Incoming Observation to the WeatherStation Database.
Definition: index.php:69
DbConnect( $DbFunction, $ConnectionType='mysqli')
Make a Connection to a MariaDB/MySQL Database.
Definition: index.php:22