List of messages

What’s the most efficient way of sending a list of messages to a serial node as per a schedule (Every 24 hours)?

I have 100 messages or “Commands” I need to send to a device with a 1s delay between messages. I then read the responses and store the data.

What I’m looking for is an example which has the following properties:

“Inject” (Every 24 hours)
“msg.payload1”
Wait 1s
“Msg.payload2”
Wait 1s
....
“Msg.payload100”

I see I could use the delay node and the inject nodes with scheduled times, but what’s the most efficient way of sending a list of messages?

Where are the commands stored? The kicking off is easy, use the inject node for that (see it's options) If you have the commands in a file, you could use the file-in node and send one message per line. Put in a delay node. and lastly send the command to the device. Since you didn't say how you are comuncation, I can't give you a suggestion there.

You didn't say how you are receiving the responses but this could be a seperatee flow. Feed the responses to a file-out and you have the results.

@zenofmud So what i would like to do is replicate the commands send by the software i use to communicate to the serial device, i have the serial device connected to a TCP node and i used the debug to capture the messages. I have them all in inject nodes for now.

[{"id":"8d0d2cb1.e72ad","type":"tab","label":"Flow 3","disabled":false,"info":""},{"id":"3d129480.b361fc","type":"inject","z":"8d0d2cb1.e72ad","name":"3","topic":"","payload":"{\"topic\":\"Serial_Send\",\"payload\":[10],\"ip\":\"::ffff:xx.xxx.xxx.x\",\"port\":8337,\"_session\":{\"type\":\"tcp\",\"id\":\"75e9dc7b.6c8f44\"},\"_msgid\":\"7bf4c9bf.6e0918\"}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":290,"y":200,"wires":[["3a285025.2d391"]]},{"id":"cd4f17d6.172fa8","type":"inject","z":"8d0d2cb1.e72ad","name":"4","topic":"","payload":"{\"topic\":\"Serial_Send\",\"payload\":[1,80,50,2,40,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,41,3,98],\"ip\":\"::ffff:xx.xx.xx.xx\",\"port\":8337,\"_session\":{\"type\":\"tcp\",\"id\":\"75e9dc7b.6c8f44\"},\"_msgid\":\"63d8dd24.876264\"}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":290,"y":240,"wires":[["3a285025.2d391"]]},{"id":"93b655af.f3c068","type":"inject","z":"8d0d2cb1.e72ad","name":"5","topic":"","payload":"{\"topic\":\"Serial_Send\",\"payload\":[1,82,49,2,55,57,56,48,48,49,40,49,48,41,3,101],\"ip\":\"::ffff:xx.xx.xxx.xx\",\"port\":8337,\"_session\":{\"type\":\"tcp\",\"id\":\"75e9dc7b.6c8f44\"},\"_msgid\":\"6a6c789f.cac758\"}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":290,"y":280,"wires":[["3a285025.2d391"]]},{"id":"28822ab9.add616","type":"inject","z":"8d0d2cb1.e72ad","name":"6","topic":"","payload":"{\"topic\":\"Serial_Send\",\"payload\":[1,82,49,2,55,57,53,48,48,49,40,48,56,41,3,97],\"ip\":\"::ffff:xx.xxx.xxx.x\",\"port\":8337,\"_session\":{\"type\":\"tcp\",\"id\":\"75e9dc7b.6c8f44\"},\"_msgid\":\"2773d50c.9d82da\"}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":290,"y":320,"wires":[["3a285025.2d391"]]},{"id":"8bd36fe6.fdbb7","type":"inject","z":"8d0d2cb1.e72ad","name":"7","topic":"","payload":"{\"topic\":\"Serial_Send\",\"payload\":[1,80,50,2,40,49,66,66,67,66,53,55,57,50,57,53,54,66,56,70,67,41,3,30],\"ip\":\"::ffff:xx.xxx.xxx.x\",\"port\":8337,\"_session\":{\"type\":\"tcp\",\"id\":\"75e9dc7b.6c8f44\"},\"_msgid\":\"4fb41a05.582f44\"}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":290,"y":360,"wires":[["3a285025.2d391"]]},{"id":"b327d69a.b4f4c8","type":"inject","z":"8d0d2cb1.e72ad","name":"8","topic":"","payload":"{\"topic\":\"Serial_Send\",\"payload\":[1,82,49,2,55,57,53,48,48,49,40,48,56,41,3,97],\"ip\":\"::ffff:xx.xxx.xxx.xx\",\"port\":8337,\"_session\":{\"type\":\"tcp\",\"id\":\"75e9dc7b.6c8f44\"},\"_msgid\":\"cf740586.aab1e8\"}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":290,"y":400,"wires":[["3a285025.2d391"]]},{"id":"b41989b7.298cb8","type":"inject","z":"8d0d2cb1.e72ad","name":"2","topic":"","payload":"{\"topic\":\"Serial_Send\",\"payload\":[6,48,53,49,13],\"ip\":\"::ffffxx.xxx.xxx.x\",\"port\":8337,\"_session\":{\"type\":\"tcp\",\"id\":\"75e9dc7b.6c8f44\"},\"_msgid\":\"21f462c2.ebd7de\"}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":290,"y":160,"wires":[["3a285025.2d391"]]},{"id":"7e108b0a.27a1b4","type":"inject","z":"8d0d2cb1.e72ad","name":"1","topic":"","payload":"{\"topic\":\"Serial_Send\",\"payload\":[47,63,48,48,49,33,13,10],\"ip\":\"::ffff:xx.xx.xxx.x\",\"port\":8337,\"_session\":{\"type\":\"tcp\",\"id\":\"75e9dc7b.6c8f44\"},\"_msgid\":\"5199bbc2.37fcd4\"}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":290,"y":120,"wires":[["3a285025.2d391"]]},{"id":"3a285025.2d391","type":"debug","z":"8d0d2cb1.e72ad","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":480,"y":300,"wires":[]}]

So i can copy the commands into a file and set it to send each command per line (I'm new to Node so apologies if this is a basic question))
Do you have an example i could see?

Why are you building an entire msg inside of msg.payload?

12/20/2019, 5:34:42 AMnode: 6698bd3c.20a8fc
msg : Object
object
  _msgid: "8afb1622.82f9c8"
  topic: ""
  payload: object
    topic: "Serial_Send"
    payload: array[8]
    ip: "::ffff:xx.xx.xxx.x"
    port: 8337
   _session: object
      type: "tcp"
      id: "75e9dc7b.6c8f44"
_msgid: "5199bbc2.37fcd4"

How much of that do you really need?
When you send a msg, a _msgid: gets generated and sent as part of the msg object, but you are adding another _msgid inside of msg.payload...why?

Might not be what you're looking for, but might give an idea that there are different ways to Rome. I was editing a few hundred photos with the only goal to scale them down in file size to be displayed on a low resolution display. Most were taken with a DSLR camera, so the file size for regular JPEGs was about 8mb per photo, but not all were. Since it was a task that could be easily dumbed down to not spend hours on doing it by hand, I put Node-RED to use for its ease in prototyping.

It uses a few custom nodes, but you should only care about the use of q-gate here: [ANNOUNCE] node-red-contrib-queue-gate


The idea is that Files has an output that's spamming 100+ messages rapidly into the q-gate, which is set to queueing mode, where a single message will come out, actions happen, and at the end with a change node it goes back into the q-gate to allow the next message to pass through. You could have the commands get send into the q-gate, then branch and send the message into the serial out, and another into a delay note with 1 second delay, with the output of that one going back into q-gate with a command set to let the next message pass through.

Or perhaps even easier, if you can get it to work with the delay node set to rate limiting and only let 1 message pass every second.

[{"id":"408bd314.dccd24","type":"jimp-image","z":"fe5232bf.c0c7f","name":"","data":"payload","dataType":"msg","ret":"img","parameter1":"50","parameter1Type":"num","parameter2":"","parameter2Type":"msg","parameter3":"","parameter3Type":"msg","parameter4":"","parameter4Type":"msg","parameter5":"","parameter5Type":"msg","parameter6":"","parameter6Type":"msg","parameter7":"","parameter7Type":"msg","parameter8":"","parameter8Type":"msg","parameterCount":1,"jimpFunction":"quality","selectedJimpFunction":{"name":"quality","fn":"quality","description":"Set the quality of the image. Usefull for reducing size of image before calling the write function.","parameters":[{"name":"quality","type":"num","required":true,"hint":"Quality value 1 ~ 100"}]},"x":790,"y":40,"wires":[["b53c1817.58c57"]]},{"id":"feae1e2e.0fcff","type":"jimp-image","z":"fe5232bf.c0c7f","name":"","data":"payload","dataType":"msg","ret":"img","parameter1":"filename","parameter1Type":"msg","parameter2":"","parameter2Type":"msg","parameter3":"","parameter3Type":"msg","parameter4":"","parameter4Type":"msg","parameter5":"","parameter5Type":"msg","parameter6":"","parameter6Type":"msg","parameter7":"","parameter7Type":"msg","parameter8":"","parameter8Type":"msg","parameterCount":1,"jimpFunction":"write","selectedJimpFunction":{"name":"write","fn":"write","description":"Write to file. NOTE: You can specify an alternative file extension type to change the type. Currently support types are jpg, png, bmp.","parameters":[{"name":"filename","type":"str","required":true,"hint":"Name of the file","defaultType":"str"}]},"x":1010,"y":40,"wires":[["4b36ff8c.bc8668"]]},{"id":"abd6a9ce.12cc88","type":"inject","z":"fe5232bf.c0c7f","name":"","topic":"","payload":"true","payloadType":"bool","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":90,"y":40,"wires":[["68713e15.0203a"]]},{"id":"b53c1817.58c57","type":"function","z":"fe5232bf.c0c7f","name":"Rename","func":"msg.filename = `${msg.filename.split('.JPG')[0]}_klein.jpg`;\nreturn msg;","outputs":1,"noerr":0,"x":915,"y":40,"wires":[["feae1e2e.0fcff"]],"l":false},{"id":"f6c8cd59.1637a8","type":"jimp-image","z":"fe5232bf.c0c7f","name":"Load ","data":"payload","dataType":"msg","ret":"img","parameter1":"","parameter1Type":"msg","parameter2":"","parameter2Type":"msg","parameter3":"","parameter3Type":"msg","parameter4":"","parameter4Type":"msg","parameter5":"","parameter5Type":"msg","parameter6":"","parameter6Type":"msg","parameter7":"","parameter7Type":"msg","parameter8":"","parameter8Type":"msg","parameterCount":0,"jimpFunction":"none","selectedJimpFunction":{"name":"none","fn":"none","description":"Just loads the image.","parameters":[]},"x":650,"y":40,"wires":[["408bd314.dccd24"]]},{"id":"68713e15.0203a","type":"fs-file-lister","z":"fe5232bf.c0c7f","name":"Files","start":"/home/pi/photo/katachtigen/","pattern":"*.JPG","path":true,"single":false,"depth":"1","stat":false,"x":230,"y":40,"wires":[["9d667618.ed8ee8","7a566e96.2c972"]]},{"id":"9d667618.ed8ee8","type":"q-gate","z":"fe5232bf.c0c7f","name":"","controlTopic":"gatectrl","defaultState":"queueing","openCmd":"open","closeCmd":"close","toggleCmd":"toggle","queueCmd":"queue","defaultCmd":"default","triggerCmd":"trigger","flushCmd":"flush","resetCmd":"reset","maxQueueLength":"-1","keepNewest":false,"qToggle":false,"persist":false,"x":410,"y":40,"wires":[["c0744ffa.1f09e8","18389d79.a1ee03"]]},{"id":"4b36ff8c.bc8668","type":"change","z":"fe5232bf.c0c7f","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"gatectrl","tot":"str"},{"t":"set","p":"payload","pt":"msg","to":"trigger","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":700,"y":120,"wires":[["9d667618.ed8ee8"]]},{"id":"40d6790b.94bfa8","type":"inject","z":"fe5232bf.c0c7f","name":"Start converting","topic":"gatectrl","payload":"trigger","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":180,"y":180,"wires":[["9d667618.ed8ee8"]]},{"id":"2f31fc9f.1c2174","type":"inject","z":"fe5232bf.c0c7f","name":"Reset queue","topic":"gatectrl","payload":"reset","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":190,"y":240,"wires":[["9d667618.ed8ee8"]]},{"id":"c0744ffa.1f09e8","type":"debug","z":"fe5232bf.c0c7f","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":470,"y":120,"wires":[]},{"id":"18389d79.a1ee03","type":"change","z":"fe5232bf.c0c7f","name":"(safekeeping)","rules":[{"t":"set","p":"filename","pt":"msg","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":555,"y":40,"wires":[["f6c8cd59.1637a8"]],"l":false},{"id":"7a566e96.2c972","type":"debug","z":"fe5232bf.c0c7f","name":"Files","active":false,"tosidebar":true,"console":true,"tostatus":false,"complete":"true","targetType":"full","x":190,"y":120,"wires":[]}]
2 Likes

I put the commands in a payload to give an idea of what i need to send to the serial device and the order. I only need the Palyload (I see I've put the complete message object!)

So let's say you have a text file (call it commands.txt with your commands - here are the payloads from the first three injects:

47,63,48,48,49,33,13,10
6,48,53,49,13
10

You could use the file-in node to read the file and set it to output one msg per line.

Send the output to a delay node with the Action to set to Rate Limit and set the limit to what you want.

Now you just need to chage the line to an array of numbers so add a function node. A quick google searchcomes up with https://stackoverflow.com/questions/15677869/how-to-convert-a-string-of-numbers-to-an-array-of-numbers in which you shoud find a simple solution.

Now send the payload where ever you want.

1 Like

I've nearly got it! I've used the File-in node which has the commands:

47,63,48,48,49,33,13,10
6,48,53,49,13
10

I have a delay to limit the messages, however the first line of the file gives the output payload "[47,63,48,48,49,33,13,10]" as a string when it should be "/?001!" so i think that's an encoding issue? i can see i get a string output when i need a buffer.

Did you have the brackets around the numbers in the file?

The link I gave you should have allowed you to create a function to convert the number string into an array of numbers. When you read them in, they will be text numbers so you need to split the string up and convert the text version of the numbers into actual numbers. Take a look at the solution pointed out by User: thg435