Skip to content

Expose serial number & device type properties #21

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2017 Radomir Dopieralski, written for Adafruit Industries
Copyright (c) 2017 Radomir Dopieralski, written for Adafruit Industries, and (c) 2016 Chris Balmer

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
69 changes: 68 additions & 1 deletion adafruit_si7021.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

This is a CircuitPython driver for the SI7021 temperature and humidity sensor.

* Author(s): Radomir Dopieralski
* Author(s): Radomir Dopieralski, Chris Balmer, Ian Grant

Implementation Notes
--------------------
Expand Down Expand Up @@ -40,6 +40,8 @@
_RESET = const(0xFE)
_READ_USER1 = const(0xE7)
_USER1_VAL = const(0x3A)
_ID1_CMD = bytearray([0xFA, 0x0F])
_ID2_CMD = bytearray([0xFC, 0xC9])


def _crc(data):
Expand All @@ -55,6 +57,36 @@ def _crc(data):
return crc


def _convert_to_integer(bytes_to_convert):
"""Use bitwise operators to convert the bytes into integers."""
integer = None
for chunk in bytes_to_convert:
if not integer:
integer = chunk
else:
integer = integer << 8
integer = integer | chunk
return integer


def _get_device_identifier(identifier_byte):
"""
Convert the identifier byte to a device identifier (model type).
Values are based on the information from page 24 of the datasheet.
"""
if identifier_byte in (0x00, 0xFF):
identifier_string = "Engineering sample"
elif identifier_byte == 0x0D:
identifier_string = "Si7013"
elif identifier_byte == 0x14:
identifier_string = "Si7020"
elif identifier_byte == 0x15:
identifier_string = "Si7021"
else:
identifier_string = "Unknown"
return identifier_string


class SI7021:
"""
A driver for the SI7021 temperature and humidity sensor.
Expand Down Expand Up @@ -141,3 +173,38 @@ def start_measurement(self, what):
elif self._measurement != what:
raise RuntimeError("other measurement in progress")
self._measurement = what

@property
def serial_number(self):
"""The device's unique ID (serial number)."""
return self._get_device_info()[0]

@property
def device_identifier(self):
"""A device identifier (model type) string."""
return self._get_device_info()[1]

def _get_device_info(self):
"""
Get the serial number and the sensor identifier (model type).
The identifier is part of the bytes returned for the serial number.
Source: https://github.com/chrisbalmer/micropython-si7021
"""
# Serial 1st half
data = _ID1_CMD
id1 = bytearray(8)
with self.i2c_device as i2c:
i2c.write_then_readinto(data, id1)
# Serial 2nd half
data = _ID2_CMD
id2 = bytearray(6)
with self.i2c_device as i2c:
i2c.write_then_readinto(data, id2)
# Combine the two halves
combined_id = bytearray(
[id1[0], id1[2], id1[4], id1[6], id2[0], id2[1], id2[3], id2[4]]
)
# Convert serial number and extract identifier part
serial = _convert_to_integer(combined_id)
identifier = _get_device_identifier(id2[0])
return serial, identifier