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.

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