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.

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