def barometer(connection, table): ipcon = IPConnection() b = Barometer(BAROMETER_UID, ipcon) ipcon.connect(HOST, PORT) value = b.get_air_pressure() / 1000.0 # Get current air pressure (unit is mbar/1000) insert(connection, table, time.time(), value) ipcon.disconnect()
class BrickletAirPressure: _QUOTIENT = 1000.0 def __init__(self, uid, connection, logging_daemon, queue, value=0.0, trigger_difference=0.5): self.bricklet = Barometer(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 ... Barometer-Bricklet UID "%s" initialisiert' % uid) def set_callback(self, timeframe=5000): self.bricklet.set_air_pressure_callback_period(timeframe) self.bricklet.register_callback(self.bricklet.CALLBACK_AIR_PRESSURE, self._changed) self._logging_daemon.debug('Tinkerforge ... Barometer-Bricklet UID "%s" Callback gesetzt' % self.uid) def read(self): return self.bricklet.get_air_pressure() / 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 ... Barometer-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", "air_pressure", self._value]) for consumer in self._queue: consumer(tmp_json) self._logging_daemon.info( 'Tinkerforge ... Barometer-Bricklet UID "%s" , neuer Wert %f -> SocketServer Warteschlange ' % ( self.uid, self._value)) air_pressure = property(read) rising = property(read_rising) falling = property(read_falling)
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
def get_airpressure(id): cfg = configparser.ConfigParser() cfg.read(cfg_filename) # open config file to read from if cfg.has_section("Barometer") == True: port = cfg.getint('Connection', 'Port') # get port entry from config file host = cfg.get('Connection', 'Host') # get host entry from config file uid = cfg.get('Barometer', 'bricklet_uid') # uid port entry from config file ipcon = IPConnection() # Create IP connection ipcon.connect(host, port) # Connect to brickd b = Barometer(uid, ipcon) # Create device object air_pressure = b.get_air_pressure()/1000.0 # Get current air pressure (unit is mbar/1000) altitude = b.get_altitude()/100.0 db = sqlite3.connect(local_db) # build connection to local database c = db.cursor() # create cursor c.execute(wd_table) # create weatherdata table c.execute('''INSERT INTO weatherdata(uid , value, keyword, date_id, device_name) VALUES(?,?,?,?,?)''', (uid,air_pressure,'air_pressure',id,'barometer',)) # insert the uid, device name the id from the date table an die humdity value into the weather table c.execute('''INSERT INTO weatherdata(uid , value, keyword, date_id, device_name) VALUES(?,?,?,?,?)''', (uid,altitude,'altitude',id,'barometer',)) db.commit() # save creates and inserts permanent ipcon.disconnect() return({"air_pressure": air_pressure, "altitude": altitude})
def read_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 1090 < pressure < 920: 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
class BrickletAirPressure: _QUOTIENT = 1000.0 def __init__(self, uid, connection, logging_daemon, queue, value=0.0, trigger_difference=0.5): self.bricklet = Barometer(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 ... Barometer-Bricklet UID "%s" initialisiert' % uid) def set_callback(self, timeframe=5000): self.bricklet.set_air_pressure_callback_period(timeframe) self.bricklet.register_callback(self.bricklet.CALLBACK_AIR_PRESSURE, self._changed) self._logging_daemon.debug( 'Tinkerforge ... Barometer-Bricklet UID "%s" Callback gesetzt' % self.uid) def read(self): return self.bricklet.get_air_pressure() / 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 ... Barometer-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", "air_pressure", self._value ]) for consumer in self._queue: consumer(tmp_json) self._logging_daemon.info( 'Tinkerforge ... Barometer-Bricklet UID "%s" , neuer Wert %f -> SocketServer Warteschlange ' % (self.uid, self._value)) air_pressure = property(read) rising = property(read_rising) falling = property(read_falling)
#!/usr/bin/env python # -*- coding: utf-8 -*- HOST = "localhost" PORT = 4223 UID = "bAc" # Change to your UID from tinkerforge.ip_connection import IPConnection from tinkerforge.bricklet_barometer import Barometer if __name__ == "__main__": ipcon = IPConnection() # Create IP connection b = Barometer(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()/1000.0 print('Air Pressure: ' + str(air_pressure) + ' mbar') # Get current altitude (unit is cm) altitude = b.get_altitude()/100.0 print('Altitude: ' + str(altitude) + ' m') raw_input('Press key to exit\n') # Use input() in Python 3 ipcon.disconnect()
##print('Stack Voltage: ' + str(voltage / 1000.0) + ' V') ##print('Stack Current: ' + str(current / 1000.0) + ' A') t = Temperature(UID_TEMPERATURE) # Create temperature object ipcon.add_device(t) # Add device to IP connection # Don't use device before it is added to connection # Get current temperature (unit is °C/ 100) temperature = t.get_temperature() / 100.0 b = Barometer(UID_BAROMETER) # Create barometer object ipcon.add_device(b) # Add device to IP connection # Don't use device before it is added to the connection # Get the current air pressure (unit is mbar/1000) air_pressure = round(b.get_air_pressure()/1000.0, 1) #print('Temperature: ' + str(temperature) + ' °C') lcd = LCD20x4(UID_LCD) # Create LCD object ipcon.add_device(lcd) # Add device to IP connection # Don't use device before it is added to a connection # Clear display lcd.clear_display() # Turn backlight on lcd.backlight_on() # Write on the LCD lcd.write_line(0, 0, unicode_to_ks0066u('°C: ' + str(temperature)))