Sending a set of pulses to a relay by pressing a button on a web page

Hi, I’m new and as many of us newcomers still have to learn a lot.

I’m in the process of modifying my jukebox AMI-H 200 selection in such a way that I can select a song via a webpage.
So far I’ve managed all with a python program, web socket and dataplicity on my raspberry PI3.
I have it more or less working, but one thing I couldn’t achieve is reading the available credit from the jukebox.
To achieve this I need to be able reading the status of a GPIO input pin.
It should be possible via dataplicity but I haven’t found how to do that.

During my search I came across Node Red, and that seems to be more easy for reading GPIO in & outs.
I already could make a small example that triggers a relay on/off. Or toggle a relay.
This is OK for muting sound, adding credit, cancel song … because it’s is just putting a relay on/off.

The problem I have is for the selections of a record on the jukebox.
I’m sending currently via a relay a sequence of pulses to the jukebox. The jukebox stepper converts this pulse-train in a song selection. (in fact I’m simulating here a wallbox)
Example for a selection of song E13.
For selection E13, I need to send 6 High pulses with a wait time of 43 milliseconds – then a pause of 125 milliseconds – followed by 13 High pulse again with a wait time of 43 milliseconds.
Pulsetrain for E13 will become something like this

My question:

How can I achieve that when I press button E13 on my webpage, this pulse train is send to the relay (jukebox).?

As the jukebox has 200 records, I have 200 buttons on my page each representing a certain selection that sends a pulse train to the relay.
My ultimate dream would be that I put 200 records into a small database. (JukeboxRecID, Artist, Titel)
Then transfer the content of the database in a listbox on the webpage, and when I select a record from the list, it will send the appropriate pulse train to the relay.
But my knowledge skills are the limited factor here.

I’m already happy if I somebody could guide me on sending a pulse-train to the relay.

Thanks

