def print_ambient_light(conn, settings, uid): from tinkerforge.bricklet_ambient_light import BrickletAmbientLight # type: ignore[import] br = BrickletAmbientLight(uid, conn) print_generic(settings, "ambient", br.get_identity(), 0.01, "L", br.get_illuminance())
PORT = 4223 UID = "XYZ" # Change XYZ to the UID of your Ambient Light Bricklet from tinkerforge.ip_connection import IPConnection from tinkerforge.bricklet_ambient_light import BrickletAmbientLight # Callback function for illuminance reached callback (parameter has unit Lux/10) def cb_illuminance_reached(illuminance): print("Illuminance: " + str(illuminance / 10.0) + " Lux") print("Too bright, close the curtains!") if __name__ == "__main__": ipcon = IPConnection() # Create IP connection al = BrickletAmbientLight(UID, ipcon) # Create device object ipcon.connect(HOST, PORT) # Connect to brickd # Don't use device before ipcon is connected # Get threshold callbacks with a debounce time of 10 seconds (10000ms) al.set_debounce_period(10000) # Register illuminance reached callback to function cb_illuminance_reached al.register_callback(al.CALLBACK_ILLUMINANCE_REACHED, cb_illuminance_reached) # Configure threshold for illuminance "greater than 200 Lux" (unit is Lux/10) al.set_illuminance_callback_threshold(">", 200 * 10, 0) raw_input("Press key to exit\n") # Use input() in Python 3
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 == BrickletAmbientLight.DEVICE_IDENTIFIER: try: self.al = BrickletAmbientLight(uid, self.ipcon) self.al.set_illuminance_callback_period(1000) 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 == BrickletAmbientLightV2.DEVICE_IDENTIFIER: try: self.al_v2 = BrickletAmbientLightV2(uid, self.ipcon) self.al_v2.set_configuration( self.al_v2.ILLUMINANCE_RANGE_64000LUX, self.al_v2.INTEGRATION_TIME_200MS) self.al_v2.set_illuminance_callback_period(1000) 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_v2 = None elif device_identifier == BrickletHumidity.DEVICE_IDENTIFIER: try: self.hum = BrickletHumidity(uid, self.ipcon) self.hum.set_humidity_callback_period(1000) 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 == BrickletHumidityV2.DEVICE_IDENTIFIER: try: self.hum_v2 = BrickletHumidityV2(uid, self.ipcon) self.hum_v2.set_humidity_callback_configuration( 1000, True, 'x', 0, 0) self.hum_v2.register_callback( self.hum_v2.CALLBACK_HUMIDITY, self.cb_humidity_v2) log.info('Humidity 2.0 initialized') except Error as e: log.error('Humidity 2.0 init failed: ' + str(e.description)) self.hum_v2 = None elif device_identifier == BrickletBarometer.DEVICE_IDENTIFIER: try: self.baro = BrickletBarometer(uid, self.ipcon) self.baro.set_air_pressure_callback_period(1000) 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
def print_ambient_light(conn, settings, uid): from tinkerforge.bricklet_ambient_light import BrickletAmbientLight # type: ignore[import] # pylint: disable=import-error,import-outside-toplevel br = BrickletAmbientLight(uid, conn) print_generic(settings, "ambient", br.get_identity(), 0.01, "L", br.get_illuminance())
# -*- coding: utf-8 -*- HOST = "localhost" PORT = 4223 UID = "mbw" # Auf die ID aus dem BrickViewer ändern! from tinkerforge.ip_connection import IPConnection from tinkerforge.bricklet_ambient_light import BrickletAmbientLight # Callback function, wird aufgerufen wenn die Helligkeit einen bestimmten Wert # (threshold) übersteigt def cb_illuminance_reached(illuminance): print("Helligkeit: " + str(illuminance / 10.0) + " lx") print("Wo ist meine Sonnebrille!") ipcon = IPConnection() # Verbindung mit dem Master herstellen al = BrickletAmbientLight(UID, ipcon) # Verbindung zum Lichtsensor anlegen ipcon.connect(HOST, PORT) # Verbinden # Callback funktion anmelden al.register_callback(al.CALLBACK_ILLUMINANCE_REACHED, cb_illuminance_reached) # Schwellwert festlegen "größer als 200 lx" al.set_illuminance_callback_threshold(">", 200 * 10, 0) input("Drücke ENTER zum beenden\n" ) # Auf irgendetwas warten, sonst habt Ihr keine Zeit etwas zu tun ipcon.disconnect() # Die Verbindung wieder abbauen
def create_instance(self, uid, ipcon): return BrickletAmbientLight(uid, ipcon)