def __init__(self): self.i2c = busio.I2C(board.SCL, board.SDA) self.tca = TCA9548A(self.i2c) self.sensor1 = HTU21D(self.tca[2]) self.sensor2 = HTU21D(self.tca[4]) self.sensor3 = HTU21D(self.tca[6]) self.sensor4 = HTU21D(self.tca[7]) self.loadCalibration()
def get_humidity(): try: # Create library object using our Bus I2C port i2c = busio.I2C(SCL, SDA) sensor = HTU21D(i2c) # print("\nTemperature: %0.1f C" % sensor.temperature) print("Humidity: %0.1f %%" % sensor.relative_humidity) return sensor.relative_humidity except Exception as e: return 0
def __init__(self, blackboard, temp_offset): self.stop_requested = False self.stop_event = threading.Event() self.blackboard = blackboard self.temp_offset = temp_offset # initialize the sensor i2c = busio.I2C(board.SCL, board.SDA) self.htu21d = HTU21D(i2c)
import time import board import busio from adafruit_htu21d import HTU21D i2c = busio.I2C(board.SCL, board.SDA) sensor = HTU21D(i2c) print(sensor.temperature) print(sensor.relative_humidity)
mport time import RPi.GPIO as GPIO from adafruit_htu21d import HTU21D import busio GPIO.setmode(GPIO.BCM) GPIO.setwarnings(False) sda = 2 # GPIO 핀 번호, sda라고 이름이 보이는 핀 scl = 3 # GPIO 핀 번호, scl이라고 이름이 보이는 핀 i2c = busio.I2C(scl, sda) sensor = HTU21D(i2c) # HTU21D 장치를 제어하는 객체 리턴 def getTemperature() : return float(sensor.temperature) # HTU21D 장치로부터 온도 값 읽기 def getHumidity() : return float(sensor.relative_humidity) # HTU21D 장치로부터 습도 값 읽기 while True : print("Current temperature is %4.1d" % getTemperature()) print("Current humidity is %4.1d %%" % getHumidity()) time.sleep(1)
def __init__(self): self.i2c = busio.I2C(board.SCL, board.SDA) self.sensor = HTU21D(self.i2c)
self.request.sendall(bytes(json_to_send, 'UTF-8')) elif ("get_something" in self.data.decode("utf-8")): self.request.sendall(bytes("whattt?", 'UTF-8')) if __name__ == "__main__": # Create logger and set options logging.getLogger().setLevel(logging.INFO) logging.info("Starting logging") # Create the data server and assigning the request handler logging.info("Starting socket server") try: i2c = busio.I2C(board.SCL, board.SDA) htu21dsensor = HTU21D(i2c) except: logging.info("No htu21dsensor detected, or a fault happened.", exc_info=True) htu21dsensor = None try: bmp085sensor = BMP085.BMP085() except: logging.info("No bmp085 detected, or a fault happened.", exc_info=True) bmp085sensor = None try: tmp102sensor = TMP102('C', 0x48, 1) except:
def work_HTU21D(name, data): def getPaths(value, value2, key, offset, raw): Erg = '' if value2: try: value3 = float(value2) Erg += '{"path": "' + key + '","value":' + str(offset + value3) + '},' except: Erg += '{"path": "' + key + '","value":"' + str(value2) + '"},' else: Erg += '{"path": "' + key + '","value": null},' if raw and value: try: value4 = float(value) Erg += '{"path": "' + key + '.raw","value":' + str( value4) + '},' except: Erg += '{"path": "' + key + '.raw","value":"' + str( value) + '"},' return Erg humidityKey = data['data'][0]['SKkey'] temperatureKey = data['data'][1]['SKkey'] if humidityKey or temperatureKey: from adafruit_htu21d import HTU21D address = data['address'] i2c = busio.I2C(board.SCL, board.SDA) sensor = HTU21D(i2c, address=int(address, 16)) if humidityKey: humidityRaw = data['data'][0]['raw'] humidityRate = data['data'][0]['rate'] humidityOffset = data['data'][0]['offset'] if temperatureKey: temperatureRaw = data['data'][1]['raw'] temperatureRate = data['data'][1]['rate'] temperatureOffset = data['data'][1]['offset'] sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) port = data['port'] tick1 = time.time() tick2 = tick1 while True: time.sleep(0.1) try: Erg = '' if humidityKey: tick0 = time.time() if tick0 - tick1 > humidityRate: try: humidityValue = round(sensor.relative_humidity, 1) except: humidityValue = sensor.relative_humidity humidityValue2 = humidityValue Erg += getPaths(humidityValue, humidityValue2, humidityKey, humidityOffset, humidityRaw) tick1 = time.time() if temperatureKey: tick0 = time.time() if tick0 - tick2 > temperatureRate: try: temperatureValue = round(sensor.temperature, 1) except: temperatureValue = sensor.temperature try: temperatureValue2 = float( temperatureValue) + 273.15 except: temperatureValue2 = '' Erg += getPaths(temperatureValue, temperatureValue2, temperatureKey, temperatureOffset, temperatureRaw) tick2 = time.time() if Erg: SignalK = '{"updates":[{"$source":"OpenPlotter.I2C.' + name + '","values":[' SignalK += Erg[0:-1] + ']}]}\n' sock.sendto(SignalK.encode('utf-8'), ('127.0.0.1', port)) except Exception as e: print("HTU21D reading failed: " + str(e))
from adafruit_htu21d import HTU21D # Create library object using our Bus I2C port i2c = busio.I2C(board.SCL, board.SDA) sensor_i2c_address = None scanning_frequency = 5 reading_frequency = 1 while True: print("\nScanning for I2C device address to appear\n") sensor_i2c_address = next(iter(i2c.scan()), None) if sensor_i2c_address: print(f"\nFound sensor address at {sensor_i2c_address}\n") break else: print("\nFailed to detect any connected I2C sensors. " f"Sleeping for {scanning_frequency} seconds before rescan...\n") time.sleep(scanning_frequency) sensor = HTU21D(i2c, sensor_i2c_address) while True: try: print("\nTemperature: %0.1f C" % sensor.temperature) print("Relative Humidity: %0.1f %%" % sensor.relative_humidity) except ValueError as e: print(f"Error while reading sensor: {e}\n") time.sleep(reading_frequency)
#smbus2 : https://pypi.org/project/smbus2/ #RTC : https://datasheets.maximintegrated.com/en/ds/DS3231.pdf #Sensor : https://cdn-shop.adafruit.com/datasheets/1899_HTU21D.pdf from smbus2 import SMBus #pip install smbus2 import blynklib #sudo pip install blynklib import board from adafruit_htu21d import HTU21D #pip install adafruit-circuitpython-htu21d BlynkAuth = 'yfW9zija6MMG9q5DKPBM8JRalVZPX845' blynk = blynklib.Blynk(BlynkAuth) i2c = SMBus(1) #número de bus i2c device = {"HTU21D": 0x40, "DS3231" : 0x68 } sensor = HTU21D(board.I2C()) setTemp = 0 temp = 0 pins = {"Temp" : 0, "Humidity" : 2, "Hours" :10, "Minutes" : 11, "Seconds" : 12, "Heating" : 3, "Cooling" : 4 } def readSensor(): return[sensor.temperature,sensor.relative_humidity]
def HTU21DF_get_data(): i2c = busio.I2C(board.SCL, board.SDA) sensor = HTU21D(i2c) temp = c_to_f(sensor.temperature) humidity = sensor.relative_humidity return {'time': time.time(), 'temp': temp, 'humidity': humidity}