Skip to content

Instantly share code, notes, and snippets.

@simrit1
Forked from schollz/aurora.py
Created August 19, 2022 18:36
Show Gist options
  • Save simrit1/a3dd12859e2a0cddd715d89b172d86aa to your computer and use it in GitHub Desktop.
Save simrit1/a3dd12859e2a0cddd715d89b172d86aa to your computer and use it in GitHub Desktop.
My Raspberry Pi Aurora Alarm Clock
import time
import json
import requests
from bs4 import BeautifulSoup
import RPi.GPIO as GPIO
# Setup GPIO
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(11, GPIO.IN)
GPIO.setup(7, GPIO.IN)
# Phillips Hue
PHILLIPS_USER = "XX"
PHILLIPS_IP = "XX"
def light_alarm():
requests.put('http://'+PHILLIPS_IP+'/api/'+PHILLIPS_USER+'/lights/2/state',data=json.dumps({'on':True,'alert':'lselect'}))
time.sleep(3)
requests.put('http://'+PHILLIPS_IP+'/api/'+PHILLIPS_USER+'/lights/2/state',data=json.dumps({'on':True,'alert':'none'}))
def percent_aurora_right_now():
r = requests.get('http://www.aurorawatch.ca/')
soup = BeautifulSoup(r.text, 'html.parser')
percent_aurora = 0
for span in soup.find_all('span'):
if '%' == span.text[-1]:
try:
percent_aurora = float(span.text.replace('%', ''))
except:
pass
return percent_aurora
def is_on():
r = requests.get('http://'+PHILLIPS_IP+'/api/'+PHILLIPS_USER+'/lights/2')
j = json.loads(r.text)
return j['state']['on']
def turn_on():
if not is_on():
requests.put('http://'+PHILLIPS_IP+'/api/'+PHILLIPS_USER+'/lights/2/state',data=json.dumps({'on':True}))
return True
else:
print("Already on")
return False
def turn_off():
if is_on():
requests.put('http://'+PHILLIPS_IP+'/api/'+PHILLIPS_USER+'/lights/2/state',data=json.dumps({'on':False}))
return True
else:
print("Already off")
return False
last_alarm = 0
off_timer = 0
offCount = 0
onCount = 0
it = 0
while True:
i=GPIO.input(11)
j=GPIO.input(7)
if i==1:
print("PIR1")
onCount = 0
offCount += 1
if offCount > 5:
offCount = 0
if turn_off():
time.sleep(3)
off_timer = time.time()
elif j==1:
print("PIR2")
offCount = 0
onCount += 1
if onCount > 5:
onCount = 0
off_timer = time.time()
if turn_on():
time.sleep(3)
time.sleep(0.1)
it += 1
if it == 30:
it = 0
check_aurora = percent_aurora_right_now()
print(check_aurora)
if check_aurora > 70 and time.time() - last_alarm > 60*60:
last_alarm = time.time()
light_alarm()
if time.time() - off_timer > 60*60:
off_timer = time.time()
turn_off()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment