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.

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