i am trying to put together some code i have found. i am noob with python. So i want to press a button and 1)arm relay for 20 sec 2)play a video at same time, and to stop both after 20 sec. in those 20 sec i need the button to be dead. in the code i have the video sometimes dose not play. can someone plz help?


import RPi.GPIO as GPIO
import time
import os
from time import sleep
from  xbmcjson import XBMC, PLAYER_VIDEO

def my_callback(channel):
  xbmc = XBMC("http://localhost/jsonrpc",'osmc','osmc')
#  print xbmc.JSONRPC.Ping()
#  video_id = ""
#  xbmc.Player.Open(item={"file":"plugin://plugin.video.youtube/?action=play_video&videoid=" + video_id})
# xbmc.GUI.ActivateWindow({"window":"videos"})
# xbmc.GUI.ActivateWindow(window="videos")

switch =  27 #enter your switch gpio number here
relay =  17 #enter your relay gpio number here
state = 0

GPIO.setmode(GPIO.BCM) # you can change this to Board if you prefere 
GPIO.setup(switch, GPIO.IN)
GPIO.setup(relay, GPIO.OUT)
GPIO.add_event_detect(switch, GPIO.FALLING, callback=my_callback, bouncetime=200)
# set relay off you may need to change this to high if you relay board works active on low, also change state to 1 if this is the case.
GPIO.output(relay, GPIO.HIGH)
while True:
# check if switch is pressed and keep checking
   while GPIO.input(switch) == 0:
      print "waiting for switch press"

      # checks current relay state and changes it to the other.
      if state == 0:
         print "Setting gpio high"
         state = 1
         GPIO.output(relay, GPIO.HIGH)

         print "setting gpio low"
         state = 0
         GPIO.output(relay, GPIO.LOW)
  • the video sometimes dose not play - probably OSMC being stupid. Check the OSMC logs for stupidity – Jaromanda X Feb 6 at 1:27

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.