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.

190 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. 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. // 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. };