def start(self):

        # Report about wakeup reason and run wakeup tasks.
        self.device.resume()

        # Turn off LTE modem and Bluetooth as we don't use them yet.
        # Todo: Revisit where this should actually go.
        self.device.power_off_lte_modem()
        self.device.power_off_bluetooth()

        log.info('Starting %s', self.appname)

        # Start the watchdog for sanity.
        self.device.start_watchdog()

        # Configure RGB-LED according to settings.
        self.device.configure_rgb_led()

        # Dump configuration settings.
        log_configuration = self.settings.get('main.logging.configuration',
                                              False)
        if log_configuration:
            self.settings.dump()

        # Initialize buttons / touch pads.
        buttons_enabled = self.settings.get('sensors.system.buttons.enabled',
                                            False)
        if buttons_enabled:
            from terkin.sensor.button import ButtonManager
            self.button_manager = ButtonManager()
            self.start_buttons()

        # Disable this if you don't want serial access.
        #self.device.enable_serial()

        # Hello world.
        self.device.print_bootscreen()

        # Bootstrap infrastructure.
        self.device.start_networking()

        # Conditionally start telemetry if networking is available.
        if self.device.status.networking:
            self.device.start_telemetry()

        # Todo: Signal readyness by publishing information about the device (Microhomie).
        # e.g. ``self.device.publish_properties()``

        # Setup sensors.
        self.device.feed_watchdog()
        bus_settings = self.settings.get('sensors.busses')
        self.sensor_manager.register_busses(bus_settings)
        self.register_sensors()

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

        # Ready.
        self.start_mainloop()
    def register_sensor_legacy(self, sensor_info, sensor_bus):

        sensor_type = sensor_info.get('type', 'unknown').lower()

        # Sensor reporting about free system memory.
        if sensor_type == 'system.memfree':
            sensor_object = SystemMemoryFree(sensor_info)

        # Sensor which reports system temperature.
        elif sensor_type == 'system.temperature':
            sensor_object = SystemTemperature(sensor_info)

        # Sensor which reports battery voltage.
        elif sensor_type in [
                'system.voltage.battery', 'system.battery-voltage'
        ]:
            sensor_object = SystemVoltage(sensor_info)

        # Sensor which reports solar panel voltage.
        elif sensor_type == 'system.voltage.solar':
            sensor_object = SystemVoltage(sensor_info)

        # Sensor which reports system uptime metrics.
        elif sensor_type == 'system.uptime':
            sensor_object = SystemUptime(sensor_info)

        # Sensor which reports WiFi metrics.
        elif sensor_type == 'system.wifi':
            try:
                sensor_object = SystemWiFiMetrics(
                    sensor_info, self.device.networking.wifi_manager.station)
            except Exception as ex:
                log.exc(ex, 'Enabling SystemWiFiMetrics sensor failed')
                return

        # Initialize buttons / touch pads.
        elif sensor_type == 'system.touch-buttons':
            from terkin.sensor.button import ButtonManager
            self.button_manager = ButtonManager()
            self.start_buttons()
            return

        else:
            raise SensorUnknownError(
                'Unknown sensor type "{}"'.format(sensor_type))

        # Register sensor object with sensor manager.
        self.sensor_manager.register_sensor(sensor_object)
Exemple #3
0
    def start(self):

        self.duty_chrono.reset()

        # Report about wakeup reason and run wakeup tasks.
        self.device.resume()

        # Start the watchdog for sanity.
        self.device.watchdog.start()

        # Configure RGB-LED according to settings.
        self.device.configure_rgb_led()

        # Alternative startup signalling: 2 x green.
        self.device.blink_led(0x000b00, count=2)

        self.device.run_gc()

        # Turn off LTE modem and Bluetooth as we don't use them yet.
        # Todo: Revisit where this should actually go.
        # The modem driver takes about six seconds to initialize, so adjust the watchdog accordingly.
        self.device.watchdog.reconfigure_minimum_timeout(15000)
        if not self.settings.get('main.fastboot', False):
            self.device.power_off_lte_modem()
        self.device.power_off_bluetooth()
        self.device.watchdog.resume()

        log.info('Starting %s', self.application_info.fullname)

        # Dump configuration settings.
        log_configuration = self.settings.get('main.logging.configuration',
                                              False)
        if log_configuration:
            self.settings.dump()

        # Initialize buttons / touch pads.
        buttons_enabled = self.settings.get('sensors.system.buttons.enabled',
                                            False)
        if buttons_enabled:
            from terkin.sensor.button import ButtonManager
            self.button_manager = ButtonManager()
            self.start_buttons()

        # Disable this if you don't want serial access.
        #self.device.enable_serial()

        # Hello world.
        self.device.print_bootscreen()

        # Start networking and telemetry subsystems.

        # Conditionally start network services and telemetry if networking is available.
        try:
            self.device.start_networking()
        except Exception as ex:
            log.exc(ex, 'Networking subsystem failed')
            self.device.status.networking = False

        self.device.start_telemetry()

        # Todo: Signal readyness by publishing information about the device (Microhomie).
        # e.g. ``self.device.publish_properties()``

        # Setup sensors.
        self.device.watchdog.feed()
        bus_settings = self.settings.get('sensors.busses', [])
        self.sensor_manager.setup_busses(bus_settings)
        self.register_sensors()

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

        # Ready.
        self.start_mainloop()
Exemple #4
0
    def register_sensors(self):
        """
        Configure and register sensor objects.
        There are three types of sensors: system, environment & busses. Only the former two are assigned to the latter (if applicable).
        Definitions are in 'settings.py'.
        The sensor are registered by calling their respective classes from terkin/drivers/
        """

        # Add sensors.
        log.info('Registering sensors')
        sensor_infos = []

        # Get list of system sensors from configuration settings.
        sensor_infos += self.settings.get('sensors.system', [])

        # Get list of environmental sensors from configuration settings.
        sensor_infos += self.settings.get('sensors.environment', [])

        # Backward compatibility for environmental sensors.
        if sensor_infos is None:
            sensor_infos += self.settings.get('sensors.registry',
                                              {}).values() or []

        # Scan sensor definitions, create and register sensor objects.
        for sensor_info in sensor_infos:

            sensor_type = sensor_info.get('type', 'unknown').lower()
            sensor_id = sensor_info.get('id',
                                        sensor_info.get('key', sensor_type))
            description = sensor_info.get('description')

            # Skip sensor if disabled in configuration.
            if sensor_info.get('enabled') is False:
                log.info(
                    'Sensor with id={} and type={} is disabled, skipping registration'
                    .format(sensor_id, sensor_type))
                continue

            # skip WiFi sensor registration when WiFi is disabled
            if sensor_type == 'system.wifi':
                if not self.settings.get('networking.wifi.enabled'):
                    log.info('WiFi is disabled, skipping sensor registration')
                    continue

            # Resolve associated bus object.
            sensor_bus = None
            sensor_bus_name = None
            if 'bus' in sensor_info:
                sensor_info_bus = sensor_info['bus']
                sensor_bus = self.sensor_manager.get_bus_by_name(
                    sensor_info_bus)

                # Skip sensor if associated bus is disabled in configuration.
                if sensor_bus is None:
                    log.info(
                        'Bus {} for sensor with id={} and type={} is disabled, '
                        'skipping registration'.format(sensor_info_bus,
                                                       sensor_id, sensor_type))
                    continue
                sensor_bus_name = sensor_bus.name

            # Human readable sensor address.
            if 'address' in sensor_info:
                sensor_address = hex(sensor_info.get('address'))
            else:
                sensor_address = None

            # Report sensor registration to user.
            message = 'Setting up sensor with with id={} and type={} on bus={} with address={} ' \
                      'described as "{}"'.format(sensor_id, sensor_type, sensor_bus_name, sensor_address, description)
            log.info(message)

            try:

                # Sensor reporting about free system memory.
                if sensor_type == 'system.memfree':
                    sensor_object = SystemMemoryFree(sensor_info)

                # Sensor which reports system temperature.
                elif sensor_type == 'system.temperature':
                    sensor_object = SystemTemperature(sensor_info)

                # Sensor which reports battery voltage.
                elif sensor_type in [
                        'system.voltage.battery', 'system.battery-voltage'
                ]:
                    sensor_object = SystemVoltage(sensor_info)

                # Sensor which reports solar panel voltage.
                elif sensor_type == 'system.voltage.solar':
                    sensor_object = SystemVoltage(sensor_info)

                # Sensor which reports system uptime metrics.
                elif sensor_type == 'system.uptime':
                    sensor_object = SystemUptime(sensor_info)

                # Sensor which reports WiFi metrics.
                elif sensor_type == 'system.wifi':
                    try:
                        sensor_object = SystemWiFiMetrics(
                            sensor_info,
                            self.device.networking.wifi_manager.station)
                    except Exception as ex:
                        log.exc(ex, 'Enabling SystemWiFiMetrics sensor failed')
                        continue

                # Initialize buttons / touch pads.
                elif sensor_type == 'system.touch-buttons':
                    from terkin.sensor.button import ButtonManager
                    self.button_manager = ButtonManager()
                    self.start_buttons()

                # Setup and register HX711 sensors.
                elif sensor_type == 'hx711':
                    sensor_object = HX711Sensor(settings=sensor_info)
                    sensor_object.set_address(
                        sensor_info.get('number',
                                        sensor_info.get('address', 0)))
                    sensor_object.register_pin('dout', sensor_info['pin_dout'])
                    sensor_object.register_pin('pdsck',
                                               sensor_info['pin_pdsck'])
                    sensor_object.register_parameter('scale',
                                                     sensor_info['scale'])
                    sensor_object.register_parameter('offset',
                                                     sensor_info['offset'])
                    sensor_object.register_parameter(
                        'gain', sensor_info.get('gain', 128))

                    # Select driver module. Use "gerber" (vanilla) or "heisenberg" (extended).
                    # hx711_sensor.select_driver('gerber')
                    sensor_object.select_driver('heisenberg')

                    # Start sensor.
                    sensor_object.start()

                # Setup and register DS18X20 sensors.
                elif sensor_type == 'ds18b20':
                    sensor_object = DS18X20Sensor(settings=sensor_info)
                    sensor_object.acquire_bus(sensor_bus)

                    # Start sensor.
                    sensor_object.start()

                # Setup and register BME280 sensors.
                elif sensor_type == 'bme280':

                    sensor_object = BME280Sensor(settings=sensor_info)
                    if 'address' in sensor_info:
                        sensor_object.set_address(sensor_info['address'])
                    sensor_object.acquire_bus(sensor_bus)

                    # Start sensor.
                    sensor_object.start()

                elif sensor_type == 'max17043':

                    sensor_object = MAX17043Sensor(settings=sensor_info)
                    if 'address' in sensor_info:
                        sensor_object.set_address(sensor_info['address'])
                    sensor_object.acquire_bus(sensor_bus)

                    # Start sensor.
                    sensor_object.start()

                else:
                    log.warning(
                        'Sensor with id={} has unknown type, skipping registration. '
                        'Sensor settings:\n{}'.format(sensor_id, sensor_info))
                    continue

                # Register sensor object with sensor manager.
                self.sensor_manager.register_sensor(sensor_object)

            except Exception as ex:
                log.exc(
                    ex,
                    'Setting up sensor with id={} and type={} failed'.format(
                        sensor_id, sensor_type))