Beispiel #1
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)
Beispiel #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)
# -*- 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()
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()