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.

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