Skip to content

Commit 93c3863

Browse files
authored
Add getSerialNumber() (#45)
* getSerialNumber() implementation * Add getSerialNumber()
1 parent 6bc4522 commit 93c3863

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

SHT31.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#define SHT31_HEAT_OFF 0x3066
2626
#define SHT31_HEATER_TIMEOUT 180000UL // milliseconds
2727

28+
static constexpr uint16_t SHT31_GET_SERIAL_NUMBER = 0x3682; // no clock stretching
2829

2930
SHT31::SHT31(uint8_t address, TwoWire *wire)
3031
{
@@ -281,6 +282,32 @@ int SHT31::getError()
281282
return rv;
282283
}
283284

285+
/**
286+
* See https://sensirion.com/media/documents/E5762713/63D103C2/Sensirion_electronic_identification_code_SHT3x.pdf
287+
*/
288+
bool SHT31::getSerialNumber(uint32_t &serial, bool fast) {
289+
if (writeCmd(SHT31_GET_SERIAL_NUMBER) == false) {
290+
return false;
291+
}
292+
delay(1);
293+
uint8_t buffer[6];
294+
if (readBytes(6, &buffer[0]) == false) {
295+
return false;
296+
}
297+
298+
if (!fast) {
299+
if (buffer[2] != crc8(buffer, 2)) {
300+
_error = SHT31_ERR_SERIAL_NUMBER_CRC;
301+
return false;
302+
}
303+
if (buffer[5] != crc8(buffer + 3, 2)) {
304+
_error = SHT31_ERR_SERIAL_NUMBER_CRC;
305+
return false;
306+
}
307+
}
308+
serial = (buffer[0] << 24) | (buffer[1] << 16) | (buffer[3] << 8) | buffer[4];
309+
return true;
310+
}
284311

285312
/////////////////////////////////////////////////////////////////
286313
//

SHT31.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
#define SHT31_ERR_CRC_STATUS 0x87
4040
#define SHT31_ERR_HEATER_COOLDOWN 0x88
4141
#define SHT31_ERR_HEATER_ON 0x89
42-
42+
#define SHT31_ERR_SERIAL_NUMBER_CRC 0x8A
4343

4444
class SHT31
4545
{
@@ -91,6 +91,7 @@ class SHT31
9191
bool readData(bool fast = true);
9292

9393
int getError(); // clears error flag
94+
bool getSerialNumber(uint32_t &serial, bool fast=true);
9495

9596
protected:
9697
uint8_t _address;

0 commit comments

Comments
 (0)