2021年8月13日 星期五

Node-Red 讀取 Opendata PM2.5 與 AQI指數

每小時提供各測站之空氣品質指標(AQI),原始資料版本公告於空氣品質監測網https://airtw.epa.gov.tw

到 https://data.gov.tw/dataset/40448 擷取空氣品質指標(AQI)-JSON檔案


程式方塊flow 如下



共有7個flow

1.時間戳 觸動flow 工作

2. http請求 :  GET  空氣品質指標(AQI)-JSON http://opendata.epa.gov.tw/webapi/Data/REWIQA/?$orderby=SiteName&$skip=0&$top=1000&format=json 

3. 擷取json的 msg.payload.records 的紀錄內容


function flow 設定如下

msg.payload= msg.payload.records

return msg;


最後擷取出84筆資料

4. 再一次=擷取84筆紀錄中的sitename="XX" 的PM2.5與AQI值


function flow 設定如下

var a = msg.payload;

a.forEach(function(e,i){

    if(e.SiteName=='大里')

    {

        msg.pm25 = '大里 PM2.5='+ e['PM2.5'];

        msg.pm25 =msg.pm25 + ',  AQI='+ e['AQI'];

    }

});

return msg;


5.  3個msg.payload 展示所得到的資料,作為debug用


Node-Red程式如下

[{"id":"b8c51aeb.89ef28","type":"inject","z":"2139582e.c1e758","name":"","repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":210,"y":980,"wires":[["331563a5.f737ac"]]},{"id":"331563a5.f737ac","type":"http request","z":"2139582e.c1e758","name":"","method":"GET","ret":"obj","paytoqs":"ignore","url":"https://data.epa.gov.tw/api/v1/aqx_p_432?limit=1000&api_key=9be7b239-557b-4c10-9775-78cadfc555e9&sort=ImportDate%20desc&format=json","tls":"","persist":false,"proxy":"","authType":"","x":380,"y":980,"wires":[["6c7e26f6.0cfa88","101403b2.cc745c"]]},{"id":"de56df8c.abd26","type":"debug","z":"2139582e.c1e758","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"pm25","x":690,"y":1100,"wires":[]},{"id":"97d429c0.720578","type":"function","z":"2139582e.c1e758","name":"","func":"var a = msg.payload;\na.forEach(function(e,i){\n    if(e.SiteName=='大里')\n    {\n        msg.pm25 = '大里 PM2.5='+ e['PM2.5'];\n        msg.pm25 =msg.pm25 + ',  AQI='+ e['AQI'];\n    }\n});\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":550,"y":1100,"wires":[["de56df8c.abd26"]]},{"id":"6c7e26f6.0cfa88","type":"debug","z":"2139582e.c1e758","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":590,"y":980,"wires":[]},{"id":"101403b2.cc745c","type":"function","z":"2139582e.c1e758","name":"","func":"msg.payload= msg.payload.records\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":510,"y":1020,"wires":[["8dcc7321.b964b","97d429c0.720578"]]},{"id":"8dcc7321.b964b","type":"debug","z":"2139582e.c1e758","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":690,"y":1020,"wires":[]}]

沒有留言:

張貼留言

2024_09 作業3 以Node-Red 為主

 2024_09 作業3  (以Node-Red 為主  Arduino 可能需要配合修改 ) Arduino 可能需要修改的部分 1)mqtt broker  2) 主題Topic (發行 接收) 3) WIFI ssid , password const char br...