def ambient(connection, table):
    ipcon = IPConnection()
    al = AmbientLight(AMBIENT_UID, ipcon)
    ipcon.connect(HOST, PORT)
    value = al.get_illuminance() / 10.0  # Get current illuminance (unit is Lux/10)
    insert(connection, table, time.time(), value)
    ipcon.disconnect()
Exemple #2
0
class BrickletAmbientLight:
    _QUOTIENT = 10.0

    def __init__(self, uid, connection, logging_daemon, queue, value=0.0, trigger_difference=0.1):
        self.bricklet = AmbientLight(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 ... AmbientLight-Bricklet UID "%s" initialisiert' % uid)

    def set_callback(self, timeframe=5000):
        self.bricklet.set_illuminance_callback_period(timeframe)
        self.bricklet.register_callback(self.bricklet.CALLBACK_ILLUMINANCE, self._changed)
        self._logging_daemon.debug('Tinkerforge ... AmbientLight-Bricklet UID "%s" Callback gesetzt' % self.uid)

    def read(self):
        return self.bricklet.get_illuminance() / self._QUOTIENT

    def read_rising(self):
        return self._rising

    def read_falling(self):
        return self._falling

    def _changed(self, tmp_value):
        tmp_value = (tmp_value / self._QUOTIENT)
        if abs(self._value - tmp_value) >= self.trigger_difference:
            if tmp_value > self._value_old:
                self._rising = True
                self._falling = False
            elif tmp_value < self._value_old:
                self._rising = False
                self._falling = True
            self._logging_daemon.debug(
                'Tinkerforge ... AmbientLight-Bricklet UID "%s" , Neu = %f , Alt = %f , rising = %s , falling = %s' % (
                    self.uid, tmp_value, self._value_old, self._rising, self._falling))
            self._value_old = tmp_value
            self._value = tmp_value
            tmp_json = json.dumps(["send_changed_data", self.uid, "sensor", "ambient_light", self._value])
            for consumer in self._queue:
                consumer(tmp_json)
                self._logging_daemon.info(
                    'Tinkerforge ... AmbientLight-Bricklet UID "%s" , neuer Wert %f -> SocketServer Warteschlange ' % (
                        self.uid, self._value))

    ambient_light = property(read)
    rising = property(read_rising)
    falling = property(read_falling)
Exemple #3
0
class BrickletAmbientLight:
    _QUOTIENT = 10.0

    def __init__(self,
                 uid,
                 connection,
                 logging_daemon,
                 queue,
                 value=0.0,
                 trigger_difference=0.1):
        self.bricklet = AmbientLight(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 ... AmbientLight-Bricklet UID "%s" initialisiert' %
            uid)

    def set_callback(self, timeframe=5000):
        self.bricklet.set_illuminance_callback_period(timeframe)
        self.bricklet.register_callback(self.bricklet.CALLBACK_ILLUMINANCE,
                                        self._changed)
        self._logging_daemon.debug(
            'Tinkerforge ... AmbientLight-Bricklet UID "%s" Callback gesetzt' %
            self.uid)

    def read(self):
        return self.bricklet.get_illuminance() / self._QUOTIENT

    def read_rising(self):
        return self._rising

    def read_falling(self):
        return self._falling

    def _changed(self, tmp_value):
        tmp_value = (tmp_value / self._QUOTIENT)
        if abs(self._value - tmp_value) >= self.trigger_difference:
            if tmp_value > self._value_old:
                self._rising = True
                self._falling = False
            elif tmp_value < self._value_old:
                self._rising = False
                self._falling = True
            self._logging_daemon.debug(
                'Tinkerforge ... AmbientLight-Bricklet UID "%s" , Neu = %f , Alt = %f , rising = %s , falling = %s'
                % (self.uid, tmp_value, self._value_old, self._rising,
                   self._falling))
            self._value_old = tmp_value
            self._value = tmp_value
            tmp_json = json.dumps([
                "send_changed_data", self.uid, "sensor", "ambient_light",
                self._value
            ])
            for consumer in self._queue:
                consumer(tmp_json)
                self._logging_daemon.info(
                    'Tinkerforge ... AmbientLight-Bricklet UID "%s" , neuer Wert %f -> SocketServer Warteschlange '
                    % (self.uid, self._value))

    ambient_light = property(read)
    rising = property(read_rising)
    falling = property(read_falling)
    dust_density = DustDetector(DUST_UID, ipcon)
    temperature = Temperature(TEMPERATURE_UID, ipcon)
    ambientlight = AmbientLight(AMBIENTLIGHT_UID, ipcon)
    uvlight = BrickletUVLight(UVLIGHT_UID, ipcon)
    motiondetect = BrickletMotionDetector(MOTIONDETECTOR_UID, ipcon)

    ipcon.connect(HOST, PORT)  # Connect to brickd

    while (True):
        curtime = datetime.now()
        print("Time: " + curtime.strftime('%Y/%m/%d %H:%M:%S'))

        motion = motiondetect.get_motion_detected()
        print("Motion: " + str(motion))

        illuminance = ambientlight.get_illuminance() / 10.0
        print("Illuminance: " + str(illuminance))

        uv_light = uvlight.get_uv_light()
        print("UV Light: " + str(uv_light))

        # Get current CO2 concentration (unit is ppm)
        cur_co2_concentration = co2.get_co2_concentration()
        print("CO2: " + str(cur_co2_concentration))

        # Get current sound intensity level
        cur_si = sound_intensity.get_intensity()
        print("Sound intensity: " + str(cur_si))

        # Get current dust density
        cur_dd = dust_density.get_dust_density()
#!/usr/bin/env python
# -*- coding: utf-8 -*-  

HOST = "localhost"
PORT = 4223
UID = "XYZ" # Change to your UID

from tinkerforge.ip_connection import IPConnection
from tinkerforge.bricklet_ambient_light import AmbientLight

if __name__ == "__main__":
    ipcon = IPConnection() # Create IP connection
    al = AmbientLight(UID, ipcon) # Create device object

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

    # Get current illuminance (unit is Lux/10)
    illuminance = al.get_illuminance()/10.0

    print('Illuminance: ' + str(illuminance) + ' Lux')

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