Esempio n. 1
0
    def __init__(self, input_dev):
        self.cmd_send = None
        self.ph_sensor_uart = None
        self.ph_sensor_i2c = None
        self.interface = input_dev.interface

        if self.interface == 'FTDI':
            from mycodo.devices.atlas_scientific_ftdi import AtlasScientificFTDI
            self.ph_sensor_ftdi = AtlasScientificFTDI(
                input_dev.ftdi_location)
        elif self.interface == 'UART':
            from mycodo.devices.atlas_scientific_uart import AtlasScientificUART
            self.ph_sensor_uart = AtlasScientificUART(
                input_dev.uart_location,
                baudrate=input_dev.baud_rate)
        elif self.interface == 'I2C':
            from mycodo.devices.atlas_scientific_i2c import AtlasScientificI2C
            self.ph_sensor_i2c = AtlasScientificI2C(
                i2c_address=int(str(input_dev.i2c_location), 16),
                i2c_bus=input_dev.i2c_bus)

        self.board_version, self.board_info = self.get_board_version()

        if self.board_version == 0:
            logger.error("Unable to retrieve device info (this indicates the "
                         "device was not properly initialized or connected)")
        else:
            # TODO: Change back to debug
            logger.error("Device Info: {info}".format(info=self.board_info))
            logger.error("Detected Version: {ver}".format(ver=self.board_version))
Esempio n. 2
0
    def __init__(self, input_dev):
        self.cmd_send = None
        self.ph_sensor_uart = None
        self.ph_sensor_i2c = None
        self.interface = input_dev.interface

        if self.interface == 'FTDI':
            from mycodo.devices.atlas_scientific_ftdi import AtlasScientificFTDI
            self.ph_sensor_ftdi = AtlasScientificFTDI(input_dev.ftdi_location)
        elif self.interface == 'UART':
            from mycodo.devices.atlas_scientific_uart import AtlasScientificUART
            self.ph_sensor_uart = AtlasScientificUART(
                input_dev.uart_location, baudrate=input_dev.baud_rate)
        elif self.interface == 'I2C':
            from mycodo.devices.atlas_scientific_i2c import AtlasScientificI2C
            self.ph_sensor_i2c = AtlasScientificI2C(i2c_address=int(
                str(input_dev.i2c_location), 16),
                                                    i2c_bus=input_dev.i2c_bus)

        self.measurement, self.board_version, self.firmware_version = self.get_board_version(
        )

        if self.board_version == 0:
            logger.error("Atlas Scientific board initialization unsuccessful. "
                         "Unable to retrieve device info (this indicates the "
                         "device was not properly initialized or connected)")
        else:
            # TODO: Change back to debug
            logger.info(
                "Atlas Scientific board initialization success. "
                "Measurement: {meas}, Board: {brd}, Firmware: {fw}".format(
                    meas=self.measurement,
                    brd=self.board_version,
                    fw=self.firmware_version))
Esempio n. 3
0
 def initialize_sensor(self):
     from mycodo.devices.atlas_scientific_ftdi import AtlasScientificFTDI
     from mycodo.devices.atlas_scientific_i2c import AtlasScientificI2C
     from mycodo.devices.atlas_scientific_uart import AtlasScientificUART
     if self.interface == 'FTDI':
         self.atlas_sensor_ftdi = AtlasScientificFTDI(self.ftdi_location)
     elif self.interface == 'UART':
         self.atlas_sensor_uart = AtlasScientificUART(self.uart_location)
     elif self.interface == 'I2C':
         self.atlas_sensor_i2c = AtlasScientificI2C(
             i2c_address=self.i2c_address, i2c_bus=self.i2c_bus)
Esempio n. 4
0
 def initialize_sensor(self):
     from mycodo.devices.atlas_scientific_ftdi import AtlasScientificFTDI
     from mycodo.devices.atlas_scientific_i2c import AtlasScientificI2C
     from mycodo.devices.atlas_scientific_uart import AtlasScientificUART
     if self.interface == 'FTDI':
         self.logger = logging.getLogger(
             "mycodo.inputs.atlas_electrical_conductivity_ftdi_{ftdi}".format(
                 ftdi=self.ftdi_location))
         self.atlas_sensor_ftdi = AtlasScientificFTDI(self.ftdi_location)
     elif self.interface == 'UART':
         self.logger = logging.getLogger(
             "mycodo.inputs.atlas_electrical_conductivity_uart_{uart}".format(
                 uart=self.uart_location))
         self.atlas_sensor_uart = AtlasScientificUART(self.uart_location)
     elif self.interface == 'I2C':
         self.logger = logging.getLogger(
             "mycodo.inputs.atlas_electrical_conductivity_i2c_{bus}_{add}".format(
                 bus=self.i2c_bus, add=self.i2c_address))
         self.atlas_sensor_i2c = AtlasScientificI2C(
             i2c_address=self.i2c_address, i2c_bus=self.i2c_bus)
Esempio n. 5
0
 def connect_atlas_ezo_pump(selected_output):
     atlas_command = None
     if selected_output.interface == 'FTDI':
         atlas_command = AtlasScientificFTDI(selected_output.ftdi_location)
     elif selected_output.interface == 'I2C':
         atlas_command = AtlasScientificI2C(i2c_address=int(
             str(selected_output.location), 16),
                                            i2c_bus=selected_output.i2c_bus)
     elif selected_output.interface == 'UART':
         atlas_command = AtlasScientificUART(
             selected_output.location, baudrate=selected_output.baud_rate)
     return atlas_command
Esempio n. 6
0
 def initialize_sensor(self):
     if self.interface == 'FTDI':
         from mycodo.devices.atlas_scientific_ftdi import AtlasScientificFTDI
         self.ftdi_location = self.input_dev.ftdi_location
         self.atlas_sensor = AtlasScientificFTDI(self.ftdi_location)
     elif self.interface == 'UART':
         from mycodo.devices.atlas_scientific_uart import AtlasScientificUART
         self.uart_location = self.input_dev.uart_location
         self.atlas_sensor = AtlasScientificUART(self.uart_location)
     elif self.interface == 'I2C':
         from mycodo.devices.atlas_scientific_i2c import AtlasScientificI2C
         self.i2c_address = int(str(self.input_dev.i2c_location), 16)
         self.i2c_bus = self.input_dev.i2c_bus
         self.atlas_sensor = AtlasScientificI2C(
             i2c_address=self.i2c_address, i2c_bus=self.i2c_bus)
Esempio n. 7
0
 def setup_output(self):
     if self.interface == 'FTDI':
         from mycodo.devices.atlas_scientific_ftdi import AtlasScientificFTDI
         self.atlas_command = AtlasScientificFTDI(self.output.ftdi_location)
     elif self.interface == 'I2C':
         from mycodo.devices.atlas_scientific_i2c import AtlasScientificI2C
         self.atlas_command = AtlasScientificI2C(
             i2c_address=int(str(self.output.i2c_location), 16),
             i2c_bus=self.output.i2c_bus)
     elif self.interface == 'UART':
         from mycodo.devices.atlas_scientific_uart import AtlasScientificUART
         self.atlas_command = AtlasScientificUART(
             self.output.uart_location, baudrate=self.output.baud_rate)
     else:
         self.logger.error("Unknown interface: {}".format(self.interface))
