Skip to content

Instantly share code, notes, and snippets.

@mdp
Last active September 26, 2017 21:02
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 mdp/b4e2b3d878d2e6d76ad738c2867f8d4e to your computer and use it in GitHub Desktop.
Save mdp/b4e2b3d878d2e6d76ad738c2867f8d4e to your computer and use it in GitHub Desktop.
The occupancy sensor at work have REALLY short timeouts.
try:
import RPi.GPIO as GPIO
except RuntimeError:
print("Error importing RPi.GPIO! This is probably because you need superuser privileges. You can achieve this by using 'sudo' to run your script")
import time
import random
import pytz
from datetime import datetime
# microsecond sleep for ultrasonic trigger
usleep = lambda x: time.sleep(x/1000000.0)
CHANNEL = 14 # Pin 08 on the RaspPi
GPIO.setmode(GPIO.BCM)
GPIO.setup(CHANNEL, GPIO.OUT)
def isBusinessHours():
today = datetime.now(pytz.utc).astimezone(pytz.timezone('America/Los_Angeles'))
# 8am to 5pm, Mon (0) to Friday (4)
if 8 <= today.hour < 17 and today.weekday() < 5:
return True
return False
def trigger():
GPIO.output(CHANNEL, 1)
usleep(10)
GPIO.output(CHANNEL, 0)
def blast():
for x in range(0, 200):
trigger()
usleep(random.randint(50, 200))
try:
while 1:
if isBusinessHours():
print("Blasting ultrasonic during business hours")
blast()
else:
print("Office is closed, no ultrasonic please")
time.sleep(30)
except KeyboardInterrupt: # If CTRL+C is pressed, exit cleanly:
GPIO.cleanup() # cleanup all GPIO
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment