Пример #1
0
def connect_sensors(sensors, uids, ipcon):
    """
	Creates Bricklet Instances
	:param sensors: connected sensors, as dictionary
	:param uids: bricklets uids, as dictionary
	:param ipcon: ipconnection from tinkerforge
	:return: Dictionary with all instances from connected sensors
	"""
    # Dictionary with all connected sensors
    con_sensors = {}
    for sensor in sensors:
        if sensor == "ambient_light":
            al = BrickletAmbientLightV2(uids["ambient_light"], ipcon)
            con_sensors.update({"ambient_light": al})
        if sensor == "barometer":
            barometer = BrickletBarometer(uids["barometer"], ipcon)
            con_sensors.update({"barometer": barometer})
        if sensor == "humidity":
            humidity = BrickletHumidity(uids["humidity"], ipcon)
            con_sensors.update({"humidity": humidity})
        if sensor == "lcd":
            lcd = BrickletLCD20x4(uids["lcd"], ipcon)
            con_sensors.update({"lcd": lcd})
        if sensor == "moisture":
            moisture = BrickletMoisture(uids["moisture"], ipcon)
            con_sensors.update({"moisture": moisture})
        if sensor == "temperature":
            temperature = BrickletTemperature(uids["temperature"], ipcon)
            con_sensors.update({"temperature": temperature})
        if sensor == "thermocouple":
            thermo = BrickletThermocouple(uids["thermocouple"], ipcon)
            con_sensors.update({"thermocouple": thermo})
    return con_sensors
Пример #2
0
def connect_sensors():
    global al
    global barometer
    global humidity
    global lcd
    global moisture
    global temperature
    global thermo

    if sensors["ambient_light"] == 1:
        al = BrickletAmbientLightV2(UID_AMBIENT, ipcon)
    if sensors["barometer"] == 1:
        barometer = BrickletBarometer(UID_BAROMETER, ipcon)
    if sensors["humidity"] == 1:
        humidity = BrickletHumidity(UID_HUMIDITY, ipcon)
    if sensors["lcd"] == 1:
        lcd = BrickletLCD20x4(UID_LCD, ipcon)
    if sensors["moisture"] == 1:
        moisture = BrickletMoisture(UID_MOISTURE, ipcon)
    if sensors["temperature"] == 1:
        temperature = BrickletTemperature(UID_TEMPERATURE, ipcon)
    if sensors["thermo_couple"] == 1:
        thermo = BrickletThermocouple(UID_THERMO, ipcon)
Пример #3
0
def print_humidity(conn, settings, uid):
    from tinkerforge.bricklet_humidity import BrickletHumidity  # type: ignore[import]
    br = BrickletHumidity(uid, conn)
    print_generic(settings, "humidity", br.get_identity(), 0.1, "RH",
                  br.get_humidity())
Пример #4
0
HOST = "localhost"
PORT = 4223
UID = "XYZ"  # Change XYZ to the UID of your Humidity Bricklet

from tinkerforge.ip_connection import IPConnection
from tinkerforge.bricklet_humidity import BrickletHumidity


# Callback function for humidity callback (parameter has unit %RH/10)
def cb_humidity(humidity):
    print("Humidity: " + str(humidity / 10.0) + " %RH")


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

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

    # Register humidity callback to function cb_humidity
    h.register_callback(h.CALLBACK_HUMIDITY, cb_humidity)

    # Set period for humidity callback to 1s (1000ms)
    # Note: The humidity callback is only called every second
    #       if the humidity has changed since the last call!
    h.set_humidity_callback_period(1000)

    raw_input("Press key to exit\n")  # Use input() in Python 3
    ipcon.disconnect()
