def __init__(self, ready, lcd_id): threading.Thread.__init__(self) self.logger = logging.getLogger("mycodo.lcd_{id}".format(id=lcd_id)) self.running = False self.thread_startup_timer = timeit.default_timer() self.thread_shutdown_timer = 0 self.ready = ready self.flash_lcd_on = False self.lcd_is_on = False self.lcd_id = lcd_id try: lcd = db_retrieve_table_daemon(LCD, device_id=self.lcd_id) self.lcd_name = lcd.name self.lcd_location = lcd.location self.lcd_period = lcd.period self.lcd_x_characters = lcd.x_characters self.lcd_y_lines = lcd.y_lines self.timer = time.time() + self.lcd_period self.backlight_timer = time.time() if lcd.multiplexer_address: self.multiplexer_address_string = lcd.multiplexer_address self.multiplexer_address = int(str(lcd.multiplexer_address), 16) self.multiplexer_channel = lcd.multiplexer_channel self.multiplexer = TCA9548A(self.multiplexer_address) else: self.multiplexer = None self.lcd_line = {} for i in range(1, 5): self.lcd_line[i] = {} self.list_sensors = MEASUREMENT_UNITS self.list_sensors.update( {'sensor_time': { 'unit': None, 'name': 'Time' }}) self.list_pids = ['setpoint', 'pid_time'] self.list_relays = ['duration_sec', 'relay_time', 'relay_state'] if self.lcd_y_lines in [2, 4]: self.setup_lcd_line(1, lcd.line_1_sensor_id, lcd.line_1_measurement) self.setup_lcd_line(2, lcd.line_2_sensor_id, lcd.line_2_measurement) if self.lcd_y_lines == 4: self.setup_lcd_line(3, lcd.line_3_sensor_id, lcd.line_3_measurement) self.setup_lcd_line(4, lcd.line_4_sensor_id, lcd.line_4_measurement) self.lcd_string_line = {} for i in range(1, self.lcd_y_lines + 1): self.lcd_string_line[i] = '' self.LCD_WIDTH = self.lcd_x_characters # Max characters per line self.LCD_LINE = {1: 0x80, 2: 0xC0, 3: 0x94, 4: 0xD4} self.LCD_CHR = 1 # Mode - Sending data self.LCD_CMD = 0 # Mode - SenLCDding command self.LCD_BACKLIGHT = 0x08 # On self.LCD_BACKLIGHT_OFF = 0x00 # Off self.ENABLE = 0b00000100 # Enable bit # Timing constants self.E_PULSE = 0.0005 self.E_DELAY = 0.0005 # Setup I2C bus try: if GPIO.RPI_REVISION == 2 or GPIO.RPI_REVISION == 3: i2c_bus_number = 1 else: i2c_bus_number = 0 self.bus = smbus.SMBus(i2c_bus_number) except Exception as except_msg: self.logger.exception( "Could not initialize I2C bus: {err}".format( err=except_msg)) self.I2C_ADDR = int(self.lcd_location, 16) self.lcd_init() self.lcd_string_write('Mycodo {}'.format(MYCODO_VERSION), self.LCD_LINE[1]) self.lcd_string_write(u'Start {}'.format(self.lcd_name), self.LCD_LINE[2]) except Exception as except_msg: self.logger.exception("Error: {err}".format(err=except_msg))
def __init__(self, ready, logger, sensor_id): threading.Thread.__init__(self) list_devices_i2c = [ 'ADS1x15', 'AM2315', 'ATLAS_PT1000', 'BMP', 'HTU21D', 'MCP342x', 'SHT2x', 'TMP006', 'TSL2561' ] self.thread_startup_timer = timeit.default_timer() self.thread_shutdown_timer = 0 self.ready = ready self.logger = logger self.lock = {} self.sensor_id = sensor_id self.control = DaemonControl() self.pause_loop = False self.verify_pause_loop = True self.setup_sensor_conditionals() with session_scope(MYCODO_DB_PATH) as new_session: sensor = new_session.query(Sensor) sensor = sensor.filter(Sensor.id == self.sensor_id).first() self.i2c_bus = sensor.i2c_bus self.location = sensor.location self.device_type = sensor.device self.sensor_type = sensor.device_type self.period = sensor.period self.multiplexer_address_raw = sensor.multiplexer_address self.multiplexer_bus = sensor.multiplexer_bus self.multiplexer_channel = sensor.multiplexer_channel self.adc_channel = sensor.adc_channel self.adc_gain = sensor.adc_gain self.adc_resolution = sensor.adc_resolution self.adc_measure = sensor.adc_measure self.adc_measure_units = sensor.adc_measure_units self.adc_volts_min = sensor.adc_volts_min self.adc_volts_max = sensor.adc_volts_max self.adc_units_min = sensor.adc_units_min self.adc_units_max = sensor.adc_units_max self.sht_clock_pin = sensor.sht_clock_pin self.sht_voltage = sensor.sht_voltage # Edge detection self.switch_edge = sensor.switch_edge self.switch_bouncetime = sensor.switch_bouncetime self.switch_reset_period = sensor.switch_reset_period # Relay that will activate prior to sensor read self.pre_relay_id = sensor.pre_relay_id self.pre_relay_duration = sensor.pre_relay_duration self.pre_relay_setup = False self.next_measurement = time.time() self.get_new_measurement = False self.measurement_acquired = False self.pre_relay_activated = False self.pre_relay_timer = time.time() relay = new_session.query(Relay).all() for each_relay in relay: # Check if relay ID actually exists if each_relay.id == self.pre_relay_id and self.pre_relay_duration: self.pre_relay_setup = True smtp = new_session.query(SMTP).first() self.smtp_max_count = smtp.hourly_max self.email_count = 0 self.allowed_to_send_notice = True # Convert string I2C address to base-16 int if self.device_type in list_devices_i2c: self.i2c_address = int(str(self.location), 16) # Set up multiplexer if enabled if self.device_type in list_devices_i2c and self.multiplexer_address_raw: self.multiplexer_address_string = self.multiplexer_address_raw self.multiplexer_address = int(str(self.multiplexer_address_raw), 16) self.multiplexer_lock_file = "/var/lock/mycodo_multiplexer_0x{:02X}.pid".format( self.multiplexer_address) self.multiplexer = TCA9548A(self.multiplexer_bus, self.multiplexer_address) else: self.multiplexer = None if self.device_type in ['ADS1x15', 'MCP342x'] and self.location: self.adc_lock_file = "/var/lock/mycodo_adc_bus{}_0x{:02X}.pid".format( self.i2c_bus, self.i2c_address) # Set up edge detection of a GPIO pin if self.device_type == 'EDGE': if self.switch_edge == 'rising': self.switch_edge_gpio = GPIO.RISING elif self.switch_edge == 'falling': self.switch_edge_gpio = GPIO.FALLING else: self.switch_edge_gpio = GPIO.BOTH # Set up analog-to-digital converter elif self.device_type == 'ADS1x15': self.adc = ADS1x15_read(self.i2c_address, self.i2c_bus, self.adc_channel, self.adc_gain) elif self.device_type == 'MCP342x': self.adc = MCP342x_read(self.i2c_address, self.i2c_bus, self.adc_channel, self.adc_gain, self.adc_resolution) else: self.adc = None self.device_recognized = True # Set up sensor if self.device_type in ['EDGE', 'ADS1x15', 'MCP342x']: self.measure_sensor = None elif self.device_type == 'RPiCPULoad': self.measure_sensor = RaspberryPiCPULoad() elif self.device_type == 'RPi': self.measure_sensor = RaspberryPiCPUTemp() elif self.device_type == 'DS18B20': self.measure_sensor = DS18B20(self.location) elif self.device_type == 'DHT11': self.measure_sensor = DHT11(pigpio.pi(), int(self.location)) elif self.device_type in ['DHT22', 'AM2302']: self.measure_sensor = DHT22(pigpio.pi(), int(self.location)) elif self.device_type == 'HTU21D': self.measure_sensor = HTU21D_read(self.i2c_bus) elif self.device_type == 'AM2315': self.measure_sensor = AM2315_read(self.i2c_bus) elif self.device_type == 'ATLAS_PT1000': self.measure_sensor = Atlas_PT1000(self.i2c_address, self.i2c_bus) elif self.device_type == 'K30': self.measure_sensor = K30() elif self.device_type == 'BMP': self.measure_sensor = BMP(self.i2c_bus) elif self.device_type == 'SHT1x_7x': self.measure_sensor = SHT1x_7x_read(self.location, self.sht_clock_pin, self.sht_voltage) elif self.device_type == 'SHT2x': self.measure_sensor = SHT2x_read(self.i2c_address, self.i2c_bus) elif self.device_type == 'TMP006': self.measure_sensor = TMP006_read(self.i2c_address, self.i2c_bus) elif self.device_type == 'TSL2561': self.measure_sensor = TSL2561_read(self.i2c_address, self.i2c_bus) else: self.device_recognized = False self.logger.debug("[Sensor {}] Device '{}' not " "recognized:".format(self.sensor_id, self.device_type)) raise Exception("{} is not a valid device type.".format( self.device_type)) self.edge_reset_timer = time.time() self.sensor_timer = time.time() self.running = False self.lastUpdate = None
def __init__(self, ready, logger, lcd_id): threading.Thread.__init__(self) self.thread_startup_timer = timeit.default_timer() self.thread_shutdown_timer = 0 self.ready = ready self.logger = logger self.flash_lcd_on = False self.lcd_is_on = False try: with session_scope(MYCODO_DB_PATH) as new_session: lcd = new_session.query(LCD).filter(LCD.id == lcd_id).first() self.lcd_id = lcd_id self.lcd_name = lcd.name self.lcd_pin = lcd.pin self.lcd_period = lcd.period self.lcd_x_characters = lcd.x_characters self.lcd_y_lines = lcd.y_lines if lcd.multiplexer_address: self.multiplexer_address_string = lcd.multiplexer_address self.multiplexer_address = int( str(lcd.multiplexer_address), 16) self.multiplexer_channel = lcd.multiplexer_channel self.multiplexer = TCA9548A(self.multiplexer_address) else: self.multiplexer = None self.lcd_line = {} for i in range(1, 5): self.lcd_line[i] = {} list_sensors = [ 'sensor_time', 'temperature', 'humidity', 'co2', 'pressure', 'altitude', 'temperature_die', 'temperature_object', 'lux' ] list_PIDs = ['setpoint', 'pid_time'] list_relays = ['duration_sec', 'relay_time', 'relay_state'] if self.lcd_y_lines in [2, 4]: self.lcd_line[1]['id'] = lcd.line_1_sensor_id self.lcd_line[1]['measurement'] = lcd.line_1_measurement if lcd.line_1_sensor_id: if lcd.line_1_measurement in list_sensors: table = Sensor elif lcd.line_1_measurement in list_PIDs: table = PID elif lcd.line_1_measurement in list_relays: table = Relay sensor_line_1 = new_session.query(table).filter( table.id == lcd.line_1_sensor_id).first() self.lcd_line[1]['name'] = sensor_line_1.name if 'time' in lcd.line_1_measurement: self.lcd_line[1]['measurement'] = 'time' self.lcd_line[2]['id'] = lcd.line_2_sensor_id self.lcd_line[2]['measurement'] = lcd.line_2_measurement if lcd.line_2_sensor_id: if lcd.line_2_measurement in list_sensors: table = Sensor elif lcd.line_2_measurement in list_PIDs: table = PID elif lcd.line_2_measurement in list_relays: table = Relay sensor_line_2 = new_session.query(table).filter( table.id == lcd.line_2_sensor_id).first() self.lcd_line[2]['name'] = sensor_line_2.name if 'time' in lcd.line_2_measurement: self.lcd_line[2]['measurement'] = 'time' if self.lcd_y_lines == 4: self.lcd_line[3]['id'] = lcd.line_3_sensor_id self.lcd_line[3]['measurement'] = lcd.line_3_measurement if lcd.line_3_sensor_id: if lcd.line_3_measurement in list_sensors: table = Sensor elif lcd.line_3_measurement in list_PIDs: table = PID elif lcd.line_3_measurement in list_relays: table = Relay sensor_line_3 = new_session.query(table).filter( table.id == lcd.line_3_sensor_id).first() self.lcd_line[3]['name'] = sensor_line_3.name if 'time' in lcd.line_3_measurement: self.lcd_line[3]['measurement'] = 'time' self.lcd_line[4]['id'] = lcd.line_4_sensor_id self.lcd_line[4]['measurement'] = lcd.line_4_measurement if lcd.line_4_sensor_id: if lcd.line_4_measurement in list_sensors: table = Sensor elif lcd.line_4_measurement in list_PIDs: table = PID elif lcd.line_4_measurement in list_relays: table = Relay sensor_line_4 = new_session.query(table).filter( table.id == lcd.line_4_sensor_id).first() self.lcd_line[4]['name'] = sensor_line_4.name if 'time' in lcd.line_4_measurement: self.lcd_line[4]['measurement'] = 'time' self.measurement_unit = {} self.measurement_unit['metric'] = { "temperature": "C", "humidity": "%", "co2": "ppmv", "pressure": "Pa", "altitude": "m", "duration_sec": "s", "temperature_die": "C", "temperature_object": "C", "lux": "lux", } self.measurement_unit['standard'] = { "temperature": "F", "humidity": "%", "co2": "ppmv", "pressure": "atm", "altitude": "ft", "duration_sec": "s", "temperature_die": "F", "temperature_object": "F", "lux": "lux", } self.timer = time.time() + self.lcd_period self.backlight_timer = time.time() self.lcd_string_line = {} for i in range(1, self.lcd_y_lines + 1): self.lcd_string_line[i] = '' self.LCD_WIDTH = self.lcd_x_characters # Maximum characters per line self.LCD_LINE = {} self.LCD_LINE[1] = 0x80 # LCD RAM address for the 1st line self.LCD_LINE[2] = 0xC0 # LCD RAM address for the 2nd line self.LCD_LINE[3] = 0x94 # LCD RAM address for the 3rd line self.LCD_LINE[4] = 0xD4 # LCD RAM address for the 4th line self.LCD_CHR = 1 # Mode - Sending data self.LCD_CMD = 0 # Mode - SenLCDding command self.LCD_BACKLIGHT = 0x08 # On self.LCD_BACKLIGHT_OFF = 0x00 # Off self.ENABLE = 0b00000100 # Enable bit # Timing constants self.E_PULSE = 0.0005 self.E_DELAY = 0.0005 # Setup I2C bus try: if GPIO.RPI_REVISION == 2 or GPIO.RPI_REVISION == 3: I2C_bus_number = 1 else: I2C_bus_number = 0 self.bus = smbus.SMBus(I2C_bus_number) except Exception as except_msg: self.logger.exception( "Could not initialize I2C bus: {}".format(except_msg)) self.I2C_ADDR = int(self.lcd_pin, 16) self.lcd_init() self.lcd_string_write('Mycodo {}'.format(MYCODO_VERSION), self.LCD_LINE[1]) self.lcd_string_write('Start {}'.format(self.lcd_name), self.LCD_LINE[2]) except Exception as except_msg: self.logger.exception("[LCD {}] Error: {}".format( self.lcd_id, except_msg))
def __init__(self, ready, sensor_id): threading.Thread.__init__(self) self.logger = logging.getLogger( "mycodo.sensor_{id}".format(id=sensor_id)) list_devices_i2c = [ 'ADS1x15', 'AM2315', 'ATLAS_PT1000', 'BME280', 'BMP', 'CHIRP', 'HTU21D', 'MCP342x', 'SHT2x', 'TMP006', 'TSL2561' ] self.thread_startup_timer = timeit.default_timer() self.thread_shutdown_timer = 0 self.ready = ready self.lock = {} self.measurement = None self.updateSuccess = False self.sensor_id = sensor_id self.control = DaemonControl() self.pause_loop = False self.verify_pause_loop = True self.cond_id = {} self.cond_name = {} self.cond_activated = {} self.cond_period = {} self.cond_measurement_type = {} self.cond_edge_select = {} self.cond_edge_detected = {} self.cond_gpio_state = {} self.cond_direction = {} self.cond_setpoint = {} self.cond_relay_id = {} self.cond_relay_state = {} self.cond_relay_on_duration = {} self.cond_execute_command = {} self.cond_email_notify = {} self.cond_flash_lcd = {} self.cond_camera_record = {} self.cond_timer = {} self.smtp_wait_timer = {} self.setup_sensor_conditionals() sensor = db_retrieve_table(MYCODO_DB_PATH, Sensor, device_id=self.sensor_id) self.i2c_bus = sensor.i2c_bus self.location = sensor.location self.device = sensor.device self.sensor_type = sensor.device_type self.period = sensor.period self.multiplexer_address_raw = sensor.multiplexer_address self.multiplexer_bus = sensor.multiplexer_bus self.multiplexer_channel = sensor.multiplexer_channel self.adc_channel = sensor.adc_channel self.adc_gain = sensor.adc_gain self.adc_resolution = sensor.adc_resolution self.adc_measure = sensor.adc_measure self.adc_measure_units = sensor.adc_measure_units self.adc_volts_min = sensor.adc_volts_min self.adc_volts_max = sensor.adc_volts_max self.adc_units_min = sensor.adc_units_min self.adc_units_max = sensor.adc_units_max self.sht_clock_pin = sensor.sht_clock_pin self.sht_voltage = sensor.sht_voltage # Edge detection self.switch_edge = sensor.switch_edge self.switch_bouncetime = sensor.switch_bouncetime self.switch_reset_period = sensor.switch_reset_period # Relay that will activate prior to sensor read self.pre_relay_id = sensor.pre_relay_id self.pre_relay_duration = sensor.pre_relay_duration self.pre_relay_setup = False self.next_measurement = time.time() self.get_new_measurement = False self.measurement_acquired = False self.pre_relay_activated = False self.pre_relay_timer = time.time() relay = db_retrieve_table(MYCODO_DB_PATH, Relay, entry='all') for each_relay in relay: # Check if relay ID actually exists if each_relay.id == self.pre_relay_id and self.pre_relay_duration: self.pre_relay_setup = True smtp = db_retrieve_table(MYCODO_DB_PATH, SMTP, entry='first') self.smtp_max_count = smtp.hourly_max self.email_count = 0 self.allowed_to_send_notice = True # Convert string I2C address to base-16 int if self.device in list_devices_i2c: self.i2c_address = int(str(self.location), 16) # Set up multiplexer if enabled if self.device in list_devices_i2c and self.multiplexer_address_raw: self.multiplexer_address_string = self.multiplexer_address_raw self.multiplexer_address = int(str(self.multiplexer_address_raw), 16) self.multiplexer_lock_file = "/var/lock/mycodo_multiplexer_0x{:02X}.pid".format( self.multiplexer_address) self.multiplexer = TCA9548A(self.multiplexer_bus, self.multiplexer_address) else: self.multiplexer = None if self.device in ['ADS1x15', 'MCP342x'] and self.location: self.adc_lock_file = "/var/lock/mycodo_adc_bus{}_0x{:02X}.pid".format( self.i2c_bus, self.i2c_address) # Set up edge detection of a GPIO pin if self.device == 'EDGE': if self.switch_edge == 'rising': self.switch_edge_gpio = GPIO.RISING elif self.switch_edge == 'falling': self.switch_edge_gpio = GPIO.FALLING else: self.switch_edge_gpio = GPIO.BOTH # Set up analog-to-digital converter elif self.device == 'ADS1x15': self.adc = ADS1x15Read(self.i2c_address, self.i2c_bus, self.adc_channel, self.adc_gain) elif self.device == 'MCP342x': self.adc = MCP342xRead(self.i2c_address, self.i2c_bus, self.adc_channel, self.adc_gain, self.adc_resolution) else: self.adc = None self.device_recognized = True # Set up sensor if self.device in ['EDGE', 'ADS1x15', 'MCP342x']: self.measure_sensor = None elif self.device == 'RPiCPULoad': self.measure_sensor = RaspberryPiCPULoad() elif self.device == 'RPi': self.measure_sensor = RaspberryPiCPUTemp() elif self.device == 'CHIRP': self.measure_sensor = ChirpSensor(self.i2c_address, self.i2c_bus) elif self.device == 'DS18B20': self.measure_sensor = DS18B20Sensor(self.location) elif self.device == 'DHT11': self.measure_sensor = DHT11Sensor(self.sensor_id, int(self.location)) elif self.device in ['DHT22', 'AM2302']: self.measure_sensor = DHT22Sensor(self.sensor_id, int(self.location)) elif self.device == 'HTU21D': self.measure_sensor = HTU21DSensor(self.i2c_bus) elif self.device == 'AM2315': self.measure_sensor = AM2315Sensor(self.i2c_bus) elif self.device == 'ATLAS_PT1000': self.measure_sensor = AtlasPT1000Sensor(self.i2c_address, self.i2c_bus) elif self.device == 'K30': self.measure_sensor = K30Sensor() elif self.device == 'BME280': self.measure_sensor = BME280Sensor(self.i2c_address, self.i2c_bus) elif self.device == 'BMP': self.measure_sensor = BMPSensor(self.i2c_bus) elif self.device == 'SHT1x_7x': self.measure_sensor = SHT1x7xSensor(self.location, self.sht_clock_pin, self.sht_voltage) elif self.device == 'SHT2x': self.measure_sensor = SHT2xSensor(self.i2c_address, self.i2c_bus) elif self.device == 'TMP006': self.measure_sensor = TMP006Sensor(self.i2c_address, self.i2c_bus) elif self.device == 'TSL2561': self.measure_sensor = TSL2561Sensor(self.i2c_address, self.i2c_bus) else: self.device_recognized = False self.logger.debug( "Device '{device}' not recognized".format(device=self.device)) raise Exception("{device} is not a valid device type.".format( device=self.device)) self.edge_reset_timer = time.time() self.sensor_timer = time.time() self.running = False self.lastUpdate = None
def __init__(self, ready, sensor_id): threading.Thread.__init__(self) self.logger = logging.getLogger( "mycodo.sensor_{id}".format(id=sensor_id)) self.stop_iteration_counter = 0 self.thread_startup_timer = timeit.default_timer() self.thread_shutdown_timer = 0 self.ready = ready self.lock = {} self.measurement = None self.updateSuccess = False self.sensor_id = sensor_id self.control = DaemonControl() self.pause_loop = False self.verify_pause_loop = True self.cond_id = {} self.cond_action_id = {} self.cond_name = {} self.cond_is_activated = {} self.cond_if_sensor_period = {} self.cond_if_sensor_measurement = {} self.cond_if_sensor_edge_select = {} self.cond_if_sensor_edge_detected = {} self.cond_if_sensor_gpio_state = {} self.cond_if_sensor_direction = {} self.cond_if_sensor_setpoint = {} self.cond_do_relay_id = {} self.cond_do_relay_state = {} self.cond_do_relay_duration = {} self.cond_execute_command = {} self.cond_email_notify = {} self.cond_do_lcd_id = {} self.cond_do_camera_id = {} self.cond_timer = {} self.smtp_wait_timer = {} self.setup_sensor_conditionals() sensor = db_retrieve_table_daemon(Sensor, device_id=self.sensor_id) self.sensor_sel = sensor self.unique_id = sensor.unique_id self.i2c_bus = sensor.i2c_bus self.location = sensor.location self.power_relay_id = sensor.power_relay_id self.measurements = sensor.measurements self.device = sensor.device self.interface = sensor.interface self.device_loc = sensor.device_loc self.baud_rate = sensor.baud_rate self.period = sensor.period self.resolution = sensor.resolution self.sensitivity = sensor.sensitivity self.mux_address_raw = sensor.multiplexer_address self.mux_bus = sensor.multiplexer_bus self.mux_chan = sensor.multiplexer_channel self.adc_chan = sensor.adc_channel self.adc_gain = sensor.adc_gain self.adc_resolution = sensor.adc_resolution self.adc_measure = sensor.adc_measure self.adc_measure_units = sensor.adc_measure_units self.adc_volts_min = sensor.adc_volts_min self.adc_volts_max = sensor.adc_volts_max self.adc_units_min = sensor.adc_units_min self.adc_units_max = sensor.adc_units_max self.sht_clock_pin = sensor.sht_clock_pin self.sht_voltage = sensor.sht_voltage # Edge detection self.switch_edge = sensor.switch_edge self.switch_bouncetime = sensor.switch_bouncetime self.switch_reset_period = sensor.switch_reset_period # Relay that will activate prior to sensor read self.pre_relay_id = sensor.pre_relay_id self.pre_relay_duration = sensor.pre_relay_duration self.pre_relay_setup = False self.next_measurement = time.time() self.get_new_measurement = False self.trigger_cond = False self.measurement_acquired = False self.pre_relay_activated = False self.pre_relay_timer = time.time() relay = db_retrieve_table_daemon(Relay, entry='all') for each_relay in relay: # Check if relay ID actually exists if each_relay.id == self.pre_relay_id and self.pre_relay_duration: self.pre_relay_setup = True smtp = db_retrieve_table_daemon(SMTP, entry='first') self.smtp_max_count = smtp.hourly_max self.email_count = 0 self.allowed_to_send_notice = True # Convert string I2C address to base-16 int if self.device in LIST_DEVICES_I2C: self.i2c_address = int(str(self.location), 16) # Set up multiplexer if enabled if self.device in LIST_DEVICES_I2C and self.mux_address_raw: self.mux_address_string = self.mux_address_raw self.mux_address = int(str(self.mux_address_raw), 16) self.mux_lock = "/var/lock/mycodo_multiplexer_0x{i2c:02X}.pid".format( i2c=self.mux_address) self.multiplexer = TCA9548A(self.mux_bus, self.mux_address) else: self.multiplexer = None if self.device in ['ADS1x15', 'MCP342x'] and self.location: self.adc_lock_file = "/var/lock/mycodo_adc_bus{bus}_0x{i2c:02X}.pid".format( bus=self.i2c_bus, i2c=self.i2c_address) # Set up edge detection of a GPIO pin if self.device == 'EDGE': if self.switch_edge == 'rising': self.switch_edge_gpio = GPIO.RISING elif self.switch_edge == 'falling': self.switch_edge_gpio = GPIO.FALLING else: self.switch_edge_gpio = GPIO.BOTH # Set up analog-to-digital converter elif self.device == 'ADS1x15': self.adc = ADS1x15Read(self.i2c_address, self.i2c_bus, self.adc_chan, self.adc_gain) elif self.device == 'MCP342x': self.adc = MCP342xRead(self.i2c_address, self.i2c_bus, self.adc_chan, self.adc_gain, self.adc_resolution) else: self.adc = None self.device_recognized = True # Set up sensors or devices if self.device in ['EDGE', 'ADS1x15', 'MCP342x']: self.measure_sensor = None elif self.device == 'MYCODO_RAM': self.measure_sensor = MycodoRam() elif self.device == 'RPiCPULoad': self.measure_sensor = RaspberryPiCPULoad() elif self.device == 'RPi': self.measure_sensor = RaspberryPiCPUTemp() elif self.device == 'RPiFreeSpace': self.measure_sensor = RaspberryPiFreeSpace(self.location) elif self.device == 'AM2302': self.measure_sensor = DHT22Sensor(self.sensor_id, int(self.location)) elif self.device == 'AM2315': self.measure_sensor = AM2315Sensor(self.sensor_id, self.i2c_bus, power=self.power_relay_id) elif self.device == 'ATLAS_PH_I2C': self.measure_sensor = AtlaspHSensor(self.interface, i2c_address=self.i2c_address, i2c_bus=self.i2c_bus, sensor_sel=self.sensor_sel) elif self.device == 'ATLAS_PH_UART': self.measure_sensor = AtlaspHSensor(self.interface, device_loc=self.device_loc, baud_rate=self.baud_rate, sensor_sel=self.sensor_sel) elif self.device == 'ATLAS_PT1000_I2C': self.measure_sensor = AtlasPT1000Sensor( self.interface, i2c_address=self.i2c_address, i2c_bus=self.i2c_bus) elif self.device == 'ATLAS_PT1000_UART': self.measure_sensor = AtlasPT1000Sensor(self.interface, device_loc=self.device_loc, baud_rate=self.baud_rate) elif self.device == 'BH1750': self.measure_sensor = BH1750Sensor(self.i2c_address, self.i2c_bus, self.resolution, self.sensitivity) elif self.device == 'BME280': self.measure_sensor = BME280Sensor(self.i2c_address, self.i2c_bus) # TODO: BMP is an old designation and will be removed in the future elif self.device in ['BMP', 'BMP180']: self.measure_sensor = BMP180Sensor(self.i2c_bus) elif self.device == 'BMP280': self.measure_sensor = BMP280Sensor(self.i2c_address, self.i2c_bus) elif self.device == 'CHIRP': self.measure_sensor = ChirpSensor(self.i2c_address, self.i2c_bus) elif self.device == 'DS18B20': self.measure_sensor = DS18B20Sensor(self.location) elif self.device == 'DHT11': self.measure_sensor = DHT11Sensor(self.sensor_id, int(self.location), power=self.power_relay_id) elif self.device == 'DHT22': self.measure_sensor = DHT22Sensor(self.sensor_id, int(self.location), power=self.power_relay_id) elif self.device == 'HTU21D': self.measure_sensor = HTU21DSensor(self.i2c_bus) elif self.device == 'K30_UART': self.measure_sensor = K30Sensor(self.device_loc, baud_rate=self.baud_rate) elif self.device == 'MH_Z16_I2C': self.measure_sensor = MHZ16Sensor(self.interface, i2c_address=self.i2c_address, i2c_bus=self.i2c_bus) elif self.device == 'MH_Z16_UART': self.measure_sensor = MHZ16Sensor(self.interface, device_loc=self.device_loc, baud_rate=self.baud_rate) elif self.device == 'MH_Z19_UART': self.measure_sensor = MHZ19Sensor(self.device_loc, baud_rate=self.baud_rate) elif self.device == 'SHT1x_7x': self.measure_sensor = SHT1x7xSensor(int(self.location), self.sht_clock_pin, self.sht_voltage) elif self.device == 'SHT2x': self.measure_sensor = SHT2xSensor(self.i2c_address, self.i2c_bus) elif self.device == 'TMP006': self.measure_sensor = TMP006Sensor(self.i2c_address, self.i2c_bus) elif self.device == 'TSL2561': self.measure_sensor = TSL2561Sensor(self.i2c_address, self.i2c_bus) elif self.device == 'TSL2591': self.measure_sensor = TSL2591Sensor(self.i2c_address, self.i2c_bus) else: self.device_recognized = False self.logger.debug( "Device '{device}' not recognized".format(device=self.device)) raise Exception("'{device}' is not a valid device type.".format( device=self.device)) self.edge_reset_timer = time.time() self.sensor_timer = time.time() self.running = False self.lastUpdate = None
def __init__(self, ready, lcd_id): threading.Thread.__init__(self) self.logger = logging.getLogger("mycodo.lcd_{id}".format(id=lcd_id)) self.running = False self.thread_startup_timer = timeit.default_timer() self.thread_shutdown_timer = 0 self.ready = ready self.flash_lcd_on = False self.lcd_is_on = False self.lcd_id = lcd_id try: lcd = db_retrieve_table(MYCODO_DB_PATH, LCD, device_id=self.lcd_id) self.lcd_name = lcd.name self.lcd_pin = lcd.pin self.lcd_period = lcd.period self.lcd_x_characters = lcd.x_characters self.lcd_y_lines = lcd.y_lines if lcd.multiplexer_address: self.multiplexer_address_string = lcd.multiplexer_address self.multiplexer_address = int(str(lcd.multiplexer_address), 16) self.multiplexer_channel = lcd.multiplexer_channel self.multiplexer = TCA9548A(self.multiplexer_address) else: self.multiplexer = None self.lcd_line = {} for i in range(1, 5): self.lcd_line[i] = {} list_sensors = [ 'sensor_time', 'temperature', 'humidity', 'co2', 'pressure', 'altitude', 'temperature_die', 'temperature_object', 'lux' ] list_pids = ['setpoint', 'pid_time'] list_relays = ['duration_sec', 'relay_time', 'relay_state'] if self.lcd_y_lines in [2, 4]: self.lcd_line[1]['id'] = lcd.line_1_sensor_id self.lcd_line[1]['measurement'] = lcd.line_1_measurement if lcd.line_1_sensor_id: table = None if lcd.line_1_measurement in list_sensors: table = Sensor elif lcd.line_1_measurement in list_pids: table = PID elif lcd.line_1_measurement in list_relays: table = Relay sensor_line_1 = db_retrieve_table( MYCODO_DB_PATH, table, device_id=lcd.line_1_sensor_id) self.lcd_line[1]['name'] = sensor_line_1.name if 'time' in lcd.line_1_measurement: self.lcd_line[1]['measurement'] = 'time' self.lcd_line[2]['id'] = lcd.line_2_sensor_id self.lcd_line[2]['measurement'] = lcd.line_2_measurement if lcd.line_2_sensor_id: table = None if lcd.line_2_measurement in list_sensors: table = Sensor elif lcd.line_2_measurement in list_pids: table = PID elif lcd.line_2_measurement in list_relays: table = Relay sensor_line_2 = db_retrieve_table( MYCODO_DB_PATH, table, device_id=lcd.line_2_sensor_id) self.lcd_line[2]['name'] = sensor_line_2.name if 'time' in lcd.line_2_measurement: self.lcd_line[2]['measurement'] = 'time' if self.lcd_y_lines == 4: self.lcd_line[3]['id'] = lcd.line_3_sensor_id self.lcd_line[3]['measurement'] = lcd.line_3_measurement if lcd.line_3_sensor_id: table = None if lcd.line_3_measurement in list_sensors: table = Sensor elif lcd.line_3_measurement in list_pids: table = PID elif lcd.line_3_measurement in list_relays: table = Relay sensor_line_3 = db_retrieve_table( MYCODO_DB_PATH, table, device_id=lcd.line_3_sensor_id) self.lcd_line[3]['name'] = sensor_line_3.name if 'time' in lcd.line_3_measurement: self.lcd_line[3]['measurement'] = 'time' self.lcd_line[4]['id'] = lcd.line_4_sensor_id self.lcd_line[4]['measurement'] = lcd.line_4_measurement if lcd.line_4_sensor_id: table = None if lcd.line_4_measurement in list_sensors: table = Sensor elif lcd.line_4_measurement in list_pids: table = PID elif lcd.line_4_measurement in list_relays: table = Relay sensor_line_4 = db_retrieve_table( MYCODO_DB_PATH, table, device_id=lcd.line_4_sensor_id) self.lcd_line[4]['name'] = sensor_line_4.name if 'time' in lcd.line_4_measurement: self.lcd_line[4]['measurement'] = 'time' self.timer = time.time() + self.lcd_period self.backlight_timer = time.time() self.lcd_string_line = {} for i in range(1, self.lcd_y_lines + 1): self.lcd_string_line[i] = '' self.LCD_WIDTH = self.lcd_x_characters # Max characters per line self.LCD_LINE = { 1: 0x80, 2: 0xC0, 3: 0x94, 4: 0xD4 } self.LCD_CHR = 1 # Mode - Sending data self.LCD_CMD = 0 # Mode - SenLCDding command self.LCD_BACKLIGHT = 0x08 # On self.LCD_BACKLIGHT_OFF = 0x00 # Off self.ENABLE = 0b00000100 # Enable bit # Timing constants self.E_PULSE = 0.0005 self.E_DELAY = 0.0005 # Setup I2C bus try: if GPIO.RPI_REVISION == 2 or GPIO.RPI_REVISION == 3: i2c_bus_number = 1 else: i2c_bus_number = 0 self.bus = smbus.SMBus(i2c_bus_number) except Exception as except_msg: self.logger.exception( "Could not initialize I2C bus: {err}".format( err=except_msg)) self.I2C_ADDR = int(self.lcd_pin, 16) self.lcd_init() self.lcd_string_write('Mycodo {}'.format(MYCODO_VERSION), self.LCD_LINE[1]) self.lcd_string_write('Start {}'.format( self.lcd_name), self.LCD_LINE[2]) except Exception as except_msg: self.logger.exception("Error: {err}".format(err=except_msg))
def __init__(self, ready, lcd_id): threading.Thread.__init__(self) self.logger = logging.getLogger("mycodo.lcd_{id}".format(id=lcd_id)) self.running = False self.thread_startup_timer = timeit.default_timer() self.thread_shutdown_timer = 0 self.ready = ready self.flash_lcd_on = False self.lcd_initilized = False self.lcd_is_on = False self.lcd_id = lcd_id self.display_ids = [] self.display_count = 0 try: lcd = db_retrieve_table_daemon(LCD, device_id=self.lcd_id) self.lcd_name = lcd.name self.lcd_location = lcd.location self.lcd_i2c_bus = lcd.i2c_bus self.lcd_period = lcd.period self.lcd_x_characters = lcd.x_characters self.lcd_y_lines = lcd.y_lines self.timer = time.time() + self.lcd_period self.backlight_timer = time.time() if lcd.multiplexer_address: self.multiplexer_address_string = lcd.multiplexer_address self.multiplexer_address = int(str(lcd.multiplexer_address), 16) self.multiplexer_channel = lcd.multiplexer_channel self.multiplexer = TCA9548A(self.multiplexer_address) else: self.multiplexer = None self.list_pids = ['setpoint', 'pid_time'] self.list_outputs = ['duration_sec', 'relay_time', 'relay_state'] self.list_inputs = MEASUREMENT_UNITS self.list_inputs.update( {'sensor_time': { 'unit': None, 'name': 'Time' }}) # Add custom measurement and units to list (From linux command input) input_dev = db_retrieve_table_daemon(Input) self.list_inputs = add_custom_measurements(input_dev, self.list_inputs, MEASUREMENT_UNITS) lcd_data = db_retrieve_table_daemon(LCDData).filter( LCDData.lcd_id == lcd.id).all() self.lcd_string_line = {} self.lcd_line = {} self.lcd_max_age = {} for each_lcd_display in lcd_data: self.display_ids.append(each_lcd_display.id) self.lcd_string_line[each_lcd_display.id] = {} self.lcd_line[each_lcd_display.id] = {} self.lcd_max_age[each_lcd_display.id] = {} for i in range(1, self.lcd_y_lines + 1): self.lcd_string_line[each_lcd_display.id][i] = '' self.lcd_line[each_lcd_display.id][i] = {} self.lcd_max_age[each_lcd_display. id][i] = each_lcd_display.line_1_max_age if self.lcd_y_lines in [2, 4]: self.setup_lcd_line(each_lcd_display.id, 1, each_lcd_display.line_1_id, each_lcd_display.line_1_measurement) self.setup_lcd_line(each_lcd_display.id, 2, each_lcd_display.line_2_id, each_lcd_display.line_2_measurement) if self.lcd_y_lines == 4: self.setup_lcd_line(each_lcd_display.id, 3, each_lcd_display.line_3_id, each_lcd_display.line_3_measurement) self.setup_lcd_line(each_lcd_display.id, 4, each_lcd_display.line_4_id, each_lcd_display.line_4_measurement) self.LCD_WIDTH = self.lcd_x_characters # Max characters per line self.LCD_LINE = {1: 0x80, 2: 0xC0, 3: 0x94, 4: 0xD4} self.LCD_CHR = 1 # Mode - Sending data self.LCD_CMD = 0 # Mode - SenLCDding command self.LCD_BACKLIGHT = 0x08 # On self.LCD_BACKLIGHT_OFF = 0x00 # Off self.ENABLE = 0b00000100 # Enable bit # Timing constants self.E_PULSE = 0.0005 self.E_DELAY = 0.0005 # Setup I2C bus try: self.bus = smbus.SMBus(self.lcd_i2c_bus) except Exception as except_msg: self.logger.exception( "Could not initialize I2C bus: {err}".format( err=except_msg)) self.I2C_ADDR = int(self.lcd_location, 16) self.lcd_init() if self.lcd_initilized: self.lcd_string_write('Mycodo {}'.format(MYCODO_VERSION), self.LCD_LINE[1]) self.lcd_string_write(u'Start {}'.format(self.lcd_name), self.LCD_LINE[2]) except Exception as except_msg: self.logger.exception("Error: {err}".format(err=except_msg))