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.

239 lines
7.4 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. 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 = SENSOR_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), "Dallas @ 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. double value = (float) raw / 16.0;
  144. if (value == DS_DISCONNECTED) {
  145. _error = SENSOR_ERROR_CRC;
  146. return 0;
  147. }
  148. _error = SENSOR_ERROR_OK;
  149. return value;
  150. }
  151. protected:
  152. unsigned char chip(unsigned char index) {
  153. if (index < _count) return _devices[index].address[0];
  154. return 0;
  155. }
  156. String chipAsString(unsigned char index) {
  157. unsigned char chip_id = chip(index);
  158. if (chip_id == DS_CHIP_DS18S20) return String("DS18S20");
  159. if (chip_id == DS_CHIP_DS18B20) return String("DS18B20");
  160. if (chip_id == DS_CHIP_DS1822) return String("DS1822");
  161. if (chip_id == DS_CHIP_DS1825) return String("DS1825");
  162. return String("Unknown");
  163. }
  164. void loadDevices() {
  165. uint8_t address[8];
  166. _wire->reset_search();
  167. while (_wire->search(address)) {
  168. // Check CRC
  169. if (_wire->crc8(address, 7) == address[7]) {
  170. ds_device_t device;
  171. memcpy(device.address, address, 8);
  172. _devices.push_back(device);
  173. }
  174. }
  175. _count = _devices.size();
  176. }
  177. typedef struct {
  178. uint8_t address[8];
  179. uint8_t data[9];
  180. } ds_device_t;
  181. std::vector<ds_device_t> _devices;
  182. unsigned char _gpio;
  183. unsigned long _interval;
  184. OneWire * _wire;
  185. };