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.

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