2021年1月2日 星期六

Node-Red 讀取opendata PM2.5

 Node-Red 讀取opendata


1)  https://data.gov.tw/dataset/34827 選json 檔案




https://data.epa.gov.tw/api/v1/aqx_p_02?limit=1000&api_key=9be7b239-557b-4c10-9775-78cadfc555e9&format=json


2)  選擇地點 例如 大里



        https://www.ntepb.gov.tw/sub/content/index.aspx?Parser=1,17,511,494,496

3) Node-Red 的 flow



GET

https://data.epa.gov.tw/api/v1/aqx_p_02?limit=1000&api_key=9be7b239-557b-4c10-9775-78cadfc555e9&format=json

a) 先找出JSON  object 資料

2021/1/2 下午3:47:59node: 560857db.b58bc8msg.payload : Object

object

2) 擷取 records 欄位



var PM25=msg.payload.records;

msg.payload=PM25;

return msg;


msg.payload : array[83]

array[83]

[20 … 29]

3) 擷取 大里區的 PM25 , DataCreationDate 欄位

var PM25=msg.payload;

//Array.from(PM25).forEach(function(e,i){

PM25.forEach(function(e,i){

    if(e['Site']=='大里'){        

        msg.PM25 = e['PM25'];                    

        msg.PublishTime=e['DataCreationDate'];       

}

});

return msg;


4) 擷取 大里區的 PM25

msg.payload=msg.PM25;

return msg;

2021/1/2 下午3:48:01node: 929873be.36b22msg.PM25 : string[2]

"14"

5) 擷取 大里區的 PublishTime

msg.payload=msg.PublishTime;

return msg;


2021/1/2 下午3:48:02node: 582734d2.8e4bacmsg.PublishTime : string[16]
"2021-01-02 15:00"




Node-Red程式

[{"id":"78e9c974.cd5fc8","type":"inject","z":"b8a8c5f1.0e8598","name":"Click","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":90,"y":100,"wires":[["12dac5f.924a13a"]]},{"id":"f8ca9503.f81d98","type":"debug","z":"b8a8c5f1.0e8598","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":670,"y":100,"wires":[]},{"id":"96cc082e.475cd8","type":"ui_switch","z":"b8a8c5f1.0e8598","name":"","label":"啟動開關","tooltip":"","group":"9d7c6798.9fc5e8","order":61,"width":0,"height":0,"passthru":true,"decouple":"false","topic":"","style":"","onvalue":"true","onvalueType":"bool","onicon":"","oncolor":"","offvalue":"false","offvalueType":"bool","officon":"","offcolor":"","x":100,"y":180,"wires":[["12dac5f.924a13a"]]},{"id":"c4678954.fddf28","type":"ui_gauge","z":"b8a8c5f1.0e8598","name":"PM2.5","group":"9d7c6798.9fc5e8","order":62,"width":0,"height":0,"gtype":"gage","title":"大里區 細懸浮微粒(PM2.5)指標","label":"units","format":"{{value}}","min":0,"max":"71","colors":["#00b500","#e6e600","#ca3838"],"seg1":"35","seg2":"59","x":870,"y":220,"wires":[]},{"id":"e7d0696b.0fb278","type":"ui_text","z":"b8a8c5f1.0e8598","group":"9d7c6798.9fc5e8","order":63,"width":0,"height":0,"name":"PM2.5","label":"發行時間","format":"{{msg.payload}}","layout":"row-spread","x":870,"y":300,"wires":[]},{"id":"c9b469d4.3d7ba8","type":"function","z":"b8a8c5f1.0e8598","name":"擷取欄位 records","func":"var PM25=msg.payload.records;\nmsg.payload=PM25;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":410,"y":140,"wires":[["f8ca9503.f81d98","64596b23.9fa034"]]},{"id":"12dac5f.924a13a","type":"http request","z":"b8a8c5f1.0e8598","name":"","method":"GET","ret":"obj","paytoqs":"ignore","url":"https://data.epa.gov.tw/api/v1/aqx_p_02?limit=1000&api_key=9be7b239-557b-4c10-9775-78cadfc555e9&format=json","tls":"","persist":false,"proxy":"","authType":"","x":240,"y":140,"wires":[["c9b469d4.3d7ba8","560857db.b58bc8"]]},{"id":"560857db.b58bc8","type":"debug","z":"b8a8c5f1.0e8598","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":390,"y":80,"wires":[]},{"id":"64596b23.9fa034","type":"function","z":"b8a8c5f1.0e8598","name":"找出大里地區 PM2.5  , PublishTime","func":"var PM25=msg.payload;\n//Array.from(PM25).forEach(function(e,i){\nPM25.forEach(function(e,i){\n    if(e['Site']=='大里'){        \n        msg.PM25 = e['PM25'];                    \n        msg.PublishTime=e['DataCreationDate'];       \n}\n});\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":420,"y":260,"wires":[["929873be.36b22","582734d2.8e4bac","b886630d.9faa","423bd5cc.ae74bc"]]},{"id":"929873be.36b22","type":"debug","z":"b8a8c5f1.0e8598","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"PM25","targetType":"msg","statusVal":"","statusType":"auto","x":690,"y":160,"wires":[]},{"id":"582734d2.8e4bac","type":"debug","z":"b8a8c5f1.0e8598","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"PublishTime","targetType":"msg","statusVal":"","statusType":"auto","x":710,"y":380,"wires":[]},{"id":"b886630d.9faa","type":"function","z":"b8a8c5f1.0e8598","name":"PM2.5資料","func":"msg.payload=msg.PM25;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":690,"y":220,"wires":[["c4678954.fddf28","e879232.a0938e"]]},{"id":"423bd5cc.ae74bc","type":"function","z":"b8a8c5f1.0e8598","name":"發行時間","func":"msg.payload=msg.PublishTime;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":700,"y":300,"wires":[["e7d0696b.0fb278","82709722.643368"]]},{"id":"e879232.a0938e","type":"debug","z":"b8a8c5f1.0e8598","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":890,"y":260,"wires":[]},{"id":"82709722.643368","type":"debug","z":"b8a8c5f1.0e8598","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":910,"y":340,"wires":[]},{"id":"9d7c6798.9fc5e8","type":"ui_group","name":"Main","tab":"89e51ea1.a64af","order":1,"disp":true,"width":"6","collapse":false},{"id":"89e51ea1.a64af","type":"ui_tab","name":"Dashboard","icon":"dashboard"}]

沒有留言:

張貼留言

2024_09 作業3 以Node-Red 為主

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