2023年9月30日 星期六

Node-RED Dashboard Chart types

 Node-RED Dashboard Chart types





節點

設定

 function

Fill Single Chart Array

var chart = [{

    "series":["A"],

    "data":[[{"x":1504029632890,"y":5},

             {"x":1504029633514,"y":8},

             {"x":1504029634400,"y":3}]],

    "labels":[""]

}];

msg.payload = chart;

 

return msg;

function

 

Fill Multiple Line Chart Array

var chart = [  {"series":["A","B","C"],

                "data":[

                    [{"x":1504029632890,"y":5},

                     {"x":1504029636001,"y":4},

                     {"x":1504029638656,"y":2}],

                    [{"x":1504029633514,"y":6},

                     {"x":1504029636622,"y":7},

                     {"x":1504029639539,"y":6}],

                    [{"x":1504029634400,"y":7},

                     {"x":1504029637959,"y":9},

                     {"x":1504029640317,"y":7}]

                ],

                "labels":[""]}];

msg.payload = chart;

 

return msg;

function

Fill Pie Chart Array

var pie={};

pie.labels = ["Geo1","Geo2","Geo3","Geo4","Geo5"];

pie.series = ['Sales A', 'Sales B', 'Sales C', 'Sales D'];

pie.data =   [ [16, 27, 42, 65, 89] ];

//return {payload:[pie]};

 

var pie={ "labels" : ["Geo1","Geo2","Geo3","Geo4","Geo5"],

          "series" : ['Sales A', 'Sales B', 'Sales C', 'Sales D'],

          "data"   : [ [16, 27, 42, 65, 89] ]

        };

msg.payload = [pie];

return msg;

function

Fill Bar Chart Array

var bar={};

bar.labels = ["Q1 16","Q2 16","Q3 16","Q4 16","Q1 17","Q2 17","Q3 17"];

bar.series = ['Sales A', 'Sales B', 'Sales C', 'Sales D'];

// An array of 4 arrays of 7 elements

bar.data = [

    [65, 59, 80, 81, 56, 55, 40],

    [28, 48, 40, 19, 86, 27, 90],

    [38, 28, 20, 49, 45, 60, 20],

    [58, 18, 40, 29, 15, 30, 60]

  ];

return {payload:[bar]};

 

