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.

168 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. #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. _init();
  33. _dirty = !_ready;
  34. }
  35. // Descriptive name of the sensor
  36. String description() {
  37. char name[10];
  38. strncpy_P(name,
  39. _chip == SI7021_CHIP_SI7021 ?
  40. si7021_chip_si7021_name :
  41. si7021_chip_htu21d_name,
  42. sizeof(name)
  43. );
  44. char buffer[25];
  45. snprintf(buffer, sizeof(buffer), "%s @ I2C (0x%02X)", name, _address);
  46. return String(buffer);
  47. }
  48. // Descriptive name of the slot # index
  49. String slot(unsigned char index) {
  50. return description();
  51. };
  52. // Type for slot # index
  53. unsigned char type(unsigned char index) {
  54. if (index == 0) return MAGNITUDE_TEMPERATURE;
  55. if (index == 1) return MAGNITUDE_HUMIDITY;
  56. return MAGNITUDE_NONE;
  57. }
  58. // Pre-read hook (usually to populate registers with up-to-date data)
  59. void pre() {
  60. _error = SENSOR_ERROR_UNKNOWN_ID;
  61. if (_chip == 0) return;
  62. _error = SENSOR_ERROR_OK;
  63. double value;
  64. value = _read(SI7021_CMD_TMP_NOHOLD);
  65. if (_error != SENSOR_ERROR_OK) return;
  66. _temperature = (175.72 * value / 65536) - 46.85;
  67. value = _read(SI7021_CMD_HUM_NOHOLD);
  68. if (_error != SENSOR_ERROR_OK) return;
  69. value = (125.0 * value / 65536) - 6;
  70. _humidity = constrain(value, 0, 100);
  71. }
  72. // Current value for slot # index
  73. double value(unsigned char index) {
  74. if (index == 0) return _temperature;
  75. if (index == 1) return _humidity;
  76. return 0;
  77. }
  78. protected:
  79. // ---------------------------------------------------------------------
  80. // Protected
  81. // ---------------------------------------------------------------------
  82. void _init() {
  83. // I2C auto-discover
  84. unsigned char addresses[] = {0x40};
  85. _address = _begin_i2c(_address, sizeof(addresses), addresses);
  86. if (_address == 0) return;
  87. // Check device
  88. i2c_write_uint8(_address, 0xFC, 0xC9);
  89. _chip = i2c_read_uint8(_address);
  90. if ((_chip != SI7021_CHIP_SI7021) & (_chip != SI7021_CHIP_HTU21D)) {
  91. _count = 0;
  92. i2cReleaseLock(_address);
  93. _previous_address = 0;
  94. _error = SENSOR_ERROR_UNKNOWN_ID;
  95. // Setting _address to 0 forces auto-discover
  96. // This might be necessary at this stage if there is a
  97. // different sensor in the hardcoded address
  98. _address = 0;
  99. } else {
  100. _count = 2;
  101. }
  102. _ready = true;
  103. }
  104. unsigned int _read(uint8_t command) {
  105. // Request measurement
  106. i2c_write_uint8(_address, command);
  107. // When not using clock stretching (*_NOHOLD commands) delay here
  108. // is needed to wait for the measurement.
  109. // According to datasheet the max. conversion time is ~22ms
  110. unsigned long start = millis();
  111. while (millis() - start < 50) delay(1);
  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