同步测试 sht3X、sht2X 两个的温湿度模块
顺手打发时间,写了一下模块的测试,这次速度较块,基本半小时就写完了,附带一下代码。
from machine import Pin, I2C
# construct an I2C bus
# i2c = I2C(scl=Pin(22), sda=Pin(21), freq=100000)
# while True:
# print(i2c.scan())
import sys
import utime
from machine import Pin
from machine import I2C
class SHT3x_Sensor:
def __init__(self, freq, sdapin, sclpin):
self.i2c = I2C(freq=freq, sda=Pin(sdapin), scl=Pin(sclpin))
addrs = self.i2c.scan()
if not addrs:
raise Exception('no SHT3X found at bus on SDA pin %d SCL pin %d' % (sdapin, sclpin))
self.addr = addrs.pop()
def read_temp_humd(self):
status = self.i2c.writeto(self.addr,b'\x24\x00')
# delay (20 slow)
utime.sleep(1)
# read 6 bytes
databytes = self.i2c.readfrom(self.addr, 6)
dataset = [databytes[0],databytes[1]]
dataset = [databytes[3],databytes[4]]
temperature_raw = databytes[0] << 8 | databytes[1]
temperature = (175.0 * float(temperature_raw) / 65535.0) - 45
# fahreheit
# temperature = (315.0 * float(temperature_raw) / 65535.0) - 49
humidity_raw = databytes[3] << 8 | databytes[4]
humidity = (100.0 * float(humidity_raw) / 65535.0)
sensor_data = [temperature, humidity]
return sensor_data
import time
class SHT25:
i2c = []
ADDR = 64
CMD_READ_TEMPERATURE = 0xF3
CMD_READ_HUMIDITY = 0xF5
CMD_READ_REGISTER = 0xE7
CMD_WRITE_REGISTER = 0xE6
CMD_RESET = 0xFE
def __init__(self, _i2c):
self.i2c = _i2c
def toTemperature(self, buf):
return -46.85 + 175.72 * ((buf[0] << 8) + buf[1]) /2**16
def toHumidity(self, buf):
return -6 + 125.0 * ((buf[0] << 8) + buf[1]) / 2**16
def decodeUserReg(self, buf):
reg = buf[0]
ret = []
if(0b10000001 & reg == 0b10000001):
ret.append("11bits")
elif(0b10000001 & reg == 0b10000000):
ret.append("RH 10bit T 13bit")
elif(0b10000001 & reg == 0b00000001):
ret.append("RH 8bit T 12bit")
elif(0b10000001 & reg == 0b00000000):
ret.append("RH 12bit T 14bit")
if(0b01000000 & reg == 0b01000000):
ret.append("VDD < 2.5")
else:
ret.append("VDD > 2.5")
if(0b00000100 & reg == 0b00000100):
ret.append("heater ON")
else:
ret.append("heater OFF")
if(0b00000010 & reg == 0b00000010):
ret.append("OTP reload disabled")
else:
ret.append("OTP reload enabled")
return ret
def runI2CCommand(self, command, bytesToRead):
b = bytearray(1)
b[0] = command
self.i2c.writeto(self.ADDR, b)
if(bytesToRead > 0):
recv = bytearray(bytesToRead)
retryCounter = 0
done = False
while retryCounter < 15 and not done:
try:
self.i2c.readfrom_into(self.ADDR, recv)
done = True
retryCounter = retryCounter + 1
except:
time.sleep(0.01)
return recv
def getTemperature(self):
return self.toTemperature(self.runI2CCommand(self.CMD_READ_TEMPERATURE, 3))
def getHumidity(self):
return self.toHumidity(self.runI2CCommand(self.CMD_READ_HUMIDITY, 3))
def getUserRegister(self):
return self.decodeUserReg(self.runI2CCommand(self.CMD_READ_REGISTER, 1))
def setUserRegister(self, register):
b = bytearray(2)
b[0] = self.CMD_WRITE_REGISTER
b[1] = register & 0b11000111
self.i2c.writeto(self.ADDR, b)
def reset(self):
self.runI2CCommand(self.CMD_RESET, 0)
while True:
try:
i2c = I2C(scl=Pin(22), sda=Pin(21), freq=100000)
addrs = i2c.scan()
if not addrs:
raise Exception('no SHT2x found at bus on SDA pin %d SCL pin %d' % (21, 22))
sensor = SHT25(i2c)
print("SHT2x Temperature : " + str(sensor.getTemperature()) + ", Humidity : " + str(sensor.getHumidity()))
except Exception as e:
print("SHT2x:", e)
try:
sht3x_sensor = SHT3x_Sensor(freq=100000, sdapin=25, sclpin=26)
measure_data = sht3x_sensor.read_temp_humd()
print("SHT3x Temperature : ", measure_data[0], ", Humidity : ", measure_data[1])
except Exception as e:
print("SHT3x:", e)
time.sleep(1)