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.

188 lines
6.0 KiB

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