def read_sht_2x(): try: # I2C bus=1, Address=0x40 sht = SHT20(1, 0x40) humid = sht.humidity().RH temp = sht.temperature().F return {'temperature': temp, 'humidity': humid} except: _LOGGER.error("SHT-2x not reporting")
def run_server(): sht = SHT20(1, 0x40) h, t = sht.all() celsius = Value(t.C) humidity = Value(h.RH) thing = Thing('urn:dev:ops:humidity-temperature-sensor', 'SHT20_Anthony', ['MultiLevelSensor']) thing.add_property( Property(thing, 'humidity', humidity, metadata={ '@type': 'LevelProperty', 'title': 'Humidity', 'type': 'number', 'unit': 'percent', 'readOnly': True })) thing.add_property( Property(thing, 'temperature', celsius, metadata={ '@type': 'LevelProperty', 'title': 'Temperature', 'type': 'number', 'unit': '°C', 'readOnly': True })) server = WebThingServer(SingleThing(thing), port=8889) def update(): h, t = sht.all() celsius.notify_of_external_update(t.C) humidity.notify_of_external_update(h.RH) timer = tornado.ioloop.PeriodicCallback(update, 3000) timer.start() try: logging.info('starting the server') server.start() except KeyboardInterrupt: logging.debug('stopping update task') timer.stop() logging.info('stopping the server') server.stop() logging.info('done')
def tryConnect(): device_online = False saveValue("sensor_online", "0", "1") while device_online == False: try: sht = SHT20(1, 0x40) device_online = True saveValue("sensor_online", "1", "0") except OSError: print("Error: sensor not found") if device_online == False: time.sleep(INTERVAL_REGISTER) return sht
def __init__(self, conn, ip, port): Thread.__init__(self) self._stop_event = Event() self.ip = ip self.port = port self.conn = conn self.hat = mcc172(0) try: self.temp = mcc134(1) except: self.temp = None try: self.sht20 = SHT20(1, 0x40) except: self.sht20 = None print "[+] New thread started for "+ip+":"+str(port)
def __init__(self, i2c_address, i2c_bus=None): if (i2c_bus): this.i2c_bus = i2c_bus self.sht = SHT20(i2c_bus, i2c_address)
from sensor import SHT20 # I2C bus=1, Address=0x40 #write the data into data.txt file1 = open('data.txt', 'w') sht = SHT20(1, 0x40) for x in range(5): h, t = sht.all() # read both at once file1.write(" ".join(str(s) for s in h)) file1.write("\n") file1.write(" ".join(str(s) for s in t)) file1.write("\n") file1.close() #h = sht.humidity() # read humidity print(h) # namedtuple print(h.RH) # relative humidity #t = sht.temperature() # read temperature print(t) # namedtuple print(t.C) # Celsius
def run_server(): ds18 = DS18B20('28-03199779f5a1') ds18_celsius = Value(ds18.temperature().C) ds18_thing = Thing('urn:dev:ops:temperature-sensor', 'Temperature Sensor', ['TemperatureSensor']) ds18_thing.add_property( Property(ds18_thing, 'celsius', ds18_celsius, metadata={ '@type': 'TemperatureProperty', 'title': 'Celsius', 'type': 'number', 'unit': '°C', 'readOnly': True })) sht = SHT20(1, 0x40) h, t = sht.all() sht_celsius = Value(t.C) sht_rh = Value(h.RH) sht_thing = Thing('urn:dev:ops:humidity-temperature-sensor', 'Humidity and Temperature Sensor', ['MultiLevelSensor', 'TemperatureSensor']) # If you want icon to show humidity: # - remove type `TemperatureSensor`, and # - change temperature metadata @type to `LevelProperty` sht_thing.add_property( Property(sht_thing, 'humidity', sht_rh, metadata={ '@type': 'LevelProperty', 'title': 'Relative humidity', 'type': 'number', 'unit': 'percent', 'readOnly': True })) sht_thing.add_property( Property(sht_thing, 'temperature', sht_celsius, metadata={ '@type': 'TemperatureProperty', 'title': 'Celsius', 'type': 'number', 'unit': '°C', 'readOnly': True })) server = WebThingServer(MultipleThings([ds18_thing, sht_thing], 'Multi-sensor Device'), port=8888) def update(): t = ds18.temperature() ds18_celsius.notify_of_external_update(t.C) h, t = sht.all() sht_celsius.notify_of_external_update(t.C) sht_rh.notify_of_external_update(h.RH) timer = tornado.ioloop.PeriodicCallback(update, 3000) timer.start() try: logging.info('starting the server') server.start() except KeyboardInterrupt: logging.debug('stopping update task') timer.stop() logging.info('stopping the server') server.stop() logging.info('done')