Skip to content

Instantly share code, notes, and snippets.

@fasmide
Created December 23, 2013 23:01
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save fasmide/8106296 to your computer and use it in GitHub Desktop.
Save fasmide/8106296 to your computer and use it in GitHub Desktop.
Turns the hdmi port on and off by reading a PIR sensor off a raspberry pi GPIO port 11
import RPi.GPIO as GPIO
import time
from threading import Timer
import subprocess
GPIO.setmode(GPIO.BCM)
GPIO.setup(11, GPIO.IN)
timer = False
monitor_is_on = True
def monitor_off():
global monitor_is_on
subprocess.call(['/opt/vc/bin/tvservice', '-o'])
monitor_is_on = False
def monitor_on():
global monitor_is_on
subprocess.call(['/opt/vc/bin/tvservice', '-p'])
subprocess.call(['fbset -depth 8'], shell=True)
subprocess.call(['fbset -depth 16'], shell=True)
subprocess.call(['xrefresh'], shell=True)
monitor_is_on = True
while True:
time.sleep(0.5)
movement = GPIO.input(11)
if movement:
if timer:
print "canceler timer"
timer.cancel()
timer = False
if not monitor_is_on:
print "Taender skaerm"
monitor_on()
else:
if not timer:
print "starter timer"
timer = Timer(60*5, monitor_off)
timer.start()
@1kohm
Copy link

1kohm commented Jan 23, 2016

Dear Fasmide,

that's nice piece of code and it works like a charm.
I'd like to start this code only in certain hours, for example: start @07:00AM and stop 10:00PM every day.
Due to fact that I'm Python beginner, could you guide me how to modify this code? I'm thinking about function time.time().

@ShazaibSohail
Copy link

ShazaibSohail commented Sep 12, 2018

Bro can you please show a hardware setup via picture, basically connection of PI Sensor to Raspberry Pi, I am newbie to Raspberry Pi environment, it will be great if you help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment