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.

123 lines
3.7 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. unsigned char buffer[6];
  54. #if I2C_USE_BRZO
  55. buffer[0] = 0x2C;
  56. buffer[1] = 0x06;
  57. brzo_i2c_start_transaction(_address, I2C_SCL_FREQUENCY);
  58. brzo_i2c_write(buffer, 2, false);
  59. #else
  60. Wire.beginTransmission(_address);
  61. Wire.write(0x2C);
  62. Wire.write(0x06);
  63. if (Wire.endTransmission() != 0) {
  64. _error = SENSOR_ERROR_TIMEOUT;
  65. return;
  66. }
  67. #endif
  68. delay(500);
  69. #if I2C_USE_BRZO
  70. brzo_i2c_read(buffer, 6, false);
  71. brzo_i2c_end_transaction();
  72. #else
  73. Wire.requestFrom(_address, (unsigned char) 6);
  74. for (int i=0; i<6; i++) buffer[i] = Wire.read();
  75. delay(50);
  76. if (Wire.available() != 0) {
  77. _error = SENSOR_ERROR_CRC;
  78. return;
  79. }
  80. #endif
  81. // cTemp msb, cTemp lsb, cTemp crc, humidity msb, humidity lsb, humidity crc
  82. _temperature = ((((buffer[0] * 256.0) + buffer[1]) * 175) / 65535.0) - 45;
  83. _humidity = ((((buffer[3] * 256.0) + buffer[4]) * 100) / 65535.0);
  84. }
  85. // Current value for slot # index
  86. double value(unsigned char index) {
  87. _error = SENSOR_ERROR_OK;
  88. if (index == 0) return _temperature;
  89. if (index == 1) return _humidity;
  90. _error = SENSOR_ERROR_OUT_OF_RANGE;
  91. return 0;
  92. }
  93. protected:
  94. double _temperature = 0;
  95. unsigned char _humidity = 0;
  96. };
  97. #endif // SENSOR_SUPPORT && SHT3X_I2C_SUPPORT