Esempio n. 8
0
 def setup_output(self):
     if self.output_interface == 'FTDI':
         from mycodo.devices.atlas_scientific_ftdi import AtlasScientificFTDI
         self.ftdi_location = self.output.ftdi_location
         self.atlas_command = AtlasScientificFTDI(self.ftdi_location)
     elif self.output_interface == 'I2C':
         from mycodo.devices.atlas_scientific_i2c import AtlasScientificI2C
         self.i2c_address = int(str(self.output.i2c_location), 16)
         self.i2c_bus = self.output.i2c_bus
         self.atlas_command = AtlasScientificI2C(
             i2c_address=self.i2c_address, i2c_bus=self.i2c_bus)
     elif self.output_interface == 'UART':
         from mycodo.devices.atlas_scientific_uart import AtlasScientificUART
         self.uart_location = self.output.uart_location
         self.output_baud_rate = self.output.baud_rate
         self.atlas_command = AtlasScientificUART(
             self.uart_location, baudrate=self.output_baud_rate)
Esempio n. 9
0
def setup_atlas_device(atlas_device):
    if atlas_device.interface == 'FTDI':
        from mycodo.devices.atlas_scientific_ftdi import AtlasScientificFTDI
        atlas_device = AtlasScientificFTDI(atlas_device.ftdi_location)
    elif atlas_device.interface == 'UART':
        from mycodo.devices.atlas_scientific_uart import AtlasScientificUART
        atlas_device = AtlasScientificUART(atlas_device.uart_location,
                                           baudrate=atlas_device.baud_rate)
    elif atlas_device.interface == 'I2C':
        from mycodo.devices.atlas_scientific_i2c import AtlasScientificI2C
        atlas_device = AtlasScientificI2C(i2c_address=int(
            str(atlas_device.i2c_location), 16),
                                          i2c_bus=atlas_device.i2c_bus)
    else:
        logger.error("Unrecognized interface: {}".format(
            atlas_device.interface))
        return
    return atlas_device
Esempio n. 10
0
 def initialize_sensor(self):
     from mycodo.devices.atlas_scientific_ftdi import AtlasScientificFTDI
     from mycodo.devices.atlas_scientific_i2c import AtlasScientificI2C
     from mycodo.devices.atlas_scientific_uart import AtlasScientificUART
     if self.interface == 'FTDI':
         self.logger = logging.getLogger(
             "mycodo.inputs.atlas_pt1000_ftdi_{ftdi}".format(
                 ftdi=self.ftdi_location))
         self.atlas_sensor_ftdi = AtlasScientificFTDI(self.ftdi_location)
     elif self.interface == 'UART':
         self.logger = logging.getLogger(
             "mycodo.inputs.atlas_pt1000_uart_{uart}".format(
                 uart=self.uart_location))
         self.atlas_sensor_uart = AtlasScientificUART(self.uart_location)
     elif self.interface == 'I2C':
         self.logger = logging.getLogger(
             "mycodo.inputs.atlas_pt1000_i2c_{bus}_{add}".format(
                 bus=self.i2c_bus, add=self.i2c_address))
         self.atlas_sensor_i2c = AtlasScientificI2C(
             i2c_address=self.i2c_address, i2c_bus=self.i2c_bus)
     else:
         self.logger = logging.getLogger("mycodo.inputs.atlas_pt1000")
Esempio n. 11
0
class InputModule(AbstractInput):
    """A sensor support class that monitors the Atlas Scientific sensor ORP"""
    def __init__(self, input_dev, testing=False):
        super(InputModule, self).__init__(input_dev,
                                          testing=testing,
                                          name=__name__)
        self.atlas_sensor_ftdi = None
        self.atlas_sensor_uart = None
        self.atlas_sensor_i2c = None
        self.ftdi_location = None
        self.uart_location = None
        self.i2c_address = None
        self.i2c_bus = None

        # Initialize custom options
        self.temperature_comp_meas_device_id = None
        self.temperature_comp_meas_measurement_id = None
        self.max_age = None
        # Set custom options
        self.setup_custom_options(INPUT_INFORMATION['custom_options'],
                                  input_dev)

        if not testing:
            self.input_dev = input_dev
            self.interface = input_dev.interface

            if self.temperature_comp_meas_measurement_id:
                self.atlas_command = AtlasScientificCommand(self.input_dev)

            try:
                self.initialize_sensor()
            except Exception:
                self.logger.exception("Exception while initializing sensor")

            # Throw out first measurement of Atlas Scientific sensor, as it may be prone to error
            self.get_measurement()

    def initialize_sensor(self):
        from mycodo.devices.atlas_scientific_ftdi import AtlasScientificFTDI
        from mycodo.devices.atlas_scientific_i2c import AtlasScientificI2C
        from mycodo.devices.atlas_scientific_uart import AtlasScientificUART
        if self.interface == 'FTDI':
            self.ftdi_location = self.input_dev.ftdi_location
            self.atlas_sensor_ftdi = AtlasScientificFTDI(self.ftdi_location)
        elif self.interface == 'UART':
            self.uart_location = self.input_dev.uart_location
            self.atlas_sensor_uart = AtlasScientificUART(self.uart_location)
        elif self.interface == 'I2C':
            self.i2c_address = int(str(self.input_dev.i2c_location), 16)
            self.i2c_bus = self.input_dev.i2c_bus
            self.atlas_sensor_i2c = AtlasScientificI2C(
                i2c_address=self.i2c_address, i2c_bus=self.i2c_bus)

    def get_measurement(self):
        """ Gets the sensor's ORP measurement via UART/I2C """
        orp = None
        self.return_dict = measurements_dict.copy()

        # Compensate measurement based on a temperature measurement
        if self.temperature_comp_meas_measurement_id and self.atlas_command:
            self.logger.debug("pH sensor set to calibrate temperature")

            last_measurement = self.get_last_measurement(
                self.temperature_comp_meas_device_id,
                self.temperature_comp_meas_measurement_id,
                max_age=self.max_age)

            if last_measurement:
                self.logger.debug(
                    "Latest temperature used to calibrate: {temp}".format(
                        temp=last_measurement[1]))

                ret_value, ret_msg = self.atlas_command.calibrate(
                    'temperature', set_amount=last_measurement[1])
                time.sleep(0.5)

                self.logger.debug("Calibration returned: {val}, {msg}".format(
                    val=ret_value, msg=ret_msg))
            else:
                self.logger.error(
                    "Calibration measurement not found within the past "
                    "{} seconds".format(self.max_age))

        # Read sensor via UART
        if self.interface == 'FTDI':
            if self.atlas_sensor_ftdi.setup:
                lines = self.atlas_sensor_ftdi.query('R')
                if lines:
                    self.logger.debug("All Lines: {lines}".format(lines=lines))

                    # 'check probe' indicates an error reading the sensor
                    if 'check probe' in lines:
                        self.logger.error('"check probe" returned from sensor')
                    # if a string resembling a float value is returned, this
                    # is out measurement value
                    elif str_is_float(lines[0]):
                        orp = float(lines[0])
                        self.logger.debug(
                            'Value[0] is float: {val}'.format(val=orp))
                    else:
                        # During calibration, the sensor is put into
                        # continuous mode, which causes a return of several
                        # values in one string. If the return value does
                        # not represent a float value, it is likely to be a
                        # string of several values. This parses and returns
                        # the first value.
                        if str_is_float(lines[0].split(b'\r')[0]):
                            orp = lines[0].split(b'\r')[0]
                        # Lastly, this is called if the return value cannot
                        # be determined. Watchthe output in the GUI to see
                        # what it is.
                        else:
                            orp = lines[0]
                            self.logger.error(
                                'Value[0] is not float or "check probe": '
                                '{val}'.format(val=orp))
            else:
                self.logger.error('FTDI device is not set up.'
                                  'Check the log for errors.')

        # Read sensor via UART
        elif self.interface == 'UART':
            if self.atlas_sensor_uart.setup:
                lines = self.atlas_sensor_uart.query('R')
                if lines:
                    self.logger.debug("All Lines: {lines}".format(lines=lines))

                    # 'check probe' indicates an error reading the sensor
                    if 'check probe' in lines:
                        self.logger.error('"check probe" returned from sensor')
                    # if a string resembling a float value is returned, this
                    # is out measurement value
                    elif str_is_float(lines[0]):
                        orp = float(lines[0])
                        self.logger.debug(
                            'Value[0] is float: {val}'.format(val=orp))
                    else:
                        # During calibration, the sensor is put into
                        # continuous mode, which causes a return of several
                        # values in one string. If the return value does
                        # not represent a float value, it is likely to be a
                        # string of several values. This parses and returns
                        # the first value.
                        if str_is_float(lines[0].split(b'\r')[0]):
                            orp = lines[0].split(b'\r')[0]
                        # Lastly, this is called if the return value cannot
                        # be determined. Watchthe output in the GUI to see
                        # what it is.
                        else:
                            orp = lines[0]
                            self.logger.error(
                                'Value[0] is not float or "check probe": '
                                '{val}'.format(val=orp))
            else:
                self.logger.error('UART device is not set up.'
                                  'Check the log for errors.')

        # Read sensor via I2C
        elif self.interface == 'I2C':
            if self.atlas_sensor_i2c.setup:
                ec_status, ec_str = self.atlas_sensor_i2c.query('R')
                if ec_status == 'error':
                    self.logger.error(
                        "Sensor read unsuccessful: {err}".format(err=ec_str))
                elif ec_status == 'success':
                    orp = float(ec_str)
            else:
                self.logger.error(
                    'I2C device is not set up. Check the log for errors.')

        self.value_set(0, orp)

        return self.return_dict
