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.

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