Sending message asynchronously? How to send payload once an event occurs?

Using the Dashboard, a user selects a sequence of commands (say for robot movement). Once the user is satisfied with their selected sequence, I want that string to be published. Only once the user is done though.

So once the user pushes the "start" button on the Dashboard, then the sequence of commands is sent to the microcontroller. I think the node.send(msg) is used somehow: Making flows asynchronous by default : Node-RED and Writing Functions : Node-RED

Yes? No? Any suggetions on how?

var commands = flow.get('commands') || []   // variable

msg.topic = 'collect'

switch(msg.payload)
{
    case 'moveForward':
    case 'moveBackward':
    case 'moveRight':
    case 'moveLeft':
        commands.push(msg.payload)  // concatenates payload
        break
    
    case 'clear':
        commands = []
        break
    
    case 'run':
        msg.topic = 'start'
        break
}
flow.set('commands',commands)

// if(msg.topic == 'start' || commands == 'clear')
// {
    msg.payload = commands
// }
return msg

This program is thanks to hotNipi:

Switch node:
msg.topic
== [string] start
image

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.