2023年6月21日 星期三

實驗 用 LINE 發送天氣預報

 用 LINE 發送天氣預報

 ---參考益師傅  益師傅 MQTT 與 IoT 整合運用

之前提到可以Node-RED 透過 MQTT 讓 ESP32 或是 MQTT Dash 收到天氣預報資料,昨晚也提到又能從 MQTT Dash 透過 ON TAP 事件再 PUBLISH 讓 ESP32 變更區域天氣,也變更了 MQTT Dash 上的天氣狀況.

這樣便能透過手機區域選單方式,運用 MQTT 機制即時變更天氣資訊.

所以在原本 Node-RED 加上 觸發 LINE 發送機制. 雖然 Node-RED 可以安裝 LINE Notify 直接發送訊息,但這個元件卻只能發送您想發送的資料訊息或圖形,無法結合 抓回天氣預報的 payload 資料. 所以就以運用以前在 MCU 上 的 HTTP POST 服務寫法來觸發 LINE 傳送天氣預報資訊.

透過 f函式元件 將氣溫相關資訊取出組合,然後透過 另一個 f函示來組合 LINE 發送的方式,最後透過 HTTP request 提出發送需求,這樣就完成了天氣預報功能.

然後再將 inject 改為每30分鐘觸發一次,



var message = "\n 台中今日天氣 :  "+msg.payload.weather[0].main+ "\n";
message += "目前溫度: " + msg.payload.main.temp+"\n";
message += "最高溫度: " + msg.payload.main.temp_max+"\n";
message += "最低溫度: " + msg.payload.main.temp_min+"\n";
message += "===========================";
msg.payload=message;
return msg;




