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.

213 lines
5.5 KiB

  1. // -----------------------------------------------------------------------------
  2. // EZO™ pH Circuit from Atlas Scientific
  3. //
  4. // Uses SoftwareSerial library
  5. // Copyright (C) 2018 by Rui Marinho <ruipmarinho at gmail dot com>
  6. // -----------------------------------------------------------------------------
  7. #if SENSOR_SUPPORT && EZOPH_SUPPORT
  8. #pragma once
  9. #include <Arduino.h>
  10. #include <SoftwareSerial.h>
  11. #include "BaseSensor.h"
  12. class EZOPHSensor : public BaseSensor {
  13. public:
  14. // ---------------------------------------------------------------------
  15. // Public
  16. // ---------------------------------------------------------------------
  17. EZOPHSensor() {
  18. _count = 1;
  19. _sensor_id = SENSOR_EZOPH_ID;
  20. }
  21. ~EZOPHSensor() {
  22. if (_serial) delete _serial;
  23. }
  24. // ---------------------------------------------------------------------
  25. void setRX(unsigned char pin_rx) {
  26. if (_pin_rx == pin_rx) return;
  27. _pin_rx = pin_rx;
  28. _dirty = true;
  29. }
  30. void setTX(unsigned char pin_tx) {
  31. if (_pin_tx == pin_tx) return;
  32. _pin_tx = pin_tx;
  33. _dirty = true;
  34. }
  35. // ---------------------------------------------------------------------
  36. unsigned char getRX() {
  37. return _pin_rx;
  38. }
  39. unsigned char getTX() {
  40. return _pin_tx;
  41. }
  42. // ---------------------------------------------------------------------
  43. // Sensor API
  44. // ---------------------------------------------------------------------
  45. // Initialization method, must be idempotent
  46. void begin() {
  47. if (!_dirty) return;
  48. if (_serial) delete _serial;
  49. _serial = new SoftwareSerial(_pin_rx, _pin_tx);
  50. _serial->enableIntTx(false);
  51. _serial->begin(9600);
  52. _ready = true;
  53. _dirty = false;
  54. }
  55. // Descriptive name of the sensor
  56. String description() {
  57. char buffer[28];
  58. snprintf(buffer, sizeof(buffer), "EZOPH @ SwSerial(%u,%u)", _pin_rx, _pin_tx);
  59. return String(buffer);
  60. }
  61. // Descriptive name of the slot # index
  62. String description(unsigned char index) {
  63. return description();
  64. };
  65. // Address of the sensor (it could be the GPIO or I2C address)
  66. String address(unsigned char index) {
  67. char buffer[6];
  68. snprintf(buffer, sizeof(buffer), "%u:%u", _pin_rx, _pin_tx);
  69. return String(buffer);
  70. }
  71. // Type for slot # index
  72. unsigned char type(unsigned char index) {
  73. if (index == 0) return MAGNITUDE_PH;
  74. return MAGNITUDE_NONE;
  75. }
  76. void tick() {
  77. _setup();
  78. _read();
  79. }
  80. // Current value for slot # index
  81. double value(unsigned char index) {
  82. if (index == 0) return _ph;
  83. return 0;
  84. }
  85. protected:
  86. // ---------------------------------------------------------------------
  87. // Protected
  88. // ---------------------------------------------------------------------
  89. void _setup() {
  90. if (_sync_responded) {
  91. return;
  92. }
  93. _error = SENSOR_ERROR_WARM_UP;
  94. String sync_serial = "";
  95. sync_serial.reserve(30);
  96. if (!_sync_requested) {
  97. _serial->write(67); // C
  98. _serial->write(44); // ,
  99. _serial->write(63); // ?
  100. _serial->write(13); // \r
  101. _serial->flush();
  102. _sync_requested = true;
  103. }
  104. while ((_serial->available() > 0)) {
  105. char sync_char = (char)_serial->read();
  106. sync_serial += sync_char;
  107. if (sync_char == '\r') {
  108. break;
  109. }
  110. }
  111. if (sync_serial.startsWith("?C,")) {
  112. _sync_interval = sync_serial.substring(sync_serial.indexOf(",") + 1).toInt() * 1000;
  113. if (_sync_interval == 0) {
  114. _error = SENSOR_ERROR_OTHER;
  115. return;
  116. }
  117. }
  118. if (sync_serial.startsWith("*OK")) {
  119. _sync_responded = true;
  120. }
  121. if (!_sync_responded) {
  122. return;
  123. }
  124. _error = SENSOR_ERROR_OK;
  125. }
  126. void _read() {
  127. if (_error != SENSOR_ERROR_OK) {
  128. return;
  129. }
  130. if (millis() - _ts <= _sync_interval) {
  131. return;
  132. }
  133. _ts = millis();
  134. String ph_serial = "";
  135. ph_serial.reserve(30);
  136. while ((_serial->available() > 0)) {
  137. char ph_char = (char)_serial->read();
  138. ph_serial += ph_char;
  139. if (ph_char == '\r') {
  140. break;
  141. }
  142. }
  143. if (ph_serial == "*ER") {
  144. _error = SENSOR_ERROR_OTHER;
  145. return;
  146. }
  147. _ph = ph_serial.toFloat();
  148. _error = SENSOR_ERROR_OK;
  149. }
  150. bool _sync_requested = false;
  151. bool _sync_responded = false;
  152. unsigned long _sync_interval = 100000; // Maximum continuous reading interval allowed is 99000 milliseconds.
  153. unsigned long _ts = 0;
  154. double _ph = 0;
  155. unsigned int _pin_rx;
  156. unsigned int _pin_tx;
  157. SoftwareSerial * _serial = NULL;
  158. };
  159. #endif // SENSOR_SUPPORT && EZOPH_SUPPORT