def connect(self, type, uid):
        self.ipcon.connect(self.host, self.port)
        self.connected_type = type

        if self.connected_type == TYPE_PTC:
            ptc = PTC(uid, self.ipcon)

            if ptc.get_identity().device_identifier == PTCV2.DEVICE_IDENTIFIER:
                ptc = PTCV2(uid, self.ipcon)

            self.func = ptc.get_temperature
            self.name = 'temperature'
            self.unit = '°C'
        elif self.connected_type == TYPE_TEMPERATURE:
            temperature = Temperature(uid, self.ipcon)

            if temperature.get_identity().device_identifier == TemperatureV2.DEVICE_IDENTIFIER:
                temperature = TemperatureV2(uid, self.ipcon)

            self.func = temperature.get_temperature
            self.name = 'temperature'
            self.unit = '°C'
        elif self.connected_type == TYPE_HUMIDITY:
            humidity = Humidity(uid, self.ipcon)

            if humidity.get_identity().device_identifier == HumidityV2.DEVICE_IDENTIFIER:
                humidity = HumidityV2(uid, self.ipcon)
                self.is_humidity_v2 = True
            else:
                self.is_humidity_v2 = False

            self.func = humidity.get_humidity
            self.name = 'humidity'
            self.unit = '%RH'
        elif self.connected_type == TYPE_MOTION_DETECTOR:
            md = MotionDetector(uid, self.ipcon)

            if md.get_identity().device_identifier == MotionDetectorV2.DEVICE_IDENTIFIER:
                md = MotionDetectorV2(uid, self.ipcon)

            self.func = md.get_motion_detected
        elif self.connected_type == TYPE_AMBIENT_LIGHT:
            al = AmbientLight(uid, self.ipcon)

            if al.get_identity().device_identifier == AmbientLightV2.DEVICE_IDENTIFIER:
                al = AmbientLightV2(uid, self.ipcon)
            elif al.get_identity().device_identifier == AmbientLightV3.DEVICE_IDENTIFIER:
                al = AmbientLightV3(uid, self.ipcon)

            self.func = al.get_illuminance
            self.name = 'Illuminance'
            self.unit = 'lux'
        elif self.connected_type == TYPE_SEGMENT_DISPLAY_4X7:
            display = SegmentDisplay4x7(uid, self.ipcon)
            self.func = display.set_segments
예제 #2
0
class BrickletPTC:
    _QUOTIENT = 100.0

    def __init__(self, uid, connection, logging_daemon, queue, value=0.0, trigger_difference=0.1):
        self._bricklet = PTC(uid, connection)
        self._value = value
        self._value_old = value
        self.trigger_difference = trigger_difference
        self.uid = uid
        self._rising = False
        self._falling = False
        self._logging_daemon = logging_daemon
        self._queue = queue
        self._logging_daemon.info('Tinkerforge ... PTC-Bricklet UID "%s" initialisiert' % uid)

    def set_callback(self, timeframe=5000):
        self._bricklet.set_temperature_callback_period(timeframe)
        self._bricklet.register_callback(self._bricklet.CALLBACK_TEMPERATURE, self._changed)
        self._logging_daemon.debug('Tinkerforge ... PTC-Bricklet UID "%s" Callback gesetzt' % self.uid)

    def read(self):
        return self._bricklet.get_temperature() / 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 ... PTC-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", "ptc", self._value])
            for consumer in self._queue:
                consumer(tmp_json)
                self._logging_daemon.info(
                    'Tinkerforge ... PTC-Bricklet UID "%s" , neuer Wert %f -> SocketServer Warteschlange ' % (
                        self.uid, self._value))

    ptc = property(read)
    rising = property(read_rising)
    falling = property(read_falling)
    def connect(self, type, uid):
        self.ipcon.connect(self.host, self.port)
        self.connected_type = type

        if self.connected_type == TYPE_PTC:
            ptc = PTC(uid, self.ipcon)

            if ptc.get_identity().device_identifier == PTCV2.DEVICE_IDENTIFIER:
                ptc = PTCV2(uid, self.ipcon)

            self.func = ptc.get_temperature
        elif self.connected_type == TYPE_TEMPERATURE:
            temperature = Temperature(uid, self.ipcon)

            if temperature.get_identity().device_identifier == TemperatureV2.DEVICE_IDENTIFIER:
                temperature = TemperatureV2(uid, self.ipcon)

            self.func = temperature.get_temperature
예제 #4
0
 def __init__(self,
              uid,
              connection,
              logging_daemon,
              queue,
              value=0.0,
              trigger_difference=0.1):
     self._bricklet = PTC(uid, connection)
     self._value = value
     self._value_old = value
     self.trigger_difference = trigger_difference
     self.uid = uid
     self._rising = False
     self._falling = False
     self._logging_daemon = logging_daemon
     self._queue = queue
     self._logging_daemon.info(
         'Tinkerforge ... PTC-Bricklet UID "%s" initialisiert' % uid)
예제 #5
0
 def __init__(self, uid, connection, logging_daemon, queue, value=0.0, trigger_difference=0.1):
     self._bricklet = PTC(uid, connection)
     self._value = value
     self._value_old = value
     self.trigger_difference = trigger_difference
     self.uid = uid
     self._rising = False
     self._falling = False
     self._logging_daemon = logging_daemon
     self._queue = queue
     self._logging_daemon.info('Tinkerforge ... PTC-Bricklet UID "%s" initialisiert' % uid)
