2020年9月30日 星期三

Node-Red + MQTT 範例

Node-Red + MQTT 範例

1) 安裝 Node-red 

    http://alex9ufoexploer.blogspot.com/2018/03/node-red_7.html

2) 程式 

     127.0.0.1:1880 匯入程式

    



 Node-Red節點若是虛線代表該節點元件沒有安裝

   


 [{"id":"7377d363.84128c","type":"tab","label":"MQTT_LED","disabled":false,"info":""},{"id":"9a9808d2.b904e8","type":"mqtt in","z":"7377d363.84128c","name":"","topic":"alex9ufo/led/led_event","qos":"1","datatype":"auto","broker":"841df58d.ee5e98","x":140,"y":80,"wires":[["ff64ce35.d54d6","cc43c13c.49236","bd66ced7.e745b","7b270a19.2bfba4","30e4d5f1.5dabca"]]},{"id":"ff64ce35.d54d6","type":"ui_text","z":"7377d363.84128c","group":"6c9116b.b62d4e8","order":0,"width":0,"height":0,"name":"","label":"MQTT Suscribe Data","format":"{{msg.payload}}","layout":"col-center","x":400,"y":80,"wires":[]},{"id":"e10b330a.0e832","type":"ui_button","z":"7377d363.84128c","name":"","group":"6c9116b.b62d4e8","order":0,"width":0,"height":0,"passthru":false,"label":"On led","tooltip":"","color":"white","bgcolor":"","icon":"fa-circle","payload":"LED 開","payloadType":"str","topic":"","x":110,"y":280,"wires":[["a4db0e32.936f8"]]},{"id":"5ccc4685.ea9d18","type":"ui_button","z":"7377d363.84128c","name":"","group":"6c9116b.b62d4e8","order":0,"width":0,"height":0,"passthru":false,"label":"Off led","tooltip":"","color":"black","bgcolor":"","icon":"fa-circle-o","payload":"LED 關","payloadType":"str","topic":"","x":110,"y":340,"wires":[["a4db0e32.936f8"]]},{"id":"a4db0e32.936f8","type":"mqtt out","z":"7377d363.84128c","name":"","topic":"alex9ufo/led/led_event","qos":"1","retain":"false","broker":"841df58d.ee5e98","x":360,"y":280,"wires":[]},{"id":"cc43c13c.49236","type":"function","z":"7377d363.84128c","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 + ' --> Time:(' + h + ':' + m + ':' + s + ')' ;\n\nreturn msg;","outputs":1,"noerr":0,"x":190,"y":220,"wires":[["d3901e4c.e507d"]]},{"id":"d3901e4c.e507d","type":"function","z":"7377d363.84128c","name":"Set Line API ","func":"msg.headers = {'content-type':'application/x-www-form-urlencoded','Authorization':'Bearer A4wwPNh2WqB7dlfeQyyIAwtggn1kfZSI5LkkCdia1gB'};\nmsg.payload = {\"message\":msg.payload};\nreturn msg;\n\n//oR7KdXvK1eobRr2sRRgsl4PMq23DjDlhfUs96SyUBZu","outputs":1,"noerr":0,"x":370,"y":220,"wires":[["2d2f8066.cb721"]]},{"id":"2d2f8066.cb721","type":"http request","z":"7377d363.84128c","name":"","method":"POST","ret":"txt","paytoqs":false,"url":"https://notify-api.line.me/api/notify","tls":"","persist":false,"proxy":"","authType":"","x":540,"y":220,"wires":[["6085953d.1462ec"]]},{"id":"6085953d.1462ec","type":"debug","z":"7377d363.84128c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":690,"y":220,"wires":[]},{"id":"bd66ced7.e745b","type":"debug","z":"7377d363.84128c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":390,"y":20,"wires":[]},{"id":"7b270a19.2bfba4","type":"line-notify","z":"7377d363.84128c","name":"NODE-RED MQTT LED","message":"Hello! \nLED Changed","contentType":"sticker","imageUrl":"","stickerPackageId":"1","stickerId":"2","x":410,"y":140,"wires":[]},{"id":"30e4d5f1.5dabca","type":"ui_audio","z":"7377d363.84128c","name":"","group":"6c9116b.b62d4e8","voice":"zh-TW","always":true,"x":340,"y":180,"wires":[]},{"id":"841df58d.ee5e98","type":"mqtt-broker","z":"","name":"","broker":"broker.mqtt-dashboard.com","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"15","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"6c9116b.b62d4e8","type":"ui_group","z":"","name":"Storing IOT Data ","tab":"eeb8e179.a47a4","order":1,"disp":true,"width":"6","collapse":false},{"id":"eeb8e179.a47a4","type":"ui_tab","z":"","name":"MySQL","icon":"dashboard","disabled":false,"hidden":false}]

  node-red-contrib-line-notify  安裝

0



安裝過程要等待不可以中途放棄

node-red-dashboard 安裝




127.0.0.1:1880/ui 使用者介面

  

 可以按 ON LED 或OFF LED 發行publish 訊息到MQTT 上

MQTT Broker :   http://broker.mqtt-dashboard.com/


3) 執行畫面

 MQTT_BOX 

MQTT Client Name : 自行命名 

Protocol : MQTTTCP 

Host : broker.mqtt-dashboard.com:1883/ws



Topic to publish : alex9ufo/led/led_event

LED 關 或是 LED 開  --> Publish

Topic to subscribe : alex9ufo/led/led_event

QoS : 0 或1 ,2  > Subscribe



Line Notify 要修改



請參考

https://alex9ufoexploer.blogspot.com/2020/10/create-line-messaging-api-channel.html


Set Line API 要修改


msg.headers = {'content-type':'application/x-www-form-urlencoded','Authorization':'Bearer A4wwaPNh2WqB7dlfeeQyyIAwtggn1kfZSI5LkkCdia1gB'};

msg.payload = {"message":msg.payload};

return msg;


紅字為自己的發行權杖

請參考


MQTT In 修改

MQTT Out  修改



主題Topic :  MQTT in 與 MQTT  out 二者TOPIC 要相同


Line Notify 有二個

1) https://notify-bot.line.me/zh_TW/ 


2) https://developers.line.biz/zh-hant/



Line Notify  需修改成自己的

 不然是我收到line 訊息

 1) 發行權杖  https://notify-bot.line.me/zh_TW/ 

https://alex9ufoexploer.blogspot.com/2019/11/node-red-line-notify-massage.html


2) 使用LINE Messaging API發送消息的節點

https://www.learncodewithmike.com/2020/06/python-line-bot.html

http://alex9ufoexploer.blogspot.com/2020/08/pushmessageline-messaging-api.html


MQTT 簡報

http://www.mediafire.com/file/fai82oxhnuqy7wm/%25E8%25AA%258D%25E8%25AD%2598MQTT.pdf/file

http://www.mediafire.com/file/g2slozzylsvapid/MQTT%25E5%258D%2594%25E5%25AE%259A.pptx/file

沒有留言:

張貼留言

Messaging API作為替代方案

  LINE超好用功能要沒了!LINE Notify明年3月底終止服務,有什麼替代方案? LINE Notify將於2025年3月31日結束服務,官方建議改用Messaging API作為替代方案。 //CHANNEL_ACCESS_TOKEN = 'Messaging ...