Esempio n. 12
0
class InputModule(AbstractInput):
    """A sensor support class that monitors the Atlas Scientific sensor DO"""
    def __init__(self, input_dev, testing=False):
        super(InputModule, self).__init__(input_dev,
                                          testing=testing,
                                          name=__name__)
        self.atlas_sensor_ftdi = None
        self.atlas_sensor_uart = None
        self.atlas_sensor_i2c = None
        self.ftdi_location = None
        self.uart_location = None
        self.i2c_address = None
        self.i2c_bus = None

        if not testing:
            self.interface = input_dev.interface
            self.calibrate_sensor_measure = input_dev.calibrate_sensor_measure
            self.max_age = None

            if input_dev.custom_options:
                for each_option in input_dev.custom_options.split(';'):
                    option = each_option.split(',')[0]
                    value = each_option.split(',')[1]
                    if option == 'max_age':
                        self.max_age = int(value)

            try:
                self.initialize_sensor()
            except Exception:
                self.logger.exception("Exception while initializing sensor")

    def initialize_sensor(self):
        from mycodo.devices.atlas_scientific_ftdi import AtlasScientificFTDI
        from mycodo.devices.atlas_scientific_i2c import AtlasScientificI2C
        from mycodo.devices.atlas_scientific_uart import AtlasScientificUART
        if self.interface == 'FTDI':
            self.ftdi_location = self.input_dev.ftdi_location
            self.atlas_sensor_ftdi = AtlasScientificFTDI(self.ftdi_location)
        elif self.interface == 'UART':
            self.uart_location = self.input_dev.uart_location
            self.atlas_sensor_uart = AtlasScientificUART(self.uart_location)
        elif self.interface == 'I2C':
            self.i2c_address = int(str(self.input_dev.i2c_location), 16)
            self.i2c_bus = self.input_dev.i2c_bus
            self.atlas_sensor_i2c = AtlasScientificI2C(
                i2c_address=self.i2c_address, i2c_bus=self.i2c_bus)

    def get_measurement(self):
        """ Gets the sensor's DO measurement via UART/I2C """
        do = None
        self.return_dict = measurements_dict.copy()

        # Read sensor via UART
        if self.interface == 'FTDI':
            if self.atlas_sensor_ftdi.setup:
                lines = self.atlas_sensor_ftdi.query('R')
                if lines:
                    self.logger.debug("All Lines: {lines}".format(lines=lines))

                    # 'check probe' indicates an error reading the sensor
                    if 'check probe' in lines:
                        self.logger.error('"check probe" returned from sensor')
                    # if a string resembling a float value is returned, this
                    # is out measurement value
                    elif str_is_float(lines[0]):
                        do = float(lines[0])
                        self.logger.debug(
                            'Value[0] is float: {val}'.format(val=do))
                    else:
                        # During calibration, the sensor is put into
                        # continuous mode, which causes a return of several
                        # values in one string. If the return value does
                        # not represent a float value, it is likely to be a
                        # string of several values. This parses and returns
                        # the first value.
                        if str_is_float(lines[0].split(b'\r')[0]):
                            do = lines[0].split(b'\r')[0]
                        # Lastly, this is called if the return value cannot
                        # be determined. Watchthe output in the GUI to see
                        # what it is.
                        else:
                            do = lines[0]
                            self.logger.error(
                                'Value[0] is not float or "check probe": '
                                '{val}'.format(val=do))
            else:
                self.logger.error('FTDI device is not set up.'
                                  'Check the log for errors.')

        # Read sensor via UART
        elif self.interface == 'UART':
            if self.atlas_sensor_uart.setup:
                lines = self.atlas_sensor_uart.query('R')
                if lines:
                    self.logger.debug("All Lines: {lines}".format(lines=lines))

                    # 'check probe' indicates an error reading the sensor
                    if 'check probe' in lines:
                        self.logger.error('"check probe" returned from sensor')
                    # if a string resembling a float value is returned, this
                    # is out measurement value
                    elif str_is_float(lines[0]):
                        do = float(lines[0])
                        self.logger.debug(
                            'Value[0] is float: {val}'.format(val=do))
                    else:
                        # During calibration, the sensor is put into
                        # continuous mode, which causes a return of several
                        # values in one string. If the return value does
                        # not represent a float value, it is likely to be a
                        # string of several values. This parses and returns
                        # the first value.
                        if str_is_float(lines[0].split(b'\r')[0]):
                            do = lines[0].split(b'\r')[0]
                        # Lastly, this is called if the return value cannot
                        # be determined. Watchthe output in the GUI to see
                        # what it is.
                        else:
                            do = lines[0]
                            self.logger.error(
                                'Value[0] is not float or "check probe": '
                                '{val}'.format(val=do))
            else:
                self.logger.error('UART device is not set up.'
                                  'Check the log for errors.')

        # Read sensor via I2C
        elif self.interface == 'I2C':
            if self.atlas_sensor_i2c.setup:
                ec_status, ec_str = self.atlas_sensor_i2c.query('R')
                if ec_status == 'error':
                    self.logger.error(
                        "Sensor read unsuccessful: {err}".format(err=ec_str))
                elif ec_status == 'success':
                    do = float(ec_str)
            else:
                self.logger.error(
                    'I2C device is not set up. Check the log for errors.')

        self.value_set(0, do)

        return self.return_dict
