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.

162 lines
4.8 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. i2cReleaseLock(_address);
  93. _error = SENSOR_ERROR_UNKNOWN_ID;
  94. _count = 0;
  95. } else {
  96. _count = 2;
  97. }
  98. }
  99. unsigned int _read(uint8_t command) {
  100. // Request measurement
  101. i2c_write_uint8(_address, command);
  102. // When not using clock stretching (*_NOHOLD commands) delay here
  103. // is needed to wait for the measurement.
  104. // According to datasheet the max. conversion time is ~22ms
  105. unsigned long start = millis();
  106. while (millis() - start < 50) delay(1);
  107. // Clear the last to bits of LSB to 00.
  108. // According to datasheet LSB of RH is always xxxxxx10
  109. unsigned int value = i2c_read_uint16(_address) & 0xFFFC;
  110. // We should be checking there are no pending bytes in the buffer
  111. // and raise a CRC error if there are
  112. _error = SENSOR_ERROR_OK;
  113. return value;
  114. }
  115. unsigned char _chip;
  116. double _temperature = 0;
  117. double _humidity = 0;
  118. };
  119. #endif // SENSOR_SUPPORT && SI7021_SUPPORT