How critical are the timings? Certainly one could failrly easily write some javascript code to put in a function node to do what you want, however It do not think it would be possible to guarantee the pulse timings down to milliseconds on a pi. A minimum time could be specified for each state but the maximum time might be much longer (as node-red is single threaded and there might be other things going on that hog the processor for significant fractions of a second.

Sure will be nice to writte a specific function for that and I’m interested into have it too but for now done with counters/Trigger & delays and asuming processing time is 0…

[{"id":"642e2d2f.16a604","type":"inject","z":"3a79879.af7b678","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":120,"y":1200,"wires":[["3af7b2ae.7ee80e"]]},{"id":"d30f2765.3dfb78","type":"trigger","z":"3a79879.af7b678","op1":"1","op2":"0","op1type":"num","op2type":"num","duration":"43","extend":false,"units":"ms","reset":"","bytopic":"all","name":"","x":470,"y":1200,"wires":[["e16770ea.03aec","423aace1.bf7eb4"]]},{"id":"b9ab097d.581118","type":"counter","z":"3a79879.af7b678","name":"","init":0,"step":1,"lower":"","upper":"6","mode":"increment","outputs":"1","x":600,"y":1260,"wires":[["38c501bb.bef7de"]]},{"id":"805089b.ccb5a78","type":"switch","z":"3a79879.af7b678","name":"","property":"count","propertyType":"msg","rules":[{"t":"lt","v":"5","vt":"num"},{"t":"eq","v":"5","vt":"num"}],"checkall":"true","repair":false,"outputs":2,"x":850,"y":1260,"wires":[["d30f2765.3dfb78"],["65a46e07.2fe5d","41c53066.9d90a"]]},{"id":"41c53066.9d90a","type":"delay","z":"3a79879.af7b678","name":"","pauseType":"delay","timeout":"125","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":1130,"y":1260,"wires":[["8f7d5f7d.2360d"]]},{"id":"e16770ea.03aec","type":"function","z":"3a79879.af7b678","name":"","func":"\nreturn msg;","outputs":1,"noerr":0,"x":1510,"y":1200,"wires":[["cb662411.dc6d58"]]},{"id":"cb662411.dc6d58","type":"debug","z":"3a79879.af7b678","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":1690,"y":1200,"wires":[]},{"id":"3af7b2ae.7ee80e","type":"delay","z":"3a79879.af7b678","name":"","pauseType":"rate","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":true,"x":290,"y":1200,"wires":[["d30f2765.3dfb78"]]},{"id":"38c501bb.bef7de","type":"switch","z":"3a79879.af7b678","name":"","property":"count","propertyType":"msg","rules":[{"t":"gt","v":"0","vt":"num"}],"checkall":"true","repair":false,"outputs":1,"x":730,"y":1260,"wires":[["805089b.ccb5a78"]]},{"id":"5060b494.0b290c","type":"inject","z":"3a79879.af7b678","name":"","topic":"","payload":"0","payloadType":"num","repeat":"","crontab":"","once":true,"onceDelay":0.1,"x":510,"y":1420,"wires":[["65a46e07.2fe5d","806a9b9a.1bb078"]]},{"id":"423aace1.bf7eb4","type":"switch","z":"3a79879.af7b678","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"1","vt":"num"}],"checkall":"true","repair":false,"outputs":1,"x":110,"y":1260,"wires":[["2d49c592.4b96aa"]]},{"id":"65a46e07.2fe5d","type":"function","z":"3a79879.af7b678","name":"reset","func":"msg.reset = true;\nmsg.payload = 0;\nreturn msg;","outputs":1,"noerr":0,"x":650,"y":1340,"wires":[["b9ab097d.581118"]]},{"id":"3ea89b52.34acd4","type":"function","z":"3a79879.af7b678","name":"increment value","func":"msg.increment = 1;\nreturn msg;","outputs":1,"noerr":0,"x":420,"y":1260,"wires":[["b9ab097d.581118"]]},{"id":"2d49c592.4b96aa","type":"delay","z":"3a79879.af7b678","name":"","pauseType":"delay","timeout":"43","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":250,"y":1260,"wires":[["3ea89b52.34acd4"]]},{"id":"8f7d5f7d.2360d","type":"trigger","z":"3a79879.af7b678","op1":"1","op2":"0","op1type":"num","op2type":"num","duration":"43","extend":false,"units":"ms","reset":"","bytopic":"all","name":"","x":1350,"y":1260,"wires":[["ce4e86e4.cb8a88","e16770ea.03aec"]]},{"id":"ce4e86e4.cb8a88","type":"switch","z":"3a79879.af7b678","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"1","vt":"num"}],"checkall":"true","repair":false,"outputs":1,"x":910,"y":1340,"wires":[["63d254f4.a2984c"]]},{"id":"63d254f4.a2984c","type":"delay","z":"3a79879.af7b678","name":"","pauseType":"delay","timeout":"43","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":1050,"y":1340,"wires":[["52cb3c7d.decd64"]]},{"id":"52cb3c7d.decd64","type":"function","z":"3a79879.af7b678","name":"increment value","func":"msg.increment = 1;\nreturn msg;","outputs":1,"noerr":0,"x":1220,"y":1340,"wires":[["c45c6db0.af1d1"]]},{"id":"c45c6db0.af1d1","type":"counter","z":"3a79879.af7b678","name":"","init":0,"step":1,"lower":"","upper":"14","mode":"increment","outputs":"1","x":1380,"y":1340,"wires":[["ea5258f8.3ebe38"]]},{"id":"ea5258f8.3ebe38","type":"switch","z":"3a79879.af7b678","name":"","property":"count","propertyType":"msg","rules":[{"t":"gt","v":"0","vt":"num"}],"checkall":"true","repair":false,"outputs":1,"x":1510,"y":1340,"wires":[["b60978a6.db2cc8"]]},{"id":"b60978a6.db2cc8","type":"switch","z":"3a79879.af7b678","name":"","property":"count","propertyType":"msg","rules":[{"t":"lt","v":"14","vt":"num"},{"t":"eq","v":"14","vt":"num"}],"checkall":"true","repair":false,"outputs":2,"x":1650,"y":1340,"wires":[["8f7d5f7d.2360d"],["806a9b9a.1bb078"]]},{"id":"806a9b9a.1bb078","type":"function","z":"3a79879.af7b678","name":"reset","func":"msg.reset = true;\nmsg.payload = 0;\nreturn msg;","outputs":1,"noerr":0,"x":1490,"y":1420,"wires":[["c45c6db0.af1d1"]]}]

