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.

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