2019年11月19日 星期二

Node-RED 入門 與 OpenData PM2.5 AQI

Node-RED 入門 與  OpenData  PM2.5  AQI


1)  inject 與 debug 
    
      手動 與 自動(定期) interval 

2) Http Request

http://opendata2.epa.gov.tw/AQI.json



http://opendata.epa.gov.tw/webapi/Data/REWIQA/?$orderby=SiteName&$skip=0&$top=1000&format=json



https://data.gov.tw/dataset/6076


JSON 下載: 




定期會改變 所以需到https://data.gov.tw/dataset/6076 update JSON


下載的UVI Data

[{"County":"\u82b1\u84ee\u7e23","PublishAgency":"\u4e2d\u592e\u6c23\u8c61\u5c40","PublishTime":"2019-10-31 12:00","SiteName":"\u82b1\u84ee","UVI":"5.14","WGS84Lat":"23,58,30","WGS84Lon":"121,36,48"},{"County":"\u9023\u6c5f\u7e23","PublishAgency":"\u4e2d\u592e\u6c23\u8c61\u5c40","PublishTime":"2019-10-31 12:00","SiteName":"\u99ac\u7956","UVI":"3.59","WGS84Lat":"26,10,09","WGS84Lon":"119,55,24"},{"County":"\u9ad8\u96c4\u5e02","PublishAgency":"\u4e2d\u592e\u6c23\u8c61\u5c40","PublishTime":"2019-10-31 12:00","SiteName":"\u9ad8\u96c4","UVI":"6.64","WGS84Lat":"22,33,58","WGS84Lon":"120,18,57"},{"County":"\u5357\u6295\u7e23","PublishAgency":"\u4e2d\u592e\u6c23\u8c61\u5c40","PublishTime":"2019-10-31 12:00","SiteName":"\u7389\u5c71","UVI":"3.79","WGS84Lat":"23,29,15","WGS84Lon":"120,57,34"},{"County":"\u81fa\u5357\u5e02","PublishAgency":"\u4e2d\u592e\u6c23\u8c61\u5c40","PublishTime":"2019-10-31 12:00","SiteName":"\u81fa\u5357","UVI":"6.17","WGS84Lat":"22,59,36","WGS84Lon":"120,12,17"},{"County":"\u65b0\u7af9\u7e23","PublishAgency":"\u4e2d\u592e\u6c23\u8c61\u5c40","PublishTime":"2019-10-31 12:00","SiteName":"\u65b0\u7af9","UVI":"2.48","WGS84Lat":"24,49,40","WGS84Lon":"121,00,51"},{"County":"\u81fa\u5317\u5e02","PublishAgency":"\u4e2d\u592e\u6c23\u8c61\u5c40","PublishTime":"2019-10-31 12:00","SiteName":"\u978d\u90e8","UVI":"0.77","WGS84Lat":"25,10,57","WGS84Lon":"121,31,47"},{"County":"\u5c4f\u6771\u7e23","PublishAgency":"\u4e2d\u592e\u6c23\u8c61\u5c40","PublishTime":"2019-10-31 12:00","SiteName":"\u6046\u6625","UVI":"4.86","WGS84Lat":"22,00,14","WGS84Lon":"120,44,47"},{"County":"\u81fa\u5317\u5e02","PublishAgency":"\u4e2d\u592e\u6c23\u8c61\u5c40","PublishTime":"2019-10-31 12:00","SiteName":"\u81fa\u5317","UVI":"2.95","WGS84Lat":"25,02,16","WGS84Lon":"121,30,53"},{"County":"\u81fa\u6771\u7e23","PublishAgency":"\u4e2d\u592e\u6c23\u8c61\u5c40","PublishTime":"2019-10-31 12:00","SiteName":"\u6210\u529f","UVI":"7.35","WGS84Lat":"23,05,51","WGS84Lon":"121,22,24"},{"County":"\u57fa\u9686\u5e02","PublishAgency":"\u4e2d\u592e\u6c23\u8c61\u5c40","PublishTime":"2019-10-31 12:00","SiteName":"\u57fa\u9686","UVI":"0.25","WGS84Lat":"25,07,60","WGS84Lon":"121,44,26"},{"County":"\u6843\u5712\u5e02","PublishAgency":"\u4e2d\u592e\u6c23\u8c61\u5c40","PublishTime":"2019-10-31 12:00","SiteName":"\u65b0\u5c4b","UVI":"5.45","WGS84Lat":"25,00,24","WGS84Lon":"121,02,51"},{"County":"\u81fa\u6771\u7e23","PublishAgency":"\u4e2d\u592e\u6c23\u8c61\u5c40","PublishTime":"2019-10-31 12:00","SiteName":"\u862d\u5dbc","UVI":"2.88","WGS84Lat":"22,02,13","WGS84Lon":"121,33,30"},{"County":"\u81fa\u6771\u7e23","PublishAgency":"\u4e2d\u592e\u6c23\u8c61\u5c40","PublishTime":"2019-10-31 12:00","SiteName":"\u81fa\u6771","UVI":"7.00","WGS84Lat":"22,45,08","WGS84Lon":"121,09,17"},{"County":"\u5357\u6295\u7e23","PublishAgency":"\u4e2d\u592e\u6c23\u8c61\u5c40","PublishTime":"2019-10-31 12:00","SiteName":"\u65e5\u6708\u6f6d","UVI":"3.78","WGS84Lat":"23,52,53","WGS84Lon":"120,54,29"},{"County":"\u91d1\u9580\u7e23","PublishAgency":"\u4e2d\u592e\u6c23\u8c61\u5c40","PublishTime":"2019-10-31 12:00","SiteName":"\u91d1\u9580","UVI":"5.52","WGS84Lat":"24,24,26","WGS84Lon":"118,17,21"},{"County":"\u5b9c\u862d\u7e23","PublishAgency":"\u4e2d\u592e\u6c23\u8c61\u5c40","PublishTime":"2019-10-31 12:00","SiteName":"\u5b9c\u862d","UVI":"0.42","WGS84Lat":"24,45,50","WGS84Lon":"121,45,24"},{"County":"\u6f8e\u6e56\u7e23","PublishAgency":"\u4e2d\u592e\u6c23\u8c61\u5c40","PublishTime":"2019-10-31 12:00","SiteName":"\u6f8e\u6e56","UVI":"3.80","WGS84Lat":"23,33,56","WGS84Lon":"119,33,47"},{"County":"\u81fa\u4e2d\u5e02","PublishAgency":"\u4e2d\u592e\u6c23\u8c61\u5c40","PublishTime":"2019-10-31 12:00","SiteName":"\u81fa\u4e2d","UVI":"5.66","WGS84Lat":"24,08,45","WGS84Lon":"120,41,03"},{"County":"\u5609\u7fa9\u5e02","PublishAgency":"\u4e2d\u592e\u6c23\u8c61\u5c40","PublishTime":"2019-10-31 12:00","SiteName":"\u5609\u7fa9","UVI":"6.29","WGS84Lat":"23,29,45","WGS84Lon":"120,25,58"},{"County":"\u5c4f\u6771\u7e23","PublishAgency":"\u74b0\u5883\u4fdd\u8b77\u7f72","PublishTime":"2019-10-31 12:00","SiteName":"\u5c4f\u6771","UVI":"6","WGS84Lat":"22,40,23.09","WGS84Lon":"120,29,16.92"},{"County":"\u9ad8\u96c4\u5e02","PublishAgency":"\u74b0\u5883\u4fdd\u8b77\u7f72","PublishTime":"2019-10-31 12:00","SiteName":"\u6a4b\u982d","UVI":"8","WGS84Lat":"22,45,27.02","WGS84Lon":"120,18,20.48"},{"County":"\u81fa\u5357\u5e02","PublishAgency":"\u74b0\u5883\u4fdd\u8b77\u7f72","PublishTime":"2019-10-31 12:00","SiteName":"\u65b0\u71df","UVI":"","WGS84Lat":"23,18,20.28","WGS84Lon":"120,19,2.10"},{"County":"\u5609\u7fa9\u7e23","PublishAgency":"\u74b0\u5883\u4fdd\u8b77\u7f72","PublishTime":"2019-10-31 12:00","SiteName":"\u6734\u5b50","UVI":"7","WGS84Lat":"23,28,1.64","WGS84Lon":"120,14,52.12"},{"County":"\u5609\u7fa9\u7e23","PublishAgency":"\u74b0\u5883\u4fdd\u8b77\u7f72","PublishTime":"2019-10-31 12:00","SiteName":"\u5854\u5854\u52a0","UVI":"9","WGS84Lat":"23,28,14.19","WGS84Lon":"120,52,50.06"},{"County":"\u5609\u7fa9\u7e23","PublishAgency":"\u74b0\u5883\u4fdd\u8b77\u7f72","PublishTime":"2019-10-31 12:00","SiteName":"\u963f\u91cc\u5c71","UVI":"3","WGS84Lat":"23,30,30.82","WGS84Lon":"120,48,05.02"},{"County":"\u96f2\u6797\u7e23","PublishAgency":"\u74b0\u5883\u4fdd\u8b77\u7f72","PublishTime":"2019-10-31 12:00","SiteName":"\u6597\u516d","UVI":"5","WGS84Lat":"23,42,42.67","WGS84Lon":"120,32,41.98"},{"County":"\u5357\u6295\u7e23","PublishAgency":"\u74b0\u5883\u4fdd\u8b77\u7f72","PublishTime":"2019-10-31 12:00","SiteName":"\u5357\u6295","UVI":"6","WGS84Lat":"23,54,46.8","WGS84Lon":"120,41,7.1"},{"County":"\u5f70\u5316\u7e23","PublishAgency":"\u74b0\u5883\u4fdd\u8b77\u7f72","PublishTime":"2019-10-31 12:00","SiteName":"\u5f70\u5316","UVI":"6","WGS84Lat":"24,03,57.60","WGS84Lon":"120,32,29.47"},{"County":"\u81fa\u4e2d\u5e02","PublishAgency":"\u74b0\u5883\u4fdd\u8b77\u7f72","PublishTime":"2019-10-31 12:00","SiteName":"\u6c99\u9e7f","UVI":"7","WGS84Lat":"24,13,32.26","WGS84Lon":"120,34,7.66"},{"County":"\u82d7\u6817\u7e23","PublishAgency":"\u74b0\u5883\u4fdd\u8b77\u7f72","PublishTime":"2019-10-31 12:00","SiteName":"\u82d7\u6817","UVI":"6","WGS84Lat":"24,33,54.97","WGS84Lon":"120,49,12.72"},{"County":"\u6843\u5712\u5e02","PublishAgency":"\u74b0\u5883\u4fdd\u8b77\u7f72","PublishTime":"2019-10-31 12:00","SiteName":"\u6843\u5712","UVI":"3","WGS84Lat":"24,59,41.02","WGS84Lon":"121,18,17.83"},{"County":"\u65b0\u5317\u5e02","PublishAgency":"\u74b0\u5883\u4fdd\u8b77\u7f72","PublishTime":"2019-10-31 12:00","SiteName":"\u677f\u6a4b","UVI":"1","WGS84Lat":"25,00,46.7","WGS84Lon":"121,27,31.2"},{"County":"\u65b0\u5317\u5e02","PublishAgency":"\u74b0\u5883\u4fdd\u8b77\u7f72","PublishTime":"2019-10-31 12:00","SiteName":"\u6de1\u6c34","UVI":"1","WGS84Lat":"25,09,52.20","WGS84Lon":"121,26,57.26"}]


