diff --git a/sht21.py b/sht21.py index c93372d..2af4e54 100755 --- a/sht21.py +++ b/sht21.py @@ -32,27 +32,27 @@ def __init__(self, device_number=0): loaded). Note that this has only been tested on first revision raspberry pi where the device_number = 0, but it should work where device_number=1""" - self.i2c = open('/dev/i2c-%s' % device_number, 'r+', 0) + self.i2c = open('/dev/i2c-%s' % device_number, 'rb+', 0) fcntl.ioctl(self.i2c, self.I2C_SLAVE, 0x40) - self.i2c.write(chr(self._SOFTRESET)) + self.i2c.write(bytes([self._SOFTRESET])) time.sleep(0.050) def read_temperature(self): """Reads the temperature from the sensor. Not that this call blocks for ~86ms to allow the sensor to return the data""" - self.i2c.write(chr(self._TRIGGER_TEMPERATURE_NO_HOLD)) + self.i2c.write(bytes([self._TRIGGER_TEMPERATURE_NO_HOLD])) time.sleep(self._TEMPERATURE_WAIT_TIME) data = self.i2c.read(3) - if self._calculate_checksum(data, 2) == ord(data[2]): + if self._calculate_checksum(data, 2) == data[2]: return self._get_temperature_from_buffer(data) def read_humidity(self): """Reads the humidity from the sensor. Not that this call blocks for ~30ms to allow the sensor to return the data""" - self.i2c.write(chr(self._TRIGGER_HUMIDITY_NO_HOLD)) + self.i2c.write(bytes([self._TRIGGER_HUMIDITY_NO_HOLD])) time.sleep(self._HUMIDITY_WAIT_TIME) data = self.i2c.read(3) - if self._calculate_checksum(data, 2) == ord(data[2]): + if self._calculate_checksum(data, 2) == data[2]: return self._get_humidity_from_buffer(data) def close(self): @@ -75,7 +75,7 @@ def _calculate_checksum(data, number_of_bytes): crc = 0 # calculates 8-Bit checksum with given polynomial for byteCtr in range(number_of_bytes): - crc ^= (ord(data[byteCtr])) + crc ^= data[byteCtr] for bit in range(8, 0, -1): if crc & 0x80: crc = (crc << 1) ^ POLYNOMIAL @@ -90,7 +90,7 @@ def _get_temperature_from_buffer(data): T = -46.85 + (175.72 * (ST/2^16)) where ST is the value from the sensor """ - unadjusted = (ord(data[0]) << 8) + ord(data[1]) + unadjusted = (data[0] << 8) + data[1] unadjusted &= SHT21._STATUS_BITS_MASK # zero the status bits unadjusted *= 175.72 unadjusted /= 1 << 16 # divide by 2^16 @@ -104,7 +104,7 @@ def _get_humidity_from_buffer(data): RH = -6 + (125 * (SRH / 2 ^16)) where SRH is the value read from the sensor """ - unadjusted = (ord(data[0]) << 8) + ord(data[1]) + unadjusted = (data[0] << 8) + data[1] unadjusted &= SHT21._STATUS_BITS_MASK # zero the status bits unadjusted *= 125.0 unadjusted /= 1 << 16 # divide by 2^16 @@ -134,9 +134,9 @@ def test_checksum(self): if __name__ == "__main__": try: - with SHT21(0) as sht21: - print "Temperature: %s" % sht21.read_temperature() - print "Humidity: %s" % sht21.read_humidity() - except IOError, e: - print e - print "Error creating connection to i2c. This must be run as root" + with SHT21(1) as sht21: + print("Temperature: %s" % sht21.read_temperature()) + print("Humidity: %s" % sht21.read_humidity()) + except IOError as e: + print(type(e), e) + print("Error creating connection to i2c. This must be run as root")