2021年8月15日 星期日

Node-Red 讀取 Opendata PM2.5 與 AQI指數 並展示於UI 介面上

 Node-Red 讀取 Opendata PM2.5 與 AQI指數 並展示於UI 介面上

到opendata 空氣品質指標(AQI) https://data.gov.tw/dataset/40448

資料資源下載網址
  空氣品質指標(AQI)-JSON  請求AQI 與 PM2.5 資料 並展示於UI介面上




Flow 說明

編號

Flow名稱

功能說明

1

時間戳 (inject)

20秒出觸發一次事件



2

http請求

(http request)

GET方式

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

返回: JSON對象





3

函數function

func1

擷取jsonrecords欄位 其他的欄位略去

msg.payload= msg.payload.records

return msg;



records欄位

4

函數function

Func2

 

var PM25,Status,PublishTime;

 

Array.from(msg.payload).forEach(function(e,i){   

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

              PM25 = {payload:Number(e['PM2.5'])};                   

              Status={payload:e['Status']};       

              PublishTime={payload:e['PublishTime']};      

 }

});

 

return [PM25,Status,PublishTime];

 

結果如下

msg.pm25 : string[19]

"大里 PM2.5=2, AQI=23"

5

Dashboardgauge

[PM25,Status,PublishTime] 展示於UI (127.0.0.1:1881/ui)





6

UI 畫面

127.0.0.1:1880/ui

選擇Weather 

(若是有其他UI)



 

flow 如下

[{"id":"d5924d54.f83c5","type":"inject","z":"e77ab25e.410ae","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"20","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":100,"y":280,"wires":[["ec7fe911.1e39e8"]]},{"id":"c66cf4c8.bd6fb8","type":"function","z":"e77ab25e.410ae","name":"func2","func":"var PM25,Status,PublishTime;\n\nArray.from(msg.payload).forEach(function(e,i){    \n          if(e.SiteName=='大里'){        \n              PM25 = {payload:Number(e['PM2.5'])};                    \n              Status={payload:e['Status']};        \n              PublishTime={payload:e['PublishTime']};       \n }\n});\n\nreturn [PM25,Status,PublishTime];\n","outputs":3,"noerr":0,"initialize":"","finalize":"","x":390,"y":360,"wires":[["a2967363.2eeff","a1b76035.83887"],["2dc2d0c7.29192"],["96e772af.1d6fe"]]},{"id":"a2967363.2eeff","type":"debug","z":"e77ab25e.410ae","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":590,"y":300,"wires":[]},{"id":"ec7fe911.1e39e8","type":"http request","z":"e77ab25e.410ae","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":240,"y":280,"wires":[["e5a317a6.009a58","6621269c.675078"]]},{"id":"2dc2d0c7.29192","type":"ui_text","z":"e77ab25e.410ae","group":"91d3cfc5.c30bb","order":1,"width":0,"height":0,"name":"","label":"空氣品質","format":"{{msg.payload}}","layout":"row-left","x":580,"y":380,"wires":[]},{"id":"96e772af.1d6fe","type":"ui_text","z":"e77ab25e.410ae","group":"91d3cfc5.c30bb","order":2,"width":0,"height":0,"name":"","label":"發佈時間","format":"{{msg.payload}}","layout":"row-left","x":580,"y":420,"wires":[]},{"id":"a1b76035.83887","type":"ui_gauge","z":"e77ab25e.410ae","name":"","group":"91d3cfc5.c30bb","order":0,"width":0,"height":0,"gtype":"gage","title":"細懸浮微粒PM2.5","label":"units","format":"{{value}}","min":0,"max":"100","colors":["#00b500","#e6e600","#ca3838"],"seg1":"","seg2":"","x":610,"y":340,"wires":[]},{"id":"e5a317a6.009a58","type":"debug","z":"e77ab25e.410ae","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":410,"y":260,"wires":[]},{"id":"6621269c.675078","type":"function","z":"e77ab25e.410ae","name":"func1","func":"msg.payload= msg.payload.records\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":230,"y":360,"wires":[["c66cf4c8.bd6fb8"]]},{"id":"91d3cfc5.c30bb","type":"ui_group","name":"IAQI","tab":"2e8eb9a2.9f45f6","order":4,"disp":true,"width":"12","collapse":false},{"id":"2e8eb9a2.9f45f6","type":"ui_tab","name":"Weather","icon":"Dashboard","disabled":false,"hidden":false}]

沒有留言:

張貼留言

113 學年度第 1 學期 RFID應用課程 Arduino程式

113 學年度第 1 學期 RFID應用課程 Arduino程式 https://www.mediafire.com/file/zr0h0p3iosq12jw/MFRC522+(2).7z/file 內含修改過後的 MFRC522 程式庫 (原程式有錯誤) //定義MFRC522...