File tree Expand file tree Collapse file tree 2 files changed +29
-1
lines changed
Expand file tree Collapse file tree 2 files changed +29
-1
lines changed Original file line number Diff line number Diff line change 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
2930SHT31::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//
Original file line number Diff line number Diff line change 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
4444class 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
9596protected:
9697 uint8_t _address;
You can’t perform that action at this time.
0 commit comments