Try and see if it works, for me seems ok

Lets try to measure... who knows ...
[{"id":"f8ed1d3.9b25ee","type":"function","z":"8c51699c.4c1948","name":"send","func":"var letter = flow.get(\"letter\");\nvar pause = flow.get(\"pause\");\nvar num = flow.get(\"num\");\nvar value;\nif(letter.length > 0){\n value = letter.shift();\n flow.set(\"letter\",letter);\n msg.payload = value;\n return [msg,null];\n}\nif(pause.length > 0){\n value = pause.shift();\n flow.set(\"pause\",pause);\n msg.payload = value;\n return [null,msg];\n}\nif(num.length > 0){\n value = num.shift();\n flow.set(\"num\",num);\n msg.payload = value;\n return [msg,null];\n}\n","outputs":2,"noerr":0,"x":1130,"y":160,"wires":[["cb958b7a.a4fa18","95e3cac0.014aa8"],["7dfc186a.9121a8","95e3cac0.014aa8"]]},{"id":"f438ca44.caea68","type":"inject","z":"8c51699c.4c1948","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":840,"y":160,"wires":[["99b3d71f.1e3af8"]]},{"id":"99b3d71f.1e3af8","type":"function","z":"8c51699c.4c1948","name":"prepare","func":"var letter = [1,0,1,0,1,0,1,0,1,0,1,0,1,0]\nflow.set(\"letter\",letter);\nvar pause = [0];\nflow.set(\"pause\",pause);\nvar num = [1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0]\nflow.set(\"num\",num);\n\nreturn msg;","outputs":1,"noerr":0,"x":980,"y":160,"wires":[["f8ed1d3.9b25ee"]]},{"id":"cb958b7a.a4fa18","type":"delay","z":"8c51699c.4c1948","name":"","pauseType":"delay","timeout":"43","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":1130,"y":80,"wires":[["f8ed1d3.9b25ee"]]},{"id":"18ae0ddd.e34d02","type":"debug","z":"8c51699c.4c1948","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":1450,"y":160,"wires":[]},{"id":"7dfc186a.9121a8","type":"delay","z":"8c51699c.4c1948","name":"","pauseType":"delay","timeout":"125","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":1130,"y":260,"wires":[["f8ed1d3.9b25ee"]]},{"id":"95e3cac0.014aa8","type":"function","z":"8c51699c.4c1948","name":"time","func":"var time = flow.get(\"time\") || new Date().getTime();\nvar now = new Date().getTime();\nvar diff = now - time;\nmsg.time = diff;\nflow.set(\"time\",now);\n\nreturn msg;","outputs":1,"noerr":0,"x":1310,"y":160,"wires":[["18ae0ddd.e34d02"]]}]

I have about 4ms drifting just for proccessing. Kind of not that good result :roll_eyes:

hotNipi, how can you check this? I’m interested into understand

Very interesting project.
On the hardware side I think that is better to use transistor instead relay. Relay is “slow” and also after some time contact on relay can be problematic.

On the software(node red) side, I will try make simple touch button which send letter and number (E13) to serial port. That is all for node red.

On the serial(usb) is the arduino. Arduino read serial and depend on received command from node red make digital write sequence of pulses.

On the arduino side is much easier to do with and you can go safely till 2 milliseconds, I will suggest to make the program on the arduino and if is the case use node red as HMI and trigger the sequence on the arduino.

Look on arduino forum or google for examples on the code for pulses, measure distance with an ultrasonic sensor or PWM it will help you a lot as a base.

Note you should use PWM pin.

Regards

Hi all,

Sorry for the delay and not giving feedback, but had urgent healty problem that needed to be solved.
Now all is fine again - I’ll check some of the solutions given here.
thanks