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.

159 lines
4.7 KiB

  1. // -----------------------------------------------------------------------------
  2. // DHT Sensor
  3. // -----------------------------------------------------------------------------
  4. #pragma once
  5. #include "Arduino.h"
  6. #include "BaseSensor.h"
  7. #if I2C_USE_BRZO
  8. #include <brzo_i2c.h>
  9. #else
  10. #include <Wire.h>
  11. #endif
  12. #define SI7021_SCL_FREQUENCY 200
  13. #define SI7021_CHIP_SI7021 0x15
  14. #define SI7021_CHIP_HTU21D 0x32
  15. #define SI7021_CMD_TMP_HOLD 0xE3
  16. #define SI7021_CMD_HUM_HOLD 0xE5
  17. #define SI7021_CMD_TMP_NOHOLD 0xF3
  18. #define SI7021_CMD_HUM_NOHOLD 0xF5
  19. #define SI7021_ERROR_UNKNOW_CHIP -1
  20. #define SI7021_ERROR_TIMEOUT -2
  21. #define SI7021_ERROR_CRC -3
  22. class SI7021Sensor : public BaseSensor {
  23. public:
  24. SI7021Sensor(unsigned char address = 0x40): BaseSensor() {
  25. // Asume I2C already started
  26. _address = address;
  27. // Check device
  28. #if I2C_USE_BRZO
  29. uint8_t buffer[2] = {0xFC, 0xC9};
  30. brzo_i2c_start_transaction(_address, SI7021_SCL_FREQUENCY);
  31. brzo_i2c_write(buffer, 2, false);
  32. brzo_i2c_read(buffer, 1, false);
  33. brzo_i2c_end_transaction();
  34. _chip = buffer[0];
  35. #else
  36. Wire.beginTransmission(_address);
  37. Wire.write(0xFC);
  38. Wire.write(0xC9);
  39. Wire.endTransmission();
  40. Wire.requestFrom(_address, (unsigned char) 1);
  41. _chip = Wire.read();
  42. #endif
  43. if ((_chip != SI7021_CHIP_SI7021) & (_chip != SI7021_CHIP_HTU21D)) {
  44. _error = SI7021_ERROR_UNKNOW_CHIP;
  45. } else {
  46. _count = 2;
  47. }
  48. }
  49. // Descriptive name of the sensor
  50. String name() {
  51. char buffer[20];
  52. snprintf(buffer, sizeof(buffer), "%s @ I2C (0x%02X)", chipAsString().c_str(), _address);
  53. return String(buffer);
  54. }
  55. // Descriptive name of the slot # index
  56. String slot(unsigned char index) {
  57. return name();
  58. }
  59. // Type for slot # index
  60. magnitude_t type(unsigned char index) {
  61. if (index < _count) {
  62. _error = SENSOR_ERROR_OK;
  63. if (index == 0) return MAGNITUDE_TEMPERATURE;
  64. if (index == 1) return MAGNITUDE_HUMIDITY;
  65. }
  66. _error = SENSOR_ERROR_OUT_OF_RANGE;
  67. return MAGNITUDE_NONE;
  68. }
  69. // Current value for slot # index
  70. double value(unsigned char index) {
  71. if (index < _count) {
  72. _error = SENSOR_ERROR_OK;
  73. double value;
  74. if (index == 0) {
  75. value = read(SI7021_CMD_TMP_NOHOLD);
  76. value = (175.72 * value / 65536) - 46.85;
  77. }
  78. if (index == 1) {
  79. value = read(SI7021_CMD_HUM_NOHOLD);
  80. value = (125.0 * value / 65536) - 6;
  81. value = constrain(value, 0, 100);
  82. }
  83. return value;
  84. }
  85. _error = SENSOR_ERROR_OUT_OF_RANGE;
  86. return 0;
  87. }
  88. protected:
  89. unsigned int read(uint8_t command) {
  90. unsigned char bytes = (command == 0xE0) ? 2 : 3;
  91. #if I2C_USE_BRZO
  92. #else
  93. Wire.beginTransmission(_address);
  94. Wire.write(command);
  95. Wire.endTransmission();
  96. #endif
  97. // When not using clock stretching (*_NOHOLD commands) delay here
  98. // is needed to wait for the measurement.
  99. // According to datasheet the max. conversion time is ~22ms
  100. unsigned long start = millis();
  101. while (millis() - start < 50) delay(1);
  102. #if I2C_USE_BRZO
  103. unsigned int msb = 0;
  104. unsigned int lsb = 0;
  105. #else
  106. Wire.requestFrom(_address, bytes);
  107. if (Wire.available() != bytes) return 100;
  108. unsigned int msb = Wire.read();
  109. unsigned int lsb = Wire.read();
  110. #endif
  111. // Clear the last to bits of LSB to 00.
  112. // According to datasheet LSB of RH is always xxxxxx10
  113. lsb &= 0xFC;
  114. unsigned int value = (msb << 8) | lsb;
  115. return value;
  116. }
  117. unsigned char chip() {
  118. return _chip;
  119. }
  120. String chipAsString() {
  121. if (_chip == SI7021_CHIP_SI7021) return String("SI7021");
  122. if (_chip == SI7021_CHIP_HTU21D) return String("HTU21D");
  123. return String("Unknown");
  124. }
  125. unsigned char _address;
  126. unsigned char _chip;
  127. bool _found = false;
  128. };