Esempio n. 1
0
def index():
    ipcon = IPConnection() # Create IP connection
    t = Temperature(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 = t.get_temperature()/100.0

    ipcon.disconnect()
    return PAGE.format(temperature)
Esempio n. 2
0
    def cb_enumerate(self, uid, connected_uid, position, hardware_version,
                     firmware_version, device_identifier, enumeration_type):
        if enumeration_type == IPConnection.ENUMERATION_TYPE_DISCONNECTED:
            return

        # Note: The order is important, detect PTC before Humidity
        #
        # https://www.tinkerforge.com/en/doc/Software/Bricklets/PTCV2_Bricklet_Python.html
        if device_identifier == BrickletPTCV2.DEVICE_IDENTIFIER:
            self.ptc = BrickletPTCV2(uid, self.ipcon)
            self.device_type = self.type_ptc

        # https://www.tinkerforge.com/en/doc/Software/Bricklets/Temperature_Bricklet_Python.html
        if device_identifier == Temperature.DEVICE_IDENTIFIER:
            self.temp = Temperature(uid, self.ipcon)
            self.device_type = self.type_temperature

        # https://www.tinkerforge.com/en/doc/Software/Bricklets/AmbientLightV2_Bricklet_Python.html
        if device_identifier == BrickletAmbientLightV2.DEVICE_IDENTIFIER:
            self.al = BrickletAmbientLightV2(uid, self.ipcon)
            self.device_type = self.type_ambient_light

        # https://www.tinkerforge.com/en/doc/Software/Bricklets/HumidityV2_Bricklet_Python.html
        if device_identifier == BrickletHumidityV2.DEVICE_IDENTIFIER:
            self.hum = BrickletHumidityV2(uid, self.ipcon)
            self.device_type = self.type_humidity

        # https://www.tinkerforge.com/en/doc/Software/Bricklets/DistanceIRV2_Bricklet_Python.html
        if device_identifier == BrickletDistanceIRV2.DEVICE_IDENTIFIER:
            self.dist = BrickletDistanceIRV2(uid, self.ipcon)
            self.device_type = self.type_distance

        # https://www.tinkerforge.com/de/doc/Software/Bricklets/MotionDetectorV2_Bricklet_Python.html
        if device_identifier == BrickletMotionDetectorV2.DEVICE_IDENTIFIER:
            self.dist = BrickletMotionDetectorV2(uid, self.ipcon)
            self.device_type = self.type_motion

        if self.verbose:
            print("UID:               " + uid)
            print("Enumeration Type:  " + str(enumeration_type))
            print("Connected UID:     " + connected_uid)
            print("Position:          " + position)
            print("Hardware Version:  " + str(hardware_version))
            print("Firmware Version:  " + str(firmware_version))
            print("Device Identifier: " + str(device_identifier))
            print("Device Type:       " + str(self.device_type))
            print("")
Esempio n. 3
0
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
Esempio n. 4
0
 def __init__(self,
              uid,
              connection,
              logging_daemon,
              queue,
              value=0.0,
              trigger_difference=0.1):
     self._bricklet = Temperature(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 ... Temperature-Bricklet UID "%s" initialisiert' %
         uid)
Esempio n. 5
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:
            
            # Enumeration is for LCD Bricklet
            if device_identifier == LCD20x4.DEVICE_IDENTIFIER:
                # Create lcd device object
                self.lcd = LCD20x4(uid, self.ipcon) 
                self.lcd.register_callback(self.lcd.CALLBACK_BUTTON_PRESSED, 
                                           self.cb_button_pressed)
                self.lcd.clear_display()
                self.lcd.backlight_on()
            # Enumeration is for Temperature Bricklet
            if device_identifier == Temperature.DEVICE_IDENTIFIER:
                # Create temperature device object
                self.temp = Temperature(uid, self.ipcon) 
                self.temp.register_callback(self.temp.CALLBACK_TEMPERATURE, 
                                            self.cb_temperature)

                self.temp.set_temperature_callback_period(50)
Esempio n. 6
0
    def __init__(self):
        QtGui.QWidget.__init__(self)
        self.button = QtGui.QPushButton('Refresh', self)
        self.button.clicked.connect(self.handle_button)
        self.label = QtGui.QLabel('TBD')
        layout = QtGui.QVBoxLayout(self)
        layout.addWidget(self.button)
        layout.addWidget(self.label)

        self.ipcon = IPConnection()
        self.temperature = Temperature(UID_TEMPERATURE, self.ipcon)
        self.ipcon.connect(HOST, PORT)

        # We send the callback through the Qt signal/slot
        # system to make sure that we can change the label
        self.qtcb_temperature.connect(self.cb_temperature)
        self.temperature.register_callback(Temperature.CALLBACK_TEMPERATURE,
                                           self.qtcb_temperature.emit)

        # Refresh every second
        self.temperature.set_temperature_callback_period(1000)

        # Refresh once on startup
        self.handle_button()
Esempio n. 7
0
from tinkerforge.bricklet_temperature import Temperature
from tinkerforge.bricklet_ambient_light import AmbientLight
from tinkerforge.bricklet_uv_light import BrickletUVLight
from tinkerforge.bricklet_motion_detector import BrickletMotionDetector
from tinkerforge.bricklet_humidity_v2 import HumidityV2

import time
from datetime import datetime

if __name__ == "__main__":
    ipcon = IPConnection()  # Create IP connection
    co2 = BrickletCO2(CO2_UID, ipcon)  # Create device object
    humidity = HumidityV2(HUMIDITY_UID, ipcon)
    sound_intensity = SoundIntensity(SOUND_INTENSITY_UID, ipcon)
    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))
