-
Notifications
You must be signed in to change notification settings - Fork 0
/
purpleair.php
1557 lines (1444 loc) · 53.4 KB
/
purpleair.php
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
<?php
// PHP Script for reading Purpleair data for web pages
// Copyright (c) 2018 Chris Formeister. www.copperwoodwx.com/www.cfcsaz.com
// Free to Distribute as long as these credits remain
//
// Special thanks to Toxic, Forever and Weatherbee on WXForum for their assistance making the script better
//
// Some code provided by Purpleair - converted from Javascript to PHP
// Considered open source.
//
// Provided exclusively to members of wxforum.net
// Tested & written under PHP 7.1.11
// Version 1.1
// * Added Sensor channel B and averaging for ug/m³ - thanks to weatherbee
// * Addded timestamp option
// * Added averaging option for AQI values (try and see if it shows more acurately to Purpleair's site when turned on)
// --
// Version 1.2
// * Changed the Average API option to show both channels or to average them (thanks to Forever)
// * Added the One Week reading (thanks to Forever)
// * Added the GetPMComment (thanks to Forever)
// * Updated GetPMComment to the PurpleAir levels as they specify for the messages rather than US Gov standard
// * Did some Error Handling for connections
// --
// Version 1.2a - all changes thanks to Forever
// * Fixed issue with styling missing in Channel B
// * Cleaned up table structure
// * Fixed typos/punctuation
// * Fixed W3 validation errors
// --
// Version 1.3
// * Added option of logging of sensor data to MySQL DB
// * Added PM10 data from Thingspeak
// --
// Version 1.3a
// * Added option to show values of ug/m³ in 2 decimal places if value is less than 1 (ex. instead of 0 you will see 0.35 or whatever the value)
// --
// Version 1.3b
// * Fixed SQL issue and issued a Import file
// * Combined HTML
// --
// Version 1.3c
// * Fixed issue with erronous echos and loss of CSS (oops)
// --
// Version 1.4
// * Added color transitions like Purpleair's site to AQI values
// * Fixed CSS missing for non-averaging mode
// * Found and added Channel B to PM10 values. You will need the Thingspeak ID for Channel B - info in thread.
// --
// Version 1.4b
// * Bug fixes
// --
// Version 1.4c
// * Fixed bug with PM10 values
// * Added experinmental PM10 AQI function
// * Tuning of script
// --
// Version 1.5 FINAL RELEASE
// * Fixed bug where if decimal point was turned off decimal point numbers would still show - now display 0 as originally intended
// * Added option to cache results - this is useful if you have a high traffic site. PA doesn't like you doing many calls to it.
// * Removed the site up checking, it slows the script and is redundant since the script will return an error anyways if it fails.
// * Changed m/3 to proper HTML entity (thanks Toxic)
// * Added "Very Low" value to GetPMMessage for values < 1.0
// * Added additional levels to GetPMComment
// * Added GetAQIExMessage - returns extended EPA recommendations for the AQI passed to it
// * Added options to turn on/off the Title and Timestamps
// ================================================================================
// BEGIN CONFIGURABLE OPTIONS HERE
// ================================================================================
// --------------------------
// INSERT YOUR SENSOR IDS HERE - THIS IS FOR BOTH CHANNEL A AND B. See thread for info.
$sensorida = "29935"; // for SSV 1D
$sensoridb = "29936";
// INSERT YOUR THINGSPEAK API KEYS HERE FOR PM10 RESULTS. See thread for info, but can be found in your sensor's JSON file.
$TSID = "748775";
$TSKey = "YLG6TNC9653D25C5";
$TSID2 = "748779";
$TSKey2 = "3EMRGGM0OEILEY41";
// --------------------------
// USE COLOR TRANSITIONS (SHADING) LIKE PURPLEAIR DOES AS VALUE GETS NEARER TO NEXT LEVEL (1) OR JUST USE STATIC COLOR TO COLOR (0)
$useblending = 1;
// --------------------------
// SHOW TIMESTAMP OF SENSOR DATA - 1= YES 0=NO
$showtimestamp = 1;
// TIMEZONE MUST BE IN FORMAT PHP RECOGNIZES - SEE PHP MANUALS IF YOU NEED HELP
$timezone = "America/Los_Angeles";
// AVERAGE THE AQI VALUES BETWEEN CHANNEL A AND B IN ADDITION TO THE PM VALUES (1), OR SHOW BOTH SENSORS INDIVIDUALLY (0)
$averageaqi = 1;
// SHOW ug/m3 RESULTS TO 2 DECIMAL PLACES INSTEAD OF JUST 0 IF LESS THAN 0.1
$showdecimal = 1;
// INSERT YOUR LOCATION OR SENSOR NAME
$showtitle = 1;
$sensorname = "SSV 1D";
// DISPLAY PM10 PARTICLE VALUES
// Note: You can display the PM10 AQI value by using the variable $AQI10. Put it in some HTML using the CSS class AQI10 as well.
$showpm10 = 1;
// CACHE RESULTS
// This will cache the results from sensor for the set time period. Once it is over, the data will be re-requested from PA/TS.
// You should turn this ON if you have a high traffic site. 1 = ON, 0 = OFF
// IMPORTANT: The script & directory needs to have WRITE permissions in the directory it is in for this to work.
$cachedata = 0;
$cacheseconds = 180; //60 = 1 min, 300 = 5 min
// Get All Sensor VALUES
// If you want all of the sensor values to be retrieved, enable this option. The script will take much more time to load since it has to retrieve more data.
// It is recommended you Cache and CRON the script ocassionally otherwise visitors will have to wait about 15-20 seconds for the data to load.
// This also requires the THINGSPEAK_SECONDARY_ID values from the PA JSON file. See thread for details.
$getalldata = 1;
$TSSecID = '748777';
$TSSecID2 = '748781';
$TSSecKey = 'VZTH0O9N2I5LAGSK';
$TSSecKey2 = 'ZOADT99G9ITR5JB1';
// --------------------------
// SQL LOGGING MODE
// 0 = DON'T LOG ANY RESULTS; JUST DISPLAYS THE PURPLEAIR RESULTS AS NORMAL
// 1 = LOG THE RESULTS TO DATABASE AND DISPLAY THE PURPLEAIR RESULTS AS NORMAL
// 2 = LOG THE RESULTS TO DATABASE AND DON'T DISPLAY ANYTHING (CRON-TYPE SCRIPT)
//
// Note: The AQI logged results are always combined values (A+B) like what is shown on Purpleairs site. You can change the script if you want it done differently.
// PM values are logged as each channel.
//
// IMPORTANT: YOU MUST CREATE THE DATABASE AND ITS LAYOUT - A MYSQL EXPORT IS AVAILABLE IN THE THREAD.
// IMPORTANT: IT IS NOT RECOMMENDED TO USE THIS WHEN USING CACHING MODE, YOU WILL JUST GET REPEATED VALUES OF THE CACHE.
$logtosql = 1;
// --------------------------
//MYSQL PARAMETERS FOR SQL LOGGING MODES
$servername = "localhost";
$username = "drewc"; // (on Drew's Macbook)
$password = "rivadog8";
$dbname = "purpleair";
$tbldata = "padata";
// END OF CONFIGURABLE OPTIONS
// ----------------------------------------------------------------------------
if ($_GET['version'] == 1) {
echo "genrel-1.5";
die;
}
if ($sensorida == "")
{
echo "No sensor ID A entered in Script source!";
die;
}
if ($sensoridb == "")
{
echo "No sensor ID B entered in Script source!";
die;
}
if ($TSID == "" || $TSID2 == "" || $TSKey == "" || $TSKey2 == "")
{
echo "No Thingspeak credentials entered.";
die;
}
if ($getalldata == 1) {
if ($TSSecID == "" || $TSSecID2 == "" || $TSSecKey == "" || $TSSecKey2 == "")
{
echo "No Secondary Thingspeak credentials entered.";
die;
}
}
// Set some of our variables as Globals otherwise PHP throws Info warnings
global $pajsona;
global $pajsonb;
global $pm10;
global $pm102;
global $json_resulta;
global $json_resultb;
global $result2;
global $result3;
global $result4;
global $result5;
global $AQIa;
global $AQIb;
global $AQI;
global $AQIshortterma;
global $AQIshorttermb;
global $AQIshortterm1;
global $AQIshortterm2;
global $AQIrealtime1;
global $AQIrealtime2;
global $AQIshortterma;
global $AQIshorttermb;
global $AQIshortterm;
global $AQI301;
global $AQI302;
global $AQI601;
global $AQI602;
global $AQI6h1;
global $AQI6h2;
global $AQI24h1;
global $AQI24h2;
global $AQI1w1;
global $AQI1w2;
global $pm1;
global $pm12;
global $parts031;
global $parts032;
global $parts051;
global $parts052;
global $parts101;
global $parts102;
global $parts251;
global $parts252;
global $parts501;
global $parts502;
global $parts1001;
global $parts1002;
global $lasertemp;
global $laserhum;
global $extrasensors;
if ($cachedata == 1) {
$file = __DIR__ . "/pacache1.txt";
$file2 = __DIR__ . "/pacache2.txt";
$file3 = __DIR__ . "/tscache1.txt";
$file4 = __DIR__ . "/tscache2.txt";
$file5 = __DIR__ . "/tscachex.txt";
$filemtime = @filemtime($file); // returns FALSE if file does not exist
if ($filemtime == FALSE) {
//Get the sensor data via JSON
$pajsona = file_get_contents("http://www.purpleair.com/json?show=" . $sensorida);
$pajsonb = file_get_contents("http://www.purpleair.com/json?show=" . $sensoridb);
//Get the PM10 value from Thingspeak
$pm10 = file_get_contents("https://api.thingspeak.com/channels/".$TSID."/fields/field3/last.txt?api_key=" . $TSKey);
$pm102 = file_get_contents("https://api.thingspeak.com/channels/".$TSID2."/fields/field3/last.txt?api_key=" . $TSKey2);
if ($getalldata == 1) {
LoadAllData();
}
$json_resulta=json_decode($pajsona);
$json_resultb=json_decode($pajsonb);
$result2 = $json_resulta->results[0]->Stats;
$result3 = json_decode($result2);
$result4 = $json_resultb->results[0]->Stats;
$result5 = json_decode($result4);
try {
file_put_contents($file, $pajsona, LOCK_EX);
file_put_contents($file2, $pajsonb, LOCK_EX);
file_put_contents($file3, $pm10, LOCK_EX);
file_put_contents($file4, $pm102, LOCK_EX);
if ($getalldata == 1)
{
$wfile = fopen($file5,"w");
for($i=0; $i<count($extrasensors); $i++) {
$data=array($extrasensors[$i]);
fputcsv($wfile, $data);
}
fclose($file);
}
} catch (Exception $e) {
echo("Error writing the cache files. Make sure they have 777 (Write) permissions.");
error_log("Could not write the cache files. Error was: ", $e->getMessage(), "\n");
die;
}
}
if (!$filemtime or (time() - $filemtime >= $cacheseconds)){
//Get the sensor data via JSON
$pajsona = file_get_contents("http://www.purpleair.com/json?show=" . $sensorida);
$pajsonb = file_get_contents("http://www.purpleair.com/json?show=" . $sensoridb);
if ($getalldata == 1) {
LoadAllData();
}
//Get the PM10 value from Thingspeak
$pm10 = file_get_contents("https://api.thingspeak.com/channels/".$TSID."/fields/field3/last.txt?api_key=" . $TSKey);
$pm102 = file_get_contents("https://api.thingspeak.com/channels/".$TSID2."/fields/field3/last.txt?api_key=" . $TSKey2);
$json_resulta=json_decode($pajsona);
$json_resultb=json_decode($pajsonb);
$result2 = $json_resulta->results[0]->Stats;
$result3 = json_decode($result2);
$result4 = $json_resultb->results[0]->Stats;
$result5 = json_decode($result4);
try {
file_put_contents($file, $pajsona, LOCK_EX);
file_put_contents($file2, $pajsonb, LOCK_EX);
file_put_contents($file3, $pm10, LOCK_EX);
file_put_contents($file4, $pm102, LOCK_EX);
if ($getalldata == 1)
{
$wfile = fopen($file5,"w");
for($i=0; $i<count($extrasensors); $i++) {
$data=array($extrasensors[$i]);
fputcsv($wfile, $data);
}
fclose($file);
}
} catch (Exception $e) {
echo("Error writing the cache files. Make sure they have 777 (Write) permissions.");
error_log("Could not write the cache files. Error was: ", $e->getMessage(), "\n");
die;
}
} else {
if ($getalldata == 1) {
$lines = file("tscachex.txt");
$pm1 = $lines[0];
$pm12 = $lines[1];
$parts031 = $lines[2];
$parts032 = $lines[3];
$parts051 = $lines[4];
$parts052 = $lines[5];
$parts101 = $lines[6];
$parts102 = $lines[7];
$parts251 = $lines[8];
$parts252 = $lines[9];
$parts501 = $lines[10];
$parts502 = $lines[11];
$parts1001 = $lines[12];
$parts1002 = $lines[13];
$lasertemp = $lines[14];
$laserhum = $lines[15];
}
$pajsona = file_get_contents($file);
$pajsonb = file_get_contents($file2);
$pm10 = file_get_contents($file3);
$pm102 = file_get_contents($file4);
$json_resulta=json_decode($pajsona);
$json_resultb=json_decode($pajsonb);
$result2 = $json_resulta->results[0]->Stats;
$result3 = json_decode($result2);
$result4 = $json_resultb->results[0]->Stats;
$result5 = json_decode($result4);
}
} else {
//Get the sensor data via JSON
$pajsona = file_get_contents("http://www.purpleair.com/json?show=" . $sensorida);
$pajsonb = file_get_contents("http://www.purpleair.com/json?show=" . $sensoridb);
if ($getalldata == 1) {
LoadAllData();
}
//Get the PM10 value from Thingspeak
$pm10 = file_get_contents("https://api.thingspeak.com/channels/".$TSID."/fields/field3/last.txt?api_key=" . $TSKey);
$pm102 = file_get_contents("https://api.thingspeak.com/channels/".$TSID2."/fields/field3/last.txt?api_key=" . $TSKey2);
$json_resulta=json_decode($pajsona);
$json_resultb=json_decode($pajsonb);
$result2 = $json_resulta->results[0]->Stats;
$result3 = json_decode($result2);
$result4 = $json_resultb->results[0]->Stats;
$result5 = json_decode($result4);
}
if ($pajsona == "" or $pajsonb == "")
{
echo "No Air Quality data is currently available for this location - Retrieval Error.";
if ($pajsona == "") {
$sensorbad = "A";
} elseif ($pajsonb == "") {
$sensorbad = "B";
} elseif ($pajson == "" || $pajsonb == "") {
$sensorbad = "A and B";
}
error_log("No data from Purpleair servers for the script to process (Sensor $sensorbad). Possibly PA site is down or incorrect sensor IDs.", 0);
die;
}
if (isset($TSID) == true && isset($TSID2) == true) {
if ($pm10 == "" or $pm102 == "")
{
echo "No Air Quality data is currently available for this location - Retrieval Error.";
if ($pm10 == "") {
$sensorbad2 = "A";
} elseif ($pm102 == "") {
$sensorbad2 = "B";
} elseif ($pm10 == "" || $pm102 == "") {
$sensorbad2 = "A and B";
}
error_log("No data from Thingspeak servers for the script to process (Sensor $sensorbad2). Possibly TS site is down or bad IDs/access keys.", 0);
die;
}
}
//Create our variables for different Sensor results
$aq1 = $result3->v;
$aqst1 = $result3->v1;
$aq301 = $result3->v2;
$aq601 = $result3->v3;
$aq6h1 = $result3->v4;
$aq24h1 = $result3->v5;
$aq1w1 = $result3->v6;
$aqpm1 = $result3->pm;
$timestamp = $result3->lastModified;
$aq2 = $result5->v;
$aqst2 = $result5->v1;
$aq302 = $result5->v2;
$aq602 = $result5->v3;
$aq6h2 = $result5->v4;
$aq24h2 = $result5->v5;
$aq1w2 = $result5->v6;
$aqpm2 = $result5->pm;
//Add the sensor results together (A+B)
if ($showdecimal == 1) {
if ($aq1 + $aq2 < "1.0") {
$aq = round(($aq1 + $aq2)/2,2);
} else {
$aq = round(($aq1 + $aq2)/2,0);
}
if ($aqst1 + $aqst2 < "1.0") {
$aqst = round(($aqst1 + $aqst2)/2,2);
} else {
$aqst = round(($aqst1 + $aqst2)/2,0);
}
if ($aq301 + $aq302 < "1.0") {
$aq30 = round(($aq301 + $aq302)/2,2);
} else {
$aq30 = round(($aq301 + $aq302)/2,0);
}
if ($aq601 + $aq602 < "1.0") {
$aq60 = round(($aq601 + $aq602)/2,2);
} else {
$aq60 = round(($aq601 + $aq602)/2,0);
}
if ($aq6h1 + $aq6h2 < "1.0") {
$aq6h = round(($aq6h1 + $aq6h2)/2,2);
} else {
$aq6h = round(($aq6h1 + $aq6h2)/2,0);
}
if ($aq24h1 + $aq24h2 < "1.0") {
$aq24h = round(($aq24h1 + $aq24h2)/2,2);
} else {
$aq24h = round(($aq24h1 + $aq24h2)/2,0);
}
if ($aq1w1 + $aq1w2 < "1.0") {
$aq1w = round(($aq1w1 + $aq1w2 )/2,2);
} else {
$aq1w = round(($aq1w1 + $aq1w2 )/2,0);
}
if ($pm10 + $pm102 < "1.0") {
$pm10val = round(($pm10 + $pm102)/2,2);
} else {
$pm10val = round(($pm10 + $pm102)/2,0);
}
} else {
if ($aq1 + $aq2 < "1.0") {
$aq = 0;
} else {
$aq = round(($aq1 + $aq2)/2,0);
}
if ($aqst + $aqst2 < "1.0") {
$aqst = 0;
} else {
$aqst = round(($aqst1 + $aqst2)/2,0);
}
if ($aq301 + $aq302 < "1.0") {
$aq30 = 0;
} else {
$aq30 = round(($aq301 + $aq302)/2,0);
}
if ($aq601 + $aq602 < "1.0") {
$aq60 = 0;
} else {
$aq60 = round(($aq601 + $aq602)/2,0);
}
if ($aq6h1 + $aq6h2 < "1.0") {
$aq6h = 0;
} else {
$aq6h = round(($aq6h1 + $aq6h2)/2,0);
}
if ($aq24h1 + $aq24h2 < "1.0") {
$aq24h = 0;
} else {
$aq24h = round(($aq24h1 + $aq24h2)/2,0);
}
if ($aq1w1 + $aq1w2 < "1.0") {
$aq1w = 0;
} else {
$aq1w = round(($aq1w1 + $aq1w2)/2,0);
}
if ($pm10 + $pm102 < "1.0") {
$pm10val = 0;
} else {
$pm10val = round(($pm10 + $pm102)/2,0);
}
}
//Set the variables and get AQI from PPM
if ($averageaqi == 1) {
$AQIa = aqiFromPM($aqst1);
$AQIb = aqiFromPM($aqst2);
$AQI = round(($AQIa + $AQIb) / 2);
$AQIrealtimea = aqiFromPM($aq1);
$AQIrealtimeb = aqiFromPM($aq2);
$AQIrealtime = round(($AQIrealtimea + $AQIrealtimeb) / 2);
$AQIshortterma = aqiFromPM($aqst1);
$AQIshorttermb = aqiFromPM($aqst2);
$AQIshortterm = round(($AQIshortterma + $AQIshorttermb) / 2);
$AQI30a = aqiFromPM($aq301);
$AQI30b = aqiFromPM($aq302);
$AQI30 = round(($AQI30a + $AQI30b) / 2);
$AQI60a = aqiFromPM($aq601);
$AQI60b = aqiFromPM($aq602);
$AQI60 = round(($AQI60a + $AQI60b) / 2);
$AQI6ha = aqiFromPM($aq6h1);
$AQI6hb = aqiFromPM($aq6h2);
$AQI6h = round(($AQI6ha + $AQI6hb) / 2);
$AQI24ha = aqiFromPM($aq24h1);
$AQI24hb = aqiFromPM($aq24h2);
$AQI24h = round(($AQI24ha + $AQI24hb) / 2);
$AQI1wa = aqiFromPM($aq1w1);
$AQI1wb = aqiFromPM($aq1w2);
$AQI1w = round(($AQI1wa + $AQI1wb) / 2);
if (isset($TSID) == true && isset($TSID2) == true) {
$AQI10a = aqiFromPM($pm10);
$AQI10b = aqiFromPM($pm102);
$AQI10 = round(($AQI10a + $AQI10b) / 2);
}
} else {
$AQIa = aqiFromPM($aqst1);
$AQIb = aqiFromPM($aqst2);
$AQI = round(($AQIa + $AQIb) / 2);
$AQIshortterma = aqiFromPM($aqst1);
$AQIshorttermb = aqiFromPM($aqst2);
$AQIshortterm = round(($AQIshortterma + $AQIshorttermb) / 2);
$AQIrealtime1 = aqiFromPM($aq1);
$AQIshortterm1 = aqiFromPM($aqst1);
$AQI301 = aqiFromPM($aq301);
$AQI601 = aqiFromPM($aq601);
$AQI6h1 = aqiFromPM($aq6h1);
$AQI24h1 = aqiFromPM($aq24h1);
$AQI1w1 = aqiFromPM($aq1w1);
$AQIrealtime2 = aqiFromPM($aq2);
$AQIshortterm2 = aqiFromPM($aqst2);
$AQI302 = aqiFromPM($aq302);
$AQI602 = aqiFromPM($aq602);
$AQI6h2 = aqiFromPM($aq6h2);
$AQI24h2 = aqiFromPM($aq24h2);
$AQI1w2 = aqiFromPM($aq1w2);
if (isset($TSID) == true && isset($TSID2) == true) {
$AQI10a = aqiFromPM($pm10);
$AQI10b = aqiFromPM($pm102);
$AQI10 = round(($AQI10a + $AQI10b) / 2);
}
}
$Description = getAQIDescription($AQI);
$Message = GetAQIMessage($AQI);
$MessageComment = GetPMComment($aqst);
if ($showtimestamp == 1) {
date_default_timezone_set($timezone);
$sensorstamp = date('D, M. d, Y - h:i a', $timestamp/1000);
}
//Timestamp for SQL
$timestamp = date("Y-m-d H:i:s");
if ($logtosql == 2) {
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO $tbldata (ID, time, aqivalue, rtaqi, staqi, 30minaqi, 1hraqi, 6hraqi, 24hraqi, 1wkaqi, chartgms, chastgms,
cha30mingms,cha1hrgms,cha6hrgms,cha24hrgms,cha1wkgms,chbrtgms,chbstgms,chb30mingms,chb1hrgms,chb6hrgms,chb24hrgms,chb1wgms)
VALUES (NULL, '$timestamp', '$AQI', '$AQIrealtime', '$AQIshortterm', '$AQI30', '$AQI60', '$AQI6h', '$AQI24h', '$AQI1w', '$aq1', '$aqst1', '$aq301', '$aq601', '$aq6h1', '$aq24h1', '$aq1w1', '$aq2', '$aqst2', '$aq302', '$aq602', '$aq6h2', '$aq24h2', '$aq1w2')";
if ($conn->query($sql) === FALSE) {
echo "Error inserting SQL for Purpleair Script: " . $sql . "<br>" . $conn->error;
die;
}
$conn->close();
die;
}
global $AQIrealtime;
global $AQI30;
global $AQI60;
global $AQI6h;
global $AQI24h;
global $AQI1w;
echo '
<style type="text/css">
.auto-style1 {
font-size: xx-large;
}
.auto-style8 {
margin: 0 0 5px;
overflow: hidden;
padding: 5px;
background-color: ' . GetColor($AQI)[0] . ';
color: ' . GetColor($AQI)[1] . ';
border: 1px solid #afcde3;
-webkit-border-radius: 5px;
border-radius: 5px;
text-align: center;
font-family: Arial, Helvetica, sans-serif;
font-size: 36pt;
}
';
if (isset($TSID) == true && isset($TSID2) == true) {
if($showpm10 == 1) {
echo '
.AQI10 {
margin: 0 0 5px;
overflow: hidden;
padding: 5px;
background-color: ' . GetColor($AQI10)[0] . ';
color: ' . GetColor($AQI10)[1] . ';
border: 1px solid #afcde3;
-webkit-border-radius: 5px;
border-radius: 5px;
text-align: center;
font-family: Arial, Helvetica, sans-serif;
font-size: 36pt;
}';
}
}
echo '
.rtitems {
margin: 0 0 5px;
overflow: hidden;
padding: 5px;
background-color: ' . GetColor($AQIrealtime)[0] . ';
color: ' . GetColor($AQIrealtime)[1] . ';
border: 1px solid #afcde3;
-webkit-border-radius: 5px;
border-radius: 5px;
text-align: center;
font-family: Arial, Helvetica, sans-serif;
}
.rtitems1 {
margin: 0 0 5px;
overflow: hidden;
padding: 5px;
background-color: ' . GetColor($AQIrealtime1)[0] . ';
color: ' . GetColor($AQIrealtime1)[1] . ';
border: 1px solid #afcde3;
-webkit-border-radius: 5px;
border-radius: 5px;
text-align: center;
font-family: Arial, Helvetica, sans-serif;
}
.rtitems2 {
margin: 0 0 5px;
overflow: hidden;
padding: 5px;
background-color: ' . GetColor($AQIrealtime2)[0] . ';
color: ' . GetColor($AQIrealtime2)[1] . ';
border: 1px solid #afcde3;
-webkit-border-radius: 5px;
border-radius: 5px;
text-align: center;
font-family: Arial, Helvetica, sans-serif;
}
.stitems {
margin: 0 0 5px;
overflow: hidden;
padding: 5px;
background-color: ' . GetColor($AQIshortterm)[0] . ';
color: ' . GetColor($AQIshortterm)[1] . ';
border: 1px solid #afcde3;
-webkit-border-radius: 5px;
border-radius: 5px;
text-align: center;
font-family: Arial, Helvetica, sans-serif;
}
.stitems1 {
margin: 0 0 5px;
overflow: hidden;
padding: 5px;
background-color: ' . GetColor($AQIshortterm1)[0] . ';
color: ' . GetColor($AQIshortterm1)[1] . ';
border: 1px solid #afcde3;
-webkit-border-radius: 5px;
border-radius: 5px;
text-align: center;
font-family: Arial, Helvetica, sans-serif;
}
.stitems2 {
margin: 0 0 5px;
overflow: hidden;
padding: 5px;
background-color: ' . GetColor($AQIshortterm2)[0] . ';
color: ' . GetColor($AQIshortterm2)[1] . ';
border: 1px solid #afcde3;
-webkit-border-radius: 5px;
border-radius: 5px;
text-align: center;
font-family: Arial, Helvetica, sans-serif;
}
.thirtyitems {
margin: 0 0 5px;
overflow: hidden;
padding: 5px;
background-color: ' . GetColor($AQI30)[0] . ';
color: ' . GetColor($AQI30)[1] . ';
border: 1px solid #afcde3;
-webkit-border-radius: 5px;
border-radius: 5px;
text-align: center;
font-family: Arial, Helvetica, sans-serif;
}
.thirtyitems1 {
margin: 0 0 5px;
overflow: hidden;
padding: 5px;
background-color: ' . GetColor($AQI301)[0] . ';
color: ' . GetColor($AQI301)[1] . ';
border: 1px solid #afcde3;
-webkit-border-radius: 5px;
border-radius: 5px;
text-align: center;
font-family: Arial, Helvetica, sans-serif;
}
.thirtyitems2 {
margin: 0 0 5px;
overflow: hidden;
padding: 5px;
background-color: ' . GetColor($AQI302)[0] . ';
color: ' . GetColor($AQI302)[1] . ';
border: 1px solid #afcde3;
-webkit-border-radius: 5px;
border-radius: 5px;
text-align: center;
font-family: Arial, Helvetica, sans-serif;
}
.sixtyitems {
margin: 0 0 5px;
overflow: hidden;
padding: 5px;
background-color: ' . GetColor($AQI60)[0] . ';
color: ' . GetColor($AQI60)[1] . ';
border: 1px solid #afcde3;
-webkit-border-radius: 5px;
border-radius: 5px;
text-align: center;
font-family: Arial, Helvetica, sans-serif;
}
.sixtyitems1 {
margin: 0 0 5px;
overflow: hidden;
padding: 5px;
background-color: ' . GetColor($AQI601)[0] . ';
color: ' . GetColor($AQI601)[1] . ';
border: 1px solid #afcde3;
-webkit-border-radius: 5px;
border-radius: 5px;
text-align: center;
font-family: Arial, Helvetica, sans-serif;
}
.sixtyitems2 {
margin: 0 0 5px;
overflow: hidden;
padding: 5px;
background-color: ' . GetColor($AQI602)[0] . ';
color: ' . GetColor($AQI602)[1] . ';
border: 1px solid #afcde3;
-webkit-border-radius: 5px;
border-radius: 5px;
text-align: center;
font-family: Arial, Helvetica, sans-serif;
}
.sixhouritems {
margin: 0 0 5px;
overflow: hidden;
padding: 5px;
background-color: ' . GetColor($AQI6h)[0] . ';
color: ' . GetColor($AQI6h)[1] . ';
border: 1px solid #afcde3;
-webkit-border-radius: 5px;
border-radius: 5px;
text-align: center;
font-family: Arial, Helvetica, sans-serif;
}
.sixhouritems1 {
margin: 0 0 5px;
overflow: hidden;
padding: 5px;
background-color: ' . GetColor($AQI6h1)[0] . ';
color: ' . GetColor($AQI6h1)[1] . ';
border: 1px solid #afcde3;
-webkit-border-radius: 5px;
border-radius: 5px;
text-align: center;
font-family: Arial, Helvetica, sans-serif;
}
.sixhouritems2 {
margin: 0 0 5px;
overflow: hidden;
padding: 5px;
background-color: ' . GetColor($AQI6h2)[0] . ';
color: ' . GetColor($AQI6h2)[1] . ';
border: 1px solid #afcde3;
-webkit-border-radius: 5px;
border-radius: 5px;
text-align: center;
font-family: Arial, Helvetica, sans-serif;
}
.twentyfouritems {
margin: 0 0 5px;
overflow: hidden;
padding: 5px;
background-color: ' . GetColor($AQI24h)[0] . ';
color: ' . GetColor($AQI24h)[1] . ';
border: 1px solid #afcde3;
-webkit-border-radius: 5px;
border-radius: 5px;
text-align: center;
font-family: Arial, Helvetica, sans-serif;
}
.twentyfouritems1 {
margin: 0 0 5px;
overflow: hidden;
padding: 5px;
background-color: ' . GetColor($AQI24h1)[0] . ';
color: ' . GetColor($AQI24h1)[1] . ';
border: 1px solid #afcde3;
-webkit-border-radius: 5px;
border-radius: 5px;
text-align: center;
font-family: Arial, Helvetica, sans-serif;
}
.twentyfouritems2 {
margin: 0 0 5px;
overflow: hidden;
padding: 5px;
background-color: ' . GetColor($AQI24h2)[0] . ';
color: ' . GetColor($AQI24h2)[1] . ';
border: 1px solid #afcde3;
-webkit-border-radius: 5px;
border-radius: 5px;
text-align: center;
font-family: Arial, Helvetica, sans-serif;
}
.oneweekitems {
margin: 0 0 5px;
overflow: hidden;
padding: 5px;
background-color: ' . GetColor($AQI1w)[0] . ';
color: ' . GetColor($AQI1w)[1] . ';
border: 1px solid #afcde3;
-webkit-border-radius: 5px;
border-radius: 5px;
text-align: center;
font-family: Arial, Helvetica, sans-serif;
}
.oneweekitems1 {
margin: 0 0 5px;
overflow: hidden;
padding: 5px;
background-color: ' . GetColor($AQI1w1)[0] . ';
color: ' . GetColor($AQI1w1)[1] . ';
border: 1px solid #afcde3;
-webkit-border-radius: 5px;
border-radius: 5px;
text-align: center;
font-family: Arial, Helvetica, sans-serif;
}
.oneweekitems2 {
margin: 0 0 5px;
overflow: hidden;
padding: 5px;
background-color: ' . GetColor($AQI1w2)[0] . ';
color: ' . GetColor($AQI1w2)[1] . ';
border: 1px solid #afcde3;
-webkit-border-radius: 5px;
border-radius: 5px;
text-align: center;
font-family: Arial, Helvetica, sans-serif;
}
.auto-style9 {
font-family: Arial, Helvetica, sans-serif;
}
.auto-style10 {
font-family: Arial, Helvetica, sans-serif;
text-align: center;
padding: 10px;
}
.auto-style11 {
font-size: x-small;
}
.auto-style12 {
font-size: medium;
}
.auto-style13 {
font-family: Arial, Helvetica, sans-serif;
font-size: small;
}
.auto-style14 {
font-family: Arial, Helvetica, sans-serif;
text-align: center;
font-size: x-small;
}
.auto-style15 {
font-family: Arial, Helvetica, sans-serif;
font-size: large;
}
</style>
<table style="width: 100%">
<tr>
<th class="auto-style9"><h2>Air Quality Index</h2></th>
</tr>';
if ($showtitle == 1);
{
echo '<tr>
<th class="auto-style9"><h2>' . $sensorname . '</h2></th>
</tr>';
}
if ($showtimestamp == 1);
{
echo '
<tr>
<th class="auto-style10">' . $sensorstamp . '</th>
</tr>';
}
echo '
<tr>
<th style="width: 100%"><span class="auto-style8">' . $AQIshortterm . '</span></th>
</tr>
<tr>
<th class="auto-style15" style="width: 100%" font-size="large"><strong>' . $Description . '</strong></th>
</tr>
';
if ($averageaqi == 1) {
echo ' <table style="width: 100%">
<tr>
<td colspan="7" style="height: 28px" class="auto-style13">' . $Message . '</td>
</tr>
<td colspan="7" style="height: 28px" class="auto-style13"><b>Running Averages of Channel A and B</b></td>
<tr>
<td class="auto-style10" style="width: 14%"><p class="rtitems">
<span class="auto-style12">' . $AQIrealtime . '</span><br>
<span class="auto-style11">' . $aq . ' µg/m³</span></p>
</td>
<td class="auto-style10" style="width: 14%"><p class="stitems">
<span class="auto-style12">' . $AQIshortterm . '</span><br>
<span class="auto-style11">' . $aqst . ' µg/m³</span></p>
</td>
<td class="auto-style10" style="width: 14%"><p class="thirtyitems">
<span class="auto-style12">' . $AQI30 . '</span><br>