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.

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