n0bel / PiClock

A Fancy Clock built around a monitor and a Raspberry Pi
MIT License
566 stars 182 forks source link

Adding air quality display #188

Open jon0x0 opened 3 years ago

jon0x0 commented 3 years ago

With all the wildfires here in the west I added an air quality index (AQI) display to my PiClock + weather interface.

IQAir has a free or paid API (free key good for a year at a time):

API docs at https://api-docs.iqair.com/

Once registered get your API key from https://www.iqair.com/us/dashboard/api

I used a call like this: http://api.airvisual.com/v2/nearest_city?lat=your_lat&lon=your_lon&key=your_key I recommend picking the appropriate station manually from their map interface and using the lat/lon which will cause that one to be selected.

You get up to 10,000 calls/month free (10 min+random period similar to temperature should work ok).

I duplicated the gettemp() and tempfinished() sections as getaqi() and aquifinished(), modified and added a very simple QUrl / QNetworkRequest string (not JSON) read and parse of the data, and searched for the tag '"aqius":' to pull out the data and display it in a text label where the other labels are created

aqistr = str(aqireply.readAll())
idxaqi=aqistr.find('"aqius":')
saq=aqistr[idxaqi:len(aqistr)]
saqi=saq[8:saq.find(',')]
s='AQI '+saqi
print s
aqi.setText(s)

[UI section, duplicated/modified] aqi = QtGui.QLabel(foreGround) aqi.setObjectName("aqi") aqi.setStyleSheet("#aqi { font-family:sans-serif; color: " + Config.textcolor + "; background-color: transparent; font-size: " + str(int(60 * xscale)) + "px; " + Config.fontattr + "}") aqi.setAlignment(Qt.AlignHCenter | Qt.AlignTop) aqi.setGeometry(0, height - 200, width, 100)

Publishing a production ready merge isn't really for me but just wanted to share what I found, maybe somebody can make use of it or run with it. Maybe the smoke coverage map could somehow be overlayed from fire.airnow.gov or somewhere. Air quality index color codes and warning text from https://www.airnow.gov/aqi/aqi-basics/ could be used. I will probably end up adding a few different local air stations since none of them are perfect for me and they can give quite different results depending on the situation. For now I am finding AQI data alone quite nice to have. Hope someone finds it useful.