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 == BrickletLCD20x4.DEVICE_IDENTIFIER: try: self.lcd = BrickletLCD20x4(uid, self.ipcon) self.lcd.clear_display() self.lcd.backlight_on() self.lcd.register_callback(self.lcd.CALLBACK_BUTTON_PRESSED, self.cb_lcd_button_pressed) log.info('LCD 20x4 initialized') except Error as e: log.error('LCD 20x4 init failed: ' + str(e.description)) self.lcd = 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 == 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 elif device_identifier == BrickMaster.DEVICE_IDENTIFIER: try: self.master = BrickMaster(uid, self.ipcon) self.master.disable_status_led() log.info('MasterBrick initialized') except Error as e: log.error('MasterBrick init failed: ' + str(e.description)) self.baro = None
def lies_temp(host, port, uid): temp = None try: ipcon = IPConnection() b = BrickletBarometer(uid, ipcon) ipcon.connect(host, port) temp = b.get_chip_temperature() / 100.0 ipcon.disconnect() except: print("Temperaturabfrage fehlgeschlagen") return temp
def index(): ipcon = IPConnection() # Create IP connection humidity_bricklet = BrickletHumidityV2(HUMIDITY_UID, ipcon) # Create device object barometer_bricklet = BrickletBarometer(BAROMETER_UID, ipcon) ambient_light_bricklet = BrickletAmbientLightV2(AMBIENT_LIGHT_UID, ipcon) ipcon.connect(HOST, PORT) # Connect to brickd # Don't use device before ipcon is connected temperature = humidity_bricklet.get_temperature()/100.0 humidity = humidity_bricklet.get_humidity()/100.0 air_pressure = barometer_bricklet.get_air_pressure()/1000.0 illuminance = ambient_light_bricklet.get_illuminance()/100.0 ipcon.disconnect() return render_template('index.html', temperature=temperature, humidity=humidity, illuminance=illuminance, air_pressure=air_pressure)
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
def lies_temp(host, port, uid): temp = None try: ipcon = IPConnection() b = BrickletBarometer(uid, ipcon) ipcon.connect(host, port) tt = 0.0 for i in range(10): tt = tt + b.get_chip_temperature() ipcon.disconnect() except: print("Temperaturabfrage fehlgeschlagen") temp = tt / 1000.0 return temp
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)
def on_disconnect(client, userdata, rc): print("Disconnected, exiting") sys.exit(1) def sendMessage(client, pressure, temperature, longitude, latitude, altitude): # timestamp; pressure; temprature; longitude, latitude, altitude payload = "{};{};{};{};{};{}".format(time.time(), pressure, temperature, longitude, latitude, altitude.altitude) topic = "collectors/{}/metrics".format(client_id) success = client.publish(topic, payload, qos=0, retain=False) print("{}: {}".format(topic, payload)) if __name__ == "__main__": # Tinkerforge setup ipcon = IPConnection() tempBricklet = BrickletTemperature(TEMPUID, ipcon) barometerBricklet = BrickletBarometer(BARUID, ipcon) gpsBricklet = BrickletGPSV2(GPSUID, ipcon) ipcon.connect(HOST, PORT) # MQTT Setup client = mqtt.Client(client_id=client_id) client.on_connect = on_connect client.on_disconnect = on_disconnect client.tls_set("dapo.pem") client.username_pw_set("rpi", "p00rT7daH7Lnb0HzMfA0d+zY2fAOo3") client.connect("dapo.0x80.ch", 8883, 30) # Send loop while(True): time.sleep(5) try:
PORT = 4223 UID = "XYZ" # Change XYZ to the UID of your Barometer Bricklet from tinkerforge.ip_connection import IPConnection from tinkerforge.bricklet_barometer import BrickletBarometer # Callback function for air pressure reached callback (parameter has unit mbar/1000) def cb_air_pressure_reached(air_pressure): print("Air Pressure: " + str(air_pressure / 1000.0) + " mbar") print("Enjoy the potentially good weather!") if __name__ == "__main__": ipcon = IPConnection() # Create IP connection b = BrickletBarometer(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) b.set_debounce_period(10000) # Register air pressure reached callback to function cb_air_pressure_reached b.register_callback(b.CALLBACK_AIR_PRESSURE_REACHED, cb_air_pressure_reached) # Configure threshold for air pressure "greater than 1025 mbar" (unit is mbar/1000) b.set_air_pressure_callback_threshold(">", 1025 * 1000, 0) raw_input("Press key to exit\n") # Use input() in Python 3
# Import from time import sleep # Tinkerforge from tinkerforge.ip_connection import IPConnection from tinkerforge.bricklet_temperature import BrickletTemperature from tinkerforge.bricklet_barometer import BrickletBarometer from tinkerforge.bricklet_humidity_v2 import BrickletHumidityV2 from tinkerforge.bricklet_ambient_light_v2 import AmbientLightV2 from tinkerforge.bricklet_sound_pressure_level import BrickletSoundPressureLevel from tinkerforge.bricklet_motorized_linear_poti import BrickletMotorizedLinearPoti # Sensors ipcon = IPConnection() # Create IP connection temp = BrickletTemperature(UIDt, ipcon) # Create device object baro = BrickletBarometer(UIDb, ipcon) humi = BrickletHumidityV2(UIDh, ipcon) ambi = AmbientLightV2(UIDa, ipcon) spl = BrickletSoundPressureLevel(UIDs, ipcon) mlp = BrickletMotorizedLinearPoti(UIDm, ipcon) ipcon.connect(HOST, PORT) # Connect to brickd # Don't use device before ipcon is connected def getBrickletTemperature(): return temp.get_temperature() def getBrickletBarometer(): return baro.get_air_pressure()
def __init__(self, parent=None): QMainWindow.__init__(self, parent) self.setupUi(self) self.setWindowTitle( 'Tinkerforge CES Demo: Thermal Imaging Bricklet by FLIR') self.qtcb_ipcon_enumerate.connect(self.cb_ipcon_enumerate) self.qtcb_ipcon_connected.connect(self.cb_ipcon_connected) self.qtcb_high_contrast_image.connect(self.cb_high_contrast_image) self.image = QImage(QSize(80, 60), QImage.Format_RGB32) # Add Tinkerforge logo # Adapt this path self.label_logo.setPixmap( QPixmap('/home/pi/Desktop/demo/logo_klein.png')) # create and setup ipcon self.ipcon = IPConnection() self.ipcon.connect(HOST, PORT) self.ipcon.register_callback(IPConnection.CALLBACK_ENUMERATE, self.qtcb_ipcon_enumerate.emit) self.ipcon.register_callback(IPConnection.CALLBACK_CONNECTED, self.qtcb_ipcon_connected.emit) self.ti = BrickletThermalImaging(UID_TI, self.ipcon) self.ti.register_callback(self.ti.CALLBACK_HIGH_CONTRAST_IMAGE, self.qtcb_high_contrast_image.emit) self.ti.set_image_transfer_config( self.ti.IMAGE_TRANSFER_CALLBACK_HIGH_CONTRAST_IMAGE) self.ti.set_spotmeter_config([35, 25, 45, 35]) #print(ti.get_spotmeter_config()) al = BrickletAmbientLightV2(UID_AL, self.ipcon) al.register_callback(al.CALLBACK_ILLUMINANCE, self.cb_illuminance) al.set_illuminance_callback_period(500) hu = BrickletHumidityV2(UID_HU, self.ipcon) hu.register_callback(hu.CALLBACK_HUMIDITY, self.cb_humidity) hu.set_humidity_callback_configuration(500, False, "x", 0, 0) bm = BrickletBarometer(UID_BM, self.ipcon) bm.register_callback(bm.CALLBACK_AIR_PRESSURE, self.cb_air_pressure) bm.set_air_pressure_callback_period(500) self.rgb_lookup = [] for x in range(256): x /= 255.0 r = int(round(255 * math.sqrt(x))) g = int(round(255 * pow(x, 3))) if math.sin(2 * math.pi * x) >= 0: b = int(round(255 * math.sin(2 * math.pi * x))) else: b = 0 self.rgb_lookup.append((r, g, b)) def paintEvent(event): painter = QPainter(self.label_image) painter.setRenderHint(QPainter.SmoothPixmapTransform, True) w = self.label_image.size().width() h = self.label_image.size().height() painter.scale(w / 80.0, h / 60.0) painter.drawImage(0, 0, self.image) if 35 != None and 45 != None: pen = QPen() pen.setColor(Qt.white) pen.setWidth(0.2) painter.setPen(pen) from_x, from_y, to_x, to_y = [35, 25, 45, 35] from_x = from_x * self.image_pixel_width + 1 from_y = from_y * self.image_pixel_width + 1 to_x = to_x * self.image_pixel_width + 1 to_y = to_y * self.image_pixel_width + 1 cross_x = from_x + (to_x - from_x) / 2.0 cross_y = from_y + (to_y - from_y) / 2.0 if to_x - from_x > 5 or to_y - from_y > 5: lines = [ QLine(from_x, from_y, from_x + self.crosshair_width, from_y), QLine(from_x, from_y, from_x, from_y + self.crosshair_width), QLine(to_x, to_y, to_x, to_y - self.crosshair_width), QLine(to_x, to_y, to_x - self.crosshair_width, to_y), QLine(from_x, to_y, from_x, to_y - self.crosshair_width), QLine(from_x, to_y, from_x + self.crosshair_width, to_y), QLine(to_x, from_y, to_x, from_y + self.crosshair_width), QLine(to_x, from_y, to_x - self.crosshair_width, from_y) ] painter.drawLines(lines) lines = [ QLine(cross_x - self.crosshair_width, cross_y, cross_x + self.crosshair_width, cross_y), QLine(cross_x, cross_y - self.crosshair_width, cross_x, cross_y + self.crosshair_width) ] painter.drawLines(lines) self.update_spotmeter_roi_label() self.label_image.paintEvent = paintEvent
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)
# -*- coding: utf-8 -*- HOST = "localhost" PORT = 4223 UID = "XYZ" # Change XYZ to the UID of your Barometer Bricklet from tinkerforge.ip_connection import IPConnection from tinkerforge.bricklet_barometer import BrickletBarometer # Callback function for air pressure callback def cb_air_pressure(air_pressure): print("Air Pressure: " + str(air_pressure/1000.0) + " mbar") if __name__ == "__main__": ipcon = IPConnection() # Create IP connection b = BrickletBarometer(UID, ipcon) # Create device object ipcon.connect(HOST, PORT) # Connect to brickd # Don't use device before ipcon is connected # Register air pressure callback to function cb_air_pressure b.register_callback(b.CALLBACK_AIR_PRESSURE, cb_air_pressure) # Set period for air pressure callback to 1s (1000ms) # Note: The air pressure callback is only called every second # if the air pressure has changed since the last call! b.set_air_pressure_callback_period(1000) raw_input("Press key to exit\n") # Use input() in Python 3 ipcon.disconnect()
def create_instance(self, uid, ipcon): return BrickletBarometer(uid, ipcon)
class WeatherStation: HOST = "localhost" PORT = 4223 ipcon = None lcd = None al_v2 = None hum_v2 = None baro = None master = None def __init__(self): 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_lcd_button_pressed(self, button): if self.lcd is not None: text = str(self.lcd.is_button_pressed(button)) log.debug('Button pressed ' + text) log.debug('It was button ' + str(button)) if button == 0: if self.lcd.is_backlight_on(): self.lcd.backlight_off() else: self.lcd.backlight_on() def cb_illuminance_v2(self, illuminance): if self.lcd is not None: text = 'Illuminanz %6.2f lx' % (illuminance/100.0) self.lcd.write_line(0, 0, text) log.debug('Write to line 0: ' + text) def cb_humidity_v2(self, humidity): if self.lcd is not None: text = 'Luftfeuchte %5.2f %%' % (humidity/100.0) self.lcd.write_line(1, 0, text) log.debug('Write to line 1: ' + text) try: temperature = self.hum_v2.get_temperature() except Error as e: log.error('Could not get temperature: ' + str(e.description)) return # \xDF == ° on LCD 20x4 charset text = 'Temperatur %6.2f \xDFC' % (temperature/100.0) self.lcd.write_line(3, 0, text) log.debug('Write to line 3: ' + text.replace('\xDF', '°')) def cb_air_pressure(self, air_pressure): if self.lcd is not None: text = 'Luftdruck %7.2f mb' % (air_pressure/1000.0) self.lcd.write_line(2, 0, text) log.debug('Write to line 2: ' + text) 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 == BrickletLCD20x4.DEVICE_IDENTIFIER: try: self.lcd = BrickletLCD20x4(uid, self.ipcon) self.lcd.clear_display() self.lcd.backlight_on() self.lcd.register_callback(self.lcd.CALLBACK_BUTTON_PRESSED, self.cb_lcd_button_pressed) log.info('LCD 20x4 initialized') except Error as e: log.error('LCD 20x4 init failed: ' + str(e.description)) self.lcd = 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 == 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 elif device_identifier == BrickMaster.DEVICE_IDENTIFIER: try: self.master = BrickMaster(uid, self.ipcon) self.master.disable_status_led() log.info('MasterBrick initialized') except Error as e: log.error('MasterBrick 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)
#!/usr/bin/env python # -*- coding: utf-8 -*- HOST = "localhost" PORT = 4223 UID = "XYZ" # Change XYZ to the UID of your Barometer Bricklet from tinkerforge.ip_connection import IPConnection from tinkerforge.bricklet_barometer import BrickletBarometer if __name__ == "__main__": ipcon = IPConnection() # Create IP connection b = BrickletBarometer(UID, ipcon) # Create device object ipcon.connect(HOST, PORT) # Connect to brickd # Don't use device before ipcon is connected # Get current air pressure (unit is mbar/1000) air_pressure = b.get_air_pressure() print("Air Pressure: " + str(air_pressure/1000.0) + " mbar") # Get current altitude (unit is cm) altitude = b.get_altitude() print("Altitude: " + str(altitude/100.0) + " m") raw_input("Press key to exit\n") # Use input() in Python 3 ipcon.disconnect()
finally: print('================================') print('[1] Number of Sent Files to Cloud: ', len(sent_msgs_list)) print('[2] Number of Received Messages From Cloud: ', len(conf_msgs_list)) # Callback function for air pressure callback def cb_air_pressure(air_pressure): print("Air Pressure: " + str(air_pressure / 1000.0) + " mbar") logger.info("|" + str(air_pressure / 1000.0)) if __name__ == "__main__": ipcon = IPConnection() # Create IP connection b = BrickletBarometer(UID, ipcon) # Create device object ipcon.connect(HOST, PORT) # Connect to brickd # create logger object try: # set TimedRotatingFileHandler for root formatter = logging.Formatter('%(asctime)s %(message)s') # use very short interval for this example, typical 'when' would be 'midnight' and no explicit interval handler = TimedRotatingFileHandler(log_file_name, when="S", interval=4, backupCount=100) handler.setFormatter(formatter) logger = logging.getLogger('root') # or pass string to give it a name logger.addHandler(handler)
HOST = "localhost" PORT = 4223 UID = "XYZ" # Change XYZ to the UID of your Barometer Bricklet from tinkerforge.ip_connection import IPConnection from tinkerforge.bricklet_barometer import BrickletBarometer # Callback function for air pressure reached callback (parameter has unit mbar/1000) def cb_air_pressure_reached(air_pressure): print("Air Pressure: " + str(air_pressure/1000.0) + " mbar") print("Enjoy the potentially good weather!") if __name__ == "__main__": ipcon = IPConnection() # Create IP connection b = BrickletBarometer(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) b.set_debounce_period(10000) # Register air pressure reached callback to function cb_air_pressure_reached b.register_callback(b.CALLBACK_AIR_PRESSURE_REACHED, cb_air_pressure_reached) # Configure threshold for air pressure "greater than 1025 mbar" (unit is mbar/1000) b.set_air_pressure_callback_threshold(">", 1025*1000, 0) raw_input("Press key to exit\n") # Use input() in Python 3 ipcon.disconnect()
#!/usr/bin/env python # -*- coding: utf-8 -*- HOST = "localhost" PORT = 4223 UID = "XYZ" # Change XYZ to the UID of your Barometer Bricklet from tinkerforge.ip_connection import IPConnection from tinkerforge.bricklet_barometer import BrickletBarometer if __name__ == "__main__": ipcon = IPConnection() # Create IP connection b = BrickletBarometer(UID, ipcon) # Create device object ipcon.connect(HOST, PORT) # Connect to brickd # Don't use device before ipcon is connected # Get current air pressure air_pressure = b.get_air_pressure() print("Air Pressure: " + str(air_pressure/1000.0) + " mbar") # Get current altitude altitude = b.get_altitude() print("Altitude: " + str(altitude/100.0) + " m") raw_input("Press key to exit\n") # Use input() in Python 3 ipcon.disconnect()