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.

278 lines
8.6 KiB

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