ESPHome  2022.8.0
ezo.cpp
Go to the documentation of this file.
1 #include "ezo.h"
2 #include "esphome/core/log.h"
3 #include "esphome/core/hal.h"
4 
5 namespace esphome {
6 namespace ezo {
7 
8 static const char *const TAG = "ezo.sensor";
9 
10 static const uint16_t EZO_STATE_WAIT = 1;
11 static const uint16_t EZO_STATE_SEND_TEMP = 2;
12 static const uint16_t EZO_STATE_WAIT_TEMP = 4;
13 
15  LOG_SENSOR("", "EZO", this);
16  LOG_I2C_DEVICE(this);
17  if (this->is_failed())
18  ESP_LOGE(TAG, "Communication with EZO circuit failed!");
19  LOG_UPDATE_INTERVAL(this);
20 }
21 
23  if (this->state_ & EZO_STATE_WAIT) {
24  ESP_LOGE(TAG, "update overrun, still waiting for previous response");
25  return;
26  }
27  uint8_t c = 'R';
28  this->write(&c, 1);
29  this->state_ |= EZO_STATE_WAIT;
30  this->start_time_ = millis();
31  this->wait_time_ = 900;
32 }
33 
35  uint8_t buf[21];
36  if (!(this->state_ & EZO_STATE_WAIT)) {
37  if (this->state_ & EZO_STATE_SEND_TEMP) {
38  int len = sprintf((char *) buf, "T,%0.3f", this->tempcomp_);
39  this->write(buf, len);
40  this->state_ = EZO_STATE_WAIT | EZO_STATE_WAIT_TEMP;
41  this->start_time_ = millis();
42  this->wait_time_ = 300;
43  }
44  return;
45  }
46  if (millis() - this->start_time_ < this->wait_time_)
47  return;
48  buf[0] = 0;
49  if (!this->read_bytes_raw(buf, 20)) {
50  ESP_LOGE(TAG, "read error");
51  this->state_ = 0;
52  return;
53  }
54  switch (buf[0]) {
55  case 1:
56  break;
57  case 2:
58  ESP_LOGE(TAG, "device returned a syntax error");
59  break;
60  case 254:
61  return; // keep waiting
62  case 255:
63  ESP_LOGE(TAG, "device returned no data");
64  break;
65  default:
66  ESP_LOGE(TAG, "device returned an unknown response: %d", buf[0]);
67  break;
68  }
69  if (this->state_ & EZO_STATE_WAIT_TEMP) {
70  this->state_ = 0;
71  return;
72  }
73  this->state_ &= ~EZO_STATE_WAIT;
74  if (buf[0] != 1)
75  return;
76 
77  // some sensors return multiple comma-separated values, terminate string after first one
78  for (size_t i = 1; i < sizeof(buf) - 1; i++) {
79  if (buf[i] == ',')
80  buf[i] = '\0';
81  }
82 
83  float val = parse_number<float>((char *) &buf[1]).value_or(0);
84  this->publish_state(val);
85 }
86 
88  this->tempcomp_ = temp;
89  this->state_ |= EZO_STATE_SEND_TEMP;
90 }
91 
92 } // namespace ezo
93 } // namespace esphome
uint32_t wait_time_
Definition: ezo.h:22
void loop() override
Definition: ezo.cpp:34
optional< std::array< uint8_t, N > > read_bytes_raw()
Definition: i2c.h:80
uint32_t IRAM_ATTR HOT millis()
Definition: core.cpp:26
void publish_state(float state)
Publish a new state to the front-end.
Definition: sensor.cpp:72
ErrorCode write(const uint8_t *data, uint8_t len, bool stop=true)
Definition: i2c.h:56
uint32_t start_time_
Definition: ezo.h:21
void set_tempcomp_value(float temp)
Definition: ezo.cpp:87
void update() override
Definition: ezo.cpp:22
std::string size_t len
Definition: helpers.h:278
uint16_t state_
Definition: ezo.h:23
Definition: a4988.cpp:4
uint32_t val
Definition: datatypes.h:85
void dump_config() override
Definition: ezo.cpp:14