Пример #5
0
class WeatherStation:
    HOST = "localhost"
    PORT = 4223

    ipcon = None
    lcd = None
    al = None
    al_v2 = None
    hum = None
    hum_v2 = None
    baro = None

    def __init__(self):
        self.xively = Xively()
        self.ipcon = IPConnection()
        while True:
            try:
                self.ipcon.connect(WeatherStation.HOST, WeatherStation.PORT)
                break
            except Error as e:
                log.error('Connection Error: ' + str(e.description))
                time.sleep(1)
            except socket.error as e:
                log.error('Socket error: ' + str(e))
                time.sleep(1)

        self.ipcon.register_callback(IPConnection.CALLBACK_ENUMERATE,
                                     self.cb_enumerate)
        self.ipcon.register_callback(IPConnection.CALLBACK_CONNECTED,
                                     self.cb_connected)

        while True:
            try:
                self.ipcon.enumerate()
                break
            except Error as e:
                log.error('Enumerate Error: ' + str(e.description))
                time.sleep(1)

    def cb_illuminance(self, illuminance):
        if self.lcd is not None:
            text = 'Illuminanc %6.2f lx' % (illuminance / 10.0)
            self.lcd.write_line(0, 0, text)
            self.xively.put('AmbientLight', illuminance / 10.0)
            log.info('Write to line 0: ' + text)

    def cb_illuminance_v2(self, illuminance):
        if self.lcd is not None:
            text = 'Illumina %8.2f lx' % (illuminance / 100.0)
            self.lcd.write_line(0, 0, text)
            self.xively.put('AmbientLight', illuminance / 100.0)
            log.info('Write to line 0: ' + text)

    def cb_humidity(self, humidity):
        if self.lcd is not None:
            text = 'Humidity   %6.2f %%' % (humidity / 10.0)
            self.lcd.write_line(1, 0, text)
            self.xively.put('Humidity', humidity / 10.0)
            log.info('Write to line 1: ' + text)

    def cb_air_pressure(self, air_pressure):
        if self.lcd is not None:
            text = 'Air Press %7.2f mb' % (air_pressure / 1000.0)
            self.lcd.write_line(2, 0, text)
            self.xively.put('AirPressure', air_pressure / 1000.0)
            log.info('Write to line 2: ' + text)

            temperature = self.baro.get_chip_temperature() / 100.0
            # \xDF == ° on LCD 20x4 charset
            text = 'Temperature %5.2f \xDFC' % temperature
            self.lcd.write_line(3, 0, text)
            self.xively.put('Temperature', temperature)
            log.info('Write to line 3: ' + text.replace('\xDF', '°'))

    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 cb_connected(self, connected_reason):
        if connected_reason == IPConnection.CONNECT_REASON_AUTO_RECONNECT:
            log.info('Auto Reconnect')

            while True:
                try:
                    self.ipcon.enumerate()
                    break
                except Error as e:
                    log.error('Enumerate Error: ' + str(e.description))
                    time.sleep(1)
Пример #6
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:
         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
#!/usr/bin/env python
# -*- coding: utf-8 -*-

HOST = "localhost"
PORT = 4223
UID = "XYZ" # Change XYZ to the UID of your Humidity Bricklet

from tinkerforge.ip_connection import IPConnection
from tinkerforge.bricklet_humidity import BrickletHumidity

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

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

    # Get current humidity
    humidity = h.get_humidity()
    print("Humidity: " + str(humidity/10.0) + " %RH")

    raw_input("Press key to exit\n") # Use input() in Python 3
    ipcon.disconnect()
HOST = "localhost"
PORT = 4223
UID = "XYZ" # Change XYZ to the UID of your Humidity Bricklet

from tinkerforge.ip_connection import IPConnection
from tinkerforge.bricklet_humidity import BrickletHumidity

# Callback function for humidity reached callback
def cb_humidity_reached(humidity):
    print("Humidity: " + str(humidity/10.0) + " %RH")
    print("Recommended humidity for human comfort is 30 to 60 %RH.")

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

    # Register humidity reached callback to function cb_humidity_reached
    h.register_callback(h.CALLBACK_HUMIDITY_REACHED, cb_humidity_reached)

    # Configure threshold for humidity "outside of 30 to 60 %RH"
    h.set_humidity_callback_threshold("o", 30*10, 60*10)

    raw_input("Press key to exit\n") # Use input() in Python 3
    ipcon.disconnect()
