Government Open Date Platform
https://data.gov.tw
Open Data EPA
https://opendata.epa.gov.tw
Open data file typ: csv, txt, JSON, XML
Two view of JSON file:
http://opendata.epa.gov.tw/ws/Data/POP00049/?$orderby=M_Time%20desc&$skip=0&$top=1000&format=json
<Please open this url by FireFox or JSON Editor Online website>
0 | |
Epb | "嘉義縣" |
CNO | "Q7600375" |
Abbr | "南亞塑膠工業股份有限公司嘉義廠" |
PolNo | "P001" |
ItemDesc | "氮氧化物監測設施十五分鐘數據紀錄值" |
Item | "923 " |
M_Time | "2017-11-21 18:45:00" |
M_Val | "83.75" |
Unit | "ppm " |
Code2 | "10" |
Code2Desc | "正常排放量測值" |
Std | "250" |
Std_s | "電力設施空氣污染物排放標準" |
{"Epb":"嘉義縣","CNO":"Q7600375","Abbr":"南亞塑膠工業股份有限公司嘉義廠","PolNo":"P001","ItemDesc":"氮氧化物監測設施十五分鐘數據紀錄值","Item":"923 ","M_Time":"2017-11-21 18:45:00","M_Val":"83.75","Unit":"ppm ","Code2":"10","Code2Desc":"正常排放量測值","Std":"250","Std_s":"電力設施空氣污染物排放標準"}
可以看到兩種結構組成:
物件(object)用大括號 { },分號隔開
例如:{"subject":"Math","score":80}
陣列(array)用中括號 [ ] ,分號:隔開
例如:[0,4,5,2,7,8,3]
JSON Editor Online
http://www.jsoneditoronline.org
open this url
https://opendata.epa.gov.tw/webapi/api/rest/datastore/355000000I-000207?offset=0&limit=1000
Maxde-MacBook-Pro:taipei_PM2.5 max$ cat taipei_PM2.5_ver1.py
#!/usr/bin/python
#-*- coding: UTF-8 -*-
import urllib, json
url = "http://opendata2.epa.gov.tw/AQI.json"
response = urllib.urlopen(url)
data_list = json.loads(response.read())
print "城市:"
print data_list[10]['County']
print "地區:"
print data_list[10]['SiteName']
print "PM2.5指數:"
print data_list[10]['PM2.5']
print "狀態:"
print data_list[10]['Status']
======
接RGB LED 進行判斷式 空氣品質->亮紅藍綠燈...
佈線圖在ppt
#!/usr/bin/python
#-*- coding: UTF-8 -*-
import urllib, json, pyfirmata
from time import sleep
#ser = serial.Serial("COM4",9600)
port = '/dev/cu.usbmodem1411'
pin1 = 11 #R
pin2 = 10 #G
pin3 = 8 #B
board = pyfirmata.Arduino(port)
sleep(5)
url = "http://opendata2.epa.gov.tw/AQI.json"
response = urllib.urlopen(url)
data_list = json.loads(response.read())
print "城市:"
print data_list[3]['County']
print "地區:"
print data_list[3]['SiteName']
print "PM2.5指數:"
print data_list[3]['PM2.5']
print "狀態:"
print data_list[3]['Status']
a = int(data_list[3]['PM2.5'])
print a
#PM2.5介於0~5亮綠燈
if a>0 and a<5:
board.digital[pin2].write(1)
print 'good'
#PM2.5介於5~10亮藍燈
elif a>=5 and a<=10:
board.digital[pin3].write(1)
print 'bad'
#PM2.5大於10亮紅燈
elif a>10:
board.digital[pin1].write(1)
print 'warning'
else:
board.digital[pin1].write(0)
board.digital[pin2].write(0)
board.digital[pin3].write(0)
成功!!