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.

122 lines
3.7 KiB

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