def update_data(): threading.Timer(600.0, update_data).start() bath_temperature.pop(0) bath_temperature.append({ 'time': time.strftime('%H:%M'), 'temp': read_device_file() })
def monitor_home(message, interval): global monitor_flag global monitor_interval global new_interval new_interval = True monitor_interval = int(interval) if (not monitor_flag): if (monitor_interval > 0): # Monitoring start monitor_flag = True while True: # Loop infinitely until monitoring is cancelled capture = True previous_time = time.time() while True: # Loop for monitor interval if (new_interval): # New interval time is set if (monitor_interval == 0): # 0 -> Cancel monitoring monitor_flag = False break # Non 0 -> continue monitoring with new interval time else: new_interval = False capture = True # forse to capture when interval is changed previous_time = time.time() # Timer reset message.reply( "Monitoring interval is set to {} minutes". format(monitor_interval)) current_temp = read_device_file() if current_temp > FIRE_WARNING: message.reply( "#### EMERGENCY EMERGENCY TEMPARATURE REACHED {} ####" .format(FIRE_WARNING)) sound_player.play_id_sound(FIRE_ALARM) capture = True # forse to capture in case of fire if (capture): capture = False fpath = camera_capture() message.channel.upload_file("{}".format(current_temp), fpath) time.sleep( 2 ) # check Monitor command interval change every 2 seconds current_time = time.time() if int(current_time - previous_time) >= monitor_interval * 60: capture = True previous_time = current_time if (not monitor_flag): break message.reply("Monitoring cancelled") else: message.reply("Monitoring not started")
def graph(): title = 'Bath Temperature' bar_chart = pygal.StackedLine(width=1200, height=600, explicit_size=True, title=title, x_label_rotation=20, style=NeonStyle, fill=True) bar_chart.x_labels = [data['time'] for data in bath_temperature] bar_chart.add('Temperature', [float(data['temp']) for data in bath_temperature]) html = """ <html> <head> <title>%s</title> </head> <body> Current Temperature is %s<br> %s </body> </html> """ % (title, str(read_device_file()), bar_chart.render()) return html
def return_temp(message): message.reply('Temperature is {} degrees'.format(read_device_file()))
def file(): return str(read_device_file())
def random_on_off(outlet_id, mode): global outlet_status status_index = int(outlet_id) - 1 random_mode[status_index] = mode if (outlet_id in config.CODES): if outlet_status[status_index] >= 2: # Already random -> do nothing return "Outlet {} is already set random or solar".format(outlet_id) outlet_status[status_index] = 2 # Set random status previous_time = time.time() trigger_seconds = 0 # Turn on immediately at start while True: if outlet_status[status_index] < 2: # Status is set to not random break current_time = time.time() if current_time - previous_time > trigger_seconds: if outlet_status[status_index] == 2: # Now off -> turn on if (random_mode[status_index] == 0): trigger_seconds = random.randint(on_min, on_max) * 60 now = time.localtime() if now.tm_hour < sunrise_hours[ now.tm_mon - 1] or now.tm_hour > sunset_hours[now.tm_mon - 1]: _send_pulse(config.CODES[outlet_id][0]) outlet_status[ status_index] = 3 # change status to random on else: trigger_seconds = solar_run now = time.localtime() if now.tm_hour >= sunrise_hours[ now.tm_mon - 1] + 3 and now.tm_hour <= sunset_hours[ now.tm_mon - 1] - 2: _send_pulse(config.CODES[outlet_id][0]) outlet_status[ status_index] = 3 # change status to random on start_temp = read_device_file() print "[{}] Pump starting at {} degrees".format( time.strftime('%H:%M'), start_temp) else: if (random_mode[status_index] == 0): trigger_seconds = random.randint(off_min, off_max) * 60 else: trigger_seconds = solar_wait if (random_mode[status_index] == 2 ): # solar with temparature monitoring end_temp = read_device_file() temp_gain = end_temp - start_temp print "[{}] {} --> {} Temparature gain is {}".format( time.strftime('%H:%M'), start_temp, end_temp, temp_gain) if ( temp_gain < 0 ): # temparature gain is negative -> exit solar mode print "No temparature gain -> exit solar mode" outlet_status[status_index] = 0 _send_pulse(config.CODES[outlet_id][1]) break if ( temp_gain < temp_threshold1 ): # Very little temparature gain -> 3 X the wait time trigger_seconds = solar_wait * 3 print "Very little temparature gain -> 3 X the wait time" elif ( temp_gain < temp_threshold2 ): # Not enough temprature gain -> 2X the wait time trigger_seconds = solar_wait * 2 print "Not enough tempararture gain -> 2 X wait time" outlet_status[status_index] = 2 _send_pulse(config.CODES[outlet_id][1]) previous_time = current_time time.sleep(sense_interval) return "Random or solar on/off outlet {} terminated".format(outlet_id) else: return "No outlet {} in the database".format(outlet_id)
def start_monitoring(self): while True: if (self.previous_target <> self.target_temp): if (self.target_temp <> 0.0): self.respond( "Now target temparature is set to {} degrees".format( self.target_temp)) else: self.respond("Temparature monitoring stopped") self.previous_target = self.target_temp if (self.target_temp > 0.0): # heating process started estimation_started = False measurement_started = False GPIO.output( LED_PIN, GPIO.HIGH) # Turn on LED lamp for temo monitoring mode skip_message = True # skip first message because it may be unreliable self.finish_in_minutes = 0.0 # estimate remaining time in minutes average = 0.0 # avarage increment for 1 minute self.is_heating = True self.respond( "O.K., I'll let you know when the temparature reaches to {} degrees" .format(self.target_temp)) previous_time = time.time() current_temp = read_device_file() average_period = 5 while True: previous_temp = current_temp if (self.loop_counter % 3 == 0): self.display_temp() elif (self.loop_counter % 3 == 1): self.display_target_temp() else: self.display_time_until_done() self.loop_counter = self.loop_counter + 1 time.sleep(self.display_duration) current_time = time.time() current_temp = read_device_file() if current_temp > float(self.target_temp): break if not estimation_started: if int(current_time - previous_time ) >= 180: # wait for 3 minutes to be stable estimation_started = True previous_time = current_time initial_time = current_time initial_temp = current_temp else: if not measurement_started: if current_temp > previous_temp: # Start measurement at the timing of temparature increase measurement_started = True start_time = current_time start_temp = current_temp else: if int(current_time - previous_time) >= ( average_period * 60 ): # Calculate average for every average period minutes if current_temp > previous_temp: # Waiting for temparature increase timing previous_time = current_time measurement_started = False average_increment_lap = ( current_temp - start_temp) * 60 / ( current_time - initial_time) average_increment_total = ( current_temp - initial_temp ) * 60 / ( current_time - initial_time ) # increment for 1 minutes from estimation start time average = (average_increment_lap + average_increment_total) / 2 print "[{}] Lap average: {} - {} -> {} degree increment in {} seconds".format( time.strftime('%H:%M:%S'), average_increment_lap, start_temp, current_temp, int(current_time - start_time)) print "Total average: {} New average {}".format( average_increment_total, average) if average > 0.0: # eastimete if average increase is positive self.finish_in_minutes = (float(self.target_temp) - current_temp) / average if self.finish_in_minutes < 5.0: self.estimate_message(0, current_temp) elif self.finish_in_minutes < 10.0: self.estimate_message(1, current_temp) elif self.finish_in_minutes < 15.0: self.estimate_message(2, current_temp) elif self.finish_in_minutes < 20.0: self.estimate_message(3, current_temp) else: self.skip_first_message = False if (self.target_temp <> 0): self.respond( "[{}] The temparature reached to {} degrees".format( time.strftime('%H:%M:%S'), self.target_temp)) self.is_heating = False self.finish_in_minutes = 0.0 GPIO.output(LED_PIN, GPIO.LOW) turn_off(HEATER_1_OUTLET) turn_off(HEATER_2_OUTLET) if (self.target_temp >= START_TEMP): sound_player.play_bath_sound() self.target_temp = 0 else: if (self.loop_counter % 2 == 0): self.display_time() else: if (is_random()): GPIO.output(LED_PIN, GPIO.HIGH) self.display_temp() GPIO.output(LED_PIN, GPIO.LOW) else: self.display_temp() self.loop_counter = self.loop_counter + 1 time.sleep(self.display_duration)
def display_temp(self): self.display.clear() self.display.print_float(read_device_file(), decimal_digits=1) self.display.write_display()