Cov txheej txheem:

AirVisual Air Quality API Dashboard: 5 Kauj Ruam
AirVisual Air Quality API Dashboard: 5 Kauj Ruam

Video: AirVisual Air Quality API Dashboard: 5 Kauj Ruam

Video: AirVisual Air Quality API Dashboard: 5 Kauj Ruam
Video: Stream Air Quality with Air Visual API | GIT TECH'D 2024, Hlis ntuj nqeg
Anonim
Image
Image

AirVisual (https://www.airvisual.com) yog lub vev xaib uas muab cov ntaub ntawv ntawm huab cua zoo thoob ntiaj teb. Lawv muaj API uas peb yuav siv kom tau txais cov ntaub ntawv huab cua zoo xa mus rau lub dashboard. Peb yuav cuam tshuam nrog API no zoo ib yam li peb tau ua nrog huab cua dashboard.

Nov yog qhov haujlwm yooj yim uas qhia koj yuav siv API li cas. Cia peb pib!

Kauj ruam 1: Pib

AirVisual API
AirVisual API

Peb twb tau sau cov cai feem ntau ua ke, tab sis koj yuav xav tau koj los kho ob peb txoj hauv kev. Muaj ntau txoj hauv kev rau nthuav dav rau yam peb tau ua, ib yam nkaus.

Txhawm rau khaws txhua yam uas peb tau npaj rau koj, koj yuav tsum tau clone lub chaw cia khoom los ntawm GitHub. GitHub yog qhov kev pabcuam txaus uas tso cai rau peb khaws, hloov kho, thiab tswj cov haujlwm zoo li no. Koj yuav xav khiav tsab ntawv no ntawm lub cuab yeej tshwj xeeb. Koj tuaj yeem siv lub khoos phis tawj, Raspberry Pi, lossis lwm lub tshuab computer. Txhawm rau clone lub chaw cia khoom txhua yam peb yuav tsum tau ua yog mus rau hauv peb lub khoos phis tawj lossis Pi lub davhlau ya nyob twg, thiab ntaus cov lus txib no:

$ git clone

Ntaus nkag mus thiab koj yuav pom cov ntaub ntawv no:

$ git clone https://github.com/InitialState/airvisual.git Cloning rau hauv 'airvisual' … deb: Kev suav cov khoom: 13, ua tiav. tej thaj chaw deb: Suav cov khoom: 100% (13/13), ua tiav. tej thaj chaw deb: Nyuaj cov khoom: 100% (12/12), ua tiav. tej thaj chaw deb: Tag nrho 13 (delta 2), rov qab siv 0 (delta 0), ntim rov qab siv 0 Cov khoom ntim khoom: 100% (13/13), ua tiav.

Thaum koj pom qhov no ces ua kev zoo siab, koj tau ua tiav cloned GitHub Repo thiab muaj tag nrho cov ntaub ntawv tsim nyog los tsim txoj haujlwm no. Cia peb txav mus rau phau ntawv teev npe tshiab. Txhawm rau hloov cov npe, txhua yam koj yuav tsum tau ua yog ntaus "cd" thiab tom qab ntawd ntaus lub npe ntawm cov npe uas koj xav mus. Hauv qhov no, peb yuav ntaus:

$ cd airvisual

Thaum peb ntaus nkag, koj yuav pom tias tam sim no peb nyob hauv phau ntawv teev npe huab cua. Cia peb ntaus "ls" kom pom cov ntaub ntawv twg peb tau teeb tsa. Koj yuav tsum zoo li qee yam raws li hauv qab no:

LICENSE README.md airquality.py

Peb xav tau qee yam khoom ua ntej peb tuaj yeem hloov kho cov cai yog li cia saib ntawm Air Quality API tom ntej.

Kauj ruam 2: AirVisual API

AirVisual API
AirVisual API
AirVisual API
AirVisual API

AirVisual muaj huab cua zoo (AQI) thiab muaj kuab paug API uas tso cai rau txog 10, 000 API hu rau ib hlis dawb. Koj tuaj yeem tso npe rau Community Tier. Thaum koj sau npe, koj tuaj yeem mus rau Kuv Cua thiab API tab. Nws nyob ntawm no uas koj yuav pom koj tus lej API thiab cov ntaub ntawv ntawm API.

Nyem rau +New Key khawm los tsim peb thawj tus lej nkag API. Txog Xaiv Txoj Haujlwm, siv qhov nco tseg los xaiv Zej Zog thiab nyem Tsim. Yog tias txhua yam mus zoo koj yuav pom cov lus ua tiav thiab koj tuaj yeem rov qab mus rau koj lub API dashboard kom pom koj cov ntaub ntawv tseem ceeb tshiab. Tus nqi tseem ceeb (tus lej thiab cim) yog yam koj yuav xav tau rau txoj haujlwm no. Yog tias koj siv cov ntaub ntawv API koj yuav pom muaj ntau hom API hu koj tuaj yeem ua tau. Txog txoj haujlwm no peb xav kom tau txais cov ntaub ntawv hauv nroog ze tshaj plaws raws GPS kev tswj hwm. Txog qhov kev hu no koj yuav xav tau koj qhov ntev, latitude, thiab tus lej API. Nkag mus rau qhov ntsuas no rau hauv kev hu hauv qab no, muab qhov ntawd tso rau hauv qhov chaw nyob ntawm koj tus browser, thiab ntaus nkag.

api.airvisual.com/v2/nearest_city?lat={{LATITUDE}}&lon={{LONGITUDE}}&key={{YOUR_API_KEY}}

Qhov no yuav xa cov nroog ze tshaj plaws cov ntaub ntawv raws li GPS kev tswj hwm. Nws yuav zoo ib yam li no:

Kuv pom zoo siv JSON tus tsim kom tau txais kev pom zoo ntawm cov ntaub ntawv. Yog tias koj siv qhov ntawd nws yuav zoo li me ntsis zoo li qhov no:

"status": "kev vam meej", "data": {"city": "Nashville", "state": "Tennessee", "country": "USA", "location": {"type": "Point", "coordinates": [-86.7386, 36.1767]}, "tam sim no": {"huab cua": {"ts": "2019-04-08T19: 00: 00.000Z", "_v": 0, "createdAt": "2019-04-08T19: 04: 18.662Z "," hu ": 88," ic ":" 04d "," pr ": 1012," tp ": 18," updatedAt ":" 2019-04-08T19: 46: 53.140Z "," wd ": 90, "ws": 3.1}, "pa phem": {"ts": "2019-04-08T18: 00: 00.000Z", "aqius": 10, "mainus": "p2", "aqicn": 3, "maincn": "p2"}

Peb tuaj yeem pom yooj yim tam sim no uas peb muaj cov ntaub ntawv qhia txog thaj chaw, huab cua, thiab pa phem. Ob qhov txiaj ntsig peb tsom mus rau txoj haujlwm no yog Air Quality Index US (aquis) thiab Main Pollutant (mainus). Cov txiaj ntsig Kev Nyab Xeeb Huab Cua qhia rau peb paub tias huab cua hauv nroog zoo npaum li cas thiab yuav cuam tshuam li cas rau koj kev noj qab haus huv. Daim duab xim xim yog hauv qab no. Cov pa phem tseem ceeb qhia peb tias huab cua phem nyob hauv huab cua yog rau koj thaj tsam twg (teeb meem me me, nitrogen oxide, ozone, carbon monoxide, sulfur oxide). Cov pa phem no feem ntau yog los ntawm cov pa luam yeeb lossis cov tsheb tso tawm.

Tam sim no peb paub siv Air Visual API li cas, tom ntej no peb yuav xav tau yog lub platform dashboard los tso saib cov ntaub ntawv.

Kauj Ruam 3: Thawj Xeev

Lub Xeev Thawj
Lub Xeev Thawj
Lub Xeev Thawj
Lub Xeev Thawj

Peb xav kom tshaj tawm tag nrho peb cov ntaub ntawv huab cua mus rau huab kev pabcuam thiab muaj qhov kev pabcuam ntawd tig peb cov ntaub ntawv mus rau hauv lub dashboard zoo. Peb cov ntaub ntawv xav tau lub hom phiaj yog li peb yuav siv Thawj Lub Xeev raws li lub hom phiaj ntawd.

Sau npe rau Lub Xeev Thawj Tus Kheej

Mus rau https://iot.app.initialstate.com thiab tsim tus as khauj tshiab.

Nruab ISStreamer

Txhim Kho Thawj Lub Xeev Python module rau koj lub khoos phis tawj lossis Raspberry Pi. Ntawm qhov hais kom ua, khiav cov lus txib hauv qab no:

$ / curl -sSL https://iot.app.initialstate.com -o -| sudo tsoo

Ua qee Automagic

Tom qab koj khiav cov lus txib curl, koj yuav pom qee yam zoo ib yam li cov hauv qab no tso rau ntawm qhov screen:

$ / curl -sSL https://iot.app.initialstate.com -o -| sudo tsoo

Lo lus zais: Pib ISStreamer Python Kev teeb tsa yooj yim! Qhov no yuav siv ob peb feeb los teeb tsa, rub qee lub kas fes:) Tab sis tsis txhob hnov qab rov qab los, Kuv yuav muaj lus nug tom qab! Pom yooj yim_install: setuptools 1.1.6 Pom pip: pip 1.5.6 los ntawm /Library/Python/2.7/site-packages/pip-1.5.6- py2.7.egg (sej 2.7) pip loj version: 1 pip me me version: 5 ISStreamer pom, hloov kho… Yuav tsum tau hloov kho tshiab tas li: ISStreamer hauv /Library/Python/2.7/site-packages Tu tu… Koj puas xav tau automagically tau txais ib qho piv txwv tsab ntawv? [y/N] Koj xav khaws qhov piv txwv nyob qhov twg? [lub neej ntawd:./is_example.py] Thov xaiv qhov Xeev Lub Xeev app uas koj siv: 1. app.initialstate.com 2. [TSHIAB!] iot.app.initialstate.com Nkag mus xaiv 1 lossis 2: Nkag mus rau iot.app.initialstate.com tus neeg siv lub npe: Nkag mus rau iot.app.initialstate.com lo lus zais:

Thaum hais kom ua kom tau txais cov ntawv piv txwv, ntaus y. Qhov no yuav tsim cov ntawv xeem uas peb tuaj yeem ua haujlwm kom ntseeg tau tias peb tuaj yeem xa cov ntaub ntawv mus rau Lub Xeev Pib. Qhov txuas ntxiv tom ntej yuav nug qhov twg koj xav khaws cov piv txwv ntawv. Koj tuaj yeem xaiv hom kev cai hauv nroog lossis ntaus nkag mus lees txais qhov chaw nyob tsis raug. Thaum kawg, koj yuav raug nug tias Thawj Lub Xeev app twg koj tab tom siv. Yog tias koj nyuam qhuav tsim ib tus as khauj, xaiv qhov 2 ces nkag mus rau tus neeg siv lub npe thiab tus password. Tom qab qhov kev teeb tsa yuav ua tiav.

Nkag Nkag

Cia peb saib qhov piv txwv tsab ntawv uas tau tsim. $ nano is_example.py On kab 15, koj yuav pom kab uas pib nrog streamer = Streamer (bucket_…. Kab ntawv no tsim cov ntaub ntawv tshiab thoob lub npe “Python Stream Piv txwv” thiab cuam tshuam nrog koj tus lej nyiaj. access_key =”…” qhov ntsuas ntawm tib kab ntawv ntawd. Cov ntawv ntev thiab cov lej yog koj li Thawj Lub Xeev tus lej nkag mus. mus rau "kuv teeb tsa", koj yuav pom tus yuam sij nkag tau zoo nyob hauv qab ntawm nplooj ntawv hauv qab "Streaming Access Keys". qhia koj tus yuam sij rau leej twg).

Khiav Cov Piv Txwv Run cov ntawv xeem kom paub tseeb tias peb tuaj yeem tsim cov ntaub ntawv xa mus rau koj Tus Thawj Xib Fwb Nyiaj Txiag. Khiav cov hauv qab no hauv koj qhov kev hais kom ua:

$ python is_example.py

Piv txwv Cov Ntaub Ntawv

Rov qab mus rau koj lub Xeev Thawj tus lej hauv koj lub vev xaib. Ib lub thoob cov ntaub ntawv tshiab hu ua "Python Stream Piv txwv" yuav tsum tau tshwm nyob rau sab laug hauv koj lub thoob ntim khoom (koj yuav tau rov ua nplooj ntawv tshiab). Nyem rau ntawm lub thoob no los saib cov ntaub ntawv.

Tam sim no koj tau npaj los pib tshaj tawm cov ntaub ntawv tiag tiag los ntawm AirVisual API.

Kauj Ruam 4: Huab Cua Zoo Dashboard

Huab Cua Zoo Dashboard
Huab Cua Zoo Dashboard
Huab Cua Zoo Dashboard
Huab Cua Zoo Dashboard
Huab Cua Zoo Dashboard
Huab Cua Zoo Dashboard
Huab Cua Zoo Dashboard
Huab Cua Zoo Dashboard

Tam sim no rau qhov lom zem. Peb npaj tau pib siv AirVisual API los tsim cov huab cua huab cua zoo thiab khaws cov ntaub ntawv huab cua tsis zoo rau txhua qhov chaw peb xaiv. Tsab ntawv airquality.py no tsuas yog hu rau AirVisual API siv koj tus lej API thiab khaws cov ntaub ntawv qhia txog huab cua tam sim no. Nws tseem ntws cov ntaub ntawv ntawd mus rau koj Lub Xeev Tus Kheej Tus Kheej, uas yuav tso cai rau koj los tsim huab cua huab cua zoo.

Koj tuaj yeem nkag mus rau tsab ntawv los ntawm Github chaw cia khoom uas peb tau cloned ua ntej. Thawj qhov peb yuav tsum tau ua yog xyuas kom peb nyob hauv AirVisual directory:

$ cd airvisual

Los ntawm no, koj yuav tuaj yeem nkag mus rau cov ntaub ntawv nab nab uas peb yuav khiav los tsim peb cov huab cua zoo dashboard. Peb yuav tsum tau hloov qee cov ntawv ua ntej peb khiav nws. Txhawm rau qhib cov ntaub ntawv sej, siv nano hais kom qhib cov ntawv sau:

$ nano airquality.py

Thaum cov ntawv nyeem qhib, koj yuav pom cov hauv qab no nyob ze rau saum koj tsab ntawv:

# --------- Neeg Siv Chaw ---------

LATITUDE = "LAT" LONGITUDE = "LONG" AIRVISUAL_API_KEY = "AIR VISUAL API KEY" BUCKET_NAME = "Local Air Quality" BUCKET_KEY = "aq1" ACCESS_KEY = "INITIAL STATE ACCES KEY" MINUTES_BETWEENRE #-5 ------------------------------- ua tsaug

koj yuav tsum tau nkag mus rau koj qhov latitude, longitude, AirVisual API qhov tseem ceeb, thiab Thawj Lub Xeev nkag mus yuam sij. MINUTES_BETWEEN_READS qhov ntsuas yuav teev ntau npaum li cas koj cov ntawv yuav txheeb xyuas AirVisual API rau cov ntaub ntawv huab cua zoo. 5 feeb sijhawm txaus kom koj yuav tsis hla koj li 10, 000 API hu rau ib hlis txwv. Rau lub hom phiaj ntawm kev sim lub sijhawm luv, koj tuaj yeem teev qhov no rau 0.5 feeb. Thaum koj tau nkag mus rau koj cov kev txwv, txuag thiab tawm cov ntawv nkag los ntawm kev ntaus Control+X. Tam sim no koj tau npaj los khiav koj tsab ntawv:

$ python airquality.py

Yog tias xav tawm tsab ntawv no ua haujlwm tsis cuam tshuam rau lub sijhawm ntev, koj tuaj yeem siv qhov hais kom ua nohup (tsis muaj qhov khwb) raws li hauv qab no:

$ nohup nab hab sej airquality.py &

Thaum qhov no tau khiav mus rau Thawj Lub Xeev kom pom koj cov ntaub ntawv. Koj lub dashboard yuav tsum zoo ib yam li daim duab hauv qab no. Koj muaj koj qhov chaw nyob GPS, koj tus lej ntsuas huab cua zoo, thiab koj cov pa phem tseem ceeb.

Kuv qhov kev pom zoo yuav hloov koj li AQI tus nqi rau cov nplais ntsuas. Tsis tas li, txav cov vuas ncig thiab hloov qhov loj raws li xav tau. Yog tias koj siv qhov no rau kev teeb tsa lub dashboard, koj tuaj yeem txav cov no ncig kom haum raws li xav tau.

Yog tias koj txiav txim siab los ua koj li AQI tus nqi ntsuas, tom qab ntawd koj tuaj yeem teeb tsa qhov pib xim kom zoo ib yam li daim ntawv qhia huab cua zoo. Qhov no muab koj hloov tshiab tam sim ntawm qhov twg AQI tus nqi poob rau ntawm daim duab thaum koj txheeb xyuas koj lub dashboard. Koj tuaj yeem ntxiv cov duab keeb kwm yav dhau los rau koj lub dashboard kom muab nws cov ntsiab lus ntxiv.

Yog li koj muaj txhua yam koj xav tau los tsim cov huab cua zoo dashboard. Tab sis dab tsi yog tias koj xav ntxiv lossis ntxiv qhov no rau huab cua dashboard koj twb tau tsim? Yog tias yog qhov xwm txheej ces nyeem ntxiv!

Kauj Ruam 5: Ua Tag Nrho Huab Cua Dashboard

Ua Tag Nrho Huab Cua Huab Cua
Ua Tag Nrho Huab Cua Huab Cua

Puas yog cov ntaub ntawv huab cua tsis zoo rau koj? Zoo muaj ntau txoj hauv kev xaiv ntxiv rau koj lub dashboard lossis tso cov ntaub ntawv no rau hauv huab cua dashboard uas koj twb muaj lawm!

Tshaj Tawm Huab Cua & Huab Cua Zoo Rau Hauv Ib Lub Dashboard

Yog tias koj twb tau ua tiav peb DarkSky API lossis Hyper-Local Weather Dashboard project, koj tuaj yeem ntxiv cov ntaub ntawv huab cua no zoo rau koj lub dashboard uas twb muaj lawm. Nws yooj yim zoo nkauj, txhua yam koj yuav tsum tau ua yog hloov koj qhov tsis nyob hauv cov ntawv huab cua kom muaj tib lub npe thoob, thoob lub thoob, thiab nkag tus yuam sij zoo li koj tau siv rau koj li huab cua dashboard. Qhov no yuav tso cai cov ntaub ntawv xa mus rau tib lub dashboard. Tam sim no koj yuav muaj tag nrho huab cua dashboard!

Muaj Koj Cov Ntaub Ntawv Huab Cua Python Hu Xov Tooj Cua Kom Zoo Python Cov Ntaub Ntawv

Lwm qhov kev xaiv yog tias koj tsis xav khiav ob txoj haujlwm sib cais yog tso cov ntaub ntawv huab cua zoo ua nab npawb hauv cov phiaj xwm huab cua. Muaj cov phiaj xwm huab cua ua ntaub ntawv xov tooj hu xov tooj mus rau huab cua huab cua kom nws ua haujlwm thaum koj cov ntaub ntawv huab cua tau khiav. Ib zaug ntxiv, nco ntsoov muab tib lub npe thoob, tus yuam sij thoob, thiab nkag tus yuam sij kom lawv khiav mus rau tib lub dashboard.

Tsim Ib Cov Ntaub Ntawv nrog Huab Cua & Huab Cua Zoo

Thiab yog tias koj xav tias muaj lub siab tawv tiag, koj tuaj yeem tso qee cov cai huab cua zoo rau hauv koj cov ntawv huab cua nab thiab tsuas muaj cov ntawv nkaus xwb los khiav. Nws yuav xav tau kev siv zog me ntsis ntau dua li ob qho kev xaiv tab sis nws ua rau txoj haujlwm yooj yim dua.

Kwj Cov Lus Qhia Ntxiv los ntawm AirVisual API

Raws li koj tau pom thaum peb tau hu mus rau AirVisual API, nws muaj cov ntaub ntawv ntau dua li tsuas yog huab cua zoo. Nws tseem muab qhov ntsuas kub, av noo, cua nrawm, cua qhia, thiab huab cua siab. Peb tuaj yeem xa cov ntaub ntawv ntawd mus rau Thawj Lub Xeev tib txoj hauv kev uas peb xa cov txiaj ntsig huab cua ntsuas tus nqi thiab cov pa phem tseem ceeb. Nws tsuas yog xav kom koj sau ob peb ntxiv yog cov lus hais.

Pom zoo: