Skip to content

Instantly share code, notes, and snippets.

@rajrao
Forked from ecaron/button.py
Created August 1, 2016 02:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rajrao/bffb5e809ab74104ee764f69876470ad to your computer and use it in GitHub Desktop.
Save rajrao/bffb5e809ab74104ee764f69876470ad to your computer and use it in GitHub Desktop.
Python script for listening to Raspberry Pi button push
import requests
import time
import RPi.GPIO as GPIO
url = 'http://nightlight/' # I map this to my internal DNS hosting the node app
gpio_pin=18 # The GPIO pin the button is attached to
longpress_threshold=5 # If button is held this length of time, tells system to leave light on
GPIO.setmode(GPIO.BCM)
GPIO.setup(gpio_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
while True:
time.sleep(0.2)
if GPIO.input(gpio_pin) == False: # Listen for the press, the loop until it steps
print "Started press"
pressed_time=time.time()
while GPIO.input(gpio_pin) == False:
time.sleep(0.2)
pressed_time=time.time()-pressed_time
print "Button pressed %d, POSTing to nightlight server" % pressed_time
if pressed_time<longpress_threshold:
data = dict(light=2, cmd="toggle-with-timer")
else:
data = dict(light=2, cmd="toggle-keep-on")
r = requests.post(url, data=data, allow_redirects=True)
print r.content
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment