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.

194 lines
7.1 KiB

  1. // -----------------------------------------------------------------------------
  2. // AM2320 Humidity & Temperature sensor over I2C
  3. // Copyright (C) 2018 by Mustafa Tufan
  4. // -----------------------------------------------------------------------------
  5. #if SENSOR_SUPPORT && AM2320_SUPPORT
  6. #pragma once
  7. #include "I2CSensor.h"
  8. // https://akizukidenshi.com/download/ds/aosong/AM2320.pdf
  9. #define AM2320_I2C_READ_REGISTER_DATA 0x03 // Read one or more data registers
  10. #define AM2320_I2C_WRITE_MULTIPLE_REGISTERS 0x10 // Multiple sets of binary data to write multiple registers
  11. /*
  12. Register | Address | Register | Address | Register | Address | Register | Address
  13. -----------------+---------+--------------------+---------+-------------------------+---------+-----------+--------
  14. High humidity | 0x00 | Model High | 0x08 | Users register a high | 0x10 | Retention | 0x18
  15. Low humidity | 0x01 | Model Low | 0x09 | Users register a low | 0x11 | Retention | 0x19
  16. High temperature | 0x02 | The version number | 0x0A | Users register 2 high | 0x12 | Retention | 0x1A
  17. Low temperature | 0x03 | Device ID(24-31)Bit| 0x0B | Users register 2 low | 0x13 | Retention | 0x1B
  18. Retention | 0x04 | Device ID(24-31)Bit| 0x0C | Retention | 0x14 | Retention | 0x1C
  19. Retention | 0x05 | Device ID(24-31)Bit| 0x0D | Retention | 0x15 | Retention | 0x1D
  20. Retention | 0x06 | Device ID(24-31)Bit| 0x0E | Retention | 0x16 | Retention | 0x1E
  21. Retention | 0x07 | Status Register | 0x0F | Retention | 0x17 | Retention | 0x1F
  22. */
  23. class AM2320Sensor : public I2CSensor<> {
  24. public:
  25. // ---------------------------------------------------------------------
  26. // Public
  27. // ---------------------------------------------------------------------
  28. AM2320Sensor() {
  29. _count = 2;
  30. _sensor_id = SENSOR_AM2320_ID;
  31. }
  32. // ---------------------------------------------------------------------
  33. // Sensor API
  34. // ---------------------------------------------------------------------
  35. // Initialization method, must be idempotent
  36. void begin() {
  37. if (!_dirty) return;
  38. // I2C auto-discover
  39. unsigned char addresses[] = {0x23, 0x5C, 0xB8};
  40. _address = _begin_i2c(_address, sizeof(addresses), addresses);
  41. if (_address == 0) return;
  42. _ready = true;
  43. _dirty = false;
  44. }
  45. // Descriptive name of the sensor
  46. String description() {
  47. char buffer[25];
  48. snprintf(buffer, sizeof(buffer), "AM2320 @ I2C (0x%02X)", _address);
  49. return String(buffer);
  50. }
  51. // Descriptive name of the slot # index
  52. String description(unsigned char index) {
  53. return description();
  54. };
  55. // Type for slot # index
  56. unsigned char type(unsigned char index) {
  57. if (index == 0) return MAGNITUDE_TEMPERATURE;
  58. if (index == 1) return MAGNITUDE_HUMIDITY;
  59. return MAGNITUDE_NONE;
  60. }
  61. // Pre-read hook (usually to populate registers with up-to-date data)
  62. void pre() {
  63. _error = SENSOR_ERROR_OK;
  64. _read();
  65. }
  66. // Current value for slot # index
  67. double value(unsigned char index) {
  68. if (index == 0) return _temperature;
  69. if (index == 1) return _humidity;
  70. return 0;
  71. }
  72. protected:
  73. // ---------------------------------------------------------------------
  74. // Protected
  75. // ---------------------------------------------------------------------
  76. /*
  77. // Get device model, version, device_id
  78. void _init() {
  79. i2c_wakeup(_address);
  80. delayMicroseconds(800);
  81. unsigned char _buffer[11];
  82. // 0x08 = read address
  83. // 7 = number of bytes to read
  84. if (i2c_write_uint8(_address, AM2320_I2C_READ_REGISTER_DATA, 0x08, 7) != I2C_TRANS_SUCCESS) {
  85. _error = SENSOR_ERROR_TIMEOUT;
  86. return false;
  87. }
  88. uint16_t model = (_buffer[2] << 8) | _buffer[3];
  89. uint8_t version = _buffer[4];
  90. uint32_t device_id = _buffer[8] << 24 | _buffer[7] << 16 | _buffer[6] << 8 | _buffer[5];
  91. }
  92. */
  93. void _read() {
  94. i2c_wakeup(_address);
  95. // waiting time of at least 800 μs, the maximum 3000 μs
  96. delayMicroseconds(800); // just to be on safe side
  97. // 0x00 = read address
  98. // 4 = number of bytes to read
  99. if (i2c_write_uint8(_address, AM2320_I2C_READ_REGISTER_DATA, 0x00, 4) != I2C_TRANS_SUCCESS) {
  100. _error = SENSOR_ERROR_TIMEOUT;
  101. return;
  102. }
  103. unsigned char _buffer[8];
  104. // waiting time of at least 800 μs, the maximum 3000 μs
  105. delayMicroseconds(800 + ((3000-800)/2) );
  106. i2c_read_buffer(_address, _buffer, 8);
  107. // Humidity : 01F4 = (1×256)+(F×16)+4 = 500 => humidity = 500÷10 = 50.0 %
  108. // 0339 = (3×256)+(3×16)+9 = 825 => humidity = 825÷10 = 82.5 %
  109. // Temperature: 00FA = (F×16)+A = 250 => temperature = 250÷10 = 25.0 C
  110. // 0115 = (1×256)+(1×16)+5 = 277 => temperature = 277÷10 = 27.7 C
  111. // Temperature resolution is 16Bit, temperature highest bit (Bit 15) is equal to 1 indicates a negative temperature
  112. // _buffer 0 = function code
  113. // _buffer 1 = number of bytes
  114. // _buffer 2-3 = high/low humidity
  115. // _buffer 4-5 = high/low temperature
  116. // _buffer 6-7 = CRC low/high
  117. unsigned int responseCRC = 0;
  118. responseCRC = ((responseCRC | _buffer[7]) << 8 | _buffer[6]);
  119. if (responseCRC == _CRC16(_buffer)) {
  120. int foo = (_buffer[2] << 8) | _buffer[3];
  121. _humidity = foo / 10.0;
  122. foo = ((_buffer[4] & 0x7F) << 8) | _buffer[5]; // clean bit 15 and merge
  123. _temperature = foo / 10.0;
  124. if (_buffer[4] & 0x80) { // is bit 15 == 1
  125. _temperature = _temperature * -1; // negative temperature
  126. }
  127. _error = SENSOR_ERROR_OK;
  128. } else {
  129. _error = SENSOR_ERROR_CRC;
  130. return;
  131. }
  132. }
  133. unsigned int _CRC16(unsigned char buffer[]) {
  134. unsigned int crc16 = 0xFFFF;
  135. for (unsigned int i = 0; i < 6; i++) {
  136. crc16 ^= buffer[i];
  137. for (unsigned int b = 8; b != 0; b--) {
  138. if (crc16 & 0x01) { // is lsb set
  139. crc16 >>= 1;
  140. crc16 ^= 0xA001;
  141. } else {
  142. crc16 >>= 1;
  143. }
  144. }
  145. }
  146. return crc16;
  147. }
  148. double _temperature = 0;
  149. double _humidity = 0;
  150. };
  151. #endif // SENSOR_SUPPORT && AM2320_SUPPORT