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.

170 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. _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. if (index == 0) return MAGNITUDE_TEMPERATURE;
  60. if (index == 1) return MAGNITUDE_HUMIDITY;
  61. return MAGNITUDE_NONE;
  62. }
  63. // Pre-read hook (usually to populate registers with up-to-date data)
  64. void pre() {
  65. _error = SENSOR_ERROR_UNKNOWN_ID;
  66. if (_chip == 0) return;
  67. _error = SENSOR_ERROR_OK;
  68. double value;
  69. value = _read(SI7021_CMD_TMP_NOHOLD);
  70. if (_error != SENSOR_ERROR_OK) return;
  71. _temperature = (175.72 * value / 65536) - 46.85;
  72. value = _read(SI7021_CMD_HUM_NOHOLD);
  73. if (_error != SENSOR_ERROR_OK) return;
  74. value = (125.0 * value / 65536) - 6;
  75. _humidity = constrain(value, 0, 100);
  76. }
  77. // Current value for slot # index
  78. double value(unsigned char index) {
  79. if (index == 0) return _temperature;
  80. if (index == 1) return _humidity;
  81. return 0;
  82. }
  83. protected:
  84. // ---------------------------------------------------------------------
  85. // Protected
  86. // ---------------------------------------------------------------------
  87. void _init() {
  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. }
  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