[{"id":"38fac2c5.ead426","type":"ui_chart","z":"12c413b5.585494","name":"","group":"89bfd092.f8bc18","order":0,"width":0,"height":0,"label":"Single Line Plot","chartType":"line","legend":"false","xformat":"HH:mm:ss","interpolate":"linear","nodata":"","dot":false,"ymin":"","ymax":"","removeOlder":1,"removeOlderPoints":"","removeOlderUnit":"3600","cutout":0,"useOneColor":false,"colors":["#1f77b4","#aec7e8","#ff7f0e","#2ca02c","#98df8a","#d62728","#ff9896","#9467bd","#c5b0d5"],"outputs":1,"x":600,"y":120,"wires":[[]]},{"id":"8b1422bb.731af","type":"function","z":"12c413b5.585494","name":"Fill Single Chart Array","func":"var chart = [{\n    \"series\":[\"A\"],\n    \"data\":[[{\"x\":1504029632890,\"y\":5},\n             {\"x\":1504029633514,\"y\":8},\n             {\"x\":1504029634400,\"y\":3}]],\n    \"labels\":[\"\"]\n}];\nmsg.payload = chart;\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":320,"y":120,"wires":[["38fac2c5.ead426","f7e82408.b55a9"]]},{"id":"f7e82408.b55a9","type":"debug","z":"12c413b5.585494","name":"","active":true,"console":"false","complete":"false","x":590,"y":160,"wires":[]},{"id":"ce4074ce.08a8d","type":"comment","z":"12c413b5.585494","name":"Sample Dashboard that plots various Chart types","info":"","x":250,"y":60,"wires":[]},{"id":"40f701a5.4c0628","type":"ui_chart","z":"12c413b5.585494","name":"","group":"89bfd092.f8bc18","order":0,"width":0,"height":0,"label":"Multiple Line Plots","chartType":"line","legend":"false","xformat":"HH:mm:ss","interpolate":"linear","nodata":"","dot":false,"ymin":"","ymax":"","removeOlder":1,"removeOlderPoints":"","removeOlderUnit":"3600","cutout":0,"useOneColor":false,"colors":["#1f77b4","#aec7e8","#ff7f0e","#2ca02c","#98df8a","#d62728","#ff9896","#9467bd","#c5b0d5"],"outputs":2,"x":610,"y":220,"wires":[[],[]]},{"id":"5ac8ae9e.8497b8","type":"function","z":"12c413b5.585494","name":"Fill Multiple Line Chart Array","func":"var chart = [  {\"series\":[\"A\",\"B\",\"C\"],\n                \"data\":[\n                    [{\"x\":1504029632890,\"y\":5},\n                     {\"x\":1504029636001,\"y\":4},\n                     {\"x\":1504029638656,\"y\":2}],\n                    [{\"x\":1504029633514,\"y\":6},\n                     {\"x\":1504029636622,\"y\":7},\n                     {\"x\":1504029639539,\"y\":6}],\n                    [{\"x\":1504029634400,\"y\":7},\n                     {\"x\":1504029637959,\"y\":9},\n                     {\"x\":1504029640317,\"y\":7}]\n                ],\n                \"labels\":[\"\"]}];\nmsg.payload = chart;\n\nreturn msg;","outputs":1,"noerr":0,"x":340,"y":220,"wires":[["40f701a5.4c0628","2b3c03cd.9670ac"]]},{"id":"2b3c03cd.9670ac","type":"debug","z":"12c413b5.585494","name":"","active":true,"console":"false","complete":"false","x":590,"y":260,"wires":[]},{"id":"b54397da.7d3c2","type":"ui_chart","z":"12c413b5.585494","name":"","group":"d6d5b6c0.39742","order":0,"width":0,"height":0,"label":"Vertical Bar Chart","chartType":"bar","legend":"false","xformat":"HH:mm:ss","interpolate":"linear","nodata":"","dot":false,"ymin":"0","ymax":"100","removeOlder":1,"removeOlderPoints":"","removeOlderUnit":"3600","cutout":0,"useOneColor":false,"colors":["#1f77b4","#aec7e8","#ff7f0e","#2ca02c","#98df8a","#d62728","#ff9896","#9467bd","#c5b0d5"],"outputs":2,"x":610,"y":540,"wires":[[],[]]},{"id":"80a37865.9a196","type":"inject","z":"12c413b5.585494","name":"","repeat":"","crontab":"","once":true,"topic":"","payload":"true","payloadType":"bool","x":130,"y":120,"wires":[["8b1422bb.731af"]]},{"id":"e1c92e88.851f","type":"inject","z":"12c413b5.585494","name":"","repeat":"","crontab":"","once":true,"topic":"","payload":"true","payloadType":"bool","x":130,"y":220,"wires":[["5ac8ae9e.8497b8"]]},{"id":"2cc30253.462f36","type":"inject","z":"12c413b5.585494","name":"","repeat":"","crontab":"","once":true,"topic":"","payload":"true","payloadType":"bool","x":130,"y":540,"wires":[["8eb046bd.518a18"]]},{"id":"8eb046bd.518a18","type":"function","z":"12c413b5.585494","name":"Fill Bar Chart Array","func":"var bar={};\nbar.labels = [\"Q1 16\",\"Q2 16\",\"Q3 16\",\"Q4 16\",\"Q1 17\",\"Q2 17\",\"Q3 17\"];\nbar.series = ['Sales A', 'Sales B', 'Sales C', 'Sales D'];\n// An array of 4 arrays of 7 elements\nbar.data = [\n    [65, 59, 80, 81, 56, 55, 40],\n    [28, 48, 40, 19, 86, 27, 90],\n    [38, 28, 20, 49, 45, 60, 20],\n    [58, 18, 40, 29, 15, 30, 60]\n  ];\nreturn {payload:[bar]};","outputs":1,"noerr":0,"x":310,"y":540,"wires":[["b54397da.7d3c2","b0740d24.5c6a48","36e16af3.974c66"]]},{"id":"b0740d24.5c6a48","type":"debug","z":"12c413b5.585494","name":"","active":true,"console":"false","complete":"false","x":590,"y":620,"wires":[]},{"id":"36e16af3.974c66","type":"ui_chart","z":"12c413b5.585494","name":"","group":"d6d5b6c0.39742","order":0,"width":0,"height":0,"label":"Horizontal Bar Chart","chartType":"horizontalBar","legend":"false","xformat":"HH:mm:ss","interpolate":"linear","nodata":"","dot":false,"ymin":"0","ymax":"100","removeOlder":1,"removeOlderPoints":"","removeOlderUnit":"3600","cutout":0,"useOneColor":false,"colors":["#1f77b4","#aec7e8","#ff7f0e","#2ca02c","#98df8a","#d62728","#ff9896","#9467bd","#c5b0d5"],"outputs":2,"x":619.0908813476562,"y":580,"wires":[[],[]]},{"id":"de99a184.f909b","type":"ui_chart","z":"12c413b5.585494","name":"","group":"5c2f5188.6d47f8","order":1,"width":0,"height":0,"label":"Pie Chart","chartType":"pie","legend":"true","xformat":"HH:mm:ss","interpolate":"linear","nodata":"","dot":false,"ymin":"","ymax":"","removeOlder":1,"removeOlderPoints":"","removeOlderUnit":"3600","cutout":0,"useOneColor":false,"colors":["#1f77b4","#aec7e8","#ff7f0e","#2ca02c","#98df8a","#d62728","#ff9896","#9467bd","#c5b0d5"],"outputs":2,"x":580,"y":320,"wires":[[],[]]},{"id":"6734c083.6a93c","type":"function","z":"12c413b5.585494","name":"Fill Pie Chart Array","func":"var pie={};\npie.labels = [\"Geo1\",\"Geo2\",\"Geo3\",\"Geo4\",\"Geo5\"];\npie.series = ['Sales A', 'Sales B', 'Sales C', 'Sales D'];\npie.data =   [ [16, 27, 42, 65, 89] ];\n//return {payload:[pie]};\n\nvar pie={ \"labels\" : [\"Geo1\",\"Geo2\",\"Geo3\",\"Geo4\",\"Geo5\"],\n          \"series\" : ['Sales A', 'Sales B', 'Sales C', 'Sales D'],\n          \"data\"   : [ [16, 27, 42, 65, 89] ]\n        };\nmsg.payload = [pie];\nreturn msg;","outputs":1,"noerr":0,"x":310,"y":320,"wires":[["de99a184.f909b","61bb503e.6ca1","37648ff7.3d875","74e87c77.7a50dc","ba58094c.aab88"]]},{"id":"c6277148.aa5538","type":"inject","z":"12c413b5.585494","name":"","repeat":"","crontab":"","once":true,"topic":"","payload":"true","payloadType":"bool","x":130,"y":320,"wires":[["6734c083.6a93c"]]},{"id":"61bb503e.6ca1","type":"debug","z":"12c413b5.585494","name":"","active":true,"console":"false","complete":"false","x":590,"y":480,"wires":[]},{"id":"37648ff7.3d875","type":"ui_chart","z":"12c413b5.585494","name":"","group":"5c2f5188.6d47f8","order":3,"width":0,"height":0,"label":"Polar Area Chart","chartType":"polar-area","legend":"false","xformat":"HH:mm:ss","interpolate":"linear","nodata":"","dot":false,"ymin":"","ymax":"","removeOlder":1,"removeOlderPoints":"","removeOlderUnit":"3600","cutout":0,"useOneColor":false,"colors":["#1f77b4","#aec7e8","#ff7f0e","#2ca02c","#98df8a","#d62728","#ff9896","#9467bd","#c5b0d5"],"outputs":2,"x":610,"y":400.9090881347656,"wires":[[],[]]},{"id":"ba58094c.aab88","type":"ui_chart","z":"12c413b5.585494","name":"","group":"5c2f5188.6d47f8","order":4,"width":0,"height":0,"label":"Radar Chart","chartType":"radar","legend":"false","xformat":"HH:mm:ss","interpolate":"linear","nodata":"","dot":false,"ymin":"","ymax":"","removeOlder":1,"removeOlderPoints":"","removeOlderUnit":"3600","cutout":0,"useOneColor":false,"colors":["#1f77b4","#aec7e8","#ff7f0e","#2ca02c","#98df8a","#d62728","#ff9896","#9467bd","#c5b0d5"],"outputs":2,"x":590,"y":440,"wires":[[],[]]},{"id":"74e87c77.7a50dc","type":"ui_chart","z":"12c413b5.585494","name":"","group":"5c2f5188.6d47f8","order":2,"width":0,"height":0,"label":"Donut Chart","chartType":"pie","legend":"false","xformat":"HH:mm:ss","interpolate":"linear","nodata":"","dot":false,"ymin":"","ymax":"","removeOlder":1,"removeOlderPoints":"","removeOlderUnit":"3600","cutout":"40","useOneColor":false,"colors":["#1f77b4","#aec7e8","#ff7f0e","#2ca02c","#98df8a","#d62728","#ff9896","#9467bd","#c5b0d5"],"outputs":2,"x":590,"y":360,"wires":[[],[]]},{"id":"89bfd092.f8bc18","type":"ui_group","name":"Line Chart Examples","tab":"642c5bf0.321384","order":1,"disp":true,"width":"8"},{"id":"d6d5b6c0.39742","type":"ui_group","name":"Bar Chart Examples","tab":"642c5bf0.321384","order":3,"disp":true,"width":"8"},{"id":"5c2f5188.6d47f8","type":"ui_group","name":"Pie Chart Examples","tab":"642c5bf0.321384","order":2,"disp":true,"width":"6"},{"id":"642c5bf0.321384","type":"ui_tab","name":"Chart Examples","icon":"fa-area-chart","order":1}]

