Skip to content

Commit e35fd90

Browse files
committed
reading pressure constants and checking CRC
1 parent e7c9196 commit e35fd90

File tree

2 files changed

+106
-5
lines changed

2 files changed

+106
-5
lines changed

adafruit_ms8607.py

Lines changed: 105 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,10 @@
4545
_MS8607_READ_SERIAL_LAST_6BYTES_COMMAND = const(0xFCC9) #
4646
_MS8607_WRITE_USER_REG_COMMAND = const(0xE6) #
4747
_MS8607_READ_USER_REG_COMMAND = const(0xE7) #
48+
_MS8607_PRESSURE_CALIB_ROM_ADDR = const(0xA0) # 16-bit registers through 0xAE
4849

50+
51+
_MS8607_PTSENSOR_ADDR = const(0x76) #
4952
_MS8607_HSENSOR_ADDR = const(0x40) #
5053
_MS8607_COEFF_MUL = const(125) #
5154
_MS8607_COEFF_ADD = const(-6) #
@@ -69,23 +72,91 @@ class MS8607Humidity:
6972

7073
def __init__(self, i2c_bus):
7174

72-
self.i2c_device = i2c_device.I2CDevice(i2c_bus, _MS8607_HSENSOR_ADDR)
75+
self.humidity_i2c_device = i2c_device.I2CDevice(i2c_bus, _MS8607_HSENSOR_ADDR)
76+
self.pressure_i2c_device = i2c_device.I2CDevice(i2c_bus, _MS8607_PTSENSOR_ADDR)
7377

74-
# self.reset()
75-
# self.initialize()
7678
self._buffer = bytearray(3)
79+
self.reset()
80+
self.initialize()
81+
82+
def reset(self):
83+
"""Reset the sensor to an initial unconfigured state"""
84+
85+
def initialize(self):
86+
"""Configure the sensors with the default settings and state.
87+
For use after calling `reset()`
88+
"""
89+
constants = []
90+
91+
for i in range(7):
92+
offset = 2 * i
93+
self._buffer[0] = _MS8607_PRESSURE_CALIB_ROM_ADDR + offset
94+
with self.pressure_i2c_device as i2c:
95+
i2c.write_then_readinto(
96+
self._buffer,
97+
self._buffer,
98+
out_start=0,
99+
out_end=1,
100+
in_start=0,
101+
in_end=2,
102+
)
103+
104+
# print("got calibration constant %d(%s)"%(calibration_const, hex(calibration_const)))
105+
constants.extend(unpack_from(">H", self._buffer[0:2]))
106+
107+
crc_value = (constants[0] & 0xF000) >> 12
108+
constants.append(0)
109+
if not self._check_press_calibration_crc(constants, crc_value):
110+
raise RuntimeError("CRC Error reading humidity calibration constants")
111+
112+
self.press_sens = constants[1]
113+
self.press_offset = constants[2]
114+
self.press_sens_temp_coeff = constants[3]
115+
self.press_offset_temp_coeff = constants[4]
116+
self.ref_temp = constants[5]
117+
self.temp_temp_coeff = constants[6]
118+
# psensor_resolution_osr = MS8607_PRESSURE_RESOLUTION_OSR_8192
119+
print("self.press_sens", self.press_sens)
120+
print("self.press_offset", self.press_offset)
121+
122+
print("self.press_sens_temp_coeff", self.press_sens_temp_coeff)
123+
print("self.press_offset_temp_coeff", self.press_offset_temp_coeff)
124+
print("self.ref_temp", self.ref_temp)
125+
print("self.temp_temp_coeff", self.temp_temp_coeff)
126+
127+
@property
128+
def temperature(self):
129+
"""The current temperature in degrees Celcius"""
130+
return 5
131+
# # self._buffer.clear()
132+
# self._buffer[0] = _MS8607_READ_HUMIDITY_WO_HOLD_COMMAND
133+
# with self.i2c_device as i2c:
134+
# i2c.write(self._buffer, end=1)
135+
# sleep(0.1) # _i2cPort->requestFrom((uint8_t)MS8607_HSENSOR_ADDR, 3U)
136+
137+
# with self.i2c_device as i2c:
138+
# i2c.readinto(self._buffer, end=3)
139+
140+
# # _adc = (buffer[0] << 8) | buffer[1]
141+
# # crc = buffer[2]
142+
# raw_humidity = unpack_from(">H", self._buffer)[0]
143+
# crc_value = unpack_from(">B", self._buffer, offset=2)[0]
144+
# humidity = (raw_humidity * (_MS8607_COEFF_MUL / (1 << 16))) + _MS8607_COEFF_ADD
145+
# if not self._check_humidity_crc(raw_humidity, crc_value):
146+
# raise RuntimeError("CRC Error reading humidity data")
147+
# return humidity
77148

78149
@property
79150
def relative_humidity(self):
80151
"""The current relative humidity in % rH"""
81152

82153
# self._buffer.clear()
83154
self._buffer[0] = _MS8607_READ_HUMIDITY_WO_HOLD_COMMAND
84-
with self.i2c_device as i2c:
155+
with self.humidity_i2c_device as i2c:
85156
i2c.write(self._buffer, end=1)
86157
sleep(0.1) # _i2cPort->requestFrom((uint8_t)MS8607_HSENSOR_ADDR, 3U)
87158

88-
with self.i2c_device as i2c:
159+
with self.humidity_i2c_device as i2c:
89160
i2c.readinto(self._buffer, end=3)
90161

91162
# _adc = (buffer[0] << 8) | buffer[1]
@@ -122,6 +193,35 @@ def _check_humidity_crc(value, crc):
122193
return True
123194
return False
124195

196+
@staticmethod
197+
def _check_press_calibration_crc(calibration_int16s, crc):
198+
cnt = 0
199+
n_rem = 0
200+
n_rem = 0
201+
crc_read = calibration_int16s[0]
202+
calibration_int16s[7] = 0
203+
calibration_int16s[0] = 0x0FFF & (calibration_int16s[0]) # Clear the CRC byte
204+
205+
for cnt in range(16):
206+
# Get next byte
207+
208+
if cnt % 2 == 1:
209+
n_rem ^= calibration_int16s[cnt >> 1] & 0x00FF
210+
else:
211+
n_rem ^= calibration_int16s[cnt >> 1] >> 8
212+
213+
for _i in range(8, 0, -1):
214+
if n_rem & 0x8000:
215+
n_rem = (n_rem << 1) ^ 0x3000
216+
else:
217+
n_rem <<= 1
218+
# we have to restrict to 16 bits
219+
n_rem &= 0xFFFF
220+
221+
n_rem >>= 12
222+
calibration_int16s[0] = crc_read
223+
return n_rem == crc
224+
125225

126226
# read_temperature_pressure_humidity(float *t, float *p,
127227

examples/ms8607_simpletest.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@
1111
while True:
1212

1313
print("%.2f %% rH" % sensor.relative_humidity)
14+
print("%.2f C" % sensor.temperature)
1415
print("\n------------------------------------------------")
1516
sleep(1)

0 commit comments

Comments
 (0)