Beispiel #1
0
    def read(self):
        """ """
        data = {'battery_voltage': float(self.sensor.read_battery_voltage())}
        # log.info('Acquire reading from Pytrack')

        # TODO: Add more data here.
        l76_data = self.read_l76gns()
        data.update(l76_data)
        lis2hh12_data = self.read_lis2hh12()
        data.update(lis2hh12_data)

        log.info("Pytrack data: {}".format(data))

        reading = SensorReading()
        reading.sensor = self
        reading.data = data

        return reading
Beispiel #2
0
    def read(self):
        """
        Read the BMP280 sensor.

        :return: SensorReading
        """

        if self.bus is None or self.driver is None:
            return self.SENSOR_NOT_INITIALIZED

        #log.info('Acquire reading from BMP280')

        data = {}

        # Adafruit CircuitPython
        if platform_info.vendor == platform_info.MICROPYTHON.RaspberryPi:

            values = {
                "temperature": self.driver.temperature,
                "pressure": self.driver.pressure,
            }

        # Build telemetry payload.
        # TODO: Push this further into the telemetry domain.
        fieldnames = values.keys()
        for name in fieldnames:
            fieldname = self.format_fieldname(name, hex(self.address))
            value = values[name]
            data[fieldname] = value

        if not data:
            log.warning("I2C device {} has no value: {}".format(
                self.address, data))

        log.debug("I2C data:     {}".format(data))

        reading = SensorReading()
        reading.sensor = self
        reading.data = data

        return reading
Beispiel #3
0
    def read_sensors(self) -> DataFrame:
        """
        Read measurements from all sensor objects that have been registered in the sensor_manager.
        Reading is done with the read() function of each respective sensor object.
        """

        # Power up sensor peripherals.
        self.sensor_manager.power_on()

        # Collect observations.
        data = {}
        richdata = {}
        readings = []

        # Iterate all registered sensors.
        sensors = self.sensor_manager.sensors
        log.info('Reading %s sensor ports', len(sensors))
        for sensor in sensors:

            # Signal sensor reading to user.
            sensorname = sensor.__class__.__name__
            log.info('Reading sensor port "%s"', sensorname)

            # Read sensor port.
            try:

                # Disable garbage collector to guarantee reasonable
                # realtime behavior before invoking sensor reading.
                with gc_disabled():
                    sensor_outcome = sensor.read()

                # Power off HX711 after reading
                if "HX711Sensor" in sensorname:
                    sensor.power_off()

                # Backward compat.
                if isinstance(sensor_outcome, SensorReading):
                    sensor_reading = sensor_outcome
                else:
                    sensor_reading = SensorReading()
                    sensor_reading.sensor = sensor
                    sensor_reading.data = sensor_outcome

                sensor_data = sensor_reading.data

                # Evaluate sensor outcome.
                if sensor_data is None or sensor_data is AbstractSensor.SENSOR_NOT_INITIALIZED:
                    continue

                # Round values according to sensor settings.
                if sensor.settings.get('decimals') is not None:
                    for key, value in sensor_data.items():
                        sensor_data[key] = round(
                            sensor_data[key], sensor.settings.get('decimals'))

                # Add sensor reading to observations.
                data.update(sensor_data)

                # Record reading for prettified output.
                self.record_reading(sensor, sensor_data, richdata)

                readings.append(sensor_reading)

            except Exception as ex:
                # Because of the ``gc_disabled`` context manager used above,
                # the propagation of exceptions has to be tweaked like that.
                log.exc(ex, 'Reading sensor "%s" failed', sensorname)

            # Feed the watchdog.
            self.device.watchdog.feed()

            # Clean up memory after reading each sensor object.
            #self.device.run_gc()

        # Debugging: Print sensor data before running telemetry.
        prettify_log = self.settings.get('sensors.prettify_log', False)
        if prettify_log:
            from terkin.util import ddformat
            log.info('Sensor data:\n\n%s', ddformat(richdata, indent=11))
        else:
            log.info('Sensor data:  %s', data)

        # Capture all sensor readings.
        result = DataFrame()
        result.readings = readings
        result.data_in = data

        return result
    def read(self):
        """
        Read the INA219 sensor.

        :return: SensorReading
        """

        if self.bus is None or self.driver is None:
            return self.SENSOR_NOT_INITIALIZED

        #log.info('Acquire reading from INA219')

        data = {}

        # MicroPython
        if platform_info.vendor in [
                platform_info.MICROPYTHON.Vanilla,
                platform_info.MICROPYTHON.Pycom
        ]:

            # Prepare readings.
            values = {
                "bus_voltage": self.driver.voltage(),
                "shunt_voltage": self.driver.shunt_voltage() / 1000.0,
                "current": self.driver.current() / 1000.0,
                "power": self.driver.power() / 1000.0,
            }

            values['psu_voltage'] = values['bus_voltage'] + values[
                'shunt_voltage']

        # Adafruit CircuitPython
        elif platform_info.vendor == platform_info.MICROPYTHON.RaspberryPi:

            values = {

                # Voltage on V- (load side).
                "bus_voltage": self.driver.bus_voltage,

                # Voltage between V+ and V- across the shunt.
                "shunt_voltage": self.driver.shunt_voltage,

                # Current will be acquired in mA, we want A.
                "current": self.driver.current / 1000.0,

                # The power through the load in Watt.
                "power": self.driver.power,

                # Some more values about how the INA219 has been configured.
                # "bus_voltage_range": self.driver.bus_voltage_range,
                # "gain": self.driver.gain,
                # "bus_adc_resolution": self.driver.bus_adc_resolution,
                # "shunt_adc_resolution": self.driver.shunt_adc_resolution,
                # "mode": self.driver.mode,
            }

            values['psu_voltage'] = values['bus_voltage'] + values[
                'shunt_voltage']

            #print(values)
            #print("PSU Voltage:   {:6.3f} V".format(values['bus_voltage'] + values['shunt_voltage']))
            #print("Shunt Voltage: {:9.6f} V".format(values['shunt_voltage']))
            #print("Load Voltage:  {:6.3f} V".format(values['bus_voltage']))
            #print("Current:       {:9.6f} A".format(values['current'] / 1000))

        # Build telemetry payload.
        # TODO: Push this further into the telemetry domain.
        fieldnames = values.keys()
        for name in fieldnames:
            fieldname = self.format_fieldname(name, hex(self.address))
            value = values[name]
            data[fieldname] = value

        if not data:
            log.warning("I2C device {} has no value: {}".format(
                self.address, data))

        log.debug("I2C data:     {}".format(data))

        reading = SensorReading()
        reading.sensor = self
        reading.data = data

        return reading