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.

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