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.

169 lines
5.0 KiB

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