def initialize_input(self): import adafruit_ads1x15.ads1115 as ADS from adafruit_ads1x15.analog_in import AnalogIn from adafruit_extended_bus import ExtendedI2C self.analog_in = AnalogIn self.ads = ADS if self.input_dev.adc_gain == 0: self.adc_gain = 2 / 3 else: self.adc_gain = self.input_dev.adc_gain self.adc = ADS.ADS1115(ExtendedI2C(self.input_dev.i2c_bus), address=int(str(self.input_dev.i2c_location), 16))
def initialize(self): import adafruit_adxl34x from adafruit_extended_bus import ExtendedI2C self.sensor = adafruit_adxl34x.ADXL345( ExtendedI2C(self.input_dev.i2c_bus), address=int(str(self.input_dev.i2c_location), 16)) if self.range == '2': self.sensor.range = adafruit_adxl34x.Range.RANGE_2_G elif self.range == '4': self.sensor.range = adafruit_adxl34x.Range.RANGE_4_G elif self.range == '8': self.sensor.range = adafruit_adxl34x.Range.RANGE_8_G elif self.range == '16': self.sensor.range = adafruit_adxl34x.Range.RANGE_16_G
def initialize(self): import adafruit_ads1x15.ads1115 as ADS from adafruit_ads1x15.analog_in import AnalogIn from adafruit_extended_bus import ExtendedI2C self.analog_in = AnalogIn self.ads = ADS if self.input_dev.adc_gain == 0: self.adc_gain = 2 / 3 else: self.adc_gain = self.input_dev.adc_gain try: self.adc = ADS.ADS1115(ExtendedI2C(self.input_dev.i2c_bus), address=int( str(self.input_dev.i2c_location), 16)) except Exception as err: self.logger.error("Error while initializing: {}".format(err))
def initialize_input(self): from adafruit_extended_bus import ExtendedI2C from anyleaf import PhSensor, CalPt self.sensor = PhSensor(ExtendedI2C(self.input_dev.i2c_bus), self.input_dev.period, address=int(str(self.input_dev.i2c_location), 16)) # cal pt 3 may be None to indicate 2-pt calibration. if self.cal3_v and self.cal3_ph and self.cal3_t: cal_pt_3 = CalPt(self.cal3_v, self.cal3_ph, self.cal3_t) else: cal_pt_3 = None # Load cal data from the database. self.sensor.calibrate_all( CalPt(self.cal1_v, self.cal1_ph, self.cal1_t), CalPt(self.cal2_v, self.cal2_ph, self.cal2_t), cal_pt_3)
def setup_output(self): from adafruit_mcp230xx.mcp23017 import MCP23017 from adafruit_extended_bus import ExtendedI2C self.setup_output_variables(OUTPUT_INFORMATION) try: self.logger.debug("I2C: Address: {}, Bus: {}".format( self.output.i2c_location, self.output.i2c_bus)) if self.output.i2c_location: self.sensor = MCP23017( ExtendedI2C(self.output.i2c_bus), address=int(str(self.output.i2c_location), 16)) self.output_setup = True except: self.logger.exception("Could not set up output") return for pin in range(0, 16): self.pins.append(self.sensor.get_pin(pin)) for channel in channels_dict: if self.options_channels['state_startup'][channel] == 1: self.pins[channel].switch_to_output(value=bool(self.options_channels['on_state'][channel])) self.output_states[channel] = bool(self.options_channels['on_state'][channel]) elif self.options_channels['state_startup'][channel] == 0: self.pins[channel].switch_to_output(value=bool(not self.options_channels['on_state'][channel])) self.output_states[channel] = bool(not self.options_channels['on_state'][channel]) else: # Default state: Off self.pins[channel].switch_to_output(value=bool(not self.options_channels['on_state'][channel])) self.output_states[channel] = bool(not self.options_channels['on_state'][channel]) if self.options_channels['trigger_functions_startup'][channel]: try: self.check_triggers(self.unique_id, output_channel=channel) except Exception as err: self.logger.error( "Could not check Trigger for channel {} of output {}: {}".format( channel, self.unique_id, err))
def initialize(self): import adafruit_mcp4728 from adafruit_extended_bus import ExtendedI2C self.setup_output_variables(OUTPUT_INFORMATION) try: self.dac = adafruit_mcp4728.MCP4728( ExtendedI2C(self.output.i2c_bus), address=int(str(self.output.i2c_location), 16)) self.channel = { 0: self.dac.channel_a, 1: self.dac.channel_b, 2: self.dac.channel_c, 3: self.dac.channel_d } # Set up Channels for channel in channels_dict: if self.options_channels['vref'][channel] == "internal": self.channel[channel].vref = adafruit_mcp4728.Vref.INTERNAL else: self.channel[channel].vref = adafruit_mcp4728.Vref.VDD self.channel[channel].gain = self.options_channels['gain'][ channel] if (self.options_channels['state_start'][channel] == "value" and self.options_channels['state_start_value'][channel]): self.channel[channel].value = int( 65535 * (self.options_channels['state_start_value'][channel] / self.vref)) self.dac.save_settings() self.output_setup = True except: self.logger.exception("Error setting up Output")
def initialize(self): from adafruit_mcp230xx.mcp23017 import MCP23017 from adafruit_extended_bus import ExtendedI2C self.setup_output_variables(OUTPUT_INFORMATION) try: self.logger.debug( f"I2C: Address: {self.output.i2c_location}, Bus: {self.output.i2c_bus}" ) if self.output.i2c_location: self.sensor = MCP23017(ExtendedI2C(self.output.i2c_bus), address=int( str(self.output.i2c_location), 16)) self.lock_file = f'/var/lock/pcf8574_{self.output.i2c_bus}_{self.output.i2c_location}' self.output_setup = True except: self.logger.exception("Could not set up output") return for pin in range(0, 16): self.pins.append(self.sensor.get_pin(pin)) for channel in channels_dict: if self.options_channels['state_startup'][channel] == 1: self.turn_on_off(channel, "on") elif self.options_channels['state_startup'][channel] == 0: self.turn_on_off(channel, "off") else: # Default state: Off self.turn_on_off(channel, "off") if self.options_channels['trigger_functions_startup'][channel]: try: self.check_triggers(self.unique_id, output_channel=channel) except Exception as err: self.logger.error( f"Could not check Trigger for channel {channel}: {err}" )
def initialize(self): from adafruit_extended_bus import ExtendedI2C import adafruit_scd30 self.sensor = adafruit_scd30.SCD30( ExtendedI2C(self.input_dev.i2c_bus, frequency=self.i2c_frequency), address=int(str(self.input_dev.i2c_location), 16)) if self.sensor.self_calibration_enabled != self.enable_self_calibration: self.sensor.self_calibration_enabled = self.enable_self_calibration self.logger.info( f"{self.sensor.temperature_offset}, {self.temperature_offset}") if self.sensor.temperature_offset != self.temperature_offset: self.sensor.temperature_offset = self.temperature_offset self.logger.info(f"New: {self.sensor.temperature_offset}") if self.sensor.ambient_pressure != self.ambient_pressure: self.sensor.ambient_pressure = self.ambient_pressure if self.sensor.altitude != self.altitude: self.sensor.altitude = self.altitude
def initialize_input(self): from adafruit_extended_bus import ExtendedI2C from anyleaf import OrpSensor, CalPtOrp self.sensor = OrpSensor(ExtendedI2C(self.input_dev.i2c_bus), self.input_dev.period, address=int(str(self.input_dev.i2c_location), 16)) # `default_value` above doesn't set the default in the database: custom options will initialize to None. if self.get_custom_option("cal_v"): cal_v = self.get_custom_option("cal_v") else: cal_v = 0.4 if self.get_custom_option("cal_orp"): cal_orp = self.get_custom_option("cal_orp") else: cal_orp = 400.0 # Load cal data from the database. self.sensor.calibrate_all(CalPtOrp( cal_v, cal_orp, ))
def setup_output(self): import adafruit_mcp4728 from adafruit_extended_bus import ExtendedI2C self.setup_output_variables(OUTPUT_INFORMATION) try: self.dac = adafruit_mcp4728.MCP4728( ExtendedI2C(self.output.i2c_bus), address=int(str(self.output.i2c_location), 16)) self.channel = { 0: self.dac.channel_a, 1: self.dac.channel_b, 2: self.dac.channel_c, 3: self.dac.channel_d } # Channel A if self.options_channels['vref'][0] == "internal": self.channel[0].vref = adafruit_mcp4728.Vref.INTERNAL else: self.channel[0].vref = adafruit_mcp4728.Vref.VDD self.channel[0].gain = self.options_channels['gain'][0] if (self.options_channels['state_start'][0] == "value" and self.options_channels['state_start_value'][0]): self.channel[0].value = int( 65535 * (self.options_channels['state_start_value'][0] / self.vref)) # Channel B if self.options_channels['vref'][1] == "internal": self.channel[1].vref = adafruit_mcp4728.Vref.INTERNAL else: self.channel[1].vref = adafruit_mcp4728.Vref.VDD self.channel[1].gain = self.options_channels['gain'][1] if (self.options_channels['state_start'][1] == "value" and self.options_channels['state_start_value'][1]): self.channel[1].value = int( 65535 * (self.options_channels['state_start_value'][1] / self.vref)) # Channel C if self.options_channels['vref'][2] == "internal": self.channel[2].vref = adafruit_mcp4728.Vref.INTERNAL else: self.channel[2].vref = adafruit_mcp4728.Vref.VDD self.channel[2].gain = self.options_channels['gain'][2] if (self.options_channels['state_start'][2] == "value" and self.options_channels['state_start_value'][2]): self.channel[2].value = int( 65535 * (self.options_channels['state_start_value'][2] / self.vref)) # Channel D if self.options_channels['vref'][3] == "internal": self.channel[3].vref = adafruit_mcp4728.Vref.INTERNAL else: self.channel[3].vref = adafruit_mcp4728.Vref.VDD self.channel[3].gain = self.options_channels['gain'][3] if (self.options_channels['state_start'][3] == "value" and self.options_channels['state_start_value'][3]): self.channel[3].value = int( 65535 * (self.options_channels['state_start_value'][3] / self.vref)) self.dac.save_settings() self.output_setup = True except: self.output_setup = False
def initialize(self): """ Initialize INA219x sensor """ from adafruit_ina219 import INA219, ADCResolution, BusVoltageRange from adafruit_extended_bus import ExtendedI2C try: self.sensor = INA219(ExtendedI2C(self.input_dev.i2c_bus), addr=int(str(self.input_dev.i2c_location), 16)) except (ValueError, OSError) as msg: self.logger.exception("INA219x Exception: %s", msg) return None if not self.sensor: self.logger.error("INA219x sensor unable to initialize.") return None self.measurements_for_average = self.measurements_for_average # calibrate voltage and current detection range if self.calibration == '1': self.sensor.set_calibration_32V_1A() self.logger.debug("INA219x: set_calibration_32V_1A()") elif self.calibration == '2': self.sensor.set_calibration_16V_400mA() self.logger.debug("INA219x: set_calibration_16V_400mA()") elif self.calibration == '3': self.sensor.set_calibration_16V_5A() self.logger.debug("INA219x: set_calibration_16V_5A()") else: # use default sensor calibration of 32V / 2A self.sensor.set_calibration_32V_2A() self.logger.debug("INA219x: set_calibration_32V_2A()") BUS_VOLTAGE_RANGE = { '0': BusVoltageRange.RANGE_16V, '1': BusVoltageRange.RANGE_32V } # calibrate sensor bus voltage range self.sensor.bus_voltage_range = BUS_VOLTAGE_RANGE.get( self.bus_voltage_range, BUS_VOLTAGE_RANGE['1']) ADC_RESOLUTION = { '00': ADCResolution.ADCRES_9BIT_1S, '01': ADCResolution.ADCRES_10BIT_1S, '02': ADCResolution.ADCRES_11BIT_1S, '03': ADCResolution.ADCRES_12BIT_1S, '09': ADCResolution.ADCRES_12BIT_2S, '0A': ADCResolution.ADCRES_12BIT_4S, '0B': ADCResolution.ADCRES_12BIT_8S, '0C': ADCResolution.ADCRES_12BIT_16S, '0D': ADCResolution.ADCRES_12BIT_32S, '0E': ADCResolution.ADCRES_12BIT_64S, '0F': ADCResolution.ADCRES_12BIT_128S } # calibrate sensor ADC resolutions self.sensor.bus_adc_resolution = ADC_RESOLUTION.get( self.bus_adc_resolution, ADC_RESOLUTION['03']) self.sensor.shunt_adc_resolution = ADC_RESOLUTION.get( self.shunt_adc_resolution, ADC_RESOLUTION['03'])
def __init__(self, lcd_dev=None, lcd_settings_dict=None): if lcd_dev: self.logger = logging.getLogger("{}_{}".format( __name__, lcd_dev.unique_id.split('-')[0])) self.interface = lcd_dev.interface self.lcd_x_characters = lcd_dev.x_characters self.lcd_type = lcd_dev.lcd_type # I2C self.i2c_address = int(lcd_dev.location, 16) self.i2c_bus = lcd_dev.i2c_bus # SPI self.spi_bus = lcd_dev.spi_bus self.spi_device = lcd_dev.spi_device self.pin_dc = lcd_dev.pin_dc self.pin_reset = lcd_dev.pin_reset self.pin_cs = lcd_dev.pin_cs elif lcd_settings_dict: self.logger = logging.getLogger("{}_{}".format( __name__, lcd_settings_dict["unique_id"].split('-')[0])) self.interface = lcd_settings_dict["interface"] self.lcd_x_characters = lcd_settings_dict["x_characters"] self.lcd_type = lcd_settings_dict["lcd_type"] # I2C self.i2c_address = int(lcd_settings_dict["i2c_address"], 16) self.i2c_bus = lcd_settings_dict["i2c_bus"] # SPI self.spi_bus = lcd_settings_dict["spi_bus"] self.spi_device = lcd_settings_dict["spi_device"] self.pin_dc = lcd_settings_dict["pin_dc"] self.pin_reset = lcd_settings_dict["pin_reset"] self.pin_cs = lcd_settings_dict["pin_cs"] self.disp = None if self.interface == 'I2C': if self.lcd_type == '128x32_pioled_circuit_python': self.disp = adafruit_ssd1306.SSD1306_I2C( 128, 32, ExtendedI2C(self.i2c_bus), addr=int(str(self.i2c_address), 16)) elif self.lcd_type == '128x64_pioled_circuit_python': self.disp = adafruit_ssd1306.SSD1306_I2C( 128, 64, ExtendedI2C(self.i2c_bus), addr=int(str(self.i2c_address), 16)) elif self.interface == 'SPI': if self.lcd_type == '128x32_pioled_circuit_python': import Adafruit_GPIO.SPI as SPI self.disp = adafruit_ssd1306.SSD1306_SPI(128, 32, spi=SPI.SpiDev( self.spi_bus, self.spi_device), dc=self.pin_dc, reset=self.pin_reset, cs=self.pin_cs) elif self.lcd_type == '128x64_pioled_circuit_python': import Adafruit_GPIO.SPI as SPI self.disp = adafruit_ssd1306.SSD1306_SPI(128, 64, spi=SPI.SpiDev( self.spi_bus, self.spi_device), dc=self.pin_dc, reset=self.pin_reset, cs=self.pin_cs) if not self.disp: self.logger.error( "Unable to set up display. Check the LCD settings.")
def initialize_input(self): from adafruit_extended_bus import ExtendedI2C from anyleaf import PhSensor, CalPt self.sensor = PhSensor(ExtendedI2C(self.input_dev.i2c_bus), self.input_dev.period, address=int(str(self.input_dev.i2c_location), 16)) # `default_value` above doesn't set the default in the database: custom options will initialize to None. if self.get_custom_option("cal1_v"): cal1_v = self.get_custom_option("cal1_v") else: cal1_v = 0. if self.get_custom_option("cal1_ph"): cal1_ph = self.get_custom_option("cal1_ph") else: cal1_ph = 7. if self.get_custom_option("cal1_t"): cal1_t = self.get_custom_option("cal1_t") else: cal1_t = 23. if self.get_custom_option("cal2_v"): cal2_v = self.get_custom_option("cal2_v") else: cal2_v = 0.17 if self.get_custom_option("cal2_ph"): cal2_ph = self.get_custom_option("cal2_ph") else: cal2_ph = 4. if self.get_custom_option("cal2_t"): cal2_t = self.get_custom_option("cal2_t") else: cal2_t = 23. if self.get_custom_option("cal3_v"): cal3_v = self.get_custom_option("cal3_v") else: cal3_v = None if self.get_custom_option("cal3_ph"): cal3_ph = self.get_custom_option("cal3_ph") else: cal3_ph = None if self.get_custom_option("cal3_t"): cal3_t = self.get_custom_option("cal3_t") else: cal3_t = None # cal pt 3 may be None to indicate 2-pt calibration. if cal3_v and cal3_ph and cal3_t: cal_pt_3 = CalPt( cal3_v, cal3_ph, cal3_t, ) else: cal_pt_3 = None cal_pt_3 = None # Load cal data from the database. self.sensor.calibrate_all(CalPt( cal1_v, cal1_ph, cal1_t, ), CalPt( cal2_v, cal2_ph, cal2_t, ), cal_pt_3)