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.

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