參考資料
https://tutorials.webduino.io/zh-tw/docs/socket/useful/node-red.html
需要用到的節點是
http request
,先把 injec
t 改成 timestam
p,點一下就啟動流程,然後把 http request
放在中間 把 ,function
節點放在 http request
和 debug
的中間,debug
放在最後。
首先我們把名為
inject
的節點放進去,當我們點選這個節點前面的方形區域,它就會送出對應的內容到下一個節點。
在
http request
結點上面點兩下,Method 設定 GET,URL 就填入氣象局空污的 JSON 網址。http request
節點,將資料格式改為 a parsed JSON object
。
編輯
function
,篩選出大里的 PM2.5 數值與 AQI數值,存在 msg 的 pm25 屬性裡。
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;
debug
也要改為顯示 msg.pm25 的值。
將程式Export如下
需留意中文是否會產生亂碼現象???
[{"id":"b8c51aeb.89ef28","type":"inject","z":"17bb63ed.68b25c","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":100,"y":80,"wires":[["331563a5.f737ac"]]},{"id":"331563a5.f737ac","type":"http request","z":"17bb63ed.68b25c","name":"","method":"GET","ret":"obj","url":"http://opendata.epa.gov.tw/webapi/Data/REWIQA/?$orderby=SiteName&$skip=0&$top=1000&format=json","tls":"","x":270,"y":80,"wires":[["97d429c0.720578"]]},{"id":"de56df8c.abd26","type":"debug","z":"17bb63ed.68b25c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"pm25","x":510,"y":200,"wires":[]},{"id":"97d429c0.720578","type":"function","z":"17bb63ed.68b25c","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,"x":330,"y":200,"wires":[["de56df8c.abd26"]]}]
沒有留言:
張貼留言