Fork of the espurna firmware for `mhsw` switches
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

166 lines
5.0 KiB

  1. // -----------------------------------------------------------------------------
  2. // SI7021 / HTU21D Sensor over I2C
  3. // Copyright (C) 2017-2018 by Xose Pérez <xose dot perez at gmail dot com>
  4. // -----------------------------------------------------------------------------
  5. #if SENSOR_SUPPORT && SI7021_SUPPORT
  6. #pragma once
  7. #include "Arduino.h"
  8. #include "I2CSensor.h"
  9. #define SI7021_SCL_FREQUENCY 200
  10. #define SI7021_CHIP_SI7021 0x15
  11. #define SI7021_CHIP_HTU21D 0x32
  12. #define SI7021_CMD_TMP_HOLD 0xE3
  13. #define SI7021_CMD_HUM_HOLD 0xE5
  14. #define SI7021_CMD_TMP_NOHOLD 0xF3
  15. #define SI7021_CMD_HUM_NOHOLD 0xF5
  16. PROGMEM const char si7021_chip_si7021_name[] = "SI7021";
  17. PROGMEM const char si7021_chip_htu21d_name[] = "HTU21D";
  18. class SI7021Sensor : public I2CSensor {
  19. public:
  20. // ---------------------------------------------------------------------
  21. // Public
  22. // ---------------------------------------------------------------------
  23. SI7021Sensor(): I2CSensor() {
  24. _sensor_id = SENSOR_SI7021_ID;
  25. }
  26. // ---------------------------------------------------------------------
  27. // Sensor API
  28. // ---------------------------------------------------------------------
  29. // Initialization method, must be idempotent
  30. void begin() {
  31. if (!_dirty) return;
  32. _dirty = false;
  33. // I2C auto-discover
  34. unsigned char addresses[] = {0x40};
  35. _address = _begin_i2c(_address, sizeof(addresses), addresses);
  36. if (_address == 0) return;
  37. // Initialize sensor
  38. _init();
  39. }
  40. // Descriptive name of the sensor
  41. String description() {
  42. char name[10];
  43. strncpy_P(name,
  44. _chip == SI7021_CHIP_SI7021 ?
  45. si7021_chip_si7021_name :
  46. si7021_chip_htu21d_name,
  47. sizeof(name)
  48. );
  49. char buffer[25];
  50. snprintf(buffer, sizeof(buffer), "%s @ I2C (0x%02X)", name, _address);
  51. return String(buffer);
  52. }
  53. // Descriptive name of the slot # index
  54. String slot(unsigned char index) {
  55. return description();
  56. };
  57. // Type for slot # index
  58. unsigned char type(unsigned char index) {
  59. _error = SENSOR_ERROR_OK;
  60. if (index == 0) return MAGNITUDE_TEMPERATURE;
  61. if (index == 1) return MAGNITUDE_HUMIDITY;
  62. _error = SENSOR_ERROR_OUT_OF_RANGE;
  63. return MAGNITUDE_NONE;
  64. }
  65. // Pre-read hook (usually to populate registers with up-to-date data)
  66. void pre() {
  67. _error = SENSOR_ERROR_UNKNOWN_ID;
  68. if (_chip == 0) return;
  69. _error = SENSOR_ERROR_OK;
  70. double value;
  71. value = _read(SI7021_CMD_TMP_NOHOLD);
  72. if (_error != SENSOR_ERROR_OK) return;
  73. _temperature = (175.72 * value / 65536) - 46.85;
  74. value = _read(SI7021_CMD_HUM_NOHOLD);
  75. if (_error != SENSOR_ERROR_OK) return;
  76. value = (125.0 * value / 65536) - 6;
  77. _humidity = constrain(value, 0, 100);
  78. }
  79. // Current value for slot # index
  80. double value(unsigned char index) {
  81. _error = SENSOR_ERROR_OK;
  82. if (index == 0) return _temperature;
  83. if (index == 1) return _humidity;
  84. _error = SENSOR_ERROR_OUT_OF_RANGE;
  85. return 0;
  86. }
  87. protected:
  88. // ---------------------------------------------------------------------
  89. // Protected
  90. // ---------------------------------------------------------------------
  91. void _init() {
  92. // Check device
  93. i2c_write_uint8(_address, 0xFC, 0xC9);
  94. _chip = i2c_read_uint8(_address);
  95. if ((_chip != SI7021_CHIP_SI7021) & (_chip != SI7021_CHIP_HTU21D)) {
  96. i2cReleaseLock(_address);
  97. _error = SENSOR_ERROR_UNKNOWN_ID;
  98. _count = 0;
  99. } else {
  100. _count = 2;
  101. }
  102. }
  103. unsigned int _read(uint8_t command) {
  104. // Request measurement
  105. i2c_write_uint8(_address, command);
  106. // When not using clock stretching (*_NOHOLD commands) delay here
  107. // is needed to wait for the measurement.
  108. // According to datasheet the max. conversion time is ~22ms
  109. unsigned long start = millis();
  110. while (millis() - start < 50) delay(1);
  111. // Clear the last to bits of LSB to 00.
  112. // According to datasheet LSB of RH is always xxxxxx10
  113. unsigned int value = i2c_read_uint16(_address) & 0xFFFC;
  114. // We should be checking there are no pending bytes in the buffer
  115. // and raise a CRC error if there are
  116. _error = SENSOR_ERROR_OK;
  117. return value;
  118. }
  119. unsigned char _chip;
  120. double _temperature = 0;
  121. double _humidity = 0;
  122. };
  123. #endif // SENSOR_SUPPORT && SI7021_SUPPORT