2023年11月11日 星期六

模擬ESP32 LED on, off 溫度 濕度 Node-RED with MQTT (Publish and Subscribe)

模擬ESP32 LED on, off 溫度 濕度  Node-RED with MQTT (Publish and Subscribe) 


原來的系統圖

源自於  https://randomnerdtutorials.com/esp8266-and-node-red-with-mqtt/


修改後的系統圖

將ESP32  使用mqtt 取代







MQTT發行主題: alex9ufo/room/lamp Payload: on , off 控制 on , off

MQTT訂閱主題: alex9ufo/room/lamp  on , off控制(原來的ESP32 ) ui-LED 綠燈 紅燈

MQTT發行主題: alex9ufo/room/temperature  Payload: 數值  

MQTT訂閱主題: alex9ufo/room/temperature  Payload: 數值  顯示於chart

MQTT發行主題: alex9ufo/room/humidity  Payload: 數值  

MQTT訂閱主題: alex9ufo/room/ humidity  Payload: 數值  顯示於chart

利用亂數產生器 產生濕度及溫度 (模擬)

 

function between(min, max) { 

    return Math.floor(

    (Math.random() * (max - min) + min)

  )

}

var min=20;

var max=90;

 

msg.payload =  between(min, max);

msg.color="red";

return msg;

定期 閃爍 HiveMQTT 文字

flow.set("onoff",true);

return msg;

 

 

var on_off=flow.get("onoff");

 

if (on_off== true)

{

    msg.payload='HiveMQTT';

    flow.set("onoff",false)

}  

else

{

    msg.payload='   ';

    flow.set("onoff",true)

}

msg.color="red";

 

return msg;

 

 

Node-Red程式