예제 #6
0
class BrickletPTC:
    _QUOTIENT = 100.0

    def __init__(self,
                 uid,
                 connection,
                 logging_daemon,
                 queue,
                 value=0.0,
                 trigger_difference=0.1):
        self._bricklet = PTC(uid, connection)
        self._value = value
        self._value_old = value
        self.trigger_difference = trigger_difference
        self.uid = uid
        self._rising = False
        self._falling = False
        self._logging_daemon = logging_daemon
        self._queue = queue
        self._logging_daemon.info(
            'Tinkerforge ... PTC-Bricklet UID "%s" initialisiert' % uid)

    def set_callback(self, timeframe=5000):
        self._bricklet.set_temperature_callback_period(timeframe)
        self._bricklet.register_callback(self._bricklet.CALLBACK_TEMPERATURE,
                                         self._changed)
        self._logging_daemon.debug(
            'Tinkerforge ... PTC-Bricklet UID "%s" Callback gesetzt' %
            self.uid)

    def read(self):
        return self._bricklet.get_temperature() / 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 ... PTC-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", "ptc", self._value])
            for consumer in self._queue:
                consumer(tmp_json)
                self._logging_daemon.info(
                    'Tinkerforge ... PTC-Bricklet UID "%s" , neuer Wert %f -> SocketServer Warteschlange '
                    % (self.uid, self._value))

    ptc = property(read)
    rising = property(read_rising)
    falling = property(read_falling)
예제 #7
0
#!/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_ptc import PTC

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

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

    # Get current temperature (unit is °C/100)
    temperature = ptc.get_temperature()/100.0

    print('Temperature: ' + str(temperature) + ' °C')

    raw_input('Press key to exit\n') # Use input() in Python 3
    ipcon.disconnect()
예제 #8
0
HOST = "localhost"
PORT = 4223
UID = "XYZ" # Change to your UID

from tinkerforge.ip_connection import IPConnection
from tinkerforge.bricklet_ptc import PTC

# Callback for temperature greater than 30 °C
def cb_reached(temperature):
    print('We have ' + str(temperature/100.0) + ' °C.')
    print('It is too hot, we need air conditioning!')

if __name__ == "__main__":
    ipcon = IPConnection() # Create IP connection
    ptc = PTC(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)
    ptc.set_debounce_period(10000)

    # Register threshold reached callback to function cb_reached
    ptc.register_callback(ptc.CALLBACK_TEMPERATURE_REACHED, cb_reached)

    # Configure threshold for "greater than 30 °C" (unit is °C/100)
    ptc.set_temperature_callback_threshold('>', 30*100, 0)

    raw_input('Press key to exit\n') # Use input() in Python 3
    ipcon.disconnect()
예제 #9
0
#!/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_ptc import PTC

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

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

    # Get current temperature (unit is °C/100)
    temperature = ptc.get_temperature() / 100.0

    print('Temperature: ' + str(temperature) + ' °C')

    raw_input('Press key to exit\n')  # Use input() in Python 3
    ipcon.disconnect()
예제 #10
0
# -*- coding: utf-8 -*-  

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

from tinkerforge.ip_connection import IPConnection
from tinkerforge.bricklet_ptc import PTC

# Callback function for temperature callback (parameter has unit °C/100)
def cb_temperature(temperature):
    print('Temperature: ' + str(temperature/100.0) + ' °C')

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

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

    # Set Period for temperature callback to 1s (1000ms)
    # Note: The callback is only called every second if the 
    #       temperature has changed since the last call!
    ptc.set_temperature_callback_period(1000)

    # Register temperature callback to function cb_temperature
    ptc.register_callback(ptc.CALLBACK_TEMPERATURE, cb_temperature)

    raw_input('Press key to exit\n') # Use input() in Python 3
    ipcon.disconnect()
예제 #11
0
HOST = "localhost"
PORT = 4223
UID = "XYZ"  # Change to your UID

from tinkerforge.ip_connection import IPConnection
from tinkerforge.bricklet_ptc import PTC


# Callback function for temperature callback (parameter has unit °C/100)
def cb_temperature(temperature):
    print('Temperature: ' + str(temperature / 100.0) + ' °C')


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

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

    # Set Period for temperature callback to 1s (1000ms)
    # Note: The callback is only called every second if the
    #       temperature has changed since the last call!
    ptc.set_temperature_callback_period(1000)

    # Register temperature callback to function cb_temperature
    ptc.register_callback(ptc.CALLBACK_TEMPERATURE, cb_temperature)

    raw_input('Press key to exit\n')  # Use input() in Python 3
    ipcon.disconnect()
예제 #12
0
PORT = 4223
UID = "XYZ"  # Change to your UID

from tinkerforge.ip_connection import IPConnection
from tinkerforge.bricklet_ptc import PTC


# Callback for temperature greater than 30 °C
def cb_reached(temperature):
    print('We have ' + str(temperature / 100.0) + ' °C.')
    print('It is too hot, we need air conditioning!')


if __name__ == "__main__":
    ipcon = IPConnection()  # Create IP connection
    ptc = PTC(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)
    ptc.set_debounce_period(10000)

    # Register threshold reached callback to function cb_reached
    ptc.register_callback(ptc.CALLBACK_TEMPERATURE_REACHED, cb_reached)

    # Configure threshold for "greater than 30 °C" (unit is °C/100)
    ptc.set_temperature_callback_threshold('>', 30 * 100, 0)

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