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.

125 lines
3.8 KiB

  1. // -----------------------------------------------------------------------------
  2. // SHT3X Sensor over I2C (Wemos)
  3. // Copyright (C) 2017-2018 by Xose Pérez <xose dot perez at gmail dot com>
  4. // -----------------------------------------------------------------------------
  5. #if SENSOR_SUPPORT && SHT3X_I2C_SUPPORT
  6. #pragma once
  7. #include "Arduino.h"
  8. #include "I2CSensor.h"
  9. #if I2C_USE_BRZO
  10. #include <brzo_i2c.h>
  11. #else
  12. #include <Wire.h>
  13. #endif
  14. class SHT3XI2CSensor : public I2CSensor {
  15. public:
  16. // ---------------------------------------------------------------------
  17. // Public
  18. // ---------------------------------------------------------------------
  19. SHT3XI2CSensor(): I2CSensor() {
  20. _sensor_id = SENSOR_SHT3X_I2C_ID;
  21. _count = 2;
  22. }
  23. // ---------------------------------------------------------------------
  24. // Sensor API
  25. // ---------------------------------------------------------------------
  26. // Initialization method, must be idempotent
  27. void begin() {
  28. if (!_dirty) return;
  29. _dirty = false;
  30. // I2C auto-discover
  31. unsigned char addresses[] = {0x45};
  32. _address = _begin_i2c(_address, sizeof(addresses), addresses);
  33. if (_address == 0) return;
  34. }
  35. // Descriptive name of the sensor
  36. String description() {
  37. char buffer[25];
  38. snprintf(buffer, sizeof(buffer), "SHT3X @ I2C (0x%02X)", _address);
  39. return String(buffer);
  40. }
  41. // Type for slot # index
  42. unsigned char type(unsigned char index) {
  43. if (index < _count) {
  44. _error = SENSOR_ERROR_OK;
  45. if (index == 0) return MAGNITUDE_TEMPERATURE;
  46. if (index == 1) return MAGNITUDE_HUMIDITY;
  47. }
  48. _error = SENSOR_ERROR_OUT_OF_RANGE;
  49. return MAGNITUDE_NONE;
  50. }
  51. // Pre-read hook (usually to populate registers with up-to-date data)
  52. void pre() {
  53. _error = SENSOR_ERROR_OK;
  54. unsigned char buffer[6];
  55. #if I2C_USE_BRZO
  56. buffer[0] = 0x2C;
  57. buffer[1] = 0x06;
  58. brzo_i2c_start_transaction(_address, I2C_SCL_FREQUENCY);
  59. brzo_i2c_write(buffer, 2, false);
  60. #else
  61. Wire.beginTransmission(_address);
  62. Wire.write(0x2C);
  63. Wire.write(0x06);
  64. if (Wire.endTransmission() != 0) {
  65. _error = SENSOR_ERROR_TIMEOUT;
  66. return;
  67. }
  68. #endif
  69. delay(500);
  70. #if I2C_USE_BRZO
  71. brzo_i2c_read(buffer, 6, false);
  72. brzo_i2c_end_transaction();
  73. #else
  74. Wire.requestFrom(_address, (unsigned char) 6);
  75. for (int i=0; i<6; i++) buffer[i] = Wire.read();
  76. delay(50);
  77. if (Wire.available() != 0) {
  78. _error = SENSOR_ERROR_CRC;
  79. return;
  80. }
  81. #endif
  82. // cTemp msb, cTemp lsb, cTemp crc, humidity msb, humidity lsb, humidity crc
  83. _temperature = ((((buffer[0] * 256.0) + buffer[1]) * 175) / 65535.0) - 45;
  84. _humidity = ((((buffer[3] * 256.0) + buffer[4]) * 100) / 65535.0);
  85. }
  86. // Current value for slot # index
  87. double value(unsigned char index) {
  88. _error = SENSOR_ERROR_OK;
  89. if (index == 0) return _temperature;
  90. if (index == 1) return _humidity;
  91. _error = SENSOR_ERROR_OUT_OF_RANGE;
  92. return 0;
  93. }
  94. protected:
  95. double _temperature = 0;
  96. unsigned char _humidity = 0;
  97. };
  98. #endif // SENSOR_SUPPORT && SHT3X_I2C_SUPPORT