3)  PM2.5 的function 

var a = msg.payload;
var date = new Date();
var h = date.getHours();
var m = date.getMinutes();
var s = date.getSeconds();
if(h<10){
    h = '0'+h;
}
if(m<10){
    m = '0' + m;
}
if(s<10){
    s = '0' + s;
}
a.forEach(function(e,i){
    if(e.SiteName=='大里'){
        msg.pm25 ='大里 PM2.5 在(' + h + ':' + m + ':' + s + ') = '+ e['PM2.5'];
    }
});

return msg;


4) UVI的function 

var a = msg.payload;

a.forEach(function(e,i){
    if(e.SiteName=='臺中'){
        msg.uvi ='臺中 UVI 在'+e['PublishTime']+'為' + e['UVI'];
    }
});

return msg;


5) 整個Node-RED 式碼

[{"id":"85893aa9.ac4e08","type":"inject","z":"fb30592f.092358","name":"","topic":"","payload":"Hello","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":230,"y":60,"wires":[["c1203e8c.d2aae"]]},{"id":"8eebbd89.f4f04","type":"inject","z":"fb30592f.092358","name":"","topic":"","payload":"勤益科大","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":220,"y":120,"wires":[["c1203e8c.d2aae"]]},{"id":"c1203e8c.d2aae","type":"debug","z":"fb30592f.092358","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":450,"y":60,"wires":[]},{"id":"6a57fad7.b1b094","type":"inject","z":"fb30592f.092358","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":220,"y":160,"wires":[["90f857c2.b72f58"]]},{"id":"8cb344fb.f45a88","type":"debug","z":"fb30592f.092358","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":590,"y":160,"wires":[]},{"id":"90f857c2.b72f58","type":"http request","z":"fb30592f.092358","name":"","method":"GET","ret":"txt","url":"http://opendata2.epa.gov.tw/AQI.json","tls":"","x":370,"y":160,"wires":[["8cb344fb.f45a88"]]},{"id":"23a04ac7.310a86","type":"inject","z":"fb30592f.092358","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":230,"y":566,"wires":[["4c85bc21.181eb4","4af9284c.6ed928"]]},{"id":"4c85bc21.181eb4","type":"http request","z":"fb30592f.092358","name":"","method":"GET","ret":"obj","url":"http://opendata2.epa.gov.tw/AQI.json","tls":"","x":400,"y":506,"wires":[["4e955892.850a48"]]},{"id":"4af9284c.6ed928","type":"http request","z":"fb30592f.092358","name":"","method":"GET","ret":"obj","url":"https://quality.data.gov.tw/dq_download_json.php?nid=6076&md5_url=c8abb41c2601d90af0635fc7b845048e","tls":"","x":400,"y":606,"wires":[["711f40eb.7544e"]]},{"id":"4e955892.850a48","type":"function","z":"fb30592f.092358","name":"PM2.5","func":"var a = msg.payload;\nvar date = new Date();\nvar h = date.getHours();\nvar m = date.getMinutes();\nvar s = date.getSeconds();\nif(h<10){\n    h = '0'+h;\n}\nif(m<10){\n    m = '0' + m;\n}\nif(s<10){\n    s = '0' + s;\n}\na.forEach(function(e,i){\n    if(e.SiteName=='大里'){\n        msg.pm25 ='大里 PM2.5 在(' + h + ':' + m + ':' + s + ') = '+ e['PM2.5'];\n    }\n});\nreturn msg;\n","outputs":1,"noerr":0,"x":560,"y":506,"wires":[["4fd67ae1.692404"]]},{"id":"711f40eb.7544e","type":"function","z":"fb30592f.092358","name":"UVI","func":"var a = msg.payload;\n\na.forEach(function(e,i){\n    if(e.SiteName=='臺中'){\n        msg.uvi ='臺中 UVI 在'+e['PublishTime']+'為' + e['UVI'];\n    }\n});\nreturn msg;\n","outputs":1,"noerr":0,"x":560,"y":606,"wires":[["22853a39.75b946"]]},{"id":"4fd67ae1.692404","type":"debug","z":"fb30592f.092358","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"pm25","x":700,"y":506,"wires":[]},{"id":"22853a39.75b946","type":"debug","z":"fb30592f.092358","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"uvi","x":710,"y":606,"wires":[]},{"id":"68a69c0b.2478e4","type":"inject","z":"fb30592f.092358","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":220,"y":280,"wires":[["9f2f1f12.e7117"]]},{"id":"9f2f1f12.e7117","type":"http request","z":"fb30592f.092358","name":"","method":"GET","ret":"obj","url":"http://opendata.epa.gov.tw/webapi/Data/REWIQA/?$orderby=SiteName&$skip=0&$top=1000&format=json","tls":"","x":390,"y":280,"wires":[["e302292.e3f8ed8","9e9a3fc0.1a242"]]},{"id":"d9e60a5d.05ff38","type":"debug","z":"fb30592f.092358","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"pm25","x":650,"y":340,"wires":[]},{"id":"e302292.e3f8ed8","type":"function","z":"fb30592f.092358","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":550,"y":280,"wires":[["d9e60a5d.05ff38"]]},{"id":"9e9a3fc0.1a242","type":"debug","z":"fb30592f.092358","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":610,"y":200,"wires":[]}]

沒有留言:

張貼留言

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

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