[Scene] IoT Studio fetch modbus tcp/rtu device store to SQLite and send to AMQP

Questions and Answers

Moderator: IoTStudio Support

Post Reply
IoTStudio Support
Posts: 39
Joined: Wed Nov 23, 2022 9:26 am

[Scene] IoT Studio fetch modbus tcp/rtu device store to SQLite and send to AMQP

Post by IoTStudio Support »

Use ModSim32 to simulate modbus TCP device.
scene1.PNG
scene1.PNG (95.03 KiB) Viewed 1710 times
Precondition:
  • AMQP Server Service Start (guest/guest)
  • Modbus Client Simulator (Modbus TCP)
Device data definition
DeviceDataDefinition.png
DeviceDataDefinition.png (179.44 KiB) Viewed 940 times
Use Modbus Read node connect to modbus tcp device and read data in period.
The write read data to SQLite db and send to RabbitMQ by AMQP protocol.

IoT Studio nodes
IoT Studio flow.PNG
IoT Studio flow.PNG (191.67 KiB) Viewed 1710 times

Code: Select all

[{"id":"91f323cbba1d4850","type":"modbus-read","z":"1f3a5eefa74bb70a","name":"","topic":"","showStatusActivities":false,"logIOActivities":false,"showErrors":false,"showWarnings":true,"unitid":"1","dataType":"HoldingRegister","adr":"99","quantity":"10","rate":"3","rateUnit":"s","delayOnStart":true,"startDelayTime":"10","server":"a55ab622632eb049","useIOFile":false,"ioFile":"","useIOForPayload":false,"emptyMsgOnFail":false,"x":190,"y":180,"wires":[["1c9ced33591a5960"],[]]},{"id":"c75bfd496fe944eb","type":"sqlite","z":"1f3a5eefa74bb70a","mydb":"a838fc54182dc54e","sqlquery":"msg.topic","sql":"","name":"testDB","x":550,"y":220,"wires":[["08758d0be4ef910b"]]},{"id":"08758d0be4ef910b","type":"debug","z":"1f3a5eefa74bb70a","name":"debug 6","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":680,"y":220,"wires":[]},{"id":"1c9ced33591a5960","type":"function","z":"1f3a5eefa74bb70a","name":"Data Proc","func":"var timestamp = new Date().getTime() / 1000;\nvar tags = msg.payload;\n\nvar largeBool = [];\nfor (var i=0; i<10; i++) {\n    largeBool.push((tags[i] > 50) ? 1 : 0);\n}\n\nvar newMsg = {\n    \"topic\": \"INSERT INTO RANDOMNUM VALUES ( \" + timestamp + \n        \", \" + tags[0] + \", \" + largeBool[0] + \n        \", \" + tags[1] + \", \" + largeBool[1] + \n        \", \" + tags[2] + \", \" + largeBool[2] + \n        \", \" + tags[3] + \", \" + largeBool[3] + \n        \", \" + tags[4] + \", \" + largeBool[4] + \n        \", \" + tags[5] + \", \" + largeBool[5] + \n        \", \" + tags[6] + \", \" + largeBool[6] + \n        \", \" + tags[7] + \", \" + largeBool[7] + \n        \", \" + tags[8] + \", \" + largeBool[8] + \n        \", \" + tags[9] + \", \" + largeBool[9] + \n        \")\"\n}\n\nvar msg = {\n    queue: 'hello',\n    payload : {\n        timestamp: timestamp,\n        value: tags,\n        largeBool: largeBool \n    }\n};\n\nreturn [msg,newMsg];","outputs":2,"noerr":0,"initialize":"","finalize":"","libs":[],"x":360,"y":180,"wires":[["7449dc6837984c39"],["c75bfd496fe944eb"]]},{"id":"3c6a792304d4e0a2","type":"debug","z":"1f3a5eefa74bb70a","name":"debug 7","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":920,"y":140,"wires":[]},{"id":"fa204c86d231e976","type":"amqp-in","z":"1f3a5eefa74bb70a","name":"","broker":"4e359aadf533cacf","prefetch":0,"noAck":false,"exchangeName":"","exchangeType":"topic","exchangeRoutingKey":"","exchangeDurable":false,"queueName":"hello","queueExclusive":false,"queueDurable":false,"queueAutoDelete":false,"headers":"{}","x":780,"y":140,"wires":[["3c6a792304d4e0a2","9fde29c26f118e26"]]},{"id":"7449dc6837984c39","type":"amqp-out","z":"1f3a5eefa74bb70a","name":"","broker":"4e359aadf533cacf","exchangeName":"","exchangeType":"topic","exchangeRoutingKey":"hello","exchangeRoutingKeyType":"str","exchangeDurable":true,"amqpProperties":"{\"appId\":\"app-123\"}","rpcTimeoutMilliseconds":"","outputs":0,"x":550,"y":140,"wires":[]},{"id":"60ef2263c597a103","type":"inject","z":"1f3a5eefa74bb70a","name":"Direct Input","props":[{"p":"payload"},{"p":"queue","v":"hello","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"\"xxxxx\"","payloadType":"str","x":370,"y":140,"wires":[["7449dc6837984c39"]]},{"id":"50f6ec8ac9e82c13","type":"comment","z":"1f3a5eefa74bb70a","name":"fetch data from Modbus TCP device","info":"","x":240,"y":100,"wires":[]},{"id":"ddd55bee47bc2d16","type":"link in","z":"1f3a5eefa74bb70a","name":"link in SQLite","links":["d901b8d0fb669a1c"],"x":455,"y":260,"wires":[["c75bfd496fe944eb"]]},{"id":"e9125042686d8a95","type":"inject","z":"1f3a5eefa74bb70a","name":"create database table","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":true,"onceDelay":0.1,"topic":"CREATE TABLE RANDOMNUM( TIMESTAMP INT PRIMARY KEY NOT NULL, VALUE1 INT NOT NULL, BOOL1 INT NOT NULL, VALUE2 INT NOT NULL, BOOL2 INT NOT NULL, VALUE3 INT NOT NULL, BOOL3 INT NOT NULL, VALUE4 INT NOT NULL, BOOL4 INT NOT NULL, VALUE5 INT NOT NULL, BOOL5 INT NOT NULL, VALUE6 INT NOT NULL, BOOL6 INT NOT NULL, VALUE7 INT NOT NULL, BOOL7 INT NOT NULL, VALUE8 INT NOT NULL, BOOL8 INT NOT NULL, VALUE9 INT NOT NULL, BOOL9 INT NOT NULL, VALUE10 INT NOT NULL, BOOL10 INT NOT NULL);","payload":"","payloadType":"date","x":620,"y":320,"wires":[["d901b8d0fb669a1c"]]},{"id":"68139fc7e18befac","type":"inject","z":"1f3a5eefa74bb70a","name":"view records","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"SELECT * FROM RANDOMNUM ORDER BY TIMESTAMP DESC LIMIT 100;","payload":"","payloadType":"date","x":590,"y":360,"wires":[["d901b8d0fb669a1c"]]},{"id":"4c3cdcb9b1365267","type":"inject","z":"1f3a5eefa74bb70a","name":"delete","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"60","crontab":"","once":false,"onceDelay":0.1,"topic":"DELETE FROM RANDOMNUM WHERE TIMESTAMP <= strftime('%s','now', '-1 hours')*1000;","payload":"","payloadType":"date","x":580,"y":400,"wires":[["d901b8d0fb669a1c"]]},{"id":"e3b5cc2466319528","type":"inject","z":"1f3a5eefa74bb70a","name":"count","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"SELECT COUNT(*) FROM RANDOMNUM","payload":"","payloadType":"date","x":570,"y":440,"wires":[["d901b8d0fb669a1c"]]},{"id":"a5a47dc17253d4ea","type":"inject","z":"1f3a5eefa74bb70a","name":"drop table","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"drop table RANDOMNUM;","payload":"","payloadType":"date","x":580,"y":480,"wires":[["d901b8d0fb669a1c"]]},{"id":"d901b8d0fb669a1c","type":"link out","z":"1f3a5eefa74bb70a","name":"link out SQLite Ops","mode":"link","links":["ddd55bee47bc2d16"],"x":795,"y":400,"wires":[]},{"id":"702a12b36eee9aa2","type":"modbus-read","z":"1f3a5eefa74bb70a","name":"","topic":"","showStatusActivities":false,"logIOActivities":false,"showErrors":false,"showWarnings":true,"unitid":"1","dataType":"HoldingRegister","adr":"99","quantity":"10","rate":"15","rateUnit":"s","delayOnStart":false,"startDelayTime":"","server":"990863cab83b94b8","useIOFile":false,"ioFile":"","useIOForPayload":false,"emptyMsgOnFail":false,"x":190,"y":240,"wires":[["b6d282cd6fe7db74"],[]]},{"id":"b6d282cd6fe7db74","type":"function","z":"1f3a5eefa74bb70a","name":"Data Proc","func":"var timestamp = new Date().getTime() / 1000;\nvar tags = msg.payload;\n\nvar largeBool = [];\nfor (var i=0; i<10; i++) {\n    largeBool.push((tags[i] > 50) ? 1 : 0);\n}\n\nvar newMsg = {\n    \"topic\": \"INSERT INTO RANDOMNUM VALUES ( \" + timestamp + \n        \", \" + tags[0] + \", \" + largeBool[0] + \n        \", \" + tags[1] + \", \" + largeBool[1] + \n        \", \" + tags[2] + \", \" + largeBool[2] + \n        \", \" + tags[3] + \", \" + largeBool[3] + \n        \", \" + tags[4] + \", \" + largeBool[4] + \n        \", \" + tags[5] + \", \" + largeBool[5] + \n        \", \" + tags[6] + \", \" + largeBool[6] + \n        \", \" + tags[7] + \", \" + largeBool[7] + \n        \", \" + tags[8] + \", \" + largeBool[8] + \n        \", \" + tags[9] + \", \" + largeBool[9] + \n        \")\"\n}\n\nvar msg = {\n    queue: 'hello',\n    payload : {\n        timestamp: timestamp,\n        value: tags,\n        largeBool: largeBool \n    }\n};\n\nreturn [msg,newMsg];","outputs":2,"noerr":0,"initialize":"","finalize":"","libs":[],"x":360,"y":240,"wires":[[],[]]},{"id":"572ac10eb919efb8","type":"ui_chart","z":"1f3a5eefa74bb70a","name":"","group":"b666944b6c14b730","order":1,"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":"30","useOneColor":false,"useUTC":false,"colors":["#1f77b4","#aec7e8","#ff7f0e","#2ca02c","#98df8a","#d62728","#ff9896","#9467bd","#c5b0d5"],"outputs":1,"useDifferentColor":false,"className":"","x":1050,"y":180,"wires":[[]]},{"id":"9fde29c26f118e26","type":"function","z":"1f3a5eefa74bb70a","name":"Data","func":"\nvar m = {};\nm.labels = ['Tag1', 'Tag2', 'Tag3', 'Tag4', 'Tag5', 'Tag6', 'Tag7', 'Tag8', 'Tag9', 'Tag10'];\nm.data = [[msg.payload.value[0], msg.payload.value[1], msg.payload.value[2], msg.payload.value[3], msg.payload.value[4], msg.payload.value[5], msg.payload.value[6], msg.payload.value[7], msg.payload.value[8], msg.payload.value[9]]];\nm.series = ['Series A'];\nreturn {payload:[m],topic:msg.topic};","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":910,"y":180,"wires":[["572ac10eb919efb8"]]},{"id":"a55ab622632eb049","type":"modbus-client","name":"","clienttype":"tcp","bufferCommands":true,"stateLogEnabled":false,"queueLogEnabled":false,"failureLogEnabled":true,"tcpHost":"127.0.0.1","tcpPort":"502","tcpType":"DEFAULT","serialPort":"/dev/ttyUSB","serialType":"RTU-BUFFERD","serialBaudrate":"9600","serialDatabits":"8","serialStopbits":"1","serialParity":"none","serialConnectionDelay":"100","serialAsciiResponseStartDelimiter":"0x3A","unit_id":"1","commandDelay":"1","clientTimeout":"1000","reconnectOnTimeout":true,"reconnectTimeout":"2000","parallelUnitIdsAllowed":true,"showWarnings":true,"showLogs":true},{"id":"a838fc54182dc54e","type":"sqlitedb","db":"sqlite_db","mode":"RWC"},{"id":"4e359aadf533cacf","type":"amqp-broker","name":"","host":"localhost","port":"5672","vhost":"","tls":false,"credsFromSettings":false},{"id":"990863cab83b94b8","type":"modbus-client","name":"","clienttype":"serial","bufferCommands":true,"stateLogEnabled":false,"queueLogEnabled":false,"failureLogEnabled":true,"tcpHost":"127.0.0.1","tcpPort":"502","tcpType":"DEFAULT","serialPort":"COM3","serialType":"RTU","serialBaudrate":"9600","serialDatabits":"8","serialStopbits":"1","serialParity":"none","serialConnectionDelay":"100","serialAsciiResponseStartDelimiter":"0x3A","unit_id":1,"commandDelay":1,"clientTimeout":1000,"reconnectOnTimeout":true,"reconnectTimeout":2000,"parallelUnitIdsAllowed":true,"showWarnings":true,"showLogs":true},{"id":"b666944b6c14b730","type":"ui_group","name":"Default","tab":"f12da07a4beb1623","order":1,"disp":true,"width":16,"collapse":false,"className":""},{"id":"f12da07a4beb1623","type":"ui_tab","name":"Scene1","icon":"dashboard","disabled":false,"hidden":false}]
Best Regards,
IoT Studio Support Team
Post Reply