2023年9月29日 星期五

Node-Red 利用亂數產生 溫度 濕度 給MQTT , 接收MQTT的溫度 濕度 顯示於Dashboard UI上

 Node-Red 利用亂數產生 溫度 濕度 給MQTT ,  接收MQTT的溫度 濕度 顯示於Dashboard UI上





[{"id":"1ce19c7d.1392e4","type":"debug","z":"4ce6f98c.254bf","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":390,"y":200,"wires":[]},{"id":"49ce87c1.4ec838","type":"change","z":"4ce6f98c.254bf","name":"Chart Temperature","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.d.temp","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":410,"y":60,"wires":[["1d4ec5b5.82c9f2"]]},{"id":"2f57084.22acd78","type":"change","z":"4ce6f98c.254bf","name":"Chart Humidity","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.d.humidity","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":400,"y":120,"wires":[["fda5229a.103b8"]]},{"id":"d7a2c22e.f27d1","type":"switch","z":"4ce6f98c.254bf","name":"Warn on High Values","property":"payload.d.temp","propertyType":"msg","rules":[{"t":"gt","v":"30","vt":"num"}],"checkall":"true","repair":false,"outputs":1,"x":420,"y":160,"wires":[["732cea1.a542294"]]},{"id":"732cea1.a542294","type":"template","z":"4ce6f98c.254bf","name":"Alert Msg","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"Alert : Critical Value Detected {{payload.d.temp}}","output":"str","x":600,"y":160,"wires":[["80bbdd78.c69ae8"]]},{"id":"1d4ec5b5.82c9f2","type":"ui_chart","z":"4ce6f98c.254bf","name":"","group":"1bb2d88d.8be177","order":1,"width":0,"height":0,"label":"DHT Temp","chartType":"line","legend":"false","xformat":"HH:mm:ss","interpolate":"linear","nodata":"","dot":false,"ymin":"0","ymax":"50","removeOlder":"10","removeOlderPoints":"","removeOlderUnit":"60","cutout":0,"useOneColor":false,"colors":["#1f77b4","#aec7e8","#ff7f0e","#2ca02c","#98df8a","#d62728","#ff9896","#9467bd","#c5b0d5"],"outputs":1,"x":610,"y":60,"wires":[[]]},{"id":"fda5229a.103b8","type":"ui_chart","z":"4ce6f98c.254bf","name":"","group":"1bb2d88d.8be177","order":2,"width":0,"height":0,"label":"DHT Humidity","chartType":"line","legend":"false","xformat":"HH:mm:ss","interpolate":"linear","nodata":"","dot":false,"ymin":"0","ymax":"100","removeOlder":"10","removeOlderPoints":"","removeOlderUnit":"60","cutout":0,"useOneColor":false,"useUTC":false,"colors":["#1f77b4","#aec7e8","#ff7f0e","#2ca02c","#98df8a","#d62728","#ff9896","#9467bd","#c5b0d5"],"outputs":1,"useDifferentColor":false,"className":"","x":620,"y":120,"wires":[[]]},{"id":"80bbdd78.c69ae8","type":"ui_toast","z":"4ce6f98c.254bf","position":"top right","displayTime":"5","highlight":"","outputs":0,"ok":"OK","cancel":"","topic":"","name":"","x":770,"y":160,"wires":[]},{"id":"46f3a55.224d15c","type":"mqtt in","z":"4ce6f98c.254bf","name":"","topic":"alex9ufo/iot-2/evt/status/fmt/json/+","qos":"2","datatype":"json","broker":"841df58d.ee5e98","nl":false,"rap":false,"inputs":0,"x":160,"y":180,"wires":[["1ce19c7d.1392e4","d7a2c22e.f27d1"]]},{"id":"1194ab7312cf9746","type":"mqtt out","z":"4ce6f98c.254bf","name":"Temp","topic":"alex9ufo/iot-2/evt/status/fmt/json/temp","qos":"1","retain":"true","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"841df58d.ee5e98","x":570,"y":340,"wires":[]},{"id":"d25b400442aa7613","type":"ui_switch","z":"4ce6f98c.254bf","name":"","label":"Switch","tooltip":"","group":"1bb2d88d.8be177","order":2,"width":0,"height":0,"passthru":true,"decouple":"false","topic":"","topicType":"str","style":"","onvalue":"true","onvalueType":"bool","onicon":"","oncolor":"","offvalue":"false","offvalueType":"bool","officon":"","offcolor":"","animate":true,"className":"","x":230,"y":360,"wires":[["402ceecea723bf3f","c3517a29fc190ba1"]]},{"id":"402ceecea723bf3f","type":"function","z":"4ce6f98c.254bf","name":"Random #s","func":"if( msg.payload === false ) {\n    return null;\n}\n\nconst max = 48\nconst min= 10\n\nmsg.payload = Math.round(Math.random()*(max - min) + min);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":390,"y":340,"wires":[["1194ab7312cf9746","62f7ebeb6cac163c"]]},{"id":"b002857713981950","type":"inject","z":"4ce6f98c.254bf","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"5","crontab":"","once":false,"onceDelay":"","topic":"","payload":"","payloadType":"date","x":100,"y":360,"wires":[["d25b400442aa7613"]]},{"id":"c3517a29fc190ba1","type":"function","z":"4ce6f98c.254bf","name":"Random #s","func":"if( msg.payload === false ) {\n    return null;\n}\nmsg.payload = Math.round(Math.random()*100);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":390,"y":400,"wires":[["498875a7d2184286","2d6d36c45c830afa"]]},{"id":"498875a7d2184286","type":"mqtt out","z":"4ce6f98c.254bf","name":"Humi","topic":"alex9ufo/iot-2/evt/status/fmt/json/humi","qos":"2","retain":"true","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"841df58d.ee5e98","x":570,"y":400,"wires":[]},{"id":"62f7ebeb6cac163c","type":"debug","z":"4ce6f98c.254bf","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":590,"y":300,"wires":[]},{"id":"2d6d36c45c830afa","type":"debug","z":"4ce6f98c.254bf","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":590,"y":440,"wires":[]},{"id":"6d4bcb15e8043a96","type":"mqtt in","z":"4ce6f98c.254bf","name":"Temp","topic":"alex9ufo/iot-2/evt/status/fmt/json/temp","qos":"2","datatype":"json","broker":"841df58d.ee5e98","nl":false,"rap":false,"inputs":0,"x":70,"y":60,"wires":[["49ce87c1.4ec838"]]},{"id":"b7565f4395ce8725","type":"mqtt in","z":"4ce6f98c.254bf","name":"Humi","topic":"alex9ufo/iot-2/evt/status/fmt/json/humi","qos":"2","datatype":"json","broker":"841df58d.ee5e98","nl":false,"rap":false,"inputs":0,"x":70,"y":120,"wires":[["2f57084.22acd78"]]},{"id":"1bb2d88d.8be177","type":"ui_group","name":"ESP8266 Sensors","tab":"88fe602a.e1955","order":1,"disp":true,"width":"6","collapse":false},{"id":"841df58d.ee5e98","type":"mqtt-broker","name":"","broker":"broker.mqtt-dashboard.com","port":"1883","clientid":"","autoConnect":true,"usetls":false,"compatmode":false,"protocolVersion":4,"keepalive":"15","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"88fe602a.e1955","type":"ui_tab","name":"ESP8266 Workshop","icon":"dashboard","order":3}]

Node-Red Dashboard

 Node-Red Dashboard 



 

節點

設定

Inject

Msg.payload :Use the toggle switch to start / stop the generation of random numbers

text

{{msg.payload}}

 

inject

週期性 1秒執行一次

switch

On payload :  true

Off payload :  false

function

Random #s

if( msg.payload === false ) {

    return null;

}

msg.payload = Math.round(Math.random()*100);

return msg;

chart

 


Gauge

 

Slider

 


Text

 

Switch

msg.payload > 90

template

msg.payload : Alert : Critical Value Detected {{payload}}

notification

Top right

 


[{"id":"2aeb3b7c.7a1e5c","type":"inject","z":"47a8c4a5.ba4714","name":"","props":[{"p":"payload","v":"","vt":"date"},{"p":"topic","v":"","vt":"str"}],"repeat":"1","crontab":"","once":false,"topic":"","payload":"","payloadType":"date","x":110,"y":140,"wires":[["cc96c240.90a93"]]},{"id":"1910e637.2cfbea","type":"function","z":"47a8c4a5.ba4714","name":"Random #s","func":"if( msg.payload === false ) {\n    return null;\n}\nmsg.payload = Math.round(Math.random()*100);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":410,"y":140,"wires":[["bc218cf4.508e2"]]},{"id":"bc10b2e7.c144f8","type":"ui_chart","z":"47a8c4a5.ba4714","name":"Chart","group":"1454ffb1.6e2f","order":1,"width":0,"height":0,"label":"Random #","chartType":"line","legend":"false","xformat":"HH:mm:ss","interpolate":"linear","nodata":"Querying Entropy","dot":false,"ymin":"0","ymax":"100","removeOlder":"1","removeOlderPoints":"","removeOlderUnit":"60","cutout":"","useOneColor":false,"colors":["#1f77b4","#aec7e8","#ff7f0e","#2ca02c","#98df8a","#d62728","#ff9896","#9467bd","#c5b0d5"],"outputs":1,"x":350,"y":240,"wires":[[]]},{"id":"557c8db7.8c2cd4","type":"ui_gauge","z":"47a8c4a5.ba4714","name":"Gauge","group":"1454ffb1.6e2f","order":2,"width":0,"height":0,"gtype":"gage","title":"Gauge","label":"units","format":"{{value}}","min":0,"max":"100","colors":["#00b500","#e6e600","#ca3838"],"seg1":"","seg2":"","className":"","x":350,"y":280,"wires":[]},{"id":"7f62527e.e7067c","type":"ui_slider","z":"47a8c4a5.ba4714","name":"","label":"Slider","group":"1454ffb1.6e2f","order":3,"width":0,"height":0,"passthru":true,"topic":"","min":0,"max":"100","step":"","x":350,"y":320,"wires":[[]]},{"id":"45ef900.f6932f","type":"ui_text","z":"47a8c4a5.ba4714","group":"1454ffb1.6e2f","order":4,"width":0,"height":0,"name":"","label":"Text","format":"{{msg.payload}}","layout":"row-spread","x":350,"y":360,"wires":[]},{"id":"cc96c240.90a93","type":"ui_switch","z":"47a8c4a5.ba4714","name":"","label":"Switch","tooltip":"","group":"ed1d5d5f.dc5c58","order":2,"width":0,"height":0,"passthru":true,"decouple":"false","topic":"","topicType":"str","style":"","onvalue":"true","onvalueType":"bool","onicon":"","oncolor":"","offvalue":"false","offvalueType":"bool","officon":"","offcolor":"","animate":true,"className":"","x":270,"y":140,"wires":[["1910e637.2cfbea"]]},{"id":"ba7045e9.bad2","type":"ui_text","z":"47a8c4a5.ba4714","group":"ed1d5d5f.dc5c58","order":1,"width":0,"height":0,"name":"","label":"","format":"{{msg.payload}}","layout":"row-spread","className":"","x":270,"y":60,"wires":[]},{"id":"8f48941c.73a7f","type":"inject","z":"47a8c4a5.ba4714","name":"Instructions","props":[{"p":"payload","v":"Use the toggle switch to start / stop the generation of random numbers","vt":"str"},{"p":"topic","v":"","vt":"str"}],"repeat":"","crontab":"","once":true,"topic":"","payload":"Use the toggle switch to start / stop the generation of random numbers","payloadType":"str","x":110,"y":60,"wires":[["ba7045e9.bad2"]]},{"id":"e24396b6.4fe748","type":"ui_toast","z":"47a8c4a5.ba4714","position":"top right","displayTime":"5","highlight":"","sendall":true,"outputs":0,"ok":"OK","cancel":"","raw":false,"className":"","topic":"","name":"","x":790,"y":400,"wires":[]},{"id":"821084fb.210548","type":"switch","z":"47a8c4a5.ba4714","name":"Warn on High Values","property":"payload","propertyType":"msg","rules":[{"t":"gt","v":"90","vt":"num"}],"checkall":"true","repair":false,"outputs":1,"x":400,"y":400,"wires":[["9de2c880.141548"]]},{"id":"9de2c880.141548","type":"template","z":"47a8c4a5.ba4714","name":"Alert Msg","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"Alert : Critical Value Detected {{payload}}","output":"str","x":600,"y":400,"wires":[["e24396b6.4fe748"]]},{"id":"bc218cf4.508e2","type":"link out","z":"47a8c4a5.ba4714","name":"","links":["5ecb2b4b.05bf4c"],"x":555,"y":140,"wires":[]},{"id":"5ecb2b4b.05bf4c","type":"link in","z":"47a8c4a5.ba4714","name":"","links":["bc218cf4.508e2"],"x":115,"y":320,"wires":[["bc10b2e7.c144f8","557c8db7.8c2cd4","7f62527e.e7067c","45ef900.f6932f","821084fb.210548"]]},{"id":"1454ffb1.6e2f","type":"ui_group","z":"47a8c4a5.ba4714","name":"Dashboard Controls","tab":"f65c9267.a2ce5","order":2,"disp":true,"width":"6"},{"id":"ed1d5d5f.dc5c58","type":"ui_group","name":"Start / Stop","tab":"f65c9267.a2ce5","order":1,"disp":true,"width":"6"},{"id":"f65c9267.a2ce5","type":"ui_tab","z":"47a8c4a5.ba4714","name":"Introduction","icon":"dashboard","order":2}]

ESP32 MQTT & MQTT Box Control ESP32 build LED on or off

 ESP32 MQTT & MQTT Box  Control ESP32 build LED on or off

系統圖


MQTT Box 設定的畫面

const char *mqtt_server = "broker.mqtt-dashboard.com";
const char *mqtt_username = "alex9ufo";
const char *mqtt_password = "public";
const int mqtt_port = 1883;
const char *topic1 = "alex9ufo/ESP32/LED_output";
const char *topic2 = "alex9ufo/ESP32/LED_status";







ESP32 Arduino 程式

#include <WiFi.h>
#include <PubSubClient.h>

// Replace the next variables with your SSID/Password combination
//const char* ssid = "XXXXXXXXX";
//const char* password = "XXXXXXXXX";
const char* ssid = "alex9ufo"; // Enter your Wi-Fi name
const char* password = "alex9981";  // Enter Wi-Fi password


// Add your MQTT Broker IP address, example:
//const char* mqtt_server = "192.168.1.144";
const char* mqtt_server = "broker.mqtt-dashboard.com";
const char *mqtt_username = "alex9ufo";
const char *mqtt_password = "public";
const int mqtt_port = 1883;


WiFiClient espClient;
PubSubClient client(espClient);
char msg[50];
const char *topic1 = "alex9ufo/ESP32/LED_output";
const char *topic2 = "alex9ufo/ESP32/LED_status";

// LED Pin
#define BUILTIN_LED 2
String json = "";     //client.publish("alex9ufo/led/led_status",
char jsonChar1[20];
bool Send = false;  //true

//宣告任務Task1
TaskHandle_t Task1;
//===========================================================
//任務1副程式Task1_senddata
void Task1_senddata(void * pvParameters ) {
  //無窮迴圈
  for (;;) {
   
  if (client.connected()) {        
    if (Send) {
      // Convert JSON string to character array
      json.toCharArray(jsonChar1, json.length()+1);
      Serial.print("Publish message: ");
      Serial.println(json);
      // Publish JSON character array to MQTT topic
      client.publish(topic2,jsonChar1); //"alex9ufo/esp32/led_status";
    }
      Send = false;    
  }
  else
  {
    if (WiFi.status() != WL_CONNECTED)  {
      Serial.println("Reconnecting to WiFi...");
      WiFi.disconnect();
       WiFi.reconnect();
    }
  }

  //Task1休息,delay(X)不可省略
  delay(1000);
  }
}
//==========================================
void setup() {
  Serial.begin(115200);
  // default settings
  delay(500);
  setup_wifi();
  client.setServer(mqtt_server, mqtt_port);
  client.setCallback(callback);
  while (!client.connected()) {
    String client_id = "esp32-client-";
    client_id += String(WiFi.macAddress());
    Serial.printf("The client %s connects to the public MQTT broker\n", client_id.c_str());
    if (client.connect(client_id.c_str(), mqtt_username, mqtt_password)) {
        Serial.println("Public HiveMQ MQTT broker (broker.mqtt-dashboard.com) connected");
    } else {
        Serial.print("Failed with state ");
        Serial.print(client.state());
        delay(2000);
    }
  }

  // Publish and subscribe
  client.subscribe(topic1);

  pinMode(BUILTIN_LED, OUTPUT);


  //在核心0啟動任務1
  xTaskCreatePinnedToCore(
  Task1_senddata, /*任務實際對應的Function*/
    "Task1",        /*任務名稱*/
    10000,          /*堆疊空間*/
    NULL,           /*無輸入值*/
    0,              /*優先序0*/
    &Task1,         /*對應的任務變數位址*/
    0);             /*指定在核心0執行 */

}
//==========================================
void setup_wifi() {
  delay(10);
  // We start by connecting to a WiFi network
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("ESP32 WiFi connected to ");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}
//==========================================
void callback(char* topic, byte* message, unsigned int length) {
  Serial.print("Message arrived on topic: ");
  Serial.print(topic);
  Serial.print(". Message: ");
  String messageTemp;
 
  for (int i = 0; i < length; i++) {
    Serial.print((char)message[i]);
    messageTemp += (char)message[i];
  }
  Serial.println();

  // Feel free to add more if statements to control more GPIOs with MQTT

  // If a message is received on the topic esp32/output, you check if the message is either "on" or "off".
  // Changes the output state according to the message
  if (String(topic) == "alex9ufo/ESP32/LED_output")
  {
    Serial.print("Changing output to ");
    if(messageTemp == "on"){
      Serial.println("on");
      digitalWrite(BUILTIN_LED, HIGH);
      json ="LED ON";
      Send = true ;
    }
    else if(messageTemp == "off"){
      Serial.println("off");
      digitalWrite(BUILTIN_LED, LOW);
      json ="LED OFF";
      Send = true ;
    }
   
  }

}
//==========================================
void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect
    if (client.connect("ESP32Client")) {
      Serial.println("connected");
      // Subscribe
      client.subscribe(topic1);
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}
//==========================================
void loop()
{
  if (!client.connected()) {
    reconnect();
    Serial.print(" client not connected  reconnect ");
    delay(200);
  }
  client.loop();

}
//=====================================================

2023年9月28日 星期四

Connecting Node-Red and MQTT Broker

 Connecting Node-Red and MQTT Broker

系統方塊圖







Node-Red  節點

節點

設定內容

備註

 on Button  

Label : ON  (大寫)

Payload : on (小寫)

 

 off Button

Label : OFF (大寫)

Payload : off (小寫)

 

 溫度 Text input

Mode : number

Delay : 300 ms

 

 

MQTT out 的設定





MQTT in 的設定




 Function

var a=msg.payload;

msg.payload='';

 

if (a=='on')

    msg.payload=true

if (a=='off')

    msg.payload=false

 

return msg;

 


LED



TEXT









Dashboard 的Layout 設定




Node-Red 程式

[{"id":"676dee5a541d2c6a","type":"ui_button","z":"9febb32c6c5543b0","name":"","group":"75ebc44c62276bfe","order":9,"width":3,"height":1,"passthru":false,"label":"ON","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"on","payloadType":"str","topic":"topic","topicType":"msg","x":230,"y":160,"wires":[["ba9dfc51c58a437c"]]},{"id":"a8f4413b32230f05","type":"ui_button","z":"9febb32c6c5543b0","name":"","group":"75ebc44c62276bfe","order":10,"width":3,"height":1,"passthru":false,"label":"OFF","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"off","payloadType":"str","topic":"topic","topicType":"msg","x":230,"y":220,"wires":[["ba9dfc51c58a437c"]]},{"id":"441c871dacc833a4","type":"ui_text_input","z":"9febb32c6c5543b0","name":"","label":"溫度","tooltip":"請輸入溫度","group":"75ebc44c62276bfe","order":11,"width":3,"height":1,"passthru":true,"mode":"number","delay":300,"topic":"topic","sendOnBlur":true,"className":"","topicType":"msg","x":230,"y":280,"wires":[["af2b617f0f818c67"]]},{"id":"ba9dfc51c58a437c","type":"mqtt out","z":"9febb32c6c5543b0","name":"ESP32 LED","topic":"alex9ufo/esp32/led","qos":"1","retain":"true","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"841df58d.ee5e98","x":450,"y":180,"wires":[]},{"id":"af2b617f0f818c67","type":"mqtt out","z":"9febb32c6c5543b0","name":"ESP32 Temperature","topic":"alex9ufo/esp32/temp","qos":"2","retain":"true","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"841df58d.ee5e98","x":480,"y":280,"wires":[]},{"id":"7f44805cc86fcf5b","type":"mqtt in","z":"9febb32c6c5543b0","name":"","topic":"alex9ufo/esp32/led","qos":"1","datatype":"auto-detect","broker":"841df58d.ee5e98","nl":false,"rap":true,"rh":0,"inputs":0,"x":270,"y":380,"wires":[["148e347b8294a3a1","6184060c2ae79db0"]]},{"id":"d8a4773e94b15f3d","type":"ui_led","z":"9febb32c6c5543b0","order":2,"group":"75ebc44c62276bfe","width":5,"height":2,"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":380,"wires":[]},{"id":"148e347b8294a3a1","type":"function","z":"9febb32c6c5543b0","name":"function ","func":"var a=msg.payload;\nmsg.payload='';\n\nif (a=='on')\n    msg.payload=true\nif (a=='off')\n    msg.payload=false\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":460,"y":380,"wires":[["d8a4773e94b15f3d","f5fa640f6a30b3b9"]]},{"id":"b3958b5fed7b60d1","type":"mqtt in","z":"9febb32c6c5543b0","name":"","topic":"alex9ufo/esp32/temp","qos":"2","datatype":"auto-detect","broker":"841df58d.ee5e98","nl":false,"rap":true,"rh":0,"inputs":0,"x":270,"y":460,"wires":[["0038c9c971028389"]]},{"id":"0038c9c971028389","type":"ui_text","z":"9febb32c6c5543b0","group":"75ebc44c62276bfe","order":6,"width":0,"height":0,"name":"","label":"溫度","format":"{{msg.payload}}","layout":"row-left","className":"","x":450,"y":460,"wires":[]},{"id":"6184060c2ae79db0","type":"debug","z":"9febb32c6c5543b0","name":"debug ","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":450,"y":420,"wires":[]},{"id":"f5fa640f6a30b3b9","type":"debug","z":"9febb32c6c5543b0","name":"debug 196","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":630,"y":340,"wires":[]},{"id":"75ebc44c62276bfe","type":"ui_group","name":"Default","tab":"f62d63d55905ecd0","order":1,"disp":true,"width":9,"collapse":false,"className":""},{"id":"841df58d.ee5e98","type":"mqtt-broker","name":"","broker":"broker.mqtt-dashboard.com","port":"1883","clientid":"","autoConnect":true,"usetls":false,"compatmode":false,"protocolVersion":4,"keepalive":"15","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"f62d63d55905ecd0","type":"ui_tab","name":"Home_MQTT","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...