[{"id":"d59d047bee137a4c","type":"change","z":"9e8aacb662c0e0ae","name":"","rules":[{"t":"move","p":"payload","pt":"msg","to":"payload.city","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":130,"y":160,"wires":[["3e2bb39cca2b23f5","ec3d48f107b7facb"]]},{"id":"3e2bb39cca2b23f5","type":"http request","z":"9e8aacb662c0e0ae","name":"Openweathermap","method":"GET","ret":"txt","paytoqs":"query","url":"http://api.openweathermap.org/data/2.5/weather?q={{{payload.city}}}&units=metric&appid=b3a1fd4de2403e47a564135a5be59821","tls":"","persist":false,"proxy":"","insecureHTTPParser":false,"authType":"","senderr":false,"headers":[],"x":350,"y":120,"wires":[["f03121d45daa9150","76bac1d6d1c091f4"]]},{"id":"0da45b6b4592ed1b","type":"ui_gauge","z":"9e8aacb662c0e0ae","name":"","group":"ce9ab99a.e8a4d8","order":5,"width":4,"height":4,"gtype":"gage","title":"最高溫度","label":"°C","format":"{{msg.payload.main.temp_max}}","min":"0","max":"40","colors":["#fdc9c9","#f37777","#f51919"],"seg1":"","seg2":"","className":"","x":540,"y":200,"wires":[]},{"id":"380c82d62ca7de96","type":"ui_gauge","z":"9e8aacb662c0e0ae","name":"","group":"ce9ab99a.e8a4d8","order":4,"width":4,"height":4,"gtype":"gage","title":"目前溫度","label":"°C","format":"{{msg.payload.main.temp}}","min":"0","max":"40","colors":["#00b500","#e6e600","#ca3838"],"seg1":"","seg2":"","className":"","x":540,"y":240,"wires":[]},{"id":"4ef88af5f33cbaf9","type":"ui_gauge","z":"9e8aacb662c0e0ae","name":"","group":"ce9ab99a.e8a4d8","order":6,"width":4,"height":4,"gtype":"gage","title":"最低溫度","label":"°C","format":"{{msg.payload.main.temp_min}}","min":"0","max":"40","colors":["#b7f5b7","#00e639","#07c304"],"seg1":"","seg2":"","className":"","x":540,"y":280,"wires":[]},{"id":"480fb6e3e33ee115","type":"function","z":"9e8aacb662c0e0ae","name":"function 24","func":"var message = \"\\n 台中今日天氣 :  \"+msg.payload.weather[0].main+ \"\\n\";\nmessage += \"目前溫度: \" + msg.payload.main.temp+\"\\n\";\nmessage += \"最高溫度: \" + msg.payload.main.temp_max+\"\\n\";\nmessage += \"最低溫度: \" + msg.payload.main.temp_min+\"\\n\";\nmessage += \"===========================\";\nmsg.payload=message;\nreturn msg;\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":270,"y":320,"wires":[["07cede70ace8a435","7979fbb333401265"]]},{"id":"7979fbb333401265","type":"function","z":"9e8aacb662c0e0ae","name":"Format timestamp","func":"var 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}\nmsg.payload = msg.payload + '\\n --> Time:(' + h + ':' + m + ':' + s + ')' ;\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":250,"y":400,"wires":[["05f21247d585fee6","1be197f1e908b85a"]]},{"id":"1be197f1e908b85a","type":"function","z":"9e8aacb662c0e0ae","name":"Set Line API ","func":"msg.headers = {'content-type':'application/x-www-form-urlencoded','Authorization':'Bearer cnEzkvKQYNdsMGC3lbanLK4UoXg5fE2jAYBryJsYzdR'};\nmsg.payload = {\"message\":msg.payload};\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":450,"y":400,"wires":[["0af5d2854603acf4"]]},{"id":"05f21247d585fee6","type":"debug","z":"9e8aacb662c0e0ae","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":450,"y":360,"wires":[]},{"id":"0af5d2854603acf4","type":"http request","z":"9e8aacb662c0e0ae","name":"","method":"POST","ret":"txt","url":"https://notify-api.line.me/api/notify","tls":"","x":600,"y":400,"wires":[["b7cf5f0c4861d5cb"]]},{"id":"b7cf5f0c4861d5cb","type":"debug","z":"9e8aacb662c0e0ae","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":750,"y":400,"wires":[]},{"id":"261308ab9ab50e67","type":"inject","z":"9e8aacb662c0e0ae","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"Taichung","payloadType":"str","x":100,"y":100,"wires":[["d59d047bee137a4c"]]},{"id":"ec3d48f107b7facb","type":"ui_text","z":"9e8aacb662c0e0ae","group":"ce9ab99a.e8a4d8","order":43,"width":0,"height":0,"name":"","label":"OpenWeather Node-Red Element 城市: ","format":"{{msg.payload.city}}","layout":"row-left","className":"","x":400,"y":60,"wires":[]},{"id":"07cede70ace8a435","type":"debug","z":"9e8aacb662c0e0ae","name":"debug 78","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":540,"y":320,"wires":[]},{"id":"f03121d45daa9150","type":"debug","z":"9e8aacb662c0e0ae","name":"debug 79","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":540,"y":120,"wires":[]},{"id":"76bac1d6d1c091f4","type":"json","z":"9e8aacb662c0e0ae","name":"","property":"payload","action":"","pretty":false,"x":330,"y":220,"wires":[["196c800d77b407db","0da45b6b4592ed1b","380c82d62ca7de96","4ef88af5f33cbaf9","480fb6e3e33ee115"]]},{"id":"196c800d77b407db","type":"debug","z":"9e8aacb662c0e0ae","name":"debug 80","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":540,"y":160,"wires":[]},{"id":"ce9ab99a.e8a4d8","type":"ui_group","name":"","tab":"d0721a70.d98a58","order":1,"disp":false,"width":"16","collapse":false,"className":""},{"id":"d0721a70.d98a58","type":"ui_tab","name":"OpenWeatherMap","icon":"dashboard","order":42,"disabled":false,"hidden":false}]


沒有留言:

張貼留言

WOKWI DHT22 & LED , Node-Red + SQLite database

 WOKWI DHT22 & LED , Node-Red + SQLite database Node-Red程式 [{"id":"6f0240353e534bbd","type":"comment&...