Пример #9
0
def print_humidity(conn, settings, uid):
    from tinkerforge.bricklet_humidity import BrickletHumidity  # type: ignore[import] # pylint: disable=import-error,import-outside-toplevel
    br = BrickletHumidity(uid, conn)
    print_generic(settings, "humidity", br.get_identity(), 0.1, "RH", br.get_humidity())
Пример #10
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-

HOST = "localhost"
PORT = 4223
UID = "XYZ"  # Change XYZ to the UID of your Humidity Bricklet

from tinkerforge.ip_connection import IPConnection
from tinkerforge.bricklet_humidity import BrickletHumidity

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

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

    # Get current humidity (unit is %RH/10)
    humidity = h.get_humidity()
    print("Humidity: " + str(humidity / 10.0) + " %RH")

    raw_input("Press key to exit\n")  # Use input() in Python 3
    ipcon.disconnect()
from time import *

HOST = "localhost"
PORT = 4223

UID_Hum = "Hu"

from tinkerforge.ip_connection import IPConnection
from tinkerforge.bricklet_humidity import BrickletHumidity

if __name__ == "__main__":

    # Verbindung zu Bricklets wird aufgebaut.
    ipcon = IPConnection()

    Hum = BrickletHumidity(UID_Hum, ipcon)
    ipcon.connect(HOST, PORT)  # Connect to brickd

    # Don't use device before ipcon is connected

    t = time()
    flag = False
    j = True

    while True:

        Time_akt = localtime()

        if ((Time_akt[4] == 0 or Time_akt[4] == 5 or Time_akt[4] == 10 or Time_akt[4] == 15 or Time_akt[4] == 20 or
             Time_akt[4] == 25 or Time_akt[4] == 30 or Time_akt[4] == 35 or Time_akt[4] == 40 or Time_akt[4] == 45 or
             Time_akt[4] == 50 or Time_akt[4] == 55) \
Пример #12
0
 def create_instance(self, uid, ipcon):
     return BrickletHumidity(uid, ipcon)
Пример #13
0
# -*- coding: utf-8 -*-

HOST = "localhost"
PORT = 4223
UID = "XYZ" # Change XYZ to the UID of your Humidity Bricklet

from tinkerforge.ip_connection import IPConnection
from tinkerforge.bricklet_humidity import BrickletHumidity

# Callback function for humidity callback (parameter has unit %RH/10)
def cb_humidity(humidity):
    print("Humidity: " + str(humidity/10.0) + " %RH")

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

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

    # Register humidity callback to function cb_humidity
    h.register_callback(h.CALLBACK_HUMIDITY, cb_humidity)

    # Set period for humidity callback to 1s (1000ms)
    # Note: The humidity callback is only called every second
    #       if the humidity has changed since the last call!
    h.set_humidity_callback_period(1000)

    raw_input("Press key to exit\n") # Use input() in Python 3
    ipcon.disconnect()
Пример #14
0
def main():
    global tfIDs
    mqttc = mqtt.Client("")
    mqttc.username_pw_set(USERNAME, password=PASSWORD)
    mqttc.tls_set(ca_certs="{}/fakelerootx1.pem".format(os.getcwd()),
                  certfile=None,
                  keyfile=None,
                  cert_reqs=ssl.CERT_REQUIRED,
                  tls_version=ssl.PROTOCOL_TLS,
                  ciphers=None)
    mqttc.connect(MQTT_ADAPTER_IP,
                  MQTT_ADAPTER_PORT)  # mqtt.abc.re.je == 35.242.131.248:30883
    mqttc.loop_start()

    if TF_CONNECT:
        # Create connection and connect to brickd
        ipcon = IPConnection()

        TPS1_bricklet = BrickletTemperature(TPS1_UID, ipcon)
        HMS1_bricklet = BrickletHumidity(HMS1_UID, ipcon)
        LPS1_bricklet = BrickletAmbientLightV2(LPS1_UID, ipcon)

        ipcon.connect(TF_HOST, TF_PORT)

        # Register Enumerate Callback
        ipcon.register_callback(IPConnection.CALLBACK_ENUMERATE, cb_enumerate)

        # Trigger Enumerate
        ipcon.enumerate()

        time.sleep(2)

    if DEBUG:
        print(tfIDs)

    if PUBLISH:

        # Get current temperature
        TPS1 = TPS1_bricklet.get_temperature() / 100.0
        HMS1 = HMS1_bricklet.get_humidity() / 10.0
        LPS1 = LPS1_bricklet.get_illuminance() / 100.0

        today = dt.now().strftime("%Y-%m-%d %H:%M")
        today_0 = dt.now().strftime("%Y-%m-%d 00:00")
        t = dt.now().strftime("%H:%M")
        # dtt = dateutil.parser.parse(today)
        dtt = dt.utcnow()

        points_TPS1 = {}
        points_TPS1["temperature"] = {"present_value": TPS1}

        points_HMS1 = {}
        points_HMS1["humidity"] = {"present_value": HMS1}

        points_LPS1 = {}
        points_LPS1["illuminance"] = {"present_value": LPS1}

        udmi_payload_TPS1 = str(udmi.Pointset(dtt, points_TPS1))
        # print(udmi_payload_TPS1)
        udmi_payload_HMS1 = str(udmi.Pointset(dtt, points_HMS1))
        # print(udmi_payload_HMS1)
        udmi_payload_LPS1 = str(udmi.Pointset(dtt, points_LPS1))
        # print(udmi_payload_LPS1)

        payload_norm_TPS1 = json_normalize(
            json.loads(udmi_payload_TPS1)).to_json(
                orient='records').strip('[]')
        print(payload_norm_TPS1)
        payload_norm_HMS1 = json_normalize(
            json.loads(udmi_payload_HMS1)).to_json(
                orient='records').strip('[]')
        print(payload_norm_HMS1)
        payload_norm_LPS1 = json_normalize(
            json.loads(udmi_payload_LPS1)).to_json(
                orient='records').strip('[]')
        print(payload_norm_LPS1)

        # msg = json.dumps({"wind": wind, "humidity": humidity, "temperature": temperature})

        # infot = mqttc.publish("telemetry", udmi_payload, qos=1)
        # print("Sending {}".format(udmi_payload_TPS1))
        infot = mqttc.publish("telemetry/myapp.iot/{}".format("TPS-1"),
                              payload_norm_TPS1,
                              qos=1)
        # print("Sending {}".format(udmi_payload_HMS1))
        infot = mqttc.publish("telemetry/myapp.iot/{}".format("HMS-1"),
                              payload_norm_HMS1,
                              qos=1)
        # print("Sending {}".format(udmi_payload_LPS1))
        infot = mqttc.publish("telemetry/myapp.iot/{}".format("LPS-1"),
                              payload_norm_LPS1,
                              qos=1)

        infot.wait_for_publish()
Пример #15
0
PORT = 4223
UID = "XYZ"  # Change XYZ to the UID of your Humidity Bricklet

from tinkerforge.ip_connection import IPConnection
from tinkerforge.bricklet_humidity import BrickletHumidity


# Callback function for humidity reached callback (parameter has unit %RH/10)
def cb_humidity_reached(humidity):
    print("Humidity: " + str(humidity / 10.0) + " %RH")
    print("Recommended humiditiy for human comfort is 30 to 60 %RH.")


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

    # Register humidity reached callback to function cb_humidity_reached
    h.register_callback(h.CALLBACK_HUMIDITY_REACHED, cb_humidity_reached)

    # Configure threshold for humidity "outside of 30 to 60 %RH" (unit is %RH/10)
    h.set_humidity_callback_threshold("o", 30 * 10, 60 * 10)

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