Page 1 of 1

Play video and use a relay

Posted: Thu Jun 04, 2020 8:02 am
by edsulst
Online I found some code to play a video, with a push on a button.
This works great.

What I also would like is to use a relay to turn on a lightbulb when the button is pressed.
So when the button is pressed, the video will start to play and a light switches on.
The light has to turn on for 1 minute.

When I press a second button, another video has to start playing (the first video is stopped) and another light must switch on AND all other lights must turn off.

The part of playing the video is working. Press button 1 and video 1 is playing. By pressing button 2, video 2 starts playing.
Now I need to use a relay to switch on a light when a video starts to play.
I don't know how to start here and to incorporate this into my exisiting code.

Can anybody help me with this?

Below the code thats works for starting the video's.

Code: Select all

import RPi.GPIO as GPIO
import os
import sys
from subprocess import Popen

GPIO.setmode(GPIO.BOARD) # Use physical pin numbering

GPIO.setup(7, GPIO.IN, pull_up_down=GPIO.PUD_UP)  # Pushbutton
GPIO.setup(11, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Pushbutton


movie1 = ("/var/www/html/dieselloc.mp4")
movie2 = ("/var/www/html/snhv2019.mp4")

last_state1 = True
last_state2 = True

input_state1 = True
input_state2 = True

player = False

while True:
    #Read states of inputs
    input_state1 = GPIO.input(7)
    input_state2 = GPIO.input(11)

    #If GPIO(7) is shorted to Ground
    if input_state1 != last_state1:
        if (player and not input_state1):
            os.system('killall omxplayer.bin')
            omxc = Popen(['omxplayer', '-b', movie1])
            player = True
        elif not input_state1:
            omxc = Popen(['omxplayer', '-b', movie1])
            player = True

    #If GPIO(11) is shorted to Ground
    elif input_state2 != last_state2:
        if (player and not input_state2):
            os.system('killall omxplayer.bin')
            omxc = Popen(['omxplayer', '-b', movie2])
            player = True
        elif not input_state2:
            omxc = Popen(['omxplayer', '-b', movie2])
            player = True

    #Set last_input states
    last_state1 = input_state1
    last_state2 = input_state2

Re: Play video and use a relay

Posted: Thu Jun 04, 2020 8:31 am
by pcmanbob
So do you have the relay board connected and working on your pi ?

what gpio pins are you using for each relay activation ?

switching on the relay when a button is pressed is easy and switching off if another is pressed is also easy , the timing of 60 seconds is going to be the difficult bit as your program is looping continuously with no sleeps at all.

So first see the lines I have added to your code indication were to add relay commands. ( I have only done one relay/video )

Code: Select all

import RPi.GPIO as GPIO
import os
import sys
from subprocess import Popen

GPIO.setmode(GPIO.BOARD) # Use physical pin numbering

GPIO.setup(7, GPIO.IN, pull_up_down=GPIO.PUD_UP)  # Pushbutton
GPIO.setup(11, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Pushbutton


movie1 = ("/var/www/html/dieselloc.mp4")
movie2 = ("/var/www/html/snhv2019.mp4")

last_state1 = True
last_state2 = True

input_state1 = True
input_state2 = True

player = False

while True:
    #Read states of inputs
    input_state1 = GPIO.input(7)
    input_state2 = GPIO.input(11)

    #If GPIO(7) is shorted to Ground
    if input_state1 != last_state1:
        if (player and not input_state1):
            os.system('killall omxplayer.bin')
            # put commands in here to turn off both relays
            omxc = Popen(['omxplayer', '-b', movie1])
            # put command in here to turn relay for video 1
            player = True
        elif not input_state1:
            omxc = Popen(['omxplayer', '-b', movie1])
            # put command in here to turn relay for video 1
            player = True

    #If GPIO(11) is shorted to Ground
    elif input_state2 != last_state2:
        if (player and not input_state2):
            os.system('killall omxplayer.bin')
            omxc = Popen(['omxplayer', '-b', movie2])
            player = True
        elif not input_state2:
            omxc = Popen(['omxplayer', '-b', movie2])
            player = True

    #Set last_input states
    last_state1 = input_state1
    last_state2 = input_state2
once you have the relays turning on we can look at turning them off after 60 seconds

Re: Play video and use a relay

Posted: Thu Jun 04, 2020 9:16 am
by edsulst
Hi pcmanbob,

I want to use 2 relays (for now). I have ordered them, but did not receive them.
I want to connect them to pin 14 and 15.

This is the code so far (without testing the relay, because I don't have them right now)

Code: Select all

import RPi.GPIO as GPIO
import os
import sys
from subprocess import Popen

GPIO.setmode(GPIO.BOARD) # Use physical pin numbering

RELAIS_1_GPIO = 14
RELAIS_2_GPIO = 15
GPIO.setup(RELAIS_1_GPIO, GPIO.OUT) # GPIO Assign mode
GPIO.setup(RELAIS_2_GPIO, GPIO.OUT) # GPIO Assign mode

GPIO.setup(7, GPIO.IN, pull_up_down=GPIO.PUD_UP)  # Pushbutton
GPIO.setup(11, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Pushbutton

movie1 = ("/var/www/html/dieselloc.mp4")
movie2 = ("/var/www/html/snhv2019.mp4")

last_state1 = True
last_state2 = True

input_state1 = True
input_state2 = True

player = False

while True:
    #Read states of inputs
    input_state1 = GPIO.input(7)
    input_state2 = GPIO.input(11)

    #If GPIO(7) is shorted to Ground
    if input_state1 != last_state1:
        if (player and not input_state1):
            os.system('killall omxplayer.bin')
            # put commands in here to turn off both relays
            GPIO.output(RELAIS_1_GPIO, GPIO.LOW)
            GPIO.output(RELAIS_2_GPIO, GPIO.LOW)
            omxc = Popen(['omxplayer', '-b', movie1])
            # put command in here to turn relay for video 1
            GPIO.output(RELAIS_1_GPIO, GPIO.HIGH)
            player = True
        elif not input_state1:
            omxc = Popen(['omxplayer', '-b', movie1])
            player = True

    #If GPIO(11) is shorted to Ground
    elif input_state2 != last_state2:
        if (player and not input_state2):
            os.system('killall omxplayer.bin')
            # put commands in here to turn off both relays
            GPIO.output(RELAIS_1_GPIO, GPIO.LOW)
            GPIO.output(RELAIS_2_GPIO, GPIO.LOW)
            omxc = Popen(['omxplayer', '-b', movie2])
            # put command in here to turn relay for video 1
            GPIO.output(RELAIS_2_GPIO, GPIO.HIGH)
            player = True
        elif not input_state2:
            omxc = Popen(['omxplayer', '-b', movie2])
            player = True

    #Set last_input states
    last_state1 = input_state1
    last_state2 = input_state2
Ik declared both of the relais and assing them to a pin.

Code: Select all

RELAIS_1_GPIO = 14
RELAIS_2_GPIO = 15
GPIO.setup(RELAIS_1_GPIO, GPIO.OUT) # GPIO Assign mode
GPIO.setup(RELAIS_2_GPIO, GPIO.OUT) # GPIO Assign mode
Then I turn both relais off:

Code: Select all

# put commands in here to turn off both relays
            GPIO.output(RELAIS_1_GPIO, GPIO.LOW)
            GPIO.output(RELAIS_2_GPIO, GPIO.LOW)
And turn the right one on:

Code: Select all

# put command in here to turn relay for video 1
            GPIO.output(RELAIS_1_GPIO, GPIO.HIGH)
I hope this is OK ?

If you think there is a better way, please let me know.

Re: Play video and use a relay

Posted: Thu Jun 04, 2020 9:23 am
by pcmanbob
So what have you actually ordered ?

You cant connect just a relay directly to the pi gpio , you have to use a driver circuit and power the relay from 5v or another supply.

most of the relay boards you buy online are actually active low , so setting the gpio to low turns the relay on not off and many of them don't work correctly with the 3.3v gpio of the pi.

So I suggest you post a link to what your have ordered.

and as you are using board numbering pin 14 is a ground pin.
if you meant to use gpio 14,15 I would not recommend that as they are used to TXD, RXD.

Re: Play video and use a relay

Posted: Thu Jun 04, 2020 9:40 am
by edsulst
You are right.
I use the wrong numbers.

The relays are connected to pin 29 and 30.

I have ordered this one: https://www.hobbyelectronica.nl/product ... anaals-5v/ (sorry, but the page is in Dutch)
As I see in the specs it needs 5 volt and pin 2 of the Raspberry would give me 5 volt.
If that can be a problem, I can always use an external 5 volt power supply.

So my code would be like this:

Code: Select all

import RPi.GPIO as GPIO
import os
import sys
from subprocess import Popen

GPIO.setmode(GPIO.BOARD) # Use physical pin numbering

RELAIS_1_GPIO = 29
RELAIS_2_GPIO = 31
GPIO.setup(RELAIS_1_GPIO, GPIO.OUT) # GPIO Assign mode
GPIO.setup(RELAIS_2_GPIO, GPIO.OUT) # GPIO Assign mode

GPIO.setup(7, GPIO.IN, pull_up_down=GPIO.PUD_UP)  # Pushbutton
GPIO.setup(11, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Pushbutton

movie1 = ("/var/www/html/dieselloc.mp4")
movie2 = ("/var/www/html/snhv2019.mp4")

last_state1 = True
last_state2 = True

input_state1 = True
input_state2 = True

player = False

while True:
    #Read states of inputs
    input_state1 = GPIO.input(7)
    input_state2 = GPIO.input(11)

    #If GPIO(7) is shorted to Ground
    if input_state1 != last_state1:
        if (player and not input_state1):
            os.system('killall omxplayer.bin')
            # put commands in here to turn off both relays
            GPIO.output(RELAIS_1_GPIO, GPIO.HIGH)
            GPIO.output(RELAIS_2_GPIO, GPIO.HIGH)
            omxc = Popen(['omxplayer', '-b', movie1])
            # put command in here to turn relay for video 1
            GPIO.output(RELAIS_1_GPIO, GPIO.LOW)
            player = True
        elif not input_state1:
            omxc = Popen(['omxplayer', '-b', movie1])
            player = True

    #If GPIO(11) is shorted to Ground
    elif input_state2 != last_state2:
        if (player and not input_state2):
            os.system('killall omxplayer.bin')
            # put commands in here to turn off both relays
            GPIO.output(RELAIS_1_GPIO, GPIO.HIGH)
            GPIO.output(RELAIS_2_GPIO, GPIO.HIGH)
            omxc = Popen(['omxplayer', '-b', movie2])
            # put command in here to turn relay for video 1
            GPIO.output(RELAIS_2_GPIO, GPIO.LOW)
            player = True
        elif not input_state2:
            omxc = Popen(['omxplayer', '-b', movie2])
            player = True

    #Set last_input states
    last_state1 = input_state1
    last_state2 = input_state2

Re: Play video and use a relay

Posted: Thu Jun 04, 2020 9:48 am
by pcmanbob
So did you order the board with a single relay on or the one with 2 relays on ?
as the control circuit on each board is different and may affect how it works with the pi.

Re: Play video and use a relay

Posted: Thu Jun 04, 2020 9:57 am
by edsulst
I ordered the double one.
As I see on the image, the relais use a common ground and 5V.
And with the two other pins I can control the relais.

Re: Play video and use a relay

Posted: Thu Jun 04, 2020 9:59 am
by PiGraham
What you describe should be quite easy to do with some Python or even bash script.
If you have bigger plans you might like to check out
https://pipresents.wordpress.com/

Which does this sort of thing and much more.

caveat: I haven't used it myself. It just looks useful.

Re: Play video and use a relay

Posted: Thu Jun 04, 2020 10:03 am
by edsulst
I stumbled upon that page also. :D

It looks great. To bad there are no screenshots available.
I'll take a look at Pi Presents.

Thanks for you support !!

Re: Play video and use a relay

Posted: Thu Jun 04, 2020 10:12 am
by pcmanbob
edsulst wrote:
Thu Jun 04, 2020 9:57 am
I ordered the double one.
As I see on the image, the relais use a common ground and 5V.
And with the two other pins I can control the relais.
Ok so just be aware that the relay board may turn the relays on but not off , and you are using the gpio as a ground for a 5V circuit , in my opinion not a good idea as the pi gpio is not 5V tolerant.

see this for more details on relay problems , viewtopic.php?f=91&t=83372&p=1225448#p1225448

So I have added the timer to switch the relays off after 60 seconds but I have not tested this code so expect errors or it not working as expected,
I have also added a time.sleep to the end of your core , otherwise you pi processor will try to run this very very fast resulting in the processor running at 100%.

Code: Select all

import RPi.GPIO as GPIO
import os
import sys
from subprocess import Popen
import time

GPIO.setmode(GPIO.BOARD) # Use physical pin numbering

RELAIS_1_GPIO = 29
RELAIS_2_GPIO = 31
GPIO.setup(RELAIS_1_GPIO, GPIO.OUT) # GPIO Assign mode
GPIO.setup(RELAIS_2_GPIO, GPIO.OUT) # GPIO Assign mode

GPIO.setup(7, GPIO.IN, pull_up_down=GPIO.PUD_UP)  # Pushbutton
GPIO.setup(11, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Pushbutton

movie1 = ("/var/www/html/dieselloc.mp4")
movie2 = ("/var/www/html/snhv2019.mp4")

last_state1 = True
last_state2 = True

input_state1 = True
input_state2 = True

player = False
now = time.time()
stop = now - 1
while True:
    #Read states of inputs
    input_state1 = GPIO.input(7)
    input_state2 = GPIO.input(11)

    # check relay time
    now = time.time()
    if now >= stop:
        # turn off both relays
        GPIO.output(RELAIS_1_GPIO, GPIO.HIGH)
        GPIO.output(RELAIS_2_GPIO, GPIO.HIGH)        

    #If GPIO(7) is shorted to Ground
    if input_state1 != last_state1:
        if (player and not input_state1):
            os.system('killall omxplayer.bin')
            # turn off both relays
            GPIO.output(RELAIS_1_GPIO, GPIO.HIGH)
            GPIO.output(RELAIS_2_GPIO, GPIO.HIGH)
            omxc = Popen(['omxplayer', '-b', movie1])
            # turn relay 1 on for video 1
            GPIO.output(RELAIS_1_GPIO, GPIO.LOW)
            #set stop time
            stop = time.time() + 60
            player = True
        elif not input_state1:
            omxc = Popen(['omxplayer', '-b', movie1])
            # turn relay 1 on for video 1
            GPIO.output(RELAIS_1_GPIO, GPIO.LOW)
            #set stop time
            stop = time.time() + 60            
            player = True

    #If GPIO(11) is shorted to Ground
    elif input_state2 != last_state2:
        if (player and not input_state2):
            os.system('killall omxplayer.bin')
            # turn off both relays
            GPIO.output(RELAIS_1_GPIO, GPIO.HIGH)
            GPIO.output(RELAIS_2_GPIO, GPIO.HIGH)
            omxc = Popen(['omxplayer', '-b', movie2])
            # turn on relay 2 for video 2
            GPIO.output(RELAIS_2_GPIO, GPIO.LOW)
            #set stop time
            stop = time.time() + 60            
            player = True
        elif not input_state2:
            omxc = Popen(['omxplayer', '-b', movie2])
            # turn on relay 2 for video 2
            GPIO.output(RELAIS_2_GPIO, GPIO.LOW)
            #set stop time
            stop = time.time() + 60            
            player = True

    #Set last_input states
    last_state1 = input_state1
    last_state2 = input_state2
    time.sleep(0.1)

Re: Play video and use a relay

Posted: Thu Jun 04, 2020 11:49 am
by edsulst
Maybe there is another solution for killing the lights.

When the video is over, the light can go out.
The problem is that I don't know the exact time of each video.
Is it possible to return a status when the video is done en when that status is set, the lights are turn out ?

Re: Play video and use a relay

Posted: Thu Jun 04, 2020 12:05 pm
by pcmanbob
The problem with waiting for the popen to report the video status is that your program would have to stop looping and wait for popen to report the video finishing.

So you could not press another button while one video was playing.

if you time the videos you could hard code the time for each relay for each video so that they switch off when the video ends purely on the timer.

So for example if video 1 runs for 53 seconds then you would change the code to reflect this,

Code: Select all

    #If GPIO(7) is shorted to Ground
    if input_state1 != last_state1:
        if (player and not input_state1):
            os.system('killall omxplayer.bin')
            # turn off both relays
            GPIO.output(RELAIS_1_GPIO, GPIO.HIGH)
            GPIO.output(RELAIS_2_GPIO, GPIO.HIGH)
            omxc = Popen(['omxplayer', '-b', movie1])
            # turn relay 1 on for video 1
            GPIO.output(RELAIS_1_GPIO, GPIO.LOW)
            #set stop time
            stop = time.time() + 53
            player = True
        elif not input_state1:
            omxc = Popen(['omxplayer', '-b', movie1])
            # turn relay 1 on for video 1
            GPIO.output(RELAIS_1_GPIO, GPIO.LOW)
            #set stop time
            stop = time.time() + 53            
            player = True
            

Re: Play video and use a relay

Posted: Thu Jun 04, 2020 12:13 pm
by PiGraham
Again I haven't used this, but it seems highly relevant.

omxplayer can be controlled by dbus interface:

https://github.com/popcornmix/omxplayer#synopsis
omxplayer can be controlled via DBUS. There are three interfaces, all of which present a different set of commands.
There's a python wrapper:

https://python-omxplayer-wrapper.readth ... omxplayer/