def thread(self, args): ALERT_FLAG = 0 while True: self.trigger_pin.write(0) time.sleep(0.000002) # 2 microseconds self.trigger_pin.write(1) time.sleep(0.00001) # 10 microseconds self.trigger_pin.write(0) duration = pulsein.pulseIn(self.echo_pin) # microseconds ultrasonic_value.value = self.initial_value - (duration / self.conv_ratio) # distance if ultrasonic_value.value >= MAX_WATER: if ALERT_FLAG == 0: ALERT_FLAG = 1 print "ALERT UP!!!!!!!!!" database.insertAlert(datetime.now(), NODE, ALERT_FLAG) else: if ALERT_FLAG == 1: ALERT_FLAG = 0 print "ALERT DOWM!!!!!!!" database.insertAlert(datetime.now(), NODE, ALERT_FLAG) if ultrasonic_value.value >= self.max_range or ultrasonic_value.value <= self.min_range: print "Out of range {0}".format(ultrasonic_value.value) ultrasonic_value.value = -1 else: print "Distance: {0} cm".format(ultrasonic_value.value) time.sleep(DELTAT_ULTRA)
def read_avg(self): duration = 0 for i in range(0, SAMPLE_RATE): self.trigger_pin.write(0) time.sleep(0.000002) #2 microseconds self.trigger_pin.write(1) time.sleep(0.00001) #10 microseconds self.trigger_pin.write(0) duration = duration + pulsein.pulseIn(self.echo_pin) # microseconds return (duration / SAMPLE_RATE)
def thread(self, args): while True: self.trigger_pin.write(0) time.sleep(0.000002) #2 microseconds self.trigger_pin.write(1) time.sleep(0.00001) #10 microseconds self.trigger_pin.write(0) duration = pulsein.pulseIn(self.echo_pin) # microseconds ultrasonic_value.value = self.initial_value - (duration / self.conv_ratio) # distance if ultrasonic_value.value >= self.max_range or \ ultrasonic_value.value <= self.min_range : print "Out of range {0}".format(ultrasonic_value.value) ultrasonic_value.value = -1 else : print "Distance: {0} cm".format(ultrasonic_value.value) time.sleep(0.2)
def thread(self, args): ALERT_FLAG = 0 lcd.color_semaphore(0) print "Entering thread" while True: self.trigger_pin.write(0) time.sleep(0.000002) #2 microseconds self.trigger_pin.write(1) time.sleep(0.00001) #10 microseconds self.trigger_pin.write(0) duration = pulsein.pulseIn(self.echo_pin) # microseconds # duration = self.read_avg() ultrasonic_value.value = self.initial_value - (duration / self.conv_ratio) # distance print "Duration {0}".format(duration) if ultrasonic_value.value >= MAX_WATER + ERROR : if ALERT_FLAG == 0: ALERT_FLAG = 1 print 'ALERT UP!!!!!!!!!' database.insertAlert(datetime.now(), NODE, ALERT_FLAG) try: lcd.color_semaphore(1) twitt.tweet_place("Ooops! Flood detected: {0:.2f} cm".format(ultrasonic_value.value), twitt.lat, twitt.lon) except Exception as e: print 'Cannot send twitt', e elif ultrasonic_value.value > 0 and ultrasonic_value.value < MAX_WATER - ERROR: if ALERT_FLAG == 1: ALERT_FLAG = 0 print 'ALERT DOWM!!!!!!!' database.insertAlert(datetime.now(), NODE ,ALERT_FLAG) try: lcd.color_semaphore(0) twitt.tweet_place("Yeii! Level fell to {0:.2f} cm".format(ultrasonic_value.value), twitt.lat, twitt.lon) except Exception as e: print 'Cannot send twitt', e if ultrasonic_value.value >= self.max_range or \ ultrasonic_value.value <= self.min_range : print "Out of range {0}".format(ultrasonic_value.value) ultrasonic_value.value = -1 else : print "Distance: {0} cm".format(ultrasonic_value.value) time.sleep(DELTAT_ULTRA)