[Resolved] Start/Stop motor with limit switch


I wish I could drive (start and stop) a motor with the switch on my smartphone and if the movement is detected by the limit switch stop the movement. No worries with the use of GPIO.
I tested several functions but with the switch of the ui I can not operate.

Could you help me.

Thank you

If you have written a function in a flow, even if they don't work, it is useful to share them here as often a member of the community can see where you are going wrong and suggest how you can improve your flow.

This is the first time I posted on this forum and I start in Node red.

I hope I have taken the right data.

[{"id":"11e0a82.22036d8","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"e1735106.aafe68","type":"ui_switch","z":"11e0a82.22036d8","name":"Moteurs Start","label":"Moteur start","group":"6781044e.5564a4","order":0,"width":0,"height":0,"passthru":true,"decouple":"false","topic":"","style":"","onvalue":"true","onvalueType":"str","onicon":"","oncolor":"","offvalue":"false","offvalueType":"str","officon":"","offcolor":"","x":280,"y":80,"wires":[["ec211cec.30bbf"]]},{"id":"8892fb3d.fda038","type":"rpi-gpio out","z":"11e0a82.22036d8","name":"Motor","pin":"3","set":true,"level":"0","freq":"","out":"out","x":730,"y":120,"wires":[]},{"id":"780f27b4.65a4d","type":"rpi-gpio in","z":"11e0a82.22036d8","name":"Switch stop","pin":"7","intype":"down","debounce":"100","read":false,"x":270,"y":160,"wires":[["ec211cec.30bbf"]]},{"id":"ec211cec.30bbf","type":"BooleanLogic","z":"11e0a82.22036d8","name":"Test","operation":"XOR","inputCount":2,"topic":"result","x":520,"y":120,"wires":[["8892fb3d.fda038"]]},{"id":"6781044e.5564a4","type":"ui_group","z":"","name":"Moteurs","tab":"6d3213cb.a45f4c","order":1,"disp":true,"width":"6","collapse":false},{"id":"6d3213cb.a45f4c","type":"ui_tab","z":"","name":"Chirocam","icon":"dashboard","order":1}]


I can't see a function node i there...

So the labels underneath the nodes might help explain some of what is happening
Your dashboard switch is sending "on" but your RPi Switch is expecting a 0 or 1

Take a look at the change node to change on or off into 1 or 0

If I use boolean or number it's the same thing. The XOR remains in "Unknow".

If i use Inject it is ok.

I still have to initialize "switch stop" for the XOR to work

Have you read the ReadMe page of the XOR node? I assume it is https://flows.nodered.org/node/node-red-contrib-boolean-logic ? The part entitled ####BooleanLogic might be or interest...

I had looked. What is problematic is that I can make it work if I replace the switch with an Inject.
I guess what I want to do with the XOR needs to be done with an If function but I do not know how to do it.

You started this thread by saying you had tried several functions what have you tried?

If it works with inject but not with an alternative technique then you are not sending the same message content. Use a debug node to see what you are actually sending it.

I did what you propose.

First image with switch I true in debug

Second image with Inject i have true too.

I do not understand what I'm not doing well

Configure the debug nodes to show the complete message.

I'm not sure I did the right thing. I have this.

With Inject
msg : Object
_msgid: "6bb413af.db164c"
topic: ""
payload: true

Witch switch
msg : Object
payload: true
socketid: "EZnvBbW-qcTwrxVFAAAB"
_msgid: "85fcfbbc.fe51a8"

I double click on the debug and I have selected complete msg object and selected the 2 items

Stick a debug on the PIN 3 node also to show what it is sending to the boolean node

I test another approach with an if function.
But I start, I learn to do
if Switch = 1 and Pin = 0 -> Motor = 1.
if Switch = 1 -> Motor = 0.
It takes me time since I can not understand the structure of the function module.

I test

if (msg.payload=="SwitchOn" && msg.payload=="PinOff")
else if (msg.payload=="SwitchOff")
return msg;

But write msg.payload=="SwitchOn" && msg.payload=="PinOff", how to write this correctly?

I test another possibility

[{"id":"64a069ff.e5db","type":"tab","label":"Flow 2","disabled":false,"info":""},{"id":"c289498d.545d98","type":"rpi-gpio in","z":"64a069ff.e5db","name":"","pin":"38","intype":"down","debounce":"100","read":false,"x":176,"y":130,"wires":[["75db7356.b10fc4"]]},{"id":"75db7356.b10fc4","type":"function","z":"64a069ff.e5db","name":"PinState","func":"if (msg.payload==1)\n{\n global.set("Pin38","PinOn");\n}\nelse if (msg.payload===0)\n{\n global.set("Pin38","PinOff");\n}","outputs":1,"noerr":0,"x":409,"y":131,"wires":[[]]},{"id":"23c3d3f.3a2742c","type":"ui_switch","z":"64a069ff.e5db","name":"","label":"Switch1","group":"6781044e.5564a4","order":0,"width":0,"height":0,"passthru":true,"decouple":"false","topic":"","style":"","onvalue":"true","onvalueType":"bool","onicon":"","oncolor":"","offvalue":"false","offvalueType":"bool","officon":"","offcolor":"","x":177,"y":202,"wires":[["843460f1.180848","505a2341.c4768c"]]},{"id":"843460f1.180848","type":"function","z":"64a069ff.e5db","name":"SwitchState","func":"global.set("Switch",msg.payload)","outputs":1,"noerr":0,"x":414,"y":201,"wires":[[]]},{"id":"ec74ae6b.9b39a","type":"function","z":"64a069ff.e5db","name":"","func":"var PinStatus=global.get("Pin38");\nvar SwitchStatus=global.get("Switch");\n\nif (SwitchStatus===true)\n{\n if (PinStatus==="PinOff")\n {\n msg.payload=1; \n }\n}\n\nif (SwitchStatus===false)\n{\n msg.payload=0; \n}\n\nreturn msg;","outputs":1,"noerr":0,"x":207,"y":293,"wires":[["bf34bf19.87fba8"]]},{"id":"bf34bf19.87fba8","type":"rpi-gpio out","z":"64a069ff.e5db","name":"","pin":"37","set":true,"level":"0","freq":"","out":"out","x":416,"y":294,"wires":[]},{"id":"505a2341.c4768c","type":"debug","z":"64a069ff.e5db","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":434,"y":244,"wires":[]},{"id":"6781044e.5564a4","type":"ui_group","z":"","name":"Moteurs","tab":"6d3213cb.a45f4c","order":1,"disp":true,"width":"6","collapse":false},{"id":"6d3213cb.a45f4c","type":"ui_tab","z":"","name":"Chirocam","icon":"dashboard","order":1}]

But does not work

Are you sure you are not setting a topic in the switch? I thought the XOR node needs topics to tell the two inputs apart.

Thank you everyone. Colin found, he was missing the topic on the switch.

If anyone can look at my other two solutions, I'd like to move on.
A big thank-you.