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.

234 lines
7.2 KiB

  1. // -----------------------------------------------------------------------------
  2. // DHT Sensor
  3. // -----------------------------------------------------------------------------
  4. #pragma once
  5. #include "Arduino.h"
  6. #include "BaseSensor.h"
  7. #include <vector>
  8. #include <OneWire.h>
  9. #define DS_CHIP_DS18S20 0x10
  10. #define DS_CHIP_DS1822 0x22
  11. #define DS_CHIP_DS18B20 0x28
  12. #define DS_CHIP_DS1825 0x3B
  13. #define DS_PARASITE 1
  14. #define DS_DISCONNECTED -127
  15. #define DS_CMD_START_CONVERSION 0x44
  16. #define DS_CMD_READ_SCRATCHPAD 0xBE
  17. #define DS_ERROR_NOT_FOUND -1
  18. #define DS_ERROR_FAILED_RESET -2
  19. #define DS_ERROR_FAILED_READ -3
  20. #define DS_ERROR_CRC -4
  21. class DallasSensor : public BaseSensor {
  22. public:
  23. DallasSensor(unsigned char gpio, unsigned long interval, bool pull_up = false): BaseSensor() {
  24. // Cache params
  25. _gpio = gpio;
  26. _interval = interval / 2;
  27. // OneWire
  28. _wire = new OneWire(_gpio);
  29. // Must be done after the OneWire initialization
  30. if (pull_up) pinMode(_gpio, INPUT_PULLUP);
  31. // Search devices
  32. loadDevices();
  33. }
  34. // Loop-like method, call it in your main loop
  35. void tick() {
  36. static unsigned long last = 0;
  37. if (millis() - last < _interval) return;
  38. last = millis();
  39. // Every second we either start a conversion or read the scratchpad
  40. static bool conversion = true;
  41. if (conversion) {
  42. // Start conversion
  43. _wire->reset();
  44. _wire->skip();
  45. _wire->write(DS_CMD_START_CONVERSION, DS_PARASITE);
  46. } else {
  47. // Read scratchpads
  48. for (unsigned char index=0; index<_devices.size(); index++) {
  49. // Read scratchpad
  50. if (_wire->reset() == 0) {
  51. _error = DS_ERROR_FAILED_RESET;
  52. return;
  53. }
  54. _wire->select(_devices[index].address);
  55. _wire->write(DS_CMD_READ_SCRATCHPAD);
  56. uint8_t data[9];
  57. for (unsigned char i = 0; i < 9; i++) {
  58. data[i] = _wire->read();
  59. }
  60. #if false
  61. Serial.printf("[DS18B20] Data = ");
  62. for (unsigned char i = 0; i < 9; i++) {
  63. Serial.printf("%02X ", data[i]);
  64. }
  65. Serial.printf(" CRC = %02X\n", OneWire::crc8(data, 8));
  66. #endif
  67. if (_wire->reset() != 1) {
  68. _error = DS_ERROR_FAILED_READ;
  69. return;
  70. }
  71. if (OneWire::crc8(data, 8) != data[8]) {
  72. _error = DS_ERROR_CRC;
  73. return;
  74. }
  75. memcpy(_devices[index].data, data, 9);
  76. }
  77. }
  78. conversion = !conversion;
  79. }
  80. // Descriptive name of the sensor
  81. String name() {
  82. char buffer[20];
  83. snprintf(buffer, sizeof(buffer), "OneWire @ GPIO%d", _gpio);
  84. return String(buffer);
  85. }
  86. // Descriptive name of the slot # index
  87. String slot(unsigned char index) {
  88. _error = SENSOR_ERROR_OK;
  89. if (index < _count) {
  90. char buffer[40];
  91. uint8_t * address = _devices[index].address;
  92. snprintf(buffer, sizeof(buffer), "%s (%02X%02X%02X%02X%02X%02X%02X%02X) @ GPIO%d",
  93. chipAsString(index).c_str(),
  94. address[0], address[1], address[2], address[3],
  95. address[4], address[5], address[6], address[7],
  96. _gpio
  97. );
  98. return String(buffer);
  99. }
  100. _error = SENSOR_ERROR_OUT_OF_RANGE;
  101. return String();
  102. }
  103. // Type for slot # index
  104. magnitude_t type(unsigned char index) {
  105. _error = SENSOR_ERROR_OK;
  106. if (index < _count) return MAGNITUDE_TEMPERATURE;
  107. _error = SENSOR_ERROR_OUT_OF_RANGE;
  108. return MAGNITUDE_NONE;
  109. }
  110. // Current value for slot # index
  111. double value(unsigned char index) {
  112. if (index >= _count) {
  113. _error = SENSOR_ERROR_OUT_OF_RANGE;
  114. return 0;
  115. }
  116. uint8_t * data = _devices[index].data;
  117. // Registers
  118. // byte 0: temperature LSB
  119. // byte 1: temperature MSB
  120. // byte 2: high alarm temp
  121. // byte 3: low alarm temp
  122. // byte 4: DS18S20: store for crc
  123. // DS18B20 & DS1822: configuration register
  124. // byte 5: internal use & crc
  125. // byte 6: DS18S20: COUNT_REMAIN
  126. // DS18B20 & DS1822: store for crc
  127. // byte 7: DS18S20: COUNT_PER_C
  128. // DS18B20 & DS1822: store for crc
  129. // byte 8: SCRATCHPAD_CRC
  130. int16_t raw = (data[1] << 8) | data[0];
  131. if (chip(index) == DS_CHIP_DS18S20) {
  132. raw = raw << 3; // 9 bit resolution default
  133. if (data[7] == 0x10) {
  134. raw = (raw & 0xFFF0) + 12 - data[6]; // "count remain" gives full 12 bit resolution
  135. }
  136. } else {
  137. byte cfg = (data[4] & 0x60);
  138. if (cfg == 0x00) raw = raw & ~7; // 9 bit res, 93.75 ms
  139. else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms
  140. else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms
  141. // 12 bit res, 750 ms
  142. }
  143. _error = SENSOR_ERROR_OK;
  144. return (float) raw / 16.0;
  145. }
  146. protected:
  147. unsigned char chip(unsigned char index) {
  148. if (index < _count) return _devices[index].address[0];
  149. return 0;
  150. }
  151. String chipAsString(unsigned char index) {
  152. unsigned char chip_id = chip(index);
  153. if (chip_id == DS_CHIP_DS18S20) return String("DS18S20");
  154. if (chip_id == DS_CHIP_DS18B20) return String("DS18B20");
  155. if (chip_id == DS_CHIP_DS1822) return String("DS1822");
  156. if (chip_id == DS_CHIP_DS1825) return String("DS1825");
  157. return String("Unknown");
  158. }
  159. void loadDevices() {
  160. uint8_t address[8];
  161. _wire->reset_search();
  162. while (_wire->search(address)) {
  163. // Check CRC
  164. if (_wire->crc8(address, 7) == address[7]) {
  165. ds_device_t device;
  166. memcpy(device.address, address, 8);
  167. _devices.push_back(device);
  168. }
  169. }
  170. _count = _devices.size();
  171. }
  172. typedef struct {
  173. uint8_t address[8];
  174. uint8_t data[9];
  175. } ds_device_t;
  176. std::vector<ds_device_t> _devices;
  177. unsigned char _gpio;
  178. unsigned long _interval;
  179. OneWire * _wire;
  180. };