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.

203 lines
6.2 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. #if I2C_USE_BRZO
  10. #include <brzo_i2c.h>
  11. #else
  12. #include <Wire.h>
  13. #endif
  14. #define SI7021_SCL_FREQUENCY 200
  15. #define SI7021_CHIP_SI7021 0x15
  16. #define SI7021_CHIP_HTU21D 0x32
  17. #define SI7021_CMD_TMP_HOLD 0xE3
  18. #define SI7021_CMD_HUM_HOLD 0xE5
  19. #define SI7021_CMD_TMP_NOHOLD 0xF3
  20. #define SI7021_CMD_HUM_NOHOLD 0xF5
  21. PROGMEM const char si7021_chip_si7021_name[] = "SI7021";
  22. PROGMEM const char si7021_chip_htu21d_name[] = "HTU21D";
  23. class SI7021Sensor : public I2CSensor {
  24. public:
  25. // ---------------------------------------------------------------------
  26. // Public
  27. // ---------------------------------------------------------------------
  28. SI7021Sensor(): I2CSensor() {
  29. _sensor_id = SENSOR_SI7021_ID;
  30. }
  31. // ---------------------------------------------------------------------
  32. // Sensor API
  33. // ---------------------------------------------------------------------
  34. // Initialization method, must be idempotent
  35. void begin() {
  36. if (!_dirty) return;
  37. _dirty = false;
  38. // I2C auto-discover
  39. unsigned char addresses[] = {0x40};
  40. _address = _begin_i2c(_address, sizeof(addresses), addresses);
  41. if (_address == 0) return;
  42. // Initialize sensor
  43. _init();
  44. }
  45. // Descriptive name of the sensor
  46. String description() {
  47. char name[10];
  48. strncpy_P(name,
  49. _chip == SI7021_CHIP_SI7021 ?
  50. si7021_chip_si7021_name :
  51. si7021_chip_htu21d_name,
  52. sizeof(name)
  53. );
  54. char buffer[25];
  55. snprintf(buffer, sizeof(buffer), "%s @ I2C (0x%02X)", name, _address);
  56. return String(buffer);
  57. }
  58. // Descriptive name of the slot # index
  59. String slot(unsigned char index) {
  60. return description();
  61. };
  62. // Type for slot # index
  63. unsigned char type(unsigned char index) {
  64. _error = SENSOR_ERROR_OK;
  65. if (index == 0) return MAGNITUDE_TEMPERATURE;
  66. if (index == 1) return MAGNITUDE_HUMIDITY;
  67. _error = SENSOR_ERROR_OUT_OF_RANGE;
  68. return MAGNITUDE_NONE;
  69. }
  70. // Pre-read hook (usually to populate registers with up-to-date data)
  71. void pre() {
  72. _error = SENSOR_ERROR_UNKNOWN_ID;
  73. if (_chip == 0) return;
  74. _error = SENSOR_ERROR_OK;
  75. double value;
  76. value = _read(SI7021_CMD_TMP_NOHOLD);
  77. if (_error != SENSOR_ERROR_OK) return;
  78. _temperature = (175.72 * value / 65536) - 46.85;
  79. value = _read(SI7021_CMD_HUM_NOHOLD);
  80. if (_error != SENSOR_ERROR_OK) return;
  81. value = (125.0 * value / 65536) - 6;
  82. _humidity = constrain(value, 0, 100);
  83. }
  84. // Current value for slot # index
  85. double value(unsigned char index) {
  86. _error = SENSOR_ERROR_OK;
  87. if (index == 0) return _temperature;
  88. if (index == 1) return _humidity;
  89. _error = SENSOR_ERROR_OUT_OF_RANGE;
  90. return 0;
  91. }
  92. protected:
  93. // ---------------------------------------------------------------------
  94. // Protected
  95. // ---------------------------------------------------------------------
  96. void _init() {
  97. // Check device
  98. #if I2C_USE_BRZO
  99. uint8_t buffer[2] = {0xFC, 0xC9};
  100. brzo_i2c_start_transaction(_address, SI7021_SCL_FREQUENCY);
  101. brzo_i2c_write(buffer, 2, false);
  102. brzo_i2c_read(buffer, 1, false);
  103. brzo_i2c_end_transaction();
  104. _chip = buffer[0];
  105. #else
  106. Wire.beginTransmission(_address);
  107. Wire.write(0xFC);
  108. Wire.write(0xC9);
  109. Wire.endTransmission();
  110. Wire.requestFrom(_address, (unsigned char) 1);
  111. _chip = Wire.read();
  112. #endif
  113. if ((_chip != SI7021_CHIP_SI7021) & (_chip != SI7021_CHIP_HTU21D)) {
  114. i2cReleaseLock(_address);
  115. _error = SENSOR_ERROR_UNKNOWN_ID;
  116. _count = 0;
  117. } else {
  118. _count = 2;
  119. }
  120. }
  121. unsigned int _read(uint8_t command) {
  122. unsigned char bytes = (command == 0xE0) ? 2 : 3;
  123. #if I2C_USE_BRZO
  124. uint8_t buffer[2] = {command, 0x00};
  125. brzo_i2c_start_transaction(_address, SI7021_SCL_FREQUENCY);
  126. brzo_i2c_write(buffer, 1, false);
  127. #else
  128. Wire.beginTransmission(_address);
  129. Wire.write(command);
  130. Wire.endTransmission();
  131. #endif
  132. // When not using clock stretching (*_NOHOLD commands) delay here
  133. // is needed to wait for the measurement.
  134. // According to datasheet the max. conversion time is ~22ms
  135. unsigned long start = millis();
  136. while (millis() - start < 50) delay(1);
  137. #if I2C_USE_BRZO
  138. brzo_i2c_read(buffer, 2, false);
  139. brzo_i2c_end_transaction();
  140. unsigned int msb = buffer[0];
  141. unsigned int lsb = buffer[1];
  142. #else
  143. Wire.requestFrom(_address, bytes);
  144. unsigned int msb = Wire.read();
  145. unsigned int lsb = Wire.read();
  146. #endif
  147. // Clear the last to bits of LSB to 00.
  148. // According to datasheet LSB of RH is always xxxxxx10
  149. lsb &= 0xFC;
  150. unsigned int value = (msb << 8) | lsb;
  151. _error = SENSOR_ERROR_OK;
  152. return value;
  153. }
  154. unsigned char _chip;
  155. double _temperature = 0;
  156. double _humidity = 0;
  157. };
  158. #endif // SENSOR_SUPPORT && SI7021_SUPPORT