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.

186 lines
5.9 KiB

  1. // -----------------------------------------------------------------------------
  2. // SI7021 / HTU21D Sensor over I2C
  3. // Copyright (C) 2017-2018 by Xose Pérez <xose dot perez at gmail dot com>
  4. // -----------------------------------------------------------------------------
  5. #if SENSOR_SUPPORT && SI7021_SUPPORT
  6. #pragma once
  7. #include "Arduino.h"
  8. #include "I2CSensor.h"
  9. #if I2C_USE_BRZO
  10. #include <brzo_i2c.h>
  11. #else
  12. #include <Wire.h>
  13. #endif
  14. #define SI7021_SCL_FREQUENCY 200
  15. #define SI7021_CHIP_SI7021 0x15
  16. #define SI7021_CHIP_HTU21D 0x32
  17. #define SI7021_CMD_TMP_HOLD 0xE3
  18. #define SI7021_CMD_HUM_HOLD 0xE5
  19. #define SI7021_CMD_TMP_NOHOLD 0xF3
  20. #define SI7021_CMD_HUM_NOHOLD 0xF5
  21. class SI7021Sensor : public I2CSensor {
  22. public:
  23. // ---------------------------------------------------------------------
  24. // Public
  25. // ---------------------------------------------------------------------
  26. SI7021Sensor(): I2CSensor() {
  27. _sensor_id = SENSOR_SI7021_ID;
  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 = _begin_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. i2cReleaseLock(_address);
  58. _error = SENSOR_ERROR_UNKNOWN_ID;
  59. } else {
  60. _count = 2;
  61. }
  62. }
  63. // Descriptive name of the sensor
  64. String description() {
  65. char buffer[25];
  66. snprintf(buffer, sizeof(buffer), "%s @ I2C (0x%02X)", chipAsString().c_str(), _address);
  67. return String(buffer);
  68. }
  69. // Descriptive name of the slot # index
  70. String slot(unsigned char index) {
  71. return description();
  72. };
  73. // Type for slot # index
  74. unsigned char type(unsigned char index) {
  75. if (index < _count) {
  76. _error = SENSOR_ERROR_OK;
  77. if (index == 0) return MAGNITUDE_TEMPERATURE;
  78. if (index == 1) return MAGNITUDE_HUMIDITY;
  79. }
  80. _error = SENSOR_ERROR_OUT_OF_RANGE;
  81. return MAGNITUDE_NONE;
  82. }
  83. // Current value for slot # index
  84. double value(unsigned char index) {
  85. if (index < _count) {
  86. _error = SENSOR_ERROR_OK;
  87. double value;
  88. if (index == 0) {
  89. value = read(SI7021_CMD_TMP_NOHOLD);
  90. value = (175.72 * value / 65536) - 46.85;
  91. }
  92. if (index == 1) {
  93. value = read(SI7021_CMD_HUM_NOHOLD);
  94. value = (125.0 * value / 65536) - 6;
  95. value = constrain(value, 0, 100);
  96. }
  97. return value;
  98. }
  99. _error = SENSOR_ERROR_OUT_OF_RANGE;
  100. return 0;
  101. }
  102. protected:
  103. // ---------------------------------------------------------------------
  104. // Protected
  105. // ---------------------------------------------------------------------
  106. unsigned int read(uint8_t command) {
  107. unsigned char bytes = (command == 0xE0) ? 2 : 3;
  108. #if I2C_USE_BRZO
  109. uint8_t buffer[2] = {command, 0x00};
  110. brzo_i2c_start_transaction(_address, SI7021_SCL_FREQUENCY);
  111. brzo_i2c_write(buffer, 1, false);
  112. #else
  113. Wire.beginTransmission(_address);
  114. Wire.write(command);
  115. Wire.endTransmission();
  116. #endif
  117. // When not using clock stretching (*_NOHOLD commands) delay here
  118. // is needed to wait for the measurement.
  119. // According to datasheet the max. conversion time is ~22ms
  120. unsigned long start = millis();
  121. while (millis() - start < 50) delay(1);
  122. #if I2C_USE_BRZO
  123. brzo_i2c_read(buffer, 2, false);
  124. brzo_i2c_end_transaction();
  125. unsigned int msb = buffer[0];
  126. unsigned int lsb = buffer[1];
  127. #else
  128. Wire.requestFrom(_address, bytes);
  129. if (Wire.available() != bytes) {
  130. _error = SENSOR_ERROR_CRC;
  131. return 0;
  132. }
  133. unsigned int msb = Wire.read();
  134. unsigned int lsb = Wire.read();
  135. #endif
  136. // Clear the last to bits of LSB to 00.
  137. // According to datasheet LSB of RH is always xxxxxx10
  138. lsb &= 0xFC;
  139. unsigned int value = (msb << 8) | lsb;
  140. _error = SENSOR_ERROR_OK;
  141. return value;
  142. }
  143. String chipAsString() {
  144. if (_chip == SI7021_CHIP_SI7021) return String("SI7021");
  145. if (_chip == SI7021_CHIP_HTU21D) return String("HTU21D");
  146. return String("Unknown");
  147. }
  148. unsigned char _chip;
  149. };
  150. #endif // SENSOR_SUPPORT && SI7021_SUPPORT