class BrickletHumidity: _QUOTIENT = 10.0 def __init__(self, uid, connection, logging_daemon, queue, value=0.0, trigger_difference=0.1): self.bricklet = Humidity(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 ... Humidity-Bricklet UID "%s" initialisiert' % uid) def set_callback(self, timeframe=5000): self.bricklet.set_humidity_callback_period(timeframe) self.bricklet.register_callback(self.bricklet.CALLBACK_HUMIDITY, self._changed) self._logging_daemon.debug('Tinkerforge ... Humidity-Bricklet UID "%s" Callback gesetzt' % self.uid) def read(self): return self.bricklet.get_humidity() / 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 ... Humidity-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", "humidity", self._value]) for consumer in self._queue: consumer(tmp_json) self._logging_daemon.info( 'Tinkerforge ... Humidity-Bricklet UID "%s" , neuer Wert %f -> SocketServer Warteschlange ' % ( self.uid, self._value)) humidity = property(read) rising = property(read_rising) falling = property(read_falling)
class WeatherStation(QApplication): HOST = "localhost" PORT = 4223 ipcon = None lcd = None al = None hum = None baro = None projects = [] active_project = None error_msg = None def __init__(self, args): super(QApplication, self).__init__(args) self.error_msg = QErrorMessage() self.ipcon = IPConnection() signal.signal(signal.SIGINT, self.exit_demo) signal.signal(signal.SIGTERM, self.exit_demo) timer = QTimer(self) timer.setSingleShot(True) timer.timeout.connect(self.connect) timer.start(1) def exit_demo(self, signl=None, frme=None): try: self.ipcon.disconnect() self.timer.stop() self.tabs.destroy() except: pass sys.exit() def open_gui(self): self.main = MainWindow(self) self.main.setFixedSize(730, 430) self.main.setWindowIcon(QIcon(os.path.join(ProgramPath.program_path(), "demo-icon.png"))) self.tabs = QTabWidget() widget = QWidget() layout = QVBoxLayout() layout.addWidget(self.tabs) widget.setLayout(layout) self.main.setCentralWidget(widget) self.projects.append(ProjectEnvDisplay(self.tabs, self)) self.projects.append(ProjectStatistics(self.tabs, self)) self.projects.append(ProjectXively(self.tabs, self)) self.tabs.addTab(self.projects[0], "Display Environment Measurements") self.tabs.addTab(self.projects[1], "Show Statistics with Button Control") self.tabs.addTab(self.projects[2], "Connect to Xively") self.active_project = self.projects[0] self.tabs.currentChanged.connect(self.tabChangedSlot) self.main.setWindowTitle("Starter Kit: Weather Station Demo " + config.DEMO_VERSION) self.main.show() def connect(self): try: self.ipcon.connect(WeatherStation.HOST, WeatherStation.PORT) except Error as e: self.error_msg.showMessage('Connection Error: ' + str(e.description) + "\nBrickd installed and running?") return except socket.error as e: self.error_msg.showMessage('Socket error: ' + str(e) + "\nBrickd installed and running?") return self.ipcon.register_callback(IPConnection.CALLBACK_ENUMERATE, self.cb_enumerate) self.ipcon.register_callback(IPConnection.CALLBACK_CONNECTED, self.cb_connected) try: self.ipcon.enumerate() except Error as e: self.error_msg.showMessage('Enumerate Error: ' + str(e.description)) return self.open_gui() def tabChangedSlot(self, tabIndex): if self.lcd is not None: self.lcd.clear_display() self.active_project = self.projects[tabIndex] def cb_illuminance(self, illuminance): for p in self.projects: p.update_illuminance(illuminance) def cb_humidity(self, humidity): for p in self.projects: p.update_humidity(humidity) def cb_air_pressure(self, air_pressure): for p in self.projects: p.update_air_pressure(air_pressure) try: temperature = self.baro.get_chip_temperature() except Error as e: print('Could not get temperature: ' + str(e.description)) return for p in self.projects: p.update_temperature(temperature) def configure_custom_chars(self): c = [[0x00 for x in range(8)] for y in range(8)] c[0] = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff] c[1] = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff] c[2] = [0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff] c[3] = [0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff] c[4] = [0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff] c[5] = [0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff] c[6] = [0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff] c[7] = [0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff] for i in range(len(c)): self.lcd.set_custom_character(i, c[i]); def cb_button_pressed(self, button): for p in self.projects: p.button_pressed(button) 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() self.lcd.register_callback(self.lcd.CALLBACK_BUTTON_PRESSED, self.cb_button_pressed) self.configure_custom_chars() except Error as e: self.error_msg.showMessage('LCD 20x4 init failed: ' + str(e.description)) self.lcd = None elif device_identifier == AmbientLight.DEVICE_IDENTIFIER: try: self.al = AmbientLight(uid, self.ipcon) self.al.set_illuminance_callback_period(1000) self.al.register_callback(self.al.CALLBACK_ILLUMINANCE, self.cb_illuminance) except Error as e: self.error_msg.showMessage('Ambient Light init failed: ' + str(e.description)) self.al = None elif device_identifier == Humidity.DEVICE_IDENTIFIER: try: self.hum = Humidity(uid, self.ipcon) self.hum.set_humidity_callback_period(1000) self.hum.register_callback(self.hum.CALLBACK_HUMIDITY, self.cb_humidity) except Error as e: self.error_msg.showMessage('Humidity init failed: ' + str(e.description)) self.hum = None elif device_identifier == Barometer.DEVICE_IDENTIFIER: try: self.baro = Barometer(uid, self.ipcon) self.baro.set_air_pressure_callback_period(1000) self.baro.register_callback(self.baro.CALLBACK_AIR_PRESSURE, self.cb_air_pressure) except Error as e: self.error_msg.showMessage('Barometer init failed: ' + str(e.description)) self.baro = None def cb_connected(self, connected_reason): if connected_reason == IPConnection.CONNECT_REASON_AUTO_RECONNECT: while True: try: self.ipcon.enumerate() break except Error as e: self.error_msg.showMessage('Enumerate Error: ' + str(e.description)) time.sleep(1)
# -*- coding: utf-8 -*- HOST = "localhost" PORT = 4223 UID = "XYZ" # Change to your UID from tinkerforge.ip_connection import IPConnection from tinkerforge.bricklet_humidity import Humidity # Callback function for humidity callback (parameter has unit %RH/10) def cb_humidity(rh): print('Relative Humidity: ' + str(rh/10.0) + ' %RH') if __name__ == "__main__": ipcon = IPConnection() # Create IP connection h = Humidity(UID, ipcon) # Create device object ipcon.connect(HOST, PORT) # Connect to brickd # Don't use device before ipcon is connected # Set Period for rh callback to 1s (1000ms) # Note: The callback is only called every second if the # humidity has changed since the last call! h.set_humidity_callback_period(1000) # Register humidity callback to function cb_humidity h.register_callback(h.CALLBACK_HUMIDITY, cb_humidity) raw_input('Press key to exit\n') # Use input() in Python 3 ipcon.disconnect()
class BrickletHumidity: _QUOTIENT = 10.0 def __init__(self, uid, connection, logging_daemon, queue, value=0.0, trigger_difference=0.1): self.bricklet = Humidity(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 ... Humidity-Bricklet UID "%s" initialisiert' % uid) def set_callback(self, timeframe=5000): self.bricklet.set_humidity_callback_period(timeframe) self.bricklet.register_callback(self.bricklet.CALLBACK_HUMIDITY, self._changed) self._logging_daemon.debug( 'Tinkerforge ... Humidity-Bricklet UID "%s" Callback gesetzt' % self.uid) def read(self): return self.bricklet.get_humidity() / 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 ... Humidity-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", "humidity", self._value ]) for consumer in self._queue: consumer(tmp_json) self._logging_daemon.info( 'Tinkerforge ... Humidity-Bricklet UID "%s" , neuer Wert %f -> SocketServer Warteschlange ' % (self.uid, self._value)) humidity = property(read) rising = property(read_rising) falling = property(read_falling)
class ClimateSensors: def __init__(self, host, port): self.hum = None self.hum_value = 0.0 self.temp = None self.temp_value = 0.0 self.lcd = None self.port = port self.host = host self.conn = IPConnection() self.conn.register_callback(IPConnection.CALLBACK_ENUMERATE, self.cb_enumerate) self.conn.register_callback(IPConnection.CALLBACK_CONNECTED, self.cb_connected) def update_display(self): if self.lcd is not None: self.lcd.write_line(1, 2, 'Temp: {:3.2f} C'.format(self.temp_value)) self.lcd.write_line(2, 2, 'RelHum: {:3.2f} %'.format(self.hum_value)) def connect(self): if self.conn.get_connection_state() == self.conn.CONNECTION_STATE_DISCONNECTED: self.conn.connect(self.host, self.port) self.conn.enumerate() def disconnect(self): if self.conn.get_connection_state() != self.conn.CONNECTION_STATE_DISCONNECTED: if self.lcd is not None: self.lcd.backlight_off() self.lcd.clear_display() self.conn.disconnect() def cb_connected(self, connected_reason): self.conn.enumerate() def cb_enumerate(self, uid, connected_uid, position, hardware_version, firmware_version, device_identifier, enumeration_type): if enumeration_type == IPConnection.ENUMERATION_TYPE_DISCONNECTED: # print("DISCONNECTED") return if device_identifier == Temperature.DEVICE_IDENTIFIER: self.temp = Temperature(uid, self.conn) self.temp.register_callback(self.temp.CALLBACK_TEMPERATURE, self.cb_temperature) self.update_temperature(self.temp.get_temperature()) self.temp.set_temperature_callback_period(UPDATE_PERIOD) if device_identifier == Humidity.DEVICE_IDENTIFIER: self.hum = Humidity(uid, self.conn) self.hum.register_callback(self.hum.CALLBACK_HUMIDITY, self.cb_humidity) self.update_humidity(self.hum.get_humidity()) self.hum.set_humidity_callback_period(UPDATE_PERIOD) if device_identifier == LCD20x4.DEVICE_IDENTIFIER: self.lcd = LCD20x4(uid, self.conn) self.lcd.backlight_on() def cb_temperature(self, temperature): self.update_temperature(temperature) self.update_display() def update_temperature(self, raw_temperature): self.temp_value = raw_temperature / 100.0 def cb_humidity(self, humidity): self.update_humidity(humidity) self.update_display() def update_humidity(self, raw_humidity): self.hum_value = raw_humidity / 10.0
class WeatherStation: HOST = "localhost" PORT = 4223 ipcon = None lcd = None al = None hum = 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_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('LCD20x4 initialized') except Error as e: log.error('LCD20x4 init failed: ' + str(e.description)) self.lcd = None elif device_identifier == AmbientLight.DEVICE_IDENTIFIER: try: self.al = AmbientLight(uid, self.ipcon) self.al.set_illuminance_callback_period(1000) self.al.register_callback(self.al.CALLBACK_ILLUMINANCE, self.cb_illuminance) log.info('AmbientLight initialized') except Error as e: log.error('AmbientLight init failed: ' + str(e.description)) self.al = None elif device_identifier == Humidity.DEVICE_IDENTIFIER: try: self.hum = Humidity(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 == Barometer.DEVICE_IDENTIFIER: try: self.baro = Barometer(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)
from tinkerforge.ip_connection import IPConnection from tinkerforge.bricklet_humidity import Humidity # Callback for humidity outside of 30 to 60 %RH def cb_reached(humidity): if humidity < 30*10: print('Humidity too low: ' + str(humidity/10.0) + ' %RH') if humidity > 60*10: print('Humidity too high: ' + 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 = Humidity(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 threshold reached callback to function cb_reached h.register_callback(h.CALLBACK_HUMIDITY_REACHED, cb_reached) # Configure threshold for "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()
class WeatherStation: HOST = "localhost" PORT = 4223 ipcon = None lcd = None al = None hum = None baro = 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_illuminance(self, illuminance): if self.lcd is not None: text = 'Illuminanc %6.2f lx' % (illuminance / 10.0) self.lcd.write_line(0, 0, text) 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) 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) log.info('Write to line 2: ' + text) try: temperature = self.baro.get_chip_temperature() except Error as e: log.error('Could not get temperature: ' + str(e.description)) return # \xDF == ° on LCD 20x4 charset text = 'Temperature %5.2f \xDFC' % (temperature / 100.0) self.lcd.write_line(3, 0, text) 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 == AmbientLight.DEVICE_IDENTIFIER: try: self.al = AmbientLight(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 == Humidity.DEVICE_IDENTIFIER: try: self.hum = Humidity(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 == Barometer.DEVICE_IDENTIFIER: try: self.baro = Barometer(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)
self.MODEM.prober.stop() if __name__ == '__main__': if len(sys.argv) != 2: print 'ERROR: Too many or too few arguments' exit(1) def cb_humidity(humidity, mm): sms_humidity = 'Humidity = ' + str(humidity/10.0) + ' %RH' mm.send_sms(PHONE_NR, sms_humidity) time.sleep(2) mm = ModemManager(sys.argv[1], PIN_SIM) # You can keep the PIN as empty string if PIN is disabled ipcon = IPConnection() # Create IP connection h = Humidity(UID, ipcon) # Create device object ipcon.connect(HOST, PORT) # Connect to brickd # Don't use device before ipcon is connected # Set period for humidity callback to 1 minute (60000ms) # Note: The humidity callback is only called every minute # if the humidity has changed since the last call! h.set_humidity_callback_period(60000) # Register humidity callback to function cb_humidity h.register_callback(h.CALLBACK_HUMIDITY, lambda humidity: cb_humidity(humidity, mm)) raw_input('Press any key to exit...\n') # Use input() in Python 3 ipcon.disconnect()
if len(sys.argv) != 2: print 'ERROR: Too many or too few arguments' exit(1) def cb_humidity(humidity, mm): sms_humidity = 'Humidity = ' + str(humidity / 10.0) + ' %RH' mm.send_sms(PHONE_NR, sms_humidity) time.sleep(2) mm = ModemManager( sys.argv[1], PIN_SIM) # You can keep the PIN as empty string if PIN is disabled ipcon = IPConnection() # Create IP connection h = Humidity(UID, ipcon) # Create device object ipcon.connect(HOST, PORT) # Connect to brickd # Don't use device before ipcon is connected # Set period for humidity callback to 1 minute (60000ms) # Note: The humidity callback is only called every minute # if the humidity has changed since the last call! h.set_humidity_callback_period(60000) # Register humidity callback to function cb_humidity h.register_callback(h.CALLBACK_HUMIDITY, lambda humidity: cb_humidity(humidity, mm)) raw_input('Press any key to exit...\n') # Use input() in Python 3 ipcon.disconnect()