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.

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