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.

189 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. i2cReleaseLock(_address);
  62. _error = SENSOR_ERROR_UNKNOWN_ID;
  63. } else {
  64. _count = 2;
  65. }
  66. }
  67. // Descriptive name of the sensor
  68. String name() {
  69. char buffer[25];
  70. snprintf(buffer, sizeof(buffer), "%s @ I2C (0x%02X)", chipAsString().c_str(), _address);
  71. return String(buffer);
  72. }
  73. // Descriptive name of the slot # index
  74. String slot(unsigned char index) {
  75. return name();
  76. }
  77. // Type for slot # index
  78. magnitude_t type(unsigned char index) {
  79. if (index < _count) {
  80. _error = SENSOR_ERROR_OK;
  81. if (index == 0) return MAGNITUDE_TEMPERATURE;
  82. if (index == 1) return MAGNITUDE_HUMIDITY;
  83. }
  84. _error = SENSOR_ERROR_OUT_OF_RANGE;
  85. return MAGNITUDE_NONE;
  86. }
  87. // Current value for slot # index
  88. double value(unsigned char index) {
  89. if (index < _count) {
  90. _error = SENSOR_ERROR_OK;
  91. double value;
  92. if (index == 0) {
  93. value = read(SI7021_CMD_TMP_NOHOLD);
  94. value = (175.72 * value / 65536) - 46.85;
  95. }
  96. if (index == 1) {
  97. value = read(SI7021_CMD_HUM_NOHOLD);
  98. value = (125.0 * value / 65536) - 6;
  99. value = constrain(value, 0, 100);
  100. }
  101. return value;
  102. }
  103. _error = SENSOR_ERROR_OUT_OF_RANGE;
  104. return 0;
  105. }
  106. protected:
  107. // ---------------------------------------------------------------------
  108. // Protected
  109. // ---------------------------------------------------------------------
  110. unsigned int read(uint8_t command) {
  111. unsigned char bytes = (command == 0xE0) ? 2 : 3;
  112. #if I2C_USE_BRZO
  113. uint8_t buffer[2] = {command, 0x00};
  114. brzo_i2c_start_transaction(_address, SI7021_SCL_FREQUENCY);
  115. brzo_i2c_write(buffer, 1, false);
  116. #else
  117. Wire.beginTransmission(_address);
  118. Wire.write(command);
  119. Wire.endTransmission();
  120. #endif
  121. // When not using clock stretching (*_NOHOLD commands) delay here
  122. // is needed to wait for the measurement.
  123. // According to datasheet the max. conversion time is ~22ms
  124. unsigned long start = millis();
  125. while (millis() - start < 50) delay(1);
  126. #if I2C_USE_BRZO
  127. brzo_i2c_read(buffer, 2, false);
  128. brzo_i2c_end_transaction();
  129. unsigned int msb = buffer[0];
  130. unsigned int lsb = buffer[1];
  131. #else
  132. Wire.requestFrom(_address, bytes);
  133. if (Wire.available() != bytes) {
  134. _error = SENSOR_ERROR_CRC;
  135. return 0;
  136. }
  137. unsigned int msb = Wire.read();
  138. unsigned int lsb = Wire.read();
  139. #endif
  140. // Clear the last to bits of LSB to 00.
  141. // According to datasheet LSB of RH is always xxxxxx10
  142. lsb &= 0xFC;
  143. unsigned int value = (msb << 8) | lsb;
  144. _error = SENSOR_ERROR_OK;
  145. return value;
  146. }
  147. String chipAsString() {
  148. if (_chip == SI7021_CHIP_SI7021) return String("SI7021");
  149. if (_chip == SI7021_CHIP_HTU21D) return String("HTU21D");
  150. return String("Unknown");
  151. }
  152. unsigned char _chip;
  153. };