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.

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