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.

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