Esempio n. 13
0
class AtlasScientificCommand:
    """
    Class to handle issuing commands to the Atlas Scientific sensor boards
    """

    def __init__(self, input_dev):
        self.cmd_send = None
        self.ph_sensor_uart = None
        self.ph_sensor_i2c = None
        self.interface = input_dev.interface

        if self.interface == 'FTDI':
            from mycodo.devices.atlas_scientific_ftdi import AtlasScientificFTDI
            self.ph_sensor_ftdi = AtlasScientificFTDI(
                input_dev.ftdi_location)
        elif self.interface == 'UART':
            from mycodo.devices.atlas_scientific_uart import AtlasScientificUART
            self.ph_sensor_uart = AtlasScientificUART(
                input_dev.uart_location,
                baudrate=input_dev.baud_rate)
        elif self.interface == 'I2C':
            from mycodo.devices.atlas_scientific_i2c import AtlasScientificI2C
            self.ph_sensor_i2c = AtlasScientificI2C(
                i2c_address=int(str(input_dev.i2c_location), 16),
                i2c_bus=input_dev.i2c_bus)

        self.board_version, self.board_info = self.get_board_version()

        if self.board_version == 0:
            logger.error("Unable to retrieve device info (this indicates the "
                         "device was not properly initialized or connected)")
        else:
            # TODO: Change back to debug
            logger.error("Device Info: {info}".format(info=self.board_info))
            logger.error("Detected Version: {ver}".format(ver=self.board_version))

    def get_board_version(self):
        """Return the board version of the Atlas Scientific pH sensor"""
        info = None

        try:
            if self.interface == 'FTDI':
                info = self.ph_sensor_ftdi.query('i')
            elif self.interface == 'UART':
                info = self.ph_sensor_uart.query('i')
            elif self.interface == 'I2C':
                info = self.ph_sensor_i2c.query('i')
        except TypeError:
            logger.exception("Unable to determine board version of Atlas sensor")
            return 0, None

        # Check first letter of info response
        # "P" indicates a legacy board version
        if info is None:
            return 0, None
        elif info is list:
            for each_line in info:
                if each_line == 'P':
                    return 1, each_line  # Older board version
                elif ',' in each_line and len(each_line.split(',')) == 3:
                    return 2, each_line  # Newer board version
        return 0, None

    def calibrate(self, command, temperature=None, custom_cmd=None):
        """
        Determine and send the correct command to an Atlas Scientific sensor,
        based on the board version
        """
        # Formulate command based on calibration step and board version.
        # Legacy boards requires a different command than recent boards.
        # Some commands are not necessary for recent boards and will not
        # generate a response.
        err = 1
        msg = "Default message"
        if command == 'temperature' and temperature is not None:
            if self.board_version == 1:
                err, msg = self.send_command(temperature)
            elif self.board_version == 2:
                err, msg = self.send_command('T,{temp}'.format(temp=temperature))
        elif command == 'clear_calibration':
            if self.board_version == 1:
                err, msg = self.send_command('X')
                self.send_command('L0')
            elif self.board_version == 2:
                err, msg = self.send_command('Cal,clear')
        elif command == 'continuous':
            if self.board_version == 1:
                err, msg = self.send_command('C')
            elif self.board_version == 2:
                err, msg = self.send_command('C,1')
        elif command == 'low':
            if self.board_version == 1:
                err, msg = self.send_command('F')
            elif self.board_version == 2:
                err, msg = self.send_command('Cal,low,4.00')
        elif command == 'mid':
            if self.board_version == 1:
                err, msg = self.send_command('S')
            elif self.board_version == 2:
                err, msg = self.send_command('Cal,mid,7.00')
        elif command == 'high':
            if self.board_version == 1:
                err, msg = self.send_command('T')
            elif self.board_version == 2:
                err, msg = self.send_command('Cal,high,10.00')
        elif command == 'end':
            if self.board_version == 1:
                err, msg = self.send_command('E')
            elif self.board_version == 2:
                err, msg = self.send_command('C,0')
        elif custom_cmd:
            err, msg = self.send_command(custom_cmd)
        return err, msg

    def send_command(self, cmd_send):
        """ Send the command (if not None) and return the response """
        try:
            return_value = "No message"
            if cmd_send is not None:
                if self.interface == 'FTDI':
                    return_value = self.ph_sensor_ftdi.query(cmd_send)
                elif self.interface == 'UART':
                    return_value = self.ph_sensor_uart.query(cmd_send)
                elif self.interface == 'I2C':
                    return_value = self.ph_sensor_i2c.query(cmd_send)
                time.sleep(0.1)
                return 0, return_value
            else:
                return 1, "No command given"
        except Exception as err:
            logger.error("{cls} raised an exception while communicating with "
                         "the board: {err}".format(cls=type(self).__name__,
                                                   err=err))
            return 1, err
Esempio n. 14
0
def setup_atlas_ph_measure(input_id):
    """
    Acquire a measurement from the Atlas Scientific pH input and return it
    Used during calibration to display the current pH to the user
    """
    if not utils_general.user_has_permission('edit_controllers'):
        return redirect(url_for('routes_page.page_atlas_ph_calibrate'))

    selected_input = Input.query.filter_by(unique_id=input_id).first()

    ph = None
    error = None

    if selected_input.interface == 'FTDI':
        ph_input_ftdi = AtlasScientificFTDI(selected_input.ftdi_location)
        lines = ph_input_ftdi.query('R')
        logger.debug("All Lines: {lines}".format(lines=lines))

        if 'check probe' in lines:
            error = '"check probe" returned from input'
        elif not lines:
            error = 'Nothing returned from input'
        elif str_is_float(lines[0]):
            ph = lines[0]
            logger.debug('Value[0] is float: {val}'.format(val=ph))
        else:
            error = 'Value[0] is not float or "check probe": {val}'.format(
                val=lines[0])

    elif selected_input.interface == 'UART':
        ph_input_uart = AtlasScientificUART(selected_input.uart_location,
                                            baudrate=selected_input.baud_rate)
        lines = ph_input_uart.query('R')
        logger.debug("All Lines: {lines}".format(lines=lines))

        if 'check probe' in lines:
            error = '"check probe" returned from input'
        elif not lines:
            error = 'Nothing returned from input'
        elif str_is_float(lines[0]):
            ph = lines[0]
            logger.debug('Value[0] is float: {val}'.format(val=ph))
        else:
            error = 'Value[0] is not float or "check probe": {val}'.format(
                val=lines[0])

    elif selected_input.interface == 'I2C':
        ph_input_i2c = AtlasScientificI2C(i2c_address=int(
            str(selected_input.i2c_location), 16),
                                          i2c_bus=selected_input.i2c_bus)
        ph_status, ph_str = ph_input_i2c.query('R')
        if ph_status == 'error':
            error = "Input read unsuccessful: {err}".format(err=ph_str)
        elif ph_status == 'success':
            ph = ph_str

    if error:
        logger.error(error)
        return error, 204
    else:
        return ph