[{"id":"55346e4b1a65ac54","type":"mqtt in","z":"9b3f215680d5968c","name":"room/lamp","topic":"alex9ufo/room/lamp","qos":"1","datatype":"auto-detect","broker":"70940176.2b2d3","nl":false,"rap":true,"rh":0,"inputs":0,"x":180,"y":560,"wires":[["6ef1afbf9aed1692","0b48bb53509adeb0"]]},{"id":"6ef1afbf9aed1692","type":"function","z":"9b3f215680d5968c","name":"function LED on ,off","func":"var onoff=msg.payload;\n\nif (onoff==\"on\")\n{\n    msg.payload=true;\n}\nif (onoff==\"off\")\n{\n    msg.payload=false;\n}  \nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":410,"y":600,"wires":[["191549f85428067c"]]},{"id":"57e051cf6128d4df","type":"inject","z":"9b3f215680d5968c","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"false","payloadType":"bool","x":390,"y":680,"wires":[["191549f85428067c"]]},{"id":"51831ec7452b4e90","type":"inject","z":"9b3f215680d5968c","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"true","payloadType":"bool","x":390,"y":640,"wires":[["191549f85428067c"]]},{"id":"191549f85428067c","type":"ui_led","z":"9b3f215680d5968c","order":1,"group":"e29eccdc124175f7","width":6,"height":5,"label":"","labelPlacement":"left","labelAlignment":"left","colorForValue":[{"color":"#ff0000","value":"false","valueType":"bool"},{"color":"#008000","value":"true","valueType":"bool"}],"allowColorForValueInMessage":false,"shape":"circle","showGlow":true,"name":"","x":610,"y":600,"wires":[]},{"id":"2557bf4236298f52","type":"ui_button","z":"9b3f215680d5968c","name":"","group":"e29eccdc124175f7","order":2,"width":0,"height":0,"passthru":false,"label":"LED ON","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"on","payloadType":"str","topic":"topic","topicType":"msg","x":180,"y":80,"wires":[["659cf1ae5c4c6714","32aeaca4cf2c8211","ce6d4358286392c8"]]},{"id":"e428bed1bea12867","type":"ui_button","z":"9b3f215680d5968c","name":"","group":"e29eccdc124175f7","order":3,"width":0,"height":0,"passthru":false,"label":"LED OFF","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"off","payloadType":"str","topic":"topic","topicType":"msg","x":180,"y":180,"wires":[["659cf1ae5c4c6714","32aeaca4cf2c8211","ce6d4358286392c8"]]},{"id":"659cf1ae5c4c6714","type":"ui_audio","z":"9b3f215680d5968c","name":"","group":"6c9116b.b62d4e8","voice":"Google US English","always":"","x":460,"y":140,"wires":[]},{"id":"32aeaca4cf2c8211","type":"mqtt out","z":"9b3f215680d5968c","name":"room/lamp","topic":"alex9ufo/room/lamp","qos":"0","retain":"true","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"70940176.2b2d3","x":470,"y":80,"wires":[]},{"id":"0b48bb53509adeb0","type":"ui_text","z":"9b3f215680d5968c","group":"e29eccdc124175f7","order":5,"width":0,"height":0,"name":"","label":"MQTT訂閱的訊息","format":"{{msg.payload}}","layout":"row-left","className":"","x":410,"y":560,"wires":[]},{"id":"ce6d4358286392c8","type":"ui_text","z":"9b3f215680d5968c","group":"e29eccdc124175f7","order":4,"width":0,"height":0,"name":"","label":"MQTT發行的訊息","format":"{{msg.payload}}","layout":"row-left","className":"","x":490,"y":180,"wires":[]},{"id":"9fee91969ae648be","type":"ui_text","z":"9b3f215680d5968c","group":"e29eccdc124175f7","order":6,"width":0,"height":0,"name":"","label":"MQTT 的Broker :","format":"<font color= {{msg.color}} > {{msg.payload}} </font>","layout":"row-left","className":"","x":670,"y":360,"wires":[]},{"id":"5f780d5f37a8d39b","type":"function","z":"9b3f215680d5968c","name":"function on off","func":"var on_off=flow.get(\"onoff\");\n\nif (on_off== true)\n{\n    msg.payload='HiveMQTT';\n    flow.set(\"onoff\",false)\n}   \nelse\n{\n    msg.payload='   ';\n    flow.set(\"onoff\",true)\n}\nmsg.color=\"red\";\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":440,"y":360,"wires":[["9fee91969ae648be","a7f2e52f0601dd9a"]]},{"id":"cd8307dbef8f4352","type":"inject","z":"9b3f215680d5968c","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":true,"onceDelay":0.1,"topic":"","payload":"true","payloadType":"bool","x":190,"y":360,"wires":[["5f780d5f37a8d39b","8651604d52b4b1e7"]]},{"id":"a7f2e52f0601dd9a","type":"delay","z":"9b3f215680d5968c","name":"","pauseType":"delay","timeout":"2","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":420,"y":300,"wires":[["5f780d5f37a8d39b"]]},{"id":"8651604d52b4b1e7","type":"function","z":"9b3f215680d5968c","name":"function  flow set","func":"flow.set(\"onoff\",true);\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":440,"y":400,"wires":[[]]},{"id":"23b36c6ba429450b","type":"function","z":"9b3f215680d5968c","name":"function random","func":"function between(min, max) {  \n    return Math.floor(\n    (Math.random() * (max - min) + min)\n  )\n}\nvar min=20;\nvar max=90;\n\nmsg.payload =  between(min, max);\nmsg.color=\"red\";\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1180,"y":80,"wires":[["c787d8cb3fb505eb","957657ec9f2221db"]]},{"id":"49d104a2e234bbc1","type":"inject","z":"9b3f215680d5968c","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"5","crontab":"","once":true,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":1000,"y":80,"wires":[["23b36c6ba429450b"]]},{"id":"010af3115caa0da8","type":"ui_chart","z":"9b3f215680d5968c","name":"","group":"687cd22dcb91b6d8","order":1,"width":0,"height":0,"label":"訂閱room/humidity的濕度","chartType":"line","legend":"false","xformat":"HH:mm:ss","interpolate":"linear","nodata":"","dot":false,"ymin":"0","ymax":"99","removeOlder":1,"removeOlderPoints":"","removeOlderUnit":"3600","cutout":0,"useOneColor":false,"useUTC":false,"colors":["#1f77b4","#aec7e8","#ff7f0e","#2ca02c","#98df8a","#d62728","#ff9896","#9467bd","#c5b0d5"],"outputs":1,"useDifferentColor":false,"className":"","x":1410,"y":360,"wires":[[]]},{"id":"4163d8ba7bedfdde","type":"comment","z":"9b3f215680d5968c","name":"發行 room/lamp","info":"","x":200,"y":220,"wires":[]},{"id":"3e99596389e57299","type":"comment","z":"9b3f215680d5968c","name":"訂閱 room/lamp","info":"","x":200,"y":500,"wires":[]},{"id":"ed7bf5649774436d","type":"comment","z":"9b3f215680d5968c","name":"定期 閃爍 HiveMQTT","info":"","x":200,"y":300,"wires":[]},{"id":"1abcdd07762c4611","type":"mqtt out","z":"9b3f215680d5968c","name":"room/temperature","topic":"alex9ufo/room/temperature","qos":"0","retain":"true","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"70940176.2b2d3","x":1390,"y":180,"wires":[]},{"id":"c787d8cb3fb505eb","type":"mqtt out","z":"9b3f215680d5968c","name":"room/humidity","topic":"alex9ufo/room/humidity","qos":"1","retain":"true","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"70940176.2b2d3","x":1380,"y":80,"wires":[]},{"id":"d5e21c7344f200ed","type":"inject","z":"9b3f215680d5968c","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"9","crontab":"","once":true,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":1000,"y":180,"wires":[["404aca76e040cc05"]]},{"id":"404aca76e040cc05","type":"function","z":"9b3f215680d5968c","name":"function random","func":"function between(min, max) {  \n    return Math.floor(\n    (Math.random() * (max - min) + min)\n  )\n}\nvar min=15;\nvar max=55;\n\nmsg.payload =  between(min, max);\nmsg.color=\"red\";\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1180,"y":180,"wires":[["1abcdd07762c4611","b127ea04c56a813c"]]},{"id":"4e8d38e20853a642","type":"mqtt in","z":"9b3f215680d5968c","name":"room/humidity","topic":"alex9ufo/room/humidity","qos":"1","datatype":"auto-detect","broker":"70940176.2b2d3","nl":false,"rap":true,"rh":0,"inputs":0,"x":990,"y":360,"wires":[["010af3115caa0da8"]]},{"id":"9bbf10499c2198bc","type":"mqtt in","z":"9b3f215680d5968c","name":"room/temperature","topic":"alex9ufo/room/temperature","qos":"1","datatype":"auto-detect","broker":"70940176.2b2d3","nl":false,"rap":true,"rh":0,"inputs":0,"x":1010,"y":420,"wires":[["601b75441bdec0f5"]]},{"id":"601b75441bdec0f5","type":"ui_chart","z":"9b3f215680d5968c","name":"","group":"687cd22dcb91b6d8","order":2,"width":0,"height":0,"label":"訂閱room/temperature的溫度","chartType":"line","legend":"false","xformat":"HH:mm:ss","interpolate":"linear","nodata":"","dot":false,"ymin":"15","ymax":"55","removeOlder":1,"removeOlderPoints":"","removeOlderUnit":"3600","cutout":0,"useOneColor":false,"useUTC":false,"colors":["#1f77b4","#aec7e8","#ff7f0e","#2ca02c","#98df8a","#d62728","#ff9896","#9467bd","#c5b0d5"],"outputs":1,"useDifferentColor":false,"className":"","x":1420,"y":420,"wires":[[]]},{"id":"957657ec9f2221db","type":"ui_text","z":"9b3f215680d5968c","group":"687cd22dcb91b6d8","order":3,"width":0,"height":0,"name":"","label":"room/dumidity發行的溼度","format":"<font color= {{msg.color}} > {{msg.payload}} </font>","layout":"row-left","className":"","x":1410,"y":140,"wires":[]},{"id":"b127ea04c56a813c","type":"ui_text","z":"9b3f215680d5968c","group":"687cd22dcb91b6d8","order":4,"width":0,"height":0,"name":"","label":"room/temperature發行的溫度","format":"<font color= {{msg.color}} > {{msg.payload}} </font>","layout":"row-left","className":"","x":1420,"y":240,"wires":[]},{"id":"94e54c60e6dbb0f9","type":"comment","z":"9b3f215680d5968c","name":"發行 room/humidity","info":"","x":1010,"y":40,"wires":[]},{"id":"ec5723852672216f","type":"comment","z":"9b3f215680d5968c","name":"發行 room/temperature","info":"","x":1020,"y":140,"wires":[]},{"id":"289d5fb16a212ff5","type":"comment","z":"9b3f215680d5968c","name":"訂閱 room/temperature","info":"","x":1020,"y":480,"wires":[]},{"id":"36ac3f56fe678f67","type":"comment","z":"9b3f215680d5968c","name":"訂閱 room/humidity","info":"","x":1010,"y":320,"wires":[]},{"id":"6757dc9f.792504","type":"inject","z":"9b3f215680d5968c","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"10","crontab":"","once":true,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":220,"y":800,"wires":[["23f5cb94.03cb04"]]},{"id":"fa1abaf4.885038","type":"template","z":"9b3f215680d5968c","name":"Image in","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"<img src=\"data:image/png;base64,{{payload}}\"style=\"width=\"400\" height=\"300\"\"/>","output":"str","x":400,"y":880,"wires":[["b42fd1dc.ba144"]]},{"id":"b42fd1dc.ba144","type":"ui_template","z":"9b3f215680d5968c","group":"1b61ef76b3739f7c","name":"MQTT","order":5,"width":"8","height":"8","format":"<div ng-bind-html=\"msg.payload\"></div>","storeOutMessages":true,"fwdInMessages":true,"resendOnRefresh":false,"templateScope":"local","className":"","x":590,"y":880,"wires":[[]]},{"id":"23f5cb94.03cb04","type":"file in","z":"9b3f215680d5968c","name":"","filename":"D:\\2023_RFID\\MQTT\\RANDOM_NERD\\mqtt.png","filenameType":"str","format":"","chunk":false,"sendError":false,"encoding":"none","allProps":false,"x":490,"y":800,"wires":[["b83253e6.36f6a"]]},{"id":"b83253e6.36f6a","type":"base64","z":"9b3f215680d5968c","name":"","action":"","property":"payload","x":780,"y":800,"wires":[["fa1abaf4.885038"]]},{"id":"5c10bdf0.aa77f4","type":"comment","z":"9b3f215680d5968c","name":"On inject add MQTT picture  to the dashboard","info":"","x":310,"y":760,"wires":[]},{"id":"70940176.2b2d3","type":"mqtt-broker","name":"","broker":"broker.mqtt-dashboard.com","port":"1883","clientid":"","autoConnect":true,"usetls":false,"protocolVersion":4,"keepalive":"15","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"e29eccdc124175f7","type":"ui_group","name":"LED顯示與控制","tab":"4100d2d18bb62981","order":1,"disp":true,"width":"6","collapse":false,"className":""},{"id":"6c9116b.b62d4e8","type":"ui_group","name":"2023 Storing IOT Data ","tab":"6249073d2714bfd4","order":1,"disp":true,"width":"16","collapse":true,"className":""},{"id":"687cd22dcb91b6d8","type":"ui_group","name":"溫溼度顯示","tab":"4100d2d18bb62981","order":3,"disp":true,"width":"6","collapse":false,"className":""},{"id":"1b61ef76b3739f7c","type":"ui_group","name":"圖示","tab":"4100d2d18bb62981","order":2,"disp":true,"width":"8","collapse":false,"className":""},{"id":"4100d2d18bb62981","type":"ui_tab","name":"模擬","icon":"dashboard","order":81,"disabled":false,"hidden":false},{"id":"6249073d2714bfd4","type":"ui_tab","name":"RFID Home","icon":"dashboard","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...