def reset(): grn_led.turnon() red_led.turnoff() def cleanup(): grn_led.turnoff() red_led.turnoff() #main program try: reset() buzzer.beep(2, blink_beep_speed) while True: distance = sensor.getdistance() if distance == -1: #sensor error continue if distance <= distance_threshold: if not picture_taken: print("too close, taking a picture...") take_picture_async() picture_taken = True else: if picture_taken: print("too far, release...")
def start(self): # change these as desired - they're the pins connected from the # SPI port on the ADC to the Cobbler SPICLK = RaspberryPi.SPI_CE1 #CLK SPIMISO = RaspberryPi.SPI_CE0 #DOUT SPIMOSI = RaspberryPi.SPI_SCLK #DIN SPICS = RaspberryPi.SPI_MISO #CS # set up the SPI interface pins GPIO.setup(SPIMOSI, GPIO.OUT) GPIO.setup(SPIMISO, GPIO.IN) GPIO.setup(SPICLK, GPIO.OUT) GPIO.setup(SPICS, GPIO.OUT) # 10k trim pot connected to adc #0 potentiometer_adc = 0; last_read = 0 # this keeps track of the last potentiometer value tolerance = 5 # to keep from being jittery we'll only change # volume when the pot has moved more than 5 'counts' buzzer = Buzzer() lcd = LCD() lcd.Blink() printed = False try: while True: # we'll assume that the pot didn't move trim_pot_changed = False # read the analog pin trim_pot = self.readadc(potentiometer_adc, SPICLK, SPIMOSI, SPIMISO, SPICS) # how much has it changed since the last read? pot_adjust = abs(trim_pot - last_read) if DEBUG: print "trim_pot:", trim_pot print "pot_adjust:", pot_adjust print "last_read", last_read if ( pot_adjust > tolerance ): trim_pot_changed = True if DEBUG: print "trim_pot_changed", trim_pot_changed if ( trim_pot_changed ): value = trim_pot / 10.24 # convert 10bit adc0 (0-1024) trim pot read into 0-100 volume level value = round(value) # round out decimal value value = int(value) # cast volume as integer number = ''; printedNumber = '' released = False if ( value != 60): if (value >= 75 and value < 80): number ='1' elif (value >= 69 and value < 74): number ='2' elif (value >= 64 and value <= 70): number ='3' elif (value >= 0 and value <= 12): number ='4' elif (value >= 32 and value <= 37): number ='5' elif (value >= 38 and value <= 44): number ='6' elif (value >= 21 and value <= 26): number ='7' elif (value >= 83 and value <= 90): number ='8' elif (value >= 45 and value <= 51): number ='9' elif (value >= 54 and value <= 59): number ='*' elif (value >= 94 and value <= 100): number ='0' elif (value >= 50 and value <= 53): number ='#' else: print "Dunno: ", value if (value == 60): released = True printed = False if(number != printedNumber and not printed): printedNumber = number print number, value lcd.message(number) buzzer.beep(659, 125) printed = True time.sleep(0.1) if(number == '*'): self.typePassword = 1 self.password = '' elif(number == '#' and self.typePassword == 1): lcd.noBlink() self.typePassword = 0 user = self.database.check_login(self.password) print user sleep_time = 1 if(user != 'Invalid Code'): sleep_time = 5 self.led.show_status_light = 1 system_status = self.database.system_status() if(system_status == '1'): self.sp.stop_timer() self.sp.soundingAlarm = 0 self.database.system_disarm() message = 'User '+user['first_name']+' '+user['last_name']+' disarmed the system' print message self.database.log(message) print user['first_name']+' '+user['last_name'] user = '******'+user['first_name']+' '+user['last_name'] else: self.database.system_arm() user = user['first_name']+' '+user['last_name']+'\nArmed The System' self.database.log(user) time.sleep(0.5) lcd.clear() lcd.message(user) self.message = '' time.sleep(sleep_time) lcd.clear() lcd.Blink() if(self.password == '1111'): print 'play mario' mario_thread = threading.Thread(target=self.play_song(),) print 'die mario' mario_thread.start() elif(self.typePassword == 1): if(number != '*'): self.password += number time.sleep(0.01) except KeyboardInterrupt: GPIO.cleanup() print "\nKill"
class Serialcom(threading.Thread): def __init__(self): threading.Thread.__init__(self) self.shutdown_flag = threading.Event() self.motor = Motorcontroller() self.buzzer = Buzzer() self.xbee = Xbee() self.decoder = Decoder() self.servo1 = 96 self.servo2 = 75 self.joycalc = Joystick() self.motor.setServo1(self.servo1) self.motor.setServo2(self.servo2) self.lastSavedTime = 0 def run(self): print('Thread #%s started' % self.ident) self.motor.timeout(1) while not self.shutdown_flag.is_set(): rcvdata = self.xbee.read() self.decoder.decode(rcvdata) self.motor.recalCommand() currenttime = time.time() if currenttime - self.lastSavedTime > 1.0: self.lastSavedTime = time.time() self.xbee.sendBat(self.decoder.getRfrating()) if self.decoder.getStatus() and self.decoder.checkCRC(): if self.decoder.getJoyStickPB1() == 0: self.motor.EmergyStop() self.buzzer.beep(300) elif self.decoder.getJoystickM1() > 248 and self.decoder.getJoystickM2() > 248: self.joycalc.calculateReg(255) self.motor.Motor1MC2(255 - self.joycalc.cor1) self.motor.Motor2MC2(255 - self.joycalc.cor2) elif (abs(self.decoder.getJoystickM1() - self.decoder.getJoystickM2()) <= 3) and (self.decoder.getJoystickM1() > 50): self.joycalc.calculateReg(self.decoder.getJoystickM1()) self.motor.Motor1MC2(self.decoder.getJoystickM1() - self.joycalc.cor1) self.motor.Motor2MC2(self.decoder.getJoystickM1() - self.joycalc.cor2) #print "drive forward without full speed" else: self.motor.Motor1MC2(self.decoder.getJoystickM1()) self.motor.Motor2MC2(self.decoder.getJoystickM2()) #print "other speeds" if self.decoder.getJoystickPB2() == 0: self.servo1 = 96 self.motor.setServo1(self.servo1) self.buzzer.beep(300) elif self.decoder.getJoystickVRX2() > 1000: if(self.servo1 > 0): self.servo1 = self.servo1 - 1 self.motor.setServo1(self.servo1) elif self.decoder.getJoystickVRX2() < 24: if(self.servo1 < 180): self.servo1 = self.servo1 + 1 self.motor.setServo1(self.servo1) if self.decoder.getJoystickPB2() == 0: self.servo2 = 75 self.motor.setServo2(self.servo2) elif self.decoder.joystick_VRY2 > 1000: if(self.servo2 > 0): self.servo2 = self.servo2 - 1 self.motor.setServo2(self.servo2) elif self.decoder.getJoystickVRY2() < 24: if(self.servo2 < 180): self.servo2 = self.servo2 + 1 self.motor.setServo2(self.servo2) time.sleep(0.001) # ... Clean shutdown code here ... self.xbee.close() self.motor.close() print('Thread #%s stopped' % self.ident)
class SensorProcess(): def __init__(self, led): self.database = database = Database() self.timeout = 60 self.timeout_thread = threading.Thread(target=self.start_timer_process, args=(self.timeout,)) self.soundingAlarm = 0 self.buzzer = Buzzer() self.led = led self.led_status_thread = threading.Thread(target=self.led.status_light) self.led_status_thread.start() self.sensor = None def trip_sensor(self,sensorId): self.cancelTimer = 0 status = self.database.system_status() print 'system status '+status if(status=='1'): sensor = self.database.get_sensor(sensorId) self.sensor = sensor if(sensor and sensor['status']): if(self.soundingAlarm == 0): self.buzzer.beep(659, 125) message = "Sensor Tripped: "+sensor['title'] self.database.log(message) print 'tripped sensor: '+sensor['title'] print sensor['type'] if(sensor['type'] == 'Door'): self.start_timer() print 'sensor type: Door' elif(sensor['type'] == 'Window'): print 'sensor type: Window' # sound alarm print 'BEEP' self.alert() elif(sensor['type'] == 'Room'): print 'sensor type: Room' # sound alarm print 'BEEP' self.alert() else: print 'sensor not found' else: self.buzzer.beep(800, 20) time.sleep(0.1) self.buzzer.beep(800, 20) def start_timer(self): print 'trying to start timer' if(self.timeout_thread.isAlive()==False): try: print 'START TIMER' self.timeout_thread = threading.Thread(target=self.start_timer_process, args=(self.timeout,)) self.timeout_thread.start() except: print 'Trouble with t1 synchronizer' def start_timer_process(self,timeout): self.cancelTimer = 0 timeout = timeout*10 while timeout > 0: if(timeout % 4 == 0): self.buzzer.beep(800, 25) # correct code entered if self.cancelTimer: break # sleep else: print(timeout) timeout -=1 # need a small time otherwise it takes too long to stop the timer time.sleep(0.1) # send alerts if(self.cancelTimer == 0): self.alert() def stop_timer(self): print 'STOP TIMER' self.cancelTimer = 1 def alert(self): print 'sending alerts' if(self.soundingAlarm == 0): self.alarmSound_thread = threading.Thread(target=self.sound_alarm) self.alarmSound_thread.start() users = self.database.get_users() client = TwilioRestClient("ACc4cb12c713e1483cb661100848c562b8", "c829b7d4169070c10e5121f0a55180af") for user in users: if(user['alert'] == 0): continue; message = self.sensor['title']+' Sensor Tripped' client.sms.messages.create(to=user['phone'], from_="+15855981936", body=message) SUBJECT = "BADAX Alerts" TO = user['email'] FROM = "*****@*****.**" BODY = string.join(( "From: %s" % FROM, "To: %s" % TO, "Subject: %s" % SUBJECT , "", message ), "\r\n") mailServer = smtplib.SMTP('smtp.gmail.com') mailServer.starttls() mailServer.login('*****@*****.**', 'sourfarm39') mailServer.sendmail(FROM, [TO], BODY) mailServer.quit() def sound_alarm(self): self.led.show_status_light = 0 if(self.soundingAlarm == 0): self.soundingAlarm = 1 while(self.soundingAlarm): self.buzzer.beep(800, 100) time.sleep(0.1)
number ='7' elif (value >= 83 and value <= 90): number ='8' elif (value >= 45 and value <= 51): number ='9' elif (value >= 53 and value <= 59): number ='*' elif (value >= 94 and value <= 100): number ='0' elif (value >= 50 and value <= 56): number ='#' else: print "Dunno: ", value if ( value == 60): released = True printed = False if(number != printedNumber and not printed): printedNumber = number print number, value #lcd.message(number) buzzer.beep(659, 125) printed = True time.sleep(0.1) time.sleep(0.01) except KeyboardInterrupt: GPIO.cleanup() print "\nKill"
from Buzzer import Buzzer import time buzz1 = Buzzer(24) buzz1.on() time.sleep(1) buzz1.off() time.sleep(1) buzz1.toggle() if buzz1.is_on(): print("on") elif buzz1.is_off(): print("off") time.sleep(1) buzz1.toggle() if buzz1.is_on(): print("on") elif buzz1.is_off(): print("off") time.sleep(1) buzz1.beep() time.sleep(5) buzz1.beep(2, 20) time.sleep(5)