Skip to content

Instantly share code, notes, and snippets.

@Jazarro
Created June 10, 2017 12:14
Show Gist options
  • Save Jazarro/c329c64e4ac17a85da7b061667f5ae36 to your computer and use it in GitHub Desktop.
Save Jazarro/c329c64e4ac17a85da7b061667f5ae36 to your computer and use it in GitHub Desktop.
import requests
import schedule
import time
import RPi.GPIO as GPIO
HTTP_PREFIX = 'http://'
BRIDGE_IP = '192.168.178.101'
API_PREFIX = '/api'
USERNAME = 'x-M0aZsE-oQVjKTpmv8EfbUKXcIW7S8kTXvX3Mlb'
GROUPS = '/groups'
ACTION = '/action'
BRI_MAX = 254
SAT_MAX = 254
SAT_MIN = 100
HUE_RED = 0
RED_PULSING = {
'on': True,
'effect': 'none',
'hue': HUE_RED,
'sat': SAT_MAX,
'bri': BRI_MAX,
'alert': 'lselect'
}
NORMAL = {
'effect': 'none',
'sat': SAT_MIN,
'bri': BRI_MAX,
}
PIN_SWITCH = 23
def authenticated():
return HTTP_PREFIX + BRIDGE_IP + API_PREFIX + '/' + USERNAME
def address_to_all():
return GROUPS + '/0' + ACTION
def refresh():
response = requests.put(authenticated() + address_to_all(), json=RED_PULSING)
print(response.url)
print(response.text)
def terminate():
response = requests.put(authenticated() + address_to_all(), json=NORMAL)
print(response.url)
print(response.text)
def trigger_red_alert():
# First turn on all lamps.
refresh()
# Then get it going.
refresh()
# Repeat every 15 seconds.
schedule.every(15).seconds.do(refresh)
while True:
if GPIO.input(PIN_SWITCH) == 0:
break
schedule.run_pending()
time.sleep(1)
GPIO.setmode(GPIO.BCM)
GPIO.setup(PIN_SWITCH, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.add_event_detect(PIN_SWITCH, GPIO.RISING, callback=trigger_red_alert, bouncetime=300)
GPIO.add_event_detect(PIN_SWITCH, GPIO.FALLING, callback=terminate, bouncetime=300)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment