예제 #1
0
 def cb_enumerate(self, uid, connected_uid, position, hardware_version,
                  firmware_version, device_identifier, enumeration_type):
     if enumeration_type == IPConnection.ENUMERATION_TYPE_CONNECTED or \
        enumeration_type == IPConnection.ENUMERATION_TYPE_AVAILABLE:
         if device_identifier == LCD20x4.DEVICE_IDENTIFIER:
             try:
                 self.lcd = LCD20x4(uid, self.ipcon)
                 self.lcd.clear_display()
                 self.lcd.backlight_on()
                 log.info('LCD 20x4 initialized')
             except Error as e:
                 log.error('LCD 20x4 init failed: ' + str(e.description))
                 self.lcd = None
         elif device_identifier == AmbientLight.DEVICE_IDENTIFIER:
             try:
                 self.al = AmbientLight(uid, self.ipcon)
                 self.al.set_illuminance_callback_period(5000)
                 self.al.register_callback(self.al.CALLBACK_ILLUMINANCE,
                                           self.cb_illuminance)
                 log.info('Ambient Light initialized')
             except Error as e:
                 log.error('Ambient Light init failed: ' +
                           str(e.description))
                 self.al = None
         elif device_identifier == AmbientLightV2.DEVICE_IDENTIFIER:
             try:
                 self.al_v2 = AmbientLightV2(uid, self.ipcon)
                 self.al_v2.set_configuration(
                     AmbientLightV2.ILLUMINANCE_RANGE_64000LUX,
                     AmbientLightV2.INTEGRATION_TIME_200MS)
                 self.al_v2.set_illuminance_callback_period(5000)
                 self.al_v2.register_callback(
                     self.al_v2.CALLBACK_ILLUMINANCE,
                     self.cb_illuminance_v2)
                 log.info('Ambient Light 2.0 initialized')
             except Error as e:
                 log.error('Ambient Light 2.0 init failed: ' +
                           str(e.description))
                 self.al = None
         elif device_identifier == Humidity.DEVICE_IDENTIFIER:
             try:
                 self.hum = Humidity(uid, self.ipcon)
                 self.hum.set_humidity_callback_period(5000)
                 self.hum.register_callback(self.hum.CALLBACK_HUMIDITY,
                                            self.cb_humidity)
                 log.info('Humidity initialized')
             except Error as e:
                 log.error('Humidity init failed: ' + str(e.description))
                 self.hum = None
         elif device_identifier == Barometer.DEVICE_IDENTIFIER:
             try:
                 self.baro = Barometer(uid, self.ipcon)
                 self.baro.set_air_pressure_callback_period(5000)
                 self.baro.register_callback(
                     self.baro.CALLBACK_AIR_PRESSURE, self.cb_air_pressure)
                 log.info('Barometer initialized')
             except Error as e:
                 log.error('Barometer init failed: ' + str(e.description))
                 self.baro = None
예제 #2
0
파일: weather.py 프로젝트: RaphaelVogel/ha2
def read_data(fake=None):
    """ Reads data from all weather sensors and returns it as Dictionary.
        In case of an error or outlier None is returned"""
    if fake:
        return weather_data

    try:
        ipcon = IPConnection()
        temp_bricklet = Temperature('qnk', ipcon)
        humidity_bricklet = Humidity('nLC', ipcon)
        barometer_bricklet = Barometer('k5g', ipcon)
        ipcon.connect(cfg['weather']['host'], int(cfg['weather']['port']))
        temp_bricklet.set_i2c_mode(Temperature.I2C_MODE_SLOW)

        temp = temp_bricklet.get_temperature() / 100.0
        if 45 < temp < -30:
            weather_data['temperature'] = None
            logger.warn(
                'Got temperature value of %s Grade which is out of range' %
                temp)
        else:
            weather_data['temperature'] = temp

        humidity = humidity_bricklet.get_humidity() / 10.0
        if humidity < 5:
            weather_data['humidity'] = None
            logger.warn('Got humidity value of %s RH which is out of range' %
                        humidity)
        else:
            weather_data['humidity'] = humidity

        pressure = barometer_bricklet.get_air_pressure() / 1000.0
        if 1080 < pressure < 930:
            weather_data['pressure'] = None
            logger.warn('Got pressure value of %s mbar which is out of range' %
                        pressure)
        else:
            weather_data['pressure'] = pressure

        ipcon.disconnect()
        return weather_data

    except Exception as e:
        logger.error('Cloud not connect to weather sensors: %s' % str(e))
        return
예제 #3
0
 def __init__(self,
              uid,
              connection,
              logging_daemon,
              queue,
              value=0.0,
              trigger_difference=0.1):
     self.bricklet = Humidity(uid, connection)
     self._value = value
     self._value_old = value
     self.trigger_difference = trigger_difference
     self._rising = False
     self._falling = False
     self.uid = uid
     self._logging_daemon = logging_daemon
     self._queue = queue
     self._logging_daemon.info(
         'Tinkerforge ... Humidity-Bricklet UID "%s" initialisiert' % uid)
예제 #4
0
if __name__ == '__main__':
    if len(sys.argv) != 2:
        print 'ERROR: Too many or too few arguments'
        exit(1)

    def cb_humidity(humidity, mm):
        sms_humidity = 'Humidity = ' + str(humidity / 10.0) + ' %RH'
        mm.send_sms(PHONE_NR, sms_humidity)
        time.sleep(2)

    mm = ModemManager(
        sys.argv[1],
        PIN_SIM)  # You can keep the PIN as empty string if PIN is disabled

    ipcon = IPConnection()  # Create IP connection
    h = Humidity(UID, ipcon)  # Create device object

    ipcon.connect(HOST, PORT)  # Connect to brickd
    # Don't use device before ipcon is connected

    # Set period for humidity callback to 1 minute (60000ms)
    # Note: The humidity callback is only called every minute
    #       if the humidity has changed since the last call!
    h.set_humidity_callback_period(60000)

    # Register humidity callback to function cb_humidity
    h.register_callback(h.CALLBACK_HUMIDITY,
                        lambda humidity: cb_humidity(humidity, mm))

    raw_input('Press any key to exit...\n')  # Use input() in Python 3
    ipcon.disconnect()