2023年7月31日 星期一

Node-Red 擷取open data CWB氣象資料

 Node-Red 擷取open data CWB氣象資料




[{"id":"29524fb9ae25eac2","type":"inject","z":"be36ae0c358ba0e7","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":true,"onceDelay":"2","topic":"","payload":"","payloadType":"date","x":100,"y":40,"wires":[["67b10f612aeba21d"]]},{"id":"67b10f612aeba21d","type":"http request","z":"be36ae0c358ba0e7","name":"","method":"GET","ret":"obj","paytoqs":"ignore","url":"https://opendata.cwb.gov.tw/api/v1/rest/datastore/O-A0003-001?Authorization=CWB-40C25FFF-1224-4250-B9D9-3735AAE17DBF&elementName=TEMP,HUMD,PRES,Weather","tls":"","persist":false,"proxy":"","insecureHTTPParser":false,"authType":"","senderr":false,"headers":[],"x":240,"y":40,"wires":[["3a247ad8571a9d43","ebfcca1381b1cb9d","f579543ae7bb2f2d"]]},{"id":"3a247ad8571a9d43","type":"debug","z":"be36ae0c358ba0e7","name":"debug 119","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":450,"y":20,"wires":[]},{"id":"ebfcca1381b1cb9d","type":"function","z":"be36ae0c358ba0e7","name":"function 42","func":"// 範例程式碼\nmsg.payload = {\n    temperature: msg.payload.records.location[0].weatherElement[0].elementValue,\n    humidity: msg.payload.records.location[0].weatherElement[1].elementValue,\n    pressure: msg.payload.records.location[0].weatherElement[2].elementValue,\n    weather: msg.payload.records.location[0].weatherElement[3].elementValue\n};\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":450,"y":60,"wires":[["ed8ee569c17ccd6a"]]},{"id":"ed8ee569c17ccd6a","type":"debug","z":"be36ae0c358ba0e7","name":"debug 120","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":610,"y":60,"wires":[]},{"id":"f579543ae7bb2f2d","type":"function","z":"be36ae0c358ba0e7","name":"function 56","func":"var number=msg.payload.records.location.length;\nvar records_array=[];\nvar i;\nfor (i=0; i<number;i++)\n{\n    records_array[i] = {\n        Name : msg.payload.records.location[i].locationName,\n        TEMP : msg.payload.records.location[i].weatherElement[0].elementValue,\n        HUMD : msg.payload.records.location[i].weatherElement[1].elementValue,\n        PRES : msg.payload.records.location[i].weatherElement[2].elementValue,   \n        Weather:msg.payload.records.location[i].weatherElement[3].elementValue  \n    }\n}\n\nmsg.payload=records_array;\nflow.set(\"RECORDS\",records_array);\n\nreturn msg;\n\n//flow.set('YourVariable', value);    // to store a variable (YourVariable)\n//var x = flow.get('YourVariable'); //to retrieve a variable (YourVariable)","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":250,"y":100,"wires":[["6c406be5e8d24135","8d7f937d81990ed0","972fb65a7d34bf3d"]]},{"id":"6c406be5e8d24135","type":"debug","z":"be36ae0c358ba0e7","name":"debug 145","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":450,"y":100,"wires":[]},{"id":"8d7f937d81990ed0","type":"function","z":"be36ae0c358ba0e7","name":"function 57","func":"flow.set(\"temp\",msg.payload);\nvar rawdata = flow.get(\"temp\");\n\nvar number=msg.payload.length;\nvar records_array=[];\nvar i;\nfor (i=0; i<number;i++)\n{\n    records_array[i] = msg.payload[i].Name\n \n}\n\nmsg.payload=records_array;\nflow.set(\"NAME_RECORDS\",records_array);\n\nreturn msg;\n\n//flow.set('YourVariable', value);    // to store a variable (YourVariable)\n//var x = flow.get('YourVariable'); //to retrieve a variable (YourVariable)","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":250,"y":180,"wires":[["dc6a54fc2b9b1532","67210cdb4382a327"]]},{"id":"dc6a54fc2b9b1532","type":"debug","z":"be36ae0c358ba0e7","name":"debug 146","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":450,"y":180,"wires":[]},{"id":"972fb65a7d34bf3d","type":"ui_table","z":"be36ae0c358ba0e7","group":"b8630066.5d446","name":"","order":37,"width":"10","height":"8","columns":[],"outputs":0,"cts":false,"x":430,"y":140,"wires":[]},{"id":"67210cdb4382a327","type":"function","z":"be36ae0c358ba0e7","name":"transform data to correct format","func":"flow.set(\"weather_temp\",msg.payload);\n\nmsg.options = [];//create empty array\n\nfor(let i = 0; i < msg.payload.length; i++){\n    let row = msg.payload[i]; //get the row\n    let opt = {};//make new opt object\n    opt[row] = i; //add a propery to object called row[0] & set its value to row[1]\n    msg.options.push(opt);//add the opt to array    \n//      msg.options.push(row);\n}\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":310,"y":220,"wires":[["e37e688e1efd4a37"]]},{"id":"498fde9063a6124a","type":"ui_toast","z":"be36ae0c358ba0e7","position":"top right","displayTime":"3","highlight":"","sendall":true,"outputs":0,"ok":"OK","cancel":"","raw":false,"className":"","topic":"","name":"","x":730,"y":220,"wires":[]},{"id":"e37e688e1efd4a37","type":"ui_dropdown","z":"be36ae0c358ba0e7","name":"","label":"","tooltip":"","place":"請選地名","group":"b8630066.5d446","order":0,"width":0,"height":0,"passthru":true,"multiple":false,"options":[{"label":"","value":"","type":"str"}],"payload":"","topic":"","topicType":"str","className":"","x":540,"y":220,"wires":[["498fde9063a6124a","847811fc15df1e67"]]},{"id":"847811fc15df1e67","type":"function","z":"be36ae0c358ba0e7","name":"function 58","func":"var i=msg.payload;\nvar rawdata = flow.get(\"temp\");\nvar array=[];\narray[0] = {\n Name : rawdata[i].Name,\n TEMP : rawdata[i].TEMP,\n HUMD : rawdata[i].HUMD,\n PRES : rawdata[i].PRES,\n Weather:rawdata[i].Weather   \n}\n//array[1] = {\n// Name : rawdata[i].Name,\n// TEMP : rawdata[i].TEMP,\n// HUMD : rawdata[i].HUMD,\n// PRES : rawdata[i].PRES,\n// Weather:rawdata[i].Weather   \n//}\n//msg.payload=rawdata[i];\nmsg.payload=array;\n\nreturn msg;\n\n\n//flow.set('YourVariable', value);    // to store a variable (YourVariable)\n//var x = flow.get('YourVariable'); //to retrieve a variable (YourVariable)","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":550,"y":280,"wires":[["e72573bb57f3c97f","2c81b478206b9837"]]},{"id":"e72573bb57f3c97f","type":"debug","z":"be36ae0c358ba0e7","name":"debug 147","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":770,"y":260,"wires":[]},{"id":"2c81b478206b9837","type":"ui_table","z":"be36ae0c358ba0e7","group":"4afff518723dbafb","name":"","order":37,"width":"0","height":"0","columns":[],"outputs":0,"cts":false,"x":750,"y":300,"wires":[]},{"id":"b8630066.5d446","type":"ui_group","name":"選定地點的天候狀況","tab":"f9ac9e91.20e588","order":1,"disp":true,"width":"12","collapse":false,"className":""},{"id":"4afff518723dbafb","type":"ui_group","name":"天候狀況","tab":"f9ac9e91.20e588","order":2,"disp":true,"width":"10","collapse":false,"className":""},{"id":"f9ac9e91.20e588","type":"ui_tab","name":"Table Examples","icon":"dashboard","order":1,"disabled":false,"hidden":false}]

沒有留言:

張貼留言

2024_09 作業3 以Node-Red 為主

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