Esempio n. 8
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-

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

import time

from tinkerforge.ip_connection import IPConnection
from tinkerforge.bricklet_temperature import Temperature

if __name__ == "__main__":
    ipcon = IPConnection()  # Create IP connection
    t = Temperature(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 = t.get_temperature() / 100.0
    print('Temperature of ' + str(temperature) + ' °C on ' + time.ctime())

    ipcon.disconnect()
Esempio n. 9
0
    def check(self, uid, warning, critical):
        # PTC
        # https://www.tinkerforge.com/en/doc/Software/Bricklets/PTCV2_Bricklet_Python.html
        if self.device_type == self.type_ptc:
            ticks = 0
            if uid:
                self.ptc = BrickletPTCV2(uid, self.ipcon)
            else:
                # TODO: refactor
                while not self.ptc:
                    time.sleep(0.1)
                    ticks = ticks + 1
                    if ticks > self.timeout * 10:
                        output("Timeout %s s reached while detecting bricklet. "
                               "Please use -u to specify the device UID." % self.timeout, 3)

            ptc_value = self.ptc.get_temperature() / 100.0

            status = self.eval_thresholds(ptc_value, warning, critical)

            perfdata = {
                "temperature": ptc_value
            }

            output("Temperature is %s degrees celcius" % ptc_value, status, [], perfdata)

        # Temperature
        # https://www.tinkerforge.com/en/doc/Software/Bricklets/Temperature_Bricklet_Python.html
        if self.device_type == self.type_temperature:
            ticks = 0
            if uid:
                self.temp = Temperature(uid, self.ipcon)
            else:
                # TODO: refactor
                while not self.temp:
                    time.sleep(0.1)
                    ticks = ticks + 1
                    if ticks > self.timeout * 10:
                        output("Timeout %s s reached while detecting bricklet. "
                               "Please use -u to specify the device UID." % self.timeout, 3)

            temp_value = self.temp.get_temperature() / 100.0

            status = self.eval_thresholds(temp_value, warning, critical)

            perfdata = {
                "temperature": temp_value
            }

            output("Temperature is %s degrees celcius" % temp_value, status, [], perfdata)

        # Ambient Light
        # https://www.tinkerforge.com/en/doc/Software/Bricklets/AmbientLightV2_Bricklet_Python.html
        if self.device_type == self.type_ambient_light:
            ticks = 0
            if uid:
                self.al = BrickletAmbientLightV2(uid, self.ipcon)
            else:
                # TODO: refactor
                while not self.al:
                    time.sleep(0.1)
                    ticks = ticks + 1
                    if ticks > self.timeout * 10:
                        output("Timeout %s s reached while detecting bricklet. "
                               "Please use -u to specify the device UID." % self.timeout, 3)

            al_value = self.al.get_illuminance() / 100.0

            status = self.eval_thresholds(al_value, warning, critical)

            perfdata = {
                "illuminance": al_value
            }

            output("Illuminance is %s lx" % al_value, status, [], perfdata)

        # Humidity
        # https://www.tinkerforge.com/en/doc/Software/Bricklets/HumidityV2_Bricklet_Python.html
        if self.device_type == self.type_humidity:
            ticks = 0
            if uid:
                self.hum = BrickletHumidityV2(uid, self.ipcon)
            else:
                # TODO: refactor
                while not self.hum:
                    time.sleep(0.1)
                    ticks = ticks + 1
                    if ticks > self.timeout * 10:
                        output("Timeout %s s reached while detecting bricklet. "
                               "Please use -u to specify the device UID." % self.timeout, 3)

            hum_value = self.hum.get_humidity() / 100.0
            hum_temp_value = self.hum.get_temperature() / 100.0

            status = self.eval_thresholds(hum_value, warning, critical)

            perfdata = {
                "humidity": hum_value,
                "temperature": hum_temp_value
            }

            output("Humidity is %s %%HR (Temperature is %s degrees celcius)" % (hum_value, hum_temp_value),
                   status, [], perfdata)

        # Distance
        # https://www.tinkerforge.com/en/doc/Software/Bricklets/DistanceIRV2_Bricklet_Python.html
        if self.device_type == self.type_distance:
            ticks = 0
            if uid:
                self.dist = BrickletDistanceIRV2(uid, self.ipcon)
            else:
                # TODO: refactor
                while not self.dist:
                    time.sleep(0.1)
                    ticks = ticks + 1
                    if ticks > self.timeout * 10:
                        output("Timeout %s s reached while detecting bricklet. "
                               "Please use -u to specify the device UID." % self.timeout, 3)

            dist_value = self.dist.get_distance() / 10.0

            status = self.eval_thresholds(dist_value, warning, critical)

            perfdata = {
                "distance": dist_value,
            }

            output("Distance is %s cm" % dist_value, status, [], perfdata)

        # Motion
        # https://www.tinkerforge.com/de/doc/Software/Bricklets/MotionDetectorV2_Bricklet_Python.html
        if self.device_type == self.type_motion:
            ticks = 0
            if uid:
                self.motion = BrickletMotionDetectorV2(uid, self.ipcon)
            else:
                # TODO: refactor
                while not self.motion:
                    time.sleep(0.1)
                    ticks = ticks + 1
                    if ticks > self.timeout * 10:
                        output("Timeout %s s reached while detecting bricklet. "
                               "Please use -u to specify the device UID." % self.timeout, 3)

            motion_value = self.motion.get_motion_detected()

            perfdata = {
                    "motion": motion_value
            }
            
            if motion_value:
                output("Motion detected!", motion_value, [], perfdata)
            else:
                output("No motion detected", motion_value, [], perfdata)
Esempio n. 10
0
    def cb_enumerate(self, uid, connected_uid, position, hardware_version,
                     firmware_version, device_identifier, enumeration_type):
        """Recherche des brickets et configuration."""

        if enumeration_type == IPConnection.ENUMERATION_TYPE_CONNECTED or \
           enumeration_type == IPConnection.ENUMERATION_TYPE_AVAILABLE:
            if device_identifier == Temperature.DEVICE_IDENTIFIER:
                try:
                    self.temp = Temperature(uid, self.ipcon)
                    log.info(
                        time.strftime("%Y-%m-%d %H:%M:%S") +
                        ' Temperature initialized')
                except Error as err:
                    log.error(
                        time.strftime("%Y-%m-%d %H:%M:%S") +
                        ' Temperature init failed: ' + str(err.description))
                    self.temp = None
            elif device_identifier == SoundIntensity.DEVICE_IDENTIFIER:
                try:
                    self.sound = SoundIntensity(uid, self.ipcon)
                    log.info(
                        time.strftime("%Y-%m-%d %H:%M:%S") +
                        ' Sound intensity initialized')
                except Error as err:
                    log.error(
                        time.strftime("%Y-%m-%d %H:%M:%S") +
                        ' Sound intensity init failed: ' +
                        str(err.description))
                    self.sound = None
            elif device_identifier == Accelerometer.DEVICE_IDENTIFIER:
                try:
                    self.accel = Accelerometer(uid, self.ipcon)
                    log.info(
                        time.strftime("%Y-%m-%d %H:%M:%S") +
                        ' Accelerometer initialized')
                except Error as err:
                    log.error(
                        time.strftime("%Y-%m-%d %H:%M:%S") +
                        ' Accelerometer init failed: ' + str(err.description))
                    self.accel = None
            elif device_identifier == IndustrialAnalogOut.DEVICE_IDENTIFIER:
                try:
                    self.aout = IndustrialAnalogOut(uid, self.ipcon)
                    self.aout.set_configuration(
                        self.aout.VOLTAGE_RANGE_0_TO_5V,
                        self.aout.CURRENT_RANGE_0_TO_20MA)
                    self.aout.enable()
                    self.aout_connected = True
                    log.info(
                        time.strftime("%Y-%m-%d %H:%M:%S") +
                        ' IndustrialAnalogOut initialized')
                except Error as err:
                    log.error(
                        time.strftime("%Y-%m-%d %H:%M:%S") +
                        ' IndustrialAnalogOut init failed: ' +
                        str(err.description))
                    self.aout = None
            elif device_identifier == IndustrialDualAnalogIn.DEVICE_IDENTIFIER:
                try:
                    self.ain = IndustrialDualAnalogIn(uid, self.ipcon)
                    self.ain.set_sample_rate(6)
                    self.ain_connected = True
                    log.info(
                        time.strftime("%Y-%m-%d %H:%M:%S") +
                        ' IndustrialDualAnalogIn initialized')
                except Error as err:
                    log.error(
                        time.strftime("%Y-%m-%d %H:%M:%S") +
                        ' IndustrialDualAnalogIn init failed: ' +
                        str(err.description))
                    self.ain = None
            elif device_identifier == IndustrialDigitalIn4.DEVICE_IDENTIFIER:
                try:
                    self.din = IndustrialDigitalIn4(uid, self.ipcon)
                    self.din.set_interrupt(8)
                    self.din.set_debounce_period(0)
                    self.din.register_callback(self.din.CALLBACK_INTERRUPT,
                                               self.cb_compteur_turbine)
                    self.din_connected = True
                    log.info(
                        time.strftime("%Y-%m-%d %H:%M:%S") +
                        ' IndustrialDigitalIn4 initialized')
                except Error as err:
                    log.error(
                        time.strftime("%Y-%m-%d %H:%M:%S") +
                        ' IndustrialDigitalIn4 init failed: ' +
                        str(err.description))
                    self.din = None