Esempio n. 15
0
class AtlasScientificCommand:
    """
    Class to handle issuing commands to the Atlas Scientific sensor boards
    """
    def __init__(self, input_dev):
        self.cmd_send = None
        self.ph_sensor_uart = None
        self.ph_sensor_i2c = None
        self.interface = input_dev.interface
        self.init_error = None

        if self.interface == 'FTDI':
            from mycodo.devices.atlas_scientific_ftdi import AtlasScientificFTDI
            self.ph_sensor_ftdi = AtlasScientificFTDI(input_dev.ftdi_location)
        elif self.interface == 'UART':
            from mycodo.devices.atlas_scientific_uart import AtlasScientificUART
            self.ph_sensor_uart = AtlasScientificUART(
                input_dev.uart_location, baudrate=input_dev.baud_rate)
        elif self.interface == 'I2C':
            from mycodo.devices.atlas_scientific_i2c import AtlasScientificI2C
            self.ph_sensor_i2c = AtlasScientificI2C(i2c_address=int(
                str(input_dev.i2c_location), 16),
                                                    i2c_bus=input_dev.i2c_bus)

        (self.measurement, self.board_version,
         self.firmware_version) = self.get_board_version()

        if self.board_version == 0:
            error_msg = "Atlas Scientific board initialization unsuccessful. " \
                        "Unable to retrieve device info (this indicates the " \
                        "device was not properly initialized or connected)"
            logger.error(error_msg)
            self.init_error = error_msg
        else:
            logger.debug(
                "Atlas Scientific board initialization success. "
                "Measurement: {meas}, Board: {brd}, Firmware: {fw}".format(
                    meas=self.measurement,
                    brd=self.board_version,
                    fw=self.firmware_version))

    def get_sensor_measurement(self):
        return self.measurement

    def get_board_version(self):
        """Return the board version of the Atlas Scientific pH sensor"""
        info = None

        try:
            if self.interface == 'FTDI':
                info = self.ph_sensor_ftdi.query('i')
            elif self.interface == 'UART':
                info = self.ph_sensor_uart.query('i')
            elif self.interface == 'I2C':
                info = self.ph_sensor_i2c.query('i')
        except TypeError:
            logger.exception(
                "Unable to determine board version of Atlas sensor")
            return None, 0, None

        # Check first letter of info response
        # "P" indicates a legacy board version
        if info is None:
            return None, 0, None
        elif isinstance(info, tuple):
            if info[0] == 'error':
                return None, 0, None
            for each_line in info:
                if each_line == 'P':
                    return 'NA', 1, each_line  # Older board version
                elif ',' in each_line and len(each_line.split(',')) == 3:
                    info_split = each_line.split(',')
                    measurement = info_split[1]
                    firmware = info_split[2]
                    return measurement, 2, firmware  # Newer board version
        return None, 0, None

    def calibrate(self, command, set_amount=None, custom_cmd=None):
        """
        Determine and send the correct command to an Atlas Scientific sensor,
        based on the board version
        """
        # Formulate command based on calibration step and board version.
        # Legacy boards requires a different command than recent boards.
        # Some commands are not necessary for recent boards and will not
        # generate a response.
        err = 1
        msg = "Default message"

        if self.init_error:
            msg = self.init_error

        # Atlas EC
        if command == 'ec_dry':
            if self.board_version == 2:
                err, msg = self.send_command('cal,dry')
        elif command == 'ec_low':
            if self.board_version == 2:
                err, msg = self.send_command(
                    'cal,low,{uS}'.format(uS=set_amount))
        elif command == 'ec_high':
            if self.board_version == 2:
                err, msg = self.send_command(
                    'cal,high,{uS}'.format(uS=set_amount))

        # Atlas pH
        elif command == 'temperature' and set_amount is not None:
            if self.board_version == 1:
                err, msg = self.send_command(set_amount)
            elif self.board_version == 2:
                err, msg = self.send_command(
                    'T,{temp}'.format(temp=set_amount))
        elif command == 'clear_calibration':
            if self.board_version == 1:
                err, msg = self.send_command('X')
                self.send_command('L0')
            elif self.board_version == 2:
                err, msg = self.send_command('Cal,clear')
        elif command == 'continuous':
            if self.board_version == 1:
                err, msg = self.send_command('C')
            elif self.board_version == 2:
                err, msg = self.send_command('C,1')
        elif command == 'low':
            if self.board_version == 1:
                err, msg = self.send_command('F')
            elif self.board_version == 2:
                err, msg = self.send_command('Cal,low,4.00')
        elif command == 'mid':
            if self.board_version == 1:
                err, msg = self.send_command('S')
            elif self.board_version == 2:
                err, msg = self.send_command('Cal,mid,7.00')
        elif command == 'high':
            if self.board_version == 1:
                err, msg = self.send_command('T')
            elif self.board_version == 2:
                err, msg = self.send_command('Cal,high,10.00')
        elif command == 'calibrated':  # Not implemented. This queries whether there is a stored calibration
            if self.board_version == 1:
                err = 'success'
                msg = 'Calibrated query not implemented on board version 1 (assume it was successfully calibrated)'
            elif self.board_version == 2:
                err, msg = self.send_command('Cal,?')
        elif command == 'end':
            if self.board_version == 1:
                err, msg = self.send_command('E')
            elif self.board_version == 2:
                err, msg = self.send_command('C,0')
        elif custom_cmd:
            err, msg = self.send_command(custom_cmd)

        return err, msg

    def send_command(self, cmd_send):
        """ Send the command (if not None) and return the response """
        try:
            return_value = "No message"
            if cmd_send is not None:
                if self.interface == 'FTDI':
                    return_value = self.ph_sensor_ftdi.query(cmd_send)
                elif self.interface == 'UART':
                    return_value = self.ph_sensor_uart.query(cmd_send)
                elif self.interface == 'I2C':
                    return_value = self.ph_sensor_i2c.query(cmd_send)
                time.sleep(0.1)
                return 0, return_value
            else:
                return 1, "No command given"
        except Exception as err:
            logger.error("{cls} raised an exception while communicating with "
                         "the board: {err}".format(cls=type(self).__name__,
                                                   err=err))
            return 1, err
Esempio n. 16
0
class InputModule(AbstractInput):
    """A sensor support class that monitors the Atlas Scientific sensor pH"""

    def __init__(self, input_dev, testing=False):
        super(InputModule, self).__init__()
        self.logger = logging.getLogger("mycodo.inputs.atlas_ph")
        self.atlas_sensor_ftdi = None
        self.atlas_sensor_uart = None
        self.atlas_sensor_i2c = None
        self.ftdi_location = None
        self.uart_location = None
        self.i2c_address = None
        self.i2c_bus = None

        if not testing:
            self.logger = logging.getLogger(
                "mycodo.inputs.atlas_ph_{id}".format(id=input_dev.unique_id.split('-')[0]))

            self.input_dev = input_dev
            self.interface = input_dev.interface
            self.calibrate_sensor_measure = input_dev.calibrate_sensor_measure
            self.max_age = None

            if input_dev.custom_options:
                for each_option in input_dev.custom_options.split(';'):
                    option = each_option.split(',')[0]
                    value = each_option.split(',')[1]
                    if option == 'max_age':
                        self.max_age = int(value)

            try:
                self.initialize_sensor()
            except Exception:
                self.logger.exception("Exception while initializing sensor")

    def initialize_sensor(self):
        from mycodo.devices.atlas_scientific_ftdi import AtlasScientificFTDI
        from mycodo.devices.atlas_scientific_i2c import AtlasScientificI2C
        from mycodo.devices.atlas_scientific_uart import AtlasScientificUART
        if self.interface == 'FTDI':
            self.ftdi_location = self.input_dev.ftdi_location
            self.logger = logging.getLogger(
                "mycodo.inputs.atlas_ph_ftdi_{ftdi}".format(
                    ftdi=self.ftdi_location))
            self.atlas_sensor_ftdi = AtlasScientificFTDI(self.ftdi_location)
        elif self.interface == 'UART':
            self.uart_location = self.input_dev.uart_location
            self.logger = logging.getLogger(
                "mycodo.inputs.atlas_ph_uart_{uart}".format(
                    uart=self.uart_location))
            self.atlas_sensor_uart = AtlasScientificUART(self.uart_location)
        elif self.interface == 'I2C':
            self.i2c_address = int(str(self.input_dev.i2c_location), 16)
            self.logger = logging.getLogger(
                "mycodo.inputs.atlas_ph_i2c_{bus}_{add}".format(
                    bus=self.i2c_bus, add=self.i2c_address))
            self.i2c_bus = self.input_dev.i2c_bus
            self.atlas_sensor_i2c = AtlasScientificI2C(
                i2c_address=self.i2c_address, i2c_bus=self.i2c_bus)

    def get_measurement(self):
        """ Gets the sensor's pH measurement via UART/I2C """
        ph = None

        return_dict = measurements_dict.copy()

        # Calibrate the pH measurement based on a temperature measurement
        if (self.calibrate_sensor_measure and
                ',' in self.calibrate_sensor_measure):
            self.logger.debug("pH sensor set to calibrate temperature")

            device_id = self.calibrate_sensor_measure.split(',')[0]
            measurement_id = self.calibrate_sensor_measure.split(',')[1]

            device_measurement = self.device_measurements.filter(
                DeviceMeasurements.unique_id == measurement_id).first()
            if device_measurement:
                conversion = db_retrieve_table_daemon(
                    Conversion, unique_id=device_measurement.conversion_id)
            else:
                conversion = None
            channel, unit, measurement = return_measurement_info(
                device_measurement, conversion)

            last_measurement = read_last_influxdb(
                device_id,
                measurement.unit,
                measurement.measurement,
                measurement.channel,
                self.max_age)

            if last_measurement:
                self.logger.debug(
                    "Latest temperature used to calibrate: {temp}".format(
                        temp=last_measurement[1]))

                atlas_command = AtlasScientificCommand(self.input_dev)
                ret_value, ret_msg = atlas_command.calibrate(
                    'temperature', temperature=last_measurement[1])
                time.sleep(0.5)

                self.logger.debug(
                    "Calibration returned: {val}, {msg}".format(
                        val=ret_value, msg=ret_msg))
            else:
                self.logger.error(
                    "Calibration measurement not found within the past "
                    "{} seconds".format(self.max_age))

        # Read sensor via FTDI
        if self.interface == 'FTDI':
            if self.atlas_sensor_ftdi.setup:
                lines = self.atlas_sensor_ftdi.query('R')
                if lines:
                    self.logger.debug(
                        "All Lines: {lines}".format(lines=lines))

                    # 'check probe' indicates an error reading the sensor
                    if 'check probe' in lines:
                        self.logger.error(
                            '"check probe" returned from sensor')
                    # if a string resembling a float value is returned, this
                    # is out measurement value
                    elif str_is_float(lines[0]):
                        ph = float(lines[0])
                        self.logger.debug(
                            'Value[0] is float: {val}'.format(val=ph))
                    else:
                        # During calibration, the sensor is put into
                        # continuous mode, which causes a return of several
                        # values in one string. If the return value does
                        # not represent a float value, it is likely to be a
                        # string of several values. This parses and returns
                        # the first value.
                        if str_is_float(lines[0].split(b'\r')[0]):
                            ph = lines[0].split(b'\r')[0]
                        # Lastly, this is called if the return value cannot
                        # be determined. Watchthe output in the GUI to see
                        # what it is.
                        else:
                            ph = lines[0]
                            self.logger.error(
                                'Value[0] is not float or "check probe": '
                                '{val}'.format(val=ph))
            else:
                self.logger.error('FTDI device is not set up.'
                                  'Check the log for errors.')

        # Read sensor via UART
        elif self.interface == 'UART':
            if self.atlas_sensor_uart.setup:
                lines = self.atlas_sensor_uart.query('R')
                if lines:
                    self.logger.debug(
                        "All Lines: {lines}".format(lines=lines))

                    # 'check probe' indicates an error reading the sensor
                    if 'check probe' in lines:
                        self.logger.error(
                            '"check probe" returned from sensor')
                    # if a string resembling a float value is returned, this
                    # is out measurement value
                    elif str_is_float(lines[0]):
                        ph = float(lines[0])
                        self.logger.debug(
                            'Value[0] is float: {val}'.format(val=ph))
                    else:
                        # During calibration, the sensor is put into
                        # continuous mode, which causes a return of several
                        # values in one string. If the return value does
                        # not represent a float value, it is likely to be a
                        # string of several values. This parses and returns
                        # the first value.
                        if str_is_float(lines[0].split(b'\r')[0]):
                            ph = lines[0].split(b'\r')[0]
                        # Lastly, this is called if the return value cannot
                        # be determined. Watchthe output in the GUI to see
                        # what it is.
                        else:
                            ph = lines[0]
                            self.logger.error(
                                'Value[0] is not float or "check probe": '
                                '{val}'.format(val=ph))
            else:
                self.logger.error('UART device is not set up.'
                                  'Check the log for errors.')

        # Read sensor via I2C
        elif self.interface == 'I2C':
            if self.atlas_sensor_i2c.setup:
                ph_status, ph_str = self.atlas_sensor_i2c.query('R')
                if ph_status == 'error':
                    self.logger.error(
                        "Sensor read unsuccessful: {err}".format(
                            err=ph_str))
                elif ph_status == 'success':
                    ph = float(ph_str)
            else:
                self.logger.error(
                    'I2C device is not set up. Check the log for errors.')

        return_dict[0]['value'] = ph

        return return_dict
Esempio n. 17
0
class InputModule(AbstractInput):
    """ A sensor support class that monitors the PT1000's temperature """
    def __init__(self, input_dev, testing=False):
        super(InputModule, self).__init__(input_dev,
                                          testing=testing,
                                          name=__name__)
        self.atlas_sensor_ftdi = None
        self.atlas_sensor_uart = None
        self.atlas_sensor_i2c = None

        if not testing:
            self.input_dev = input_dev
            self.interface = input_dev.interface

            try:
                self.initialize_sensor()
            except Exception:
                self.logger.exception("Exception while initializing sensor")

            # Throw out first measurement of Atlas Scientific sensor, as it may be prone to error
            self.get_measurement()

    def initialize_sensor(self):
        from mycodo.devices.atlas_scientific_ftdi import AtlasScientificFTDI
        from mycodo.devices.atlas_scientific_i2c import AtlasScientificI2C
        from mycodo.devices.atlas_scientific_uart import AtlasScientificUART
        if self.interface == 'FTDI':
            self.ftdi_location = self.input_dev.ftdi_location
            self.atlas_sensor_ftdi = AtlasScientificFTDI(self.ftdi_location)
        elif self.interface == 'UART':
            self.uart_location = self.input_dev.uart_location
            self.atlas_sensor_uart = AtlasScientificUART(self.uart_location)
        elif self.interface == 'I2C':
            self.i2c_address = int(str(self.input_dev.i2c_location), 16)
            self.i2c_bus = self.input_dev.i2c_bus
            self.atlas_sensor_i2c = AtlasScientificI2C(
                i2c_address=self.i2c_address, i2c_bus=self.i2c_bus)

    def get_measurement(self):
        """ Gets the Atlas PT1000's temperature in Celsius """
        temp = None
        self.return_dict = measurements_dict.copy()

        if self.interface == 'FTDI':
            if self.atlas_sensor_ftdi.setup:
                lines = self.atlas_sensor_ftdi.query('R')
                self.logger.debug("All Lines: {lines}".format(lines=lines))

                if 'check probe' in lines:
                    self.logger.error('"check probe" returned from sensor')
                elif isinstance(lines, list):
                    if str_is_float(lines[0]):
                        temp = float(lines[0])
                        self.logger.debug(
                            'Value[0] is float: {val}'.format(val=temp))
                elif str_is_float(lines):
                    temp = float(lines)
                    self.logger.debug('Value is float: {val}'.format(val=temp))
                else:
                    self.logger.error('Unknown value: {val}'.format(val=lines))
            else:
                self.logger.error('FTDI device is not set up. '
                                  'Check the log for errors.')

        elif self.interface == 'UART':
            if self.atlas_sensor_uart.setup:
                lines = self.atlas_sensor_uart.query('R')
                self.logger.debug("All Lines: {lines}".format(lines=lines))

                if 'check probe' in lines:
                    self.logger.error('"check probe" returned from sensor')
                elif str_is_float(lines[0]):
                    temp = float(lines[0])
                    self.logger.debug(
                        'Value[0] is float: {val}'.format(val=temp))
                else:
                    self.logger.error(
                        'Value[0] is not float or "check probe": '
                        '{val}'.format(val=lines[0]))
            else:
                self.logger.error('UART device is not set up. '
                                  'Check the log for errors.')

        elif self.interface == 'I2C':
            if self.atlas_sensor_i2c.setup:
                temp_status, temp_str = self.atlas_sensor_i2c.query('R')
                if temp_status == 'error':
                    self.logger.error(
                        "Sensor read unsuccessful: {err}".format(err=temp_str))
                elif temp_status == 'success':
                    temp = float(temp_str)
            else:
                self.logger.error('I2C device is not set up.'
                                  'Check the log for errors.')

        if temp == -1023:  # Erroneous measurement
            return

        self.value_set(0, temp)

        return self.return_dict
Esempio n. 18
0
class InputModule(AbstractInput):
    """ A sensor support class that monitors the PT1000's temperature """

    def __init__(self, input_dev, testing=False):
        super(InputModule, self).__init__()
        self.logger = logging.getLogger("mycodo.inputs.atlas_pt1000")
        self.atlas_sensor_ftdi = None
        self.atlas_sensor_uart = None
        self.atlas_sensor_i2c = None

        if not testing:
            self.logger = logging.getLogger(
                "mycodo.inputs.atlas_pt1000_{id}".format(id=input_dev.unique_id.split('-')[0]))

            self.interface = input_dev.interface
            if self.interface == 'FTDI':
                self.ftdi_location = input_dev.ftdi_location
            elif self.interface == 'UART':
                self.uart_location = input_dev.uart_location
            elif self.interface == 'I2C':
                self.i2c_address = int(str(input_dev.i2c_location), 16)
                self.i2c_bus = input_dev.i2c_bus
            self.initialize_sensor()

    def initialize_sensor(self):
        from mycodo.devices.atlas_scientific_ftdi import AtlasScientificFTDI
        from mycodo.devices.atlas_scientific_i2c import AtlasScientificI2C
        from mycodo.devices.atlas_scientific_uart import AtlasScientificUART
        if self.interface == 'FTDI':
            self.logger = logging.getLogger(
                "mycodo.inputs.atlas_pt1000_ftdi_{ftdi}".format(
                    ftdi=self.ftdi_location))
            self.atlas_sensor_ftdi = AtlasScientificFTDI(self.ftdi_location)
        elif self.interface == 'UART':
            self.logger = logging.getLogger(
                "mycodo.inputs.atlas_pt1000_uart_{uart}".format(
                    uart=self.uart_location))
            self.atlas_sensor_uart = AtlasScientificUART(self.uart_location)
        elif self.interface == 'I2C':
            self.logger = logging.getLogger(
                "mycodo.inputs.atlas_pt1000_i2c_{bus}_{add}".format(
                    bus=self.i2c_bus, add=self.i2c_address))
            self.atlas_sensor_i2c = AtlasScientificI2C(
                i2c_address=self.i2c_address, i2c_bus=self.i2c_bus)
        else:
            self.logger = logging.getLogger("mycodo.inputs.atlas_pt1000")

    def get_measurement(self):
        """ Gets the Atlas PT1000's temperature in Celsius """
        temp = None

        return_dict = measurements_dict.copy()

        if self.interface == 'FTDI':
            if self.atlas_sensor_ftdi.setup:
                lines = self.atlas_sensor_ftdi.query('R')
                self.logger.debug("All Lines: {lines}".format(lines=lines))

                if 'check probe' in lines:
                    self.logger.error('"check probe" returned from sensor')
                elif isinstance(lines, list):
                    if str_is_float(lines[0]):
                        temp = float(lines[0])
                        self.logger.debug(
                            'Value[0] is float: {val}'.format(val=temp))
                elif str_is_float(lines):
                    temp = float(lines)
                    self.logger.debug(
                        'Value is float: {val}'.format(val=temp))
                else:
                    self.logger.error(
                        'Unknown value: {val}'.format(val=lines))
            else:
                self.logger.error('FTDI device is not set up. '
                                  'Check the log for errors.')

        elif self.interface == 'UART':
            if self.atlas_sensor_uart.setup:
                lines = self.atlas_sensor_uart.query('R')
                self.logger.debug("All Lines: {lines}".format(lines=lines))

                if 'check probe' in lines:
                    self.logger.error('"check probe" returned from sensor')
                elif str_is_float(lines[0]):
                    temp = float(lines[0])
                    self.logger.debug(
                        'Value[0] is float: {val}'.format(val=temp))
                else:
                    self.logger.error(
                        'Value[0] is not float or "check probe": '
                        '{val}'.format(val=lines[0]))
            else:
                self.logger.error('UART device is not set up. '
                                  'Check the log for errors.')

        elif self.interface == 'I2C':
            if self.atlas_sensor_i2c.setup:
                temp_status, temp_str = self.atlas_sensor_i2c.query('R')
                if temp_status == 'error':
                    self.logger.error(
                        "Sensor read unsuccessful: {err}".format(
                            err=temp_str))
                elif temp_status == 'success':
                    temp = float(temp_str)
            else:
                self.logger.error('I2C device is not set up.'
                                  'Check the log for errors.')

        return_dict[0]['value'] = temp

        return return_dict
Esempio n. 19
0
    def initialize_sensor(self):
        from mycodo.devices.atlas_scientific_ftdi import AtlasScientificFTDI
        from mycodo.devices.atlas_scientific_i2c import AtlasScientificI2C
        from mycodo.devices.atlas_scientific_uart import AtlasScientificUART
        if self.interface == 'FTDI':
            self.ftdi_location = self.input_dev.ftdi_location
            self.atlas_sensor = AtlasScientificFTDI(self.ftdi_location)
        elif self.interface == 'UART':
            self.uart_location = self.input_dev.uart_location
            self.atlas_sensor = AtlasScientificUART(self.uart_location)
        elif self.interface == 'I2C':
            self.i2c_address = int(str(self.input_dev.i2c_location), 16)
            self.i2c_bus = self.input_dev.i2c_bus
            self.atlas_sensor = AtlasScientificI2C(
                i2c_address=self.i2c_address, i2c_bus=self.i2c_bus)

        if self.is_enabled(0):
            self.atlas_sensor.query('O,TV,1')
            self.logger.debug("Enabling Total Volume measurement")
        else:
            self.atlas_sensor.query('O,TV,0')
            self.logger.debug("Disabling Total Volume measurement")

        if self.is_enabled(1):
            self.atlas_sensor.query('O,FR,1')
            self.logger.debug("Enabling Flow Rate measurement")
        else:
            self.atlas_sensor.query('O,FR,0')
            self.logger.debug("Disabling Flow Rate measurement")

        if self.flow_meter_type:
            if self.flow_meter_type == 'non-atlas':
                if ';' in self.custom_k_values:
                    list_k_values = self.custom_k_values.split(';')
                    self.atlas_sensor.query('K,clear')
                    self.logger.debug("Cleared K Values")
                    for each_k in list_k_values:
                        if ',' in each_k:
                            self.atlas_sensor.query('K,{}'.format(each_k))
                            self.logger.debug("Set K Value: {}".format(each_k))
                        else:
                            self.logger.error(
                                "Improperly-formatted K-value: {}".format(
                                    each_k))
                elif ',' in self.custom_k_values:
                    self.atlas_sensor.query('K,clear')
                    self.logger.debug("Cleared K Values")
                    self.atlas_sensor.query('K,{}'.format(
                        self.custom_k_values))
                    self.logger.debug("Set K Value: {}".format(
                        self.custom_k_values))
                else:
                    self.logger.error(
                        "Improperly-formatted K-value: {}".format(
                            self.custom_k_values))
                self.atlas_sensor.query('Vp,{}'.format(
                    self.custom_k_value_time_base))
                self.logger.debug("Set Custom Time Base: {}".format(
                    self.custom_k_value_time_base))
            else:
                self.atlas_sensor.query('Set,{}'.format(self.flow_meter_type))
                self.logger.debug("Set Flow Meter: {}".format(
                    self.flow_meter_type))

        if self.flow_rate_unit:
            self.atlas_sensor.query('Frp,{}'.format(self.flow_rate_unit))
            self.logger.debug("Set Flow Rate: l/{}".format(
                self.flow_rate_unit))

        if self.internal_resistor and self.internal_resistor != 'atlas':
            self.atlas_sensor.query('P,{}'.format(int(self.internal_resistor)))
            if self.internal_resistor == '0':
                self.logger.debug("Internal Resistor disabled")
            else:
                self.logger.debug("Internal Resistor set: {} K".format(
                    self.internal_resistor))
Esempio n. 20
0
class InputModule(AbstractInput):
    """A sensor support class that monitors the Atlas Scientific sensor ElectricalConductivity"""

    def __init__(self, input_dev, testing=False):
        super(InputModule, self).__init__()
        self.logger = logging.getLogger("mycodo.inputs.atlas_ec")
        self.atlas_sensor_ftdi = None
        self.atlas_sensor_uart = None
        self.atlas_sensor_i2c = None

        if not testing:
            self.logger = logging.getLogger(
                "mycodo.inputs.atlas_ec_{id}".format(
                    id=input_dev.unique_id.split('-')[0]))

            self.interface = input_dev.interface
            if self.interface == 'FTDI':
                self.ftdi_location = input_dev.ftdi_location
            elif self.interface == 'UART':
                self.uart_location = input_dev.uart_location
            elif self.interface == 'I2C':
                self.i2c_address = int(str(input_dev.i2c_location), 16)
                self.i2c_bus = input_dev.i2c_bus
            try:
                self.initialize_sensor()
            except Exception:
                self.logger.exception("Exception while initializing sensor")

    def initialize_sensor(self):
        from mycodo.devices.atlas_scientific_ftdi import AtlasScientificFTDI
        from mycodo.devices.atlas_scientific_i2c import AtlasScientificI2C
        from mycodo.devices.atlas_scientific_uart import AtlasScientificUART
        if self.interface == 'FTDI':
            self.logger = logging.getLogger(
                "mycodo.inputs.atlas_electrical_conductivity_ftdi_{ftdi}".format(
                    ftdi=self.ftdi_location))
            self.atlas_sensor_ftdi = AtlasScientificFTDI(self.ftdi_location)
        elif self.interface == 'UART':
            self.logger = logging.getLogger(
                "mycodo.inputs.atlas_electrical_conductivity_uart_{uart}".format(
                    uart=self.uart_location))
            self.atlas_sensor_uart = AtlasScientificUART(self.uart_location)
        elif self.interface == 'I2C':
            self.logger = logging.getLogger(
                "mycodo.inputs.atlas_electrical_conductivity_i2c_{bus}_{add}".format(
                    bus=self.i2c_bus, add=self.i2c_address))
            self.atlas_sensor_i2c = AtlasScientificI2C(
                i2c_address=self.i2c_address, i2c_bus=self.i2c_bus)

    def get_measurement(self):
        """ Gets the sensor's Electrical Conductivity measurement via UART/I2C """
        electrical_conductivity = None

        return_dict = measurements_dict.copy()

        # Read sensor via UART
        if self.interface == 'FTDI':
            if self.atlas_sensor_ftdi.setup:
                lines = self.atlas_sensor_ftdi.query('R')
                if lines:
                    self.logger.debug(
                        "All Lines: {lines}".format(lines=lines))

                    # 'check probe' indicates an error reading the sensor
                    if 'check probe' in lines:
                        self.logger.error(
                            '"check probe" returned from sensor')
                    # if a string resembling a float value is returned, this
                    # is out measurement value
                    elif str_is_float(lines[0]):
                        electrical_conductivity = float(lines[0])
                        self.logger.debug(
                            'Value[0] is float: {val}'.format(val=electrical_conductivity))
                    else:
                        # During calibration, the sensor is put into
                        # continuous mode, which causes a return of several
                        # values in one string. If the return value does
                        # not represent a float value, it is likely to be a
                        # string of several values. This parses and returns
                        # the first value.
                        if str_is_float(lines[0].split(b'\r')[0]):
                            electrical_conductivity = lines[0].split(b'\r')[0]
                        # Lastly, this is called if the return value cannot
                        # be determined. Watchthe output in the GUI to see
                        # what it is.
                        else:
                            electrical_conductivity = lines[0]
                            self.logger.error(
                                'Value[0] is not float or "check probe": '
                                '{val}'.format(val=electrical_conductivity))
            else:
                self.logger.error('FTDI device is not set up.'
                                  'Check the log for errors.')

        # Read sensor via UART
        elif self.interface == 'UART':
            if self.atlas_sensor_uart.setup:
                lines = self.atlas_sensor_uart.query('R')
                if lines:
                    self.logger.debug(
                        "All Lines: {lines}".format(lines=lines))

                    # 'check probe' indicates an error reading the sensor
                    if 'check probe' in lines:
                        self.logger.error(
                            '"check probe" returned from sensor')
                    # if a string resembling a float value is returned, this
                    # is out measurement value
                    elif str_is_float(lines[0]):
                        electrical_conductivity = float(lines[0])
                        self.logger.debug(
                            'Value[0] is float: {val}'.format(val=electrical_conductivity))
                    else:
                        # During calibration, the sensor is put into
                        # continuous mode, which causes a return of several
                        # values in one string. If the return value does
                        # not represent a float value, it is likely to be a
                        # string of several values. This parses and returns
                        # the first value.
                        if str_is_float(lines[0].split(b'\r')[0]):
                            electrical_conductivity = lines[0].split(b'\r')[0]
                        # Lastly, this is called if the return value cannot
                        # be determined. Watchthe output in the GUI to see
                        # what it is.
                        else:
                            electrical_conductivity = lines[0]
                            self.logger.error(
                                'Value[0] is not float or "check probe": '
                                '{val}'.format(val=electrical_conductivity))
            else:
                self.logger.error('UART device is not set up.'
                                  'Check the log for errors.')

        # Read sensor via I2C
        elif self.interface == 'I2C':
            if self.atlas_sensor_i2c.setup:
                ec_status, ec_str = self.atlas_sensor_i2c.query('R')
                if ec_status == 'error':
                    self.logger.error(
                        "Sensor read unsuccessful: {err}".format(
                            err=ec_str))
                elif ec_status == 'success':
                    electrical_conductivity = float(ec_str)
            else:
                self.logger.error(
                    'I2C device is not set up. Check the log for errors.')

        return_dict[0]['value'] = electrical_conductivity

        return return_dict