2020年12月4日 星期五

Using the rbe (report by exception) node & Scaling input with the range node

Using the rbe (report by exception) node & Scaling input with the range node

To test the flow, deploy this flow and then return to the HiveMQ page and send a series of messages. Firstly, you’ll need to set the analyze value to true so that the switch node sends through a message on output 1. If you use the original message value of 6, this will fail to pass the rbe node. If you then send a second message of value 10, the  rbe node  will evaluate the difference between 6 and 10, see it is greater than 20%, and send on a message to the final debug node


{"analyze": true , "value": 10 }

{"analyze": true , "value":  5 }

5 , 10  greater than 20%,







Let’s assume you want to scale your value (originally in the range 0-10) to a range (0-255) when you aren’t doing any analysis. This means we are dealing with the lower part of the flow fired when the switch node evaluates the Analyze property as false.

Then return to the HiveMQ test page and post {“analyze”:false, “value”:10} as a new MQTT message to the same topic.


{"analyze": false , "value": 10 }

{"analyze": false , "value":  5 }








[{"id":"995c991a.566ba8","type":"mqtt in","z":"2b3214e5.a3a7cc","name":"","topic":"alex9ufo/sensor1/outTopic","qos":"2","datatype":"auto","broker":"e4d9b72d.d14398","x":330,"y":320,"wires":[["ba25f21f.ffa71","d25fff9a.fa3ac"]]},{"id":"ba25f21f.ffa71","type":"json","z":"2b3214e5.a3a7cc","name":"","property":"payload","action":"","pretty":false,"x":530,"y":160,"wires":[["9660236a.9c53d","1d645c6b.ebe424","aecc5427.7bfa98"]]},{"id":"9660236a.9c53d","type":"debug","z":"2b3214e5.a3a7cc","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":730,"y":100,"wires":[]},{"id":"f55b4a38.72baf8","type":"mqtt out","z":"2b3214e5.a3a7cc","name":"","topic":"alex9ufo/sensor2/inTopic","qos":"1","retain":"","broker":"e4d9b72d.d14398","x":790,"y":640,"wires":[]},{"id":"1175a0c6.d4ca9f","type":"function","z":"2b3214e5.a3a7cc","name":"","func":"var outstr={\"analyze\": true ,\"value\":50};\nmsg.payload= outstr;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":470,"y":640,"wires":[["4582efd.f2bb31"]]},{"id":"4582efd.f2bb31","type":"json","z":"2b3214e5.a3a7cc","name":"","property":"payload","action":"","pretty":false,"x":590,"y":640,"wires":[["f55b4a38.72baf8","3fb07ae9.09b6b6"]]},{"id":"19c954a9.3f835b","type":"inject","z":"2b3214e5.a3a7cc","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":330,"y":640,"wires":[["1175a0c6.d4ca9f"]]},{"id":"3fb07ae9.09b6b6","type":"debug","z":"2b3214e5.a3a7cc","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":750,"y":700,"wires":[]},{"id":"fc8bf9ea.d0bde8","type":"switch","z":"2b3214e5.a3a7cc","name":"","property":"payload","propertyType":"msg","rules":[{"t":"true"},{"t":"false"}],"checkall":"true","repair":false,"outputs":2,"x":690,"y":220,"wires":[["7ae78527.7c838c","9a089abc.740618"],["3f794d9.0b3f8b2","70c4abed.4d5cd4"]]},{"id":"7ae78527.7c838c","type":"debug","z":"2b3214e5.a3a7cc","name":"","active":true,"console":"false","complete":"false","x":990,"y":200,"wires":[]},{"id":"3f794d9.0b3f8b2","type":"debug","z":"2b3214e5.a3a7cc","name":"","active":true,"tosidebar":true,"console":false,"complete":"false","statusVal":"","statusType":"auto","x":990,"y":240,"wires":[]},{"id":"9a089abc.740618","type":"ui_led","z":"2b3214e5.a3a7cc","group":"57fb2f24.9f1f1","order":11,"width":"0","height":"0","label":"LED1","labelPlacement":"left","labelAlignment":"right","colorForValue":[{"color":"red","value":"false","valueType":"bool"},{"color":"green","value":"true","valueType":"bool"}],"allowColorForValueInMessage":false,"name":"","x":850,"y":180,"wires":[]},{"id":"70c4abed.4d5cd4","type":"ui_led","z":"2b3214e5.a3a7cc","group":"57fb2f24.9f1f1","order":12,"width":"0","height":"0","label":"LED2","labelPlacement":"left","labelAlignment":"right","colorForValue":[{"color":"red","value":"false","valueType":"bool"},{"color":"green","value":"true","valueType":"bool"}],"allowColorForValueInMessage":false,"name":"","x":850,"y":260,"wires":[]},{"id":"fee116dd.9f9388","type":"ui_gauge","z":"2b3214e5.a3a7cc","name":"","group":"57fb2f24.9f1f1","order":13,"width":0,"height":0,"gtype":"gage","title":"gauge","label":"units","format":"{{value}}","min":0,"max":"100","colors":["#00b500","#e6e600","#ca3838"],"seg1":"35","seg2":"75","x":830,"y":140,"wires":[]},{"id":"1d645c6b.ebe424","type":"function","z":"2b3214e5.a3a7cc","name":"","func":"msg.payload=msg.payload.value;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":710,"y":140,"wires":[["fee116dd.9f9388"]]},{"id":"aecc5427.7bfa98","type":"function","z":"2b3214e5.a3a7cc","name":"","func":"msg.payload=msg.payload.analyze;\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":570,"y":220,"wires":[["fc8bf9ea.d0bde8"]]},{"id":"6b427479.ba4efc","type":"debug","z":"2b3214e5.a3a7cc","name":"","active":true,"console":"false","complete":"payload","x":970,"y":380,"wires":[]},{"id":"747bc901.5c0a18","type":"switch","z":"2b3214e5.a3a7cc","name":"","property":"payload.analyze","rules":[{"t":"true"},{"t":"false"}],"checkall":"true","outputs":2,"x":490,"y":380,"wires":[["48c37be1.39b3a4"],["b70e8a30.940338","59a38158.dba48"]]},{"id":"d25fff9a.fa3ac","type":"json","z":"2b3214e5.a3a7cc","name":"","x":370,"y":380,"wires":[["747bc901.5c0a18"]]},{"id":"dc82faf5.70bb98","type":"comment","z":"2b3214e5.a3a7cc","name":"Analyze = true","info":"","x":630,"y":320,"wires":[]},{"id":"6b1cc2cc.18652c","type":"comment","z":"2b3214e5.a3a7cc","name":"Analyze = false","info":"","x":660,"y":540,"wires":[]},{"id":"d39edec8.2ddb6","type":"rbe","z":"2b3214e5.a3a7cc","name":"rbe","func":"deadband","gap":"20%","start":"","inout":"out","property":"payload","x":830,"y":380,"wires":[["6b427479.ba4efc"]]},{"id":"48c37be1.39b3a4","type":"change","z":"2b3214e5.a3a7cc","name":"","rules":[{"t":"set","p":"payload","to":"payload.value","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":670,"y":380,"wires":[["d39edec8.2ddb6"]]},{"id":"bd7a244c.921f88","type":"range","z":"2b3214e5.a3a7cc","minin":"0","maxin":"10","minout":"0","maxout":"255","action":"clamp","round":false,"property":"payload","name":"","x":840,"y":500,"wires":[["f688d424.ab7db8"]]},{"id":"59a38158.dba48","type":"change","z":"2b3214e5.a3a7cc","name":"","rules":[{"t":"set","p":"payload","to":"payload.value","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":670,"y":500,"wires":[["bd7a244c.921f88"]]},{"id":"f688d424.ab7db8","type":"debug","z":"2b3214e5.a3a7cc","name":"","active":true,"console":"false","complete":"false","x":990,"y":500,"wires":[]},{"id":"b70e8a30.940338","type":"change","z":"2b3214e5.a3a7cc","name":"","rules":[{"t":"set","p":"payload.note","to":"this is not being analysed"}],"action":"","property":"","from":"","to":"","reg":false,"x":680,"y":440,"wires":[["a635a98.bfe5758"]]},{"id":"a635a98.bfe5758","type":"debug","z":"2b3214e5.a3a7cc","name":"","active":true,"console":"false","complete":"false","x":870,"y":440,"wires":[]},{"id":"e4d9b72d.d14398","type":"mqtt-broker","z":"","name":"","broker":"broker.mqtt-dashboard.com","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"15","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"57fb2f24.9f1f1","type":"ui_group","z":"","name":"ON OFF 1","tab":"6f605bb4.60a174","order":1,"disp":true,"width":"6","collapse":false},{"id":"6f605bb4.60a174","type":"ui_tab","z":"","name":"Home","icon":"dashboard"}]

Using the switch node to handle a JSON object

利用 開關節點處理Json物件


If you now go back to the HiveMQ input page and send the MQTT message {“analyze”:true, “value”:6}, you will see that the first (top) output is activated and the incoming messages is routed, or ‘switched’, to output 1. If you send the original message {“analyze”:false, “value”:10}, the switch node will activate output 2 and the original debug node will fire.


{"analyze": false , "value":70}

http://www.hivemq.com/demos/websocket-client/




{"analyze": true , "value": 20}

http://www.hivemq.com/demos/websocket-client/




[{"id":"995c991a.566ba8","type":"mqtt in","z":"2b3214e5.a3a7cc","name":"","topic":"alex9ufo/sensor1/outTopic","qos":"2","datatype":"auto","broker":"e4d9b72d.d14398","x":350,"y":180,"wires":[["ba25f21f.ffa71"]]},{"id":"ba25f21f.ffa71","type":"json","z":"2b3214e5.a3a7cc","name":"","property":"payload","action":"","pretty":false,"x":530,"y":180,"wires":[["9660236a.9c53d","1d645c6b.ebe424","aecc5427.7bfa98"]]},{"id":"9660236a.9c53d","type":"debug","z":"2b3214e5.a3a7cc","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":730,"y":100,"wires":[]},{"id":"f55b4a38.72baf8","type":"mqtt out","z":"2b3214e5.a3a7cc","name":"","topic":"alex9ufo/sensor2/inTopic","qos":"1","retain":"","broker":"e4d9b72d.d14398","x":770,"y":360,"wires":[]},{"id":"1175a0c6.d4ca9f","type":"function","z":"2b3214e5.a3a7cc","name":"","func":"var outstr={\"analyze\": true ,\"value\":50};\nmsg.payload= outstr;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":450,"y":360,"wires":[["4582efd.f2bb31"]]},{"id":"4582efd.f2bb31","type":"json","z":"2b3214e5.a3a7cc","name":"","property":"payload","action":"","pretty":false,"x":570,"y":360,"wires":[["f55b4a38.72baf8","3fb07ae9.09b6b6"]]},{"id":"19c954a9.3f835b","type":"inject","z":"2b3214e5.a3a7cc","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":310,"y":360,"wires":[["1175a0c6.d4ca9f"]]},{"id":"3fb07ae9.09b6b6","type":"debug","z":"2b3214e5.a3a7cc","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":730,"y":420,"wires":[]},{"id":"fc8bf9ea.d0bde8","type":"switch","z":"2b3214e5.a3a7cc","name":"","property":"payload","propertyType":"msg","rules":[{"t":"true"},{"t":"false"}],"checkall":"true","repair":false,"outputs":2,"x":690,"y":240,"wires":[["7ae78527.7c838c","9a089abc.740618"],["3f794d9.0b3f8b2","70c4abed.4d5cd4"]]},{"id":"7ae78527.7c838c","type":"debug","z":"2b3214e5.a3a7cc","name":"","active":true,"console":"false","complete":"false","x":870,"y":220,"wires":[]},{"id":"3f794d9.0b3f8b2","type":"debug","z":"2b3214e5.a3a7cc","name":"","active":true,"tosidebar":true,"console":false,"complete":"false","statusVal":"","statusType":"auto","x":870,"y":260,"wires":[]},{"id":"9a089abc.740618","type":"ui_led","z":"2b3214e5.a3a7cc","group":"57fb2f24.9f1f1","order":11,"width":"0","height":"0","label":"LED1","labelPlacement":"left","labelAlignment":"right","colorForValue":[{"color":"red","value":"false","valueType":"bool"},{"color":"green","value":"true","valueType":"bool"}],"allowColorForValueInMessage":false,"name":"","x":850,"y":180,"wires":[]},{"id":"70c4abed.4d5cd4","type":"ui_led","z":"2b3214e5.a3a7cc","group":"57fb2f24.9f1f1","order":12,"width":"0","height":"0","label":"LED2","labelPlacement":"left","labelAlignment":"right","colorForValue":[{"color":"red","value":"false","valueType":"bool"},{"color":"green","value":"true","valueType":"bool"}],"allowColorForValueInMessage":false,"name":"","x":850,"y":300,"wires":[]},{"id":"fee116dd.9f9388","type":"ui_gauge","z":"2b3214e5.a3a7cc","name":"","group":"57fb2f24.9f1f1","order":13,"width":0,"height":0,"gtype":"gage","title":"gauge","label":"units","format":"{{value}}","min":0,"max":"100","colors":["#00b500","#e6e600","#ca3838"],"seg1":"35","seg2":"75","x":830,"y":140,"wires":[]},{"id":"1d645c6b.ebe424","type":"function","z":"2b3214e5.a3a7cc","name":"","func":"msg.payload=msg.payload.value;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":710,"y":140,"wires":[["fee116dd.9f9388"]]},{"id":"aecc5427.7bfa98","type":"function","z":"2b3214e5.a3a7cc","name":"","func":"msg.payload=msg.payload.analyze;\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":570,"y":240,"wires":[["fc8bf9ea.d0bde8"]]},{"id":"e4d9b72d.d14398","type":"mqtt-broker","z":"","name":"","broker":"broker.mqtt-dashboard.com","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"15","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"57fb2f24.9f1f1","type":"ui_group","z":"","name":"ON OFF 1","tab":"6f605bb4.60a174","order":1,"disp":true,"width":"6","collapse":false},{"id":"6f605bb4.60a174","type":"ui_tab","z":"","name":"Home","icon":"dashboard"}]

2020年12月3日 星期四

Receiving a JSON object via MQTT messaging / Sending a JSON object via MQTT to Node-Red

接收http://www.hivemq.com/demos/websocket-client/ 來的Json資料, 發送Json資料 給http://www.hivemq.com/demos/websocket-client




 

Node-Red 上 收 hivemq 資料

alex9ufo/sensor1/outTopic 

broker.mqtt-dashboard.com Port:1883


hivemq上 收  Node-Red  資料

alex9ufo/sensor2/inTop

http://www.hivemq.com/demos/websocket-client/

broker.mqttdashboard.com  port:8000









[{"id":"995c991a.566ba8","type":"mqtt in","z":"2b3214e5.a3a7cc","name":"","topic":"alex9ufo/sensor1/outTopic","qos":"2","datatype":"auto","broker":"e4d9b72d.d14398","x":350,"y":180,"wires":[["ba25f21f.ffa71"]]},{"id":"ba25f21f.ffa71","type":"json","z":"2b3214e5.a3a7cc","name":"","property":"payload","action":"","pretty":false,"x":550,"y":180,"wires":[["9660236a.9c53d"]]},{"id":"9660236a.9c53d","type":"debug","z":"2b3214e5.a3a7cc","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":710,"y":180,"wires":[]},{"id":"f55b4a38.72baf8","type":"mqtt out","z":"2b3214e5.a3a7cc","name":"","topic":"alex9ufo/sensor2/inTopic","qos":"1","retain":"","broker":"e4d9b72d.d14398","x":830,"y":260,"wires":[]},{"id":"1175a0c6.d4ca9f","type":"function","z":"2b3214e5.a3a7cc","name":"","func":"var outstr={\"analyze\": true ,\"value\":50};\nmsg.payload= outstr;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":470,"y":260,"wires":[["4582efd.f2bb31"]]},{"id":"4582efd.f2bb31","type":"json","z":"2b3214e5.a3a7cc","name":"","property":"payload","action":"","pretty":false,"x":610,"y":260,"wires":[["f55b4a38.72baf8","3fb07ae9.09b6b6"]]},{"id":"19c954a9.3f835b","type":"inject","z":"2b3214e5.a3a7cc","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":330,"y":260,"wires":[["1175a0c6.d4ca9f"]]},{"id":"3fb07ae9.09b6b6","type":"debug","z":"2b3214e5.a3a7cc","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":790,"y":320,"wires":[]},{"id":"e4d9b72d.d14398","type":"mqtt-broker","z":"","name":"","broker":"broker.mqtt-dashboard.com","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"15","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]

2020年12月1日 星期二

以Windows安裝Node-RED 及dashboard UI 節點 SQLite 節點

 以Windows安裝Node-RED


1.先到Node.js   https://nodejs.org/dist./v12.18.3/   下載LTS版

x64.msi 與 x86.msi  版本太新node-red會有問題

Index of /dist./v12.18.3/


../
docs/                                              22-Jul-2020 15:34                   -
win-x64/                                           22-Jul-2020 15:21                   -
win-x86/                                           22-Jul-2020 15:20                   -
SHASUMS256.txt                                     22-Jul-2020 16:55                3147
SHASUMS256.txt.asc                                 22-Jul-2020 16:55                4029
SHASUMS256.txt.sig                                 22-Jul-2020 16:55                 566
node-v12.18.3-aix-ppc64.tar.gz                     22-Jul-2020 14:56            32361031
node-v12.18.3-darwin-x64.tar.gz                    22-Jul-2020 15:51            20889560
node-v12.18.3-darwin-x64.tar.xz                    22-Jul-2020 15:52            13790280
node-v12.18.3-headers.tar.gz                       22-Jul-2020 15:34              555440
node-v12.18.3-headers.tar.xz                       22-Jul-2020 15:34              366264
node-v12.18.3-linux-arm64.tar.gz                   22-Jul-2020 14:57            23059455
node-v12.18.3-linux-arm64.tar.xz                   22-Jul-2020 14:59            13914756
node-v12.18.3-linux-armv7l.tar.gz                  22-Jul-2020 15:01            21253435
node-v12.18.3-linux-armv7l.tar.xz                  22-Jul-2020 15:01            12505784
node-v12.18.3-linux-ppc64le.tar.gz                 22-Jul-2020 15:01            23224873
node-v12.18.3-linux-ppc64le.tar.xz                 22-Jul-2020 15:02            13856940
node-v12.18.3-linux-s390x.tar.gz                   22-Jul-2020 15:00            23529229
node-v12.18.3-linux-s390x.tar.xz                   22-Jul-2020 15:01            13972340
node-v12.18.3-linux-x64.tar.gz                     22-Jul-2020 15:00            23185868
node-v12.18.3-linux-x64.tar.xz                     22-Jul-2020 15:01            14700900
node-v12.18.3-sunos-x64.tar.gz                     22-Jul-2020 15:38            27414512
node-v12.18.3-sunos-x64.tar.xz                     22-Jul-2020 15:39            16893888
node-v12.18.3-win-x64.7z                           22-Jul-2020 15:22            10797087
node-v12.18.3-win-x64.zip                          22-Jul-2020 15:22            18743597
node-v12.18.3-win-x86.7z                           22-Jul-2020 15:21             9732315
node-v12.18.3-win-x86.zip                          22-Jul-2020 15:21            17223625
node-v12.18.3-x64.msi                              22-Jul-2020 15:22            19763200
node-v12.18.3-x86.msi                              22-Jul-2020 15:21            18153472
node-v12.18.3.pkg                                  22-Jul-2020 16:09            21183432
node-v12.18.3.tar.gz                               22-Jul-2020 15:27            52449074
node-v12.18.3.tar.xz                               22-Jul-2020 15:32            23679596






2.執行後一路下一步
https://ithelp.ithome.com.tw/upload/images/20181016/20112219NdAuwQwW0N.png



3.確認Node.Js與NPM版本



4.輸入 npm install -g --unsafe-perm node-red 安裝Node-RED
https://ithelp.ithome.com.tw/upload/images/20181016/20112219R2UY4wxHiB.png
5.輸入node-red啟動Node-RED
https://ithelp.ithome.com.tw/upload/images/20181016/20112219qYt2zzQqBS.png
或是 在C碟中 搜尋node-red 並將捷徑放到 桌面 直接用滑鼠 啟動





    

6.啟動瀏覽器 輸入127.0.0.1:1880
https://ithelp.ithome.com.tw/upload/images/20181016/20112219PvSQ9Mus5k.png
即可使用Node-RED

7.安裝節點 dashboard

   進入 安裝 輸入 dashboard 選擇node-red-dashboard






8.安裝節點 SQLite

輸入 sqlite 選擇node-red-node-sqlite 



9.匯入 node-red RFID 2-2程式


找到  5. Node-Red 程式 下載


 可以用wordpad 打開檔案 (Json檔案)


 匯入 所選檔案




   重新 部署


 



配合 硬體 進入127.0.0.1:1880/ui  使用者介面 控制 ESP32 





2020年11月30日 星期一

Reading data from a SQL Database Using Node-Red (讀取SQLite資料庫內容)

 Reading data from a SQL Database Using Node-Red (讀取SQLite資料庫內容)

源自於 http://www.steves-internet-guide.com/storing-iot-data-sql-database/




1.Convert time

//var payload=msg.payload;

for (var i=0;i<msg.payload.length;i++)

{

 dateObj = new Date(msg.payload[i].TIMESTAMP); 

 var date=dateObj.getDate();

 var month=dateObj.getMonth()+1;

 var year=dateObj.getFullYear();

 var hours=dateObj.getHours();

 var minutes=dateObj.getMinutes(); 

 var seconds=dateObj.getSeconds(); 

 var newtime=date+"/"+month+"/"+year;

 newtime=newtime+"-"+hours+":"+minutes+":"+seconds;

 msg.payload[i].TIMESTAMP=newtime

}


return msg;


2. 函數

var data=context.get('data')|| {value:"",condition:">="};

var topic=msg.topic;

var payload=msg.payload;

if (topic=="site")

data.site=msg.payload;

if (topic=="value")

data.value=msg.payload;

if (topic=="condition")

data.condition=msg.payload;

if (topic=="sdate")

data.sdate=msg.payload;

if (topic=="edate")

data.edate=msg.payload;



//

var d=new Date();

now=d.getTime();

if (data.edate==undefined || data.edate=="")

    edate=now;

else

edate=data.edate;

if (data.sdate==undefined || data.sdate=="")

    sdate=0;

else

    sdate=data.sdate;

context.set('data',data)

if (sdate==0)

{

   var part_msg=""; 

    

}

else

{

   var part_msg=" AND TIMESTAMP >"+sdate+ " AND TIMESTAMP<"+edate+ " "; 

}

if(topic=="submit")

{

    var query="";

    if(data.value==undefined ||data.value=="" )

    {

        if(data.site!=undefined)

             query ="SELECT * FROM SITES WHERE SITE="+"\""+data.site+"\""+part_msg+" ORDER BY TIMESTAMP DESC LIMIT 100";

    }

    else if(data.value!=undefined && data.value!="" ) 

    {

             if(data.site!=undefined && data.condition !=undefined)

             {

            query ="SELECT * FROM SITES WHERE SITE="+"\""+data.site+"\""+" AND PAGE_SPEED ";

             query=query+data.condition + data.value +part_msg;

            query=query+" ORDER BY TIMESTAMP DESC LIMIT 100";

             }

    }

node.log("query "+query);

msg.topic=query;


context.set('data',data)

if (query!="")

    return msg;

}

return;

3. Node-Red程式

[{"id":"a40cd66e.d9f398","type":"sqlite","z":"579c5a8c.871a84","mydb":"b30b3534.bde2f8","sqlquery":"msg.topic","sql":"","name":"sites database","x":340,"y":80,"wires":[["f7328f55.20ab3"]]},{"id":"4aa0c162.e6b9b","type":"inject","z":"579c5a8c.871a84","name":"show results","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"SELECT * FROM SITES ORDER BY TIMESTAMP DESC LIMIT 100","payload":"","payloadType":"date","x":110,"y":60,"wires":[["a40cd66e.d9f398"]]},{"id":"475a4754.a4f3e8","type":"ui_template","z":"579c5a8c.871a84","group":"725a612e.289f8","name":"","order":0,"width":0,"height":0,"format":"<style>\ntable\n{\n    background:blue;\n}\n.main\n{\n\n\n    height:500px;\n}\n</style>\n<div class=\"main\">\n<table style=\"width:100%\">\n  <tr>\n    <th>TIMESTAMP</th> \n    <th>SITE</th>\n    <th>PAGE LOAD SPEED</th> \n\n  \n  </tr>\n  <tr ng-repeat=\"x in msg.payload | limitTo:100\">\n    <td>{{msg.payload[$index].TIMESTAMP}}</td>\n    <td>{{msg.payload[$index].SITE}}</td>\n    <td>{{msg.payload[$index].PAGE_SPEED}}</td> \n   \n\n  </tr>\n</table>\n</div>","storeOutMessages":true,"fwdInMessages":true,"resendOnRefresh":false,"templateScope":"local","x":660,"y":80,"wires":[[]]},{"id":"af55f545.af9538","type":"ui_dropdown","z":"579c5a8c.871a84","name":"","label":"Site","tooltip":"","place":"Select option","group":"cc87581b.e05208","order":2,"width":"6","height":"1","passthru":false,"multiple":true,"options":[{"label":"www.build-your-website.co.u","value":"www.build-your-website.co.uk","type":"str"},{"label":"www.steves-internet-guide.com","value":"www.steves-internet-guide.com","type":"str"},{"label":"stevessmarthomeguide.com","value":"stevessmarthomeguide.com","type":"str"}],"payload":"","topic":"site","x":70,"y":120,"wires":[["edaeabe5.ebbc88"]]},{"id":"b017e4b0.802d28","type":"debug","z":"579c5a8c.871a84","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":270,"y":420,"wires":[]},{"id":"edaeabe5.ebbc88","type":"function","z":"579c5a8c.871a84","name":"","func":"var data=context.get('data')|| {value:\"\",condition:\">=\"};\nvar topic=msg.topic;\nvar payload=msg.payload;\nif (topic==\"site\")\ndata.site=msg.payload;\nif (topic==\"value\")\ndata.value=msg.payload;\nif (topic==\"condition\")\ndata.condition=msg.payload;\nif (topic==\"sdate\")\ndata.sdate=msg.payload;\nif (topic==\"edate\")\ndata.edate=msg.payload;\n\n\n//\nvar d=new Date();\nnow=d.getTime();\nif (data.edate==undefined || data.edate==\"\")\n    edate=now;\nelse\nedate=data.edate;\nif (data.sdate==undefined || data.sdate==\"\")\n    sdate=0;\nelse\n    sdate=data.sdate;\ncontext.set('data',data)\nif (sdate==0)\n{\n   var part_msg=\"\"; \n    \n}\nelse\n{\n   var part_msg=\" AND TIMESTAMP >\"+sdate+ \" AND TIMESTAMP<\"+edate+ \" \"; \n}\nif(topic==\"submit\")\n{\n    var query=\"\";\n    if(data.value==undefined ||data.value==\"\" )\n    {\n        if(data.site!=undefined)\n             query =\"SELECT * FROM SITES WHERE SITE=\"+\"\\\"\"+data.site+\"\\\"\"+part_msg+\" ORDER BY TIMESTAMP DESC LIMIT 100\";\n    }\n    else if(data.value!=undefined && data.value!=\"\" ) \n    {\n             if(data.site!=undefined && data.condition !=undefined)\n             {\n            query =\"SELECT * FROM SITES WHERE SITE=\"+\"\\\"\"+data.site+\"\\\"\"+\" AND PAGE_SPEED \";\n             query=query+data.condition + data.value +part_msg;\n            query=query+\" ORDER BY TIMESTAMP DESC LIMIT 100\";\n             }\n    }\nnode.log(\"query \"+query);\nmsg.topic=query;\n\ncontext.set('data',data)\nif (query!=\"\")\n    return msg;\n}\nreturn;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":290,"y":260,"wires":[["a40cd66e.d9f398"]]},{"id":"f7328f55.20ab3","type":"function","z":"579c5a8c.871a84","name":"Convert time","func":"//var payload=msg.payload;\nfor (var i=0;i<msg.payload.length;i++)\n{\n dateObj = new Date(msg.payload[i].TIMESTAMP); \n var date=dateObj.getDate();\n var month=dateObj.getMonth()+1;\n var year=dateObj.getFullYear();\n var hours=dateObj.getHours();\n var minutes=dateObj.getMinutes(); \n var seconds=dateObj.getSeconds(); \n var newtime=date+\"/\"+month+\"/\"+year;\n newtime=newtime+\"-\"+hours+\":\"+minutes+\":\"+seconds;\n msg.payload[i].TIMESTAMP=newtime\n}\n\nreturn msg;","outputs":1,"noerr":0,"x":510,"y":80,"wires":[["475a4754.a4f3e8"]]},{"id":"d1d4d396.c01fd","type":"ui_dropdown","z":"579c5a8c.871a84","name":"Page Load Speed","label":"Page Load Speed","tooltip":"","place":"Select option","group":"cc87581b.e05208","order":2,"width":"5","height":"1","passthru":true,"options":[{"label":">=","value":">=","type":"str"},{"label":"<=","value":"<=","type":"str"},{"label":"=","value":"=","type":"str"}],"payload":"","topic":"condition","x":110,"y":180,"wires":[["edaeabe5.ebbc88"]]},{"id":"1e2fa072.75b04","type":"ui_text_input","z":"579c5a8c.871a84","name":"value","label":"value","tooltip":"","group":"cc87581b.e05208","order":2,"width":"4","height":"1","passthru":false,"mode":"text","delay":"100","topic":"value","x":70,"y":240,"wires":[["edaeabe5.ebbc88"]]},{"id":"b67a72f3.4c536","type":"ui_button","z":"579c5a8c.871a84","name":"submit","group":"cc87581b.e05208","order":0,"width":"4","height":"1","passthru":false,"label":"Submit","tooltip":"","color":"","bgcolor":"","icon":"","payload":"1","payloadType":"str","topic":"submit","x":70,"y":300,"wires":[["edaeabe5.ebbc88"]]},{"id":"f1b960b3.e813","type":"ui_date_picker","z":"579c5a8c.871a84","name":"start date","label":"Start date","group":"cc87581b.e05208","order":4,"width":0,"height":0,"passthru":true,"topic":"sdate","x":80,"y":420,"wires":[["edaeabe5.ebbc88","b017e4b0.802d28"]]},{"id":"4bd0ba18.db4644","type":"ui_date_picker","z":"579c5a8c.871a84","name":"End Date","label":"End Date","group":"cc87581b.e05208","order":4,"width":0,"height":0,"passthru":true,"topic":"edate","x":80,"y":360,"wires":[["edaeabe5.ebbc88"]]},{"id":"b30b3534.bde2f8","type":"sqlitedb","z":"","db":"sites.db","mode":"RWC"},{"id":"725a612e.289f8","type":"ui_group","z":"","name":"Results","tab":"5fb4c7c3.738e88","order":2,"disp":true,"width":"14","collapse":false},{"id":"cc87581b.e05208","type":"ui_group","z":"","name":"Default","tab":"5fb4c7c3.738e88","order":1,"disp":true,"width":"12","collapse":false},{"id":"5fb4c7c3.738e88","type":"ui_tab","z":"","name":"SITE-RESULTS","icon":"dashboard"}]

Node-Red Storing IOT data In a SQL Database Part2

 Node-Red Storing IOT data In a SQL Database Part2

 (存入 Json 格式)

參考來源 http://www.steves-internet-guide.com/storing-iot-data-sql-database/




1. 建立資料表 TABLE data

CREATE TABLE data(TIMESTAMP INT PRIMARY KEY NOT NULL, SENSOR TEXT NOT NULL,RAW_DATA TEXT NOT NULL)


2. 刪除資料表 TABLE data

drop table data


3.刪除紀錄

DELETE FROM data WHERE TIMESTAMP>=0


4.檢視資料庫內容

SELECT * FROM data ORDER BY TIMESTAMP DESC LIMIT 100


5. function test data 


var count=context.get('count');

var timestamp=context.get('timestamp');


if (count==undefined)

{

    count=1;

    timestamp=1;

}


var data1={"sensor":"sensor1","temperature":21,"humidity":61};

var data2={"sensor":"sensor2","temperature":22,"humidity":62};

var data3={"sensor":"sensor3","temperature":23,"humidity":63};

if (count%20==0)

    count=-count;

data1.temperature+=(count*.25);

data2.temperature+=(count*.5);

data3.temperature+=(count*.75);

data1.humidity+=(count*.25);

data2.humidity+=(count*.5);

data3.humidity+=(count*.75);


msg.payload=[data1];

var msg1={};

var msg2={};

var msg3={};


data1.timestamp=timestamp;

timestamp+=1;

data2.timestamp=timestamp;

timestamp+=1;

data3.timestamp=timestamp;


msg1.payload=JSON.stringify(data1);

msg2.payload=JSON.stringify(data2);

msg3.payload=JSON.stringify(data3);

timestamp+=1;

count+=1;

context.set('count',count);

context.set('timestamp',timestamp);


return [[msg1,msg2,msg3]];


6.Write Query


var raw_data=msg.payload;

var temp=JSON.parse(msg.payload);

var timestamp=temp.timestamp;

var sensor=temp.sensor;

msg="(timestamp,sensor,raw_data) values("+timestamp +",\'"+sensor +"\'"+ ",\'"+raw_data+"\'"  +")";

var topic="INSERT INTO DATA " +msg;

var msg1={};

msg1.topic=topic;

msg1.payload=timestamp;

return msg1;

7. UI 樣板

<style>

.table

{

    height:400px;

    width:700px;

    background:blue;

}

</style>

<div class="table">

<table style="width:100%">

  <tr>

    <th>TIMESTAMP</th> 

    <th>SENSOR</th>

    <th>RAW DATA (JSON)</th>

  </tr>

  <tr ng-repeat="x in msg.payload | limitTo:20">

    <td>{{msg.payload[$index].TIMESTAMP}}</td>

    <td>{{msg.payload[$index].SENSOR}}</td>

    <td>{{msg.payload[$index].RAW_DATA}}</td>

  </tr>

</table>

</div>

8. Node-Red程式

[{"id":"b02620d.8a494e","type":"ui_template","z":"84ff1570.906d18","group":"cfc9bd1b.be5ed","name":"UI Table JSON","order":1,"width":"","height":"","format":"<style>\n.table\n{\n    height:400px;\n    width:700px;\n    background:blue;\n}\n</style>\n<div class=\"table\">\n<table style=\"width:100%\">\n  <tr>\n    <th>TIMESTAMP</th> \n    <th>SENSOR</th>\n    <th>RAW DATA (JSON)</th>\n  </tr>\n  <tr ng-repeat=\"x in msg.payload | limitTo:20\">\n    <td>{{msg.payload[$index].TIMESTAMP}}</td>\n    <td>{{msg.payload[$index].SENSOR}}</td>\n    <td>{{msg.payload[$index].RAW_DATA}}</td>\n  </tr>\n</table>\n</div>","storeOutMessages":true,"fwdInMessages":true,"resendOnRefresh":false,"templateScope":"local","x":760,"y":60,"wires":[[]]},{"id":"d8c3cfd5.23763","type":"inject","z":"84ff1570.906d18","name":"Test Data","props":[{"p":"payload","v":"","vt":"date"},{"p":"topic","v":"","vt":"string"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":160,"y":260,"wires":[["6e6167a8.9ca048"]]},{"id":"6e6167a8.9ca048","type":"function","z":"84ff1570.906d18","name":"test data","func":"var count=context.get('count');\nvar timestamp=context.get('timestamp');\n\nif (count==undefined)\n{\n    count=1;\n    timestamp=1;\n}\nvar data1={\"sensor\":\"sensor1\",\"temperature\":21,\"humidity\":61};\nvar data2={\"sensor\":\"sensor2\",\"temperature\":22,\"humidity\":62};\nvar data3={\"sensor\":\"sensor3\",\"temperature\":23,\"humidity\":63};\nif (count%20==0)\n    count=-count;\ndata1.temperature+=(count*.25);\ndata2.temperature+=(count*.5);\ndata3.temperature+=(count*.75);\ndata1.humidity+=(count*.25);\ndata2.humidity+=(count*.5);\ndata3.humidity+=(count*.75);\nmsg.payload=[data1];\nvar msg1={};\nvar msg2={};\nvar msg3={};\n\ndata1.timestamp=timestamp;\ntimestamp+=1;\ndata2.timestamp=timestamp;\ntimestamp+=1;\ndata3.timestamp=timestamp;\n\nmsg1.payload=JSON.stringify(data1);\nmsg2.payload=JSON.stringify(data2);\nmsg3.payload=JSON.stringify(data3);\ntimestamp+=1;\ncount+=1;\ncontext.set('count',count);\ncontext.set('timestamp',timestamp);\nreturn [[msg1,msg2,msg3]];","outputs":1,"noerr":0,"initialize":"","finalize":"","x":300,"y":260,"wires":[["f1d1209.b27a8e","e6593493.09e348"]]},{"id":"e6593493.09e348","type":"function","z":"84ff1570.906d18","name":"write query","func":"var raw_data=msg.payload;\nvar temp=JSON.parse(msg.payload);\nvar timestamp=temp.timestamp;\nvar sensor=temp.sensor;\nmsg=\"(timestamp,sensor,raw_data) values(\"+timestamp +\",\\'\"+sensor +\"\\'\"+ \",\\'\"+raw_data+\"\\'\"  +\")\";\nvar topic=\"INSERT INTO DATA \" +msg;\nvar msg1={};\nmsg1.topic=topic;\nmsg1.payload=timestamp;\nreturn msg1;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":470,"y":260,"wires":[["ed24e8ca.4cdab8","9369eae3.0d5888"]]},{"id":"ed24e8ca.4cdab8","type":"debug","z":"84ff1570.906d18","name":"sql out","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":630,"y":300,"wires":[]},{"id":"f1d1209.b27a8e","type":"debug","z":"84ff1570.906d18","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":450,"y":300,"wires":[]},{"id":"9369eae3.0d5888","type":"delay","z":"84ff1570.906d18","name":"","pauseType":"delay","timeout":"1","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":640,"y":260,"wires":[["a15fdc7a.b5753"]]},{"id":"a15fdc7a.b5753","type":"sqlite","z":"84ff1570.906d18","mydb":"34f76900.4ed308","sqlquery":"msg.topic","sql":"","name":"","x":450,"y":140,"wires":[["f87ff1a7.6ae62"]]},{"id":"8e78b579.0ebce8","type":"inject","z":"84ff1570.906d18","name":"show RECORDS","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"5","crontab":"","once":true,"onceDelay":"","topic":"SELECT * FROM  data ORDER BY TIMESTAMP DESC LIMIT 100","payload":"","payloadType":"date","x":190,"y":60,"wires":[["5247b4fb.6f4cdc"]]},{"id":"5247b4fb.6f4cdc","type":"sqlite","z":"84ff1570.906d18","mydb":"34f76900.4ed308","sqlquery":"msg.topic","sql":"","name":"","x":450,"y":60,"wires":[["b02620d.8a494e"]]},{"id":"35ff2bd4.4a8984","type":"inject","z":"84ff1570.906d18","name":"create database table","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":"","topic":"CREATE TABLE data(TIMESTAMP INT PRIMARY KEY NOT NULL, SENSOR TEXT NOT NULL,RAW_DATA TEXT NOT NULL)","payload":"","payloadType":"date","x":200,"y":110,"wires":[["a15fdc7a.b5753"]]},{"id":"a2c9398b.4061a8","type":"inject","z":"84ff1570.906d18","name":"delete records","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":"","topic":"DELETE FROM data WHERE TIMESTAMP>=0","payload":"","payloadType":"date","x":170,"y":160,"wires":[["a15fdc7a.b5753"]]},{"id":"317d154d.651c2a","type":"inject","z":"84ff1570.906d18","name":"Drop Table","props":[{"p":"payload","v":"","vt":"date"},{"p":"topic","v":"drop table data","vt":"string"}],"repeat":"","crontab":"","once":false,"onceDelay":"","topic":"drop table data","payload":"","payloadType":"date","x":160,"y":209,"wires":[["a15fdc7a.b5753"]]},{"id":"f87ff1a7.6ae62","type":"debug","z":"84ff1570.906d18","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":750,"y":140,"wires":[]},{"id":"cfc9bd1b.be5ed","type":"ui_group","z":"","name":"Sensors","tab":"39bfa985.c84436","order":1,"disp":true,"width":"14","collapse":false},{"id":"34f76900.4ed308","type":"sqlitedb","z":"","db":"test20201130-2.db","mode":"RWC"},{"id":"39bfa985.c84436","type":"ui_tab","z":"","name":"Home","icon":"dashboard"}]

  清除項目編譯快取 執行 flutter clean 重新取得套件依賴 執行 flutter pub get 完整重新編譯並執行 flutter run 1. 新增專案套件 (pubspec.yaml) 在 Flutter 專案根目錄下的 pubspec.yaml 中,新增 ...