ESPHome  2024.4.0
pmwcs3.cpp
Go to the documentation of this file.
1 #include "pmwcs3.h"
2 #include "esphome/core/hal.h"
3 #include "esphome/core/log.h"
4 
5 namespace esphome {
6 namespace pmwcs3 {
7 
8 static const uint8_t PMWCS3_I2C_ADDRESS = 0x63;
9 static const uint8_t PMWCS3_REG_READ_START = 0x01;
10 static const uint8_t PMWCS3_REG_READ_E25 = 0x02;
11 static const uint8_t PMWCS3_REG_READ_EC = 0x03;
12 static const uint8_t PMWCS3_REG_READ_TEMP = 0x04;
13 static const uint8_t PMWCS3_REG_READ_VWC = 0x05;
14 static const uint8_t PMWCS3_REG_CALIBRATE_AIR = 0x06;
15 static const uint8_t PMWCS3_REG_CALIBRATE_WATER = 0x07;
16 static const uint8_t PMWCS3_SET_I2C_ADDRESS = 0x08;
17 static const uint8_t PMWCS3_REG_GET_DATA = 0x09;
18 static const uint8_t PMWCS3_REG_CALIBRATE_EC = 0x10;
19 static const uint8_t PMWCS3_REG_CAP = 0x0A;
20 static const uint8_t PMWCS3_REG_RES = 0x0B;
21 static const uint8_t PMWCS3_REG_RC = 0x0C;
22 static const uint8_t PMWCS3_REG_RT = 0x0D;
23 
24 static const char *const TAG = "pmwcs3";
25 
26 void PMWCS3Component::new_i2c_address(uint8_t address) {
27  if (!this->write_byte(PMWCS3_SET_I2C_ADDRESS, address)) {
28  this->status_set_warning();
29  ESP_LOGW(TAG, "couldn't write the new I2C address %d", address);
30  return;
31  }
32  this->set_i2c_address(address); // Allows device to continue working until new firmware is written with new address.
33  ESP_LOGVV(TAG, "changed I2C address to %d", address);
34  this->status_clear_warning();
35 }
36 
38  if (!this->write_bytes(PMWCS3_REG_CALIBRATE_AIR, nullptr, 0)) {
39  this->status_set_warning();
40  ESP_LOGW(TAG, "couldn't start air calibration");
41  return;
42  }
43  ESP_LOGW(TAG, "Start air calibration during the next 300s");
44 }
46  if (!this->write_bytes(PMWCS3_REG_CALIBRATE_WATER, nullptr, 0)) {
47  this->status_set_warning();
48  ESP_LOGW(TAG, "couldn't start water calibration");
49  return;
50  }
51  ESP_LOGW(TAG, "Start water calibration during the next 300s");
52 }
53 
54 void PMWCS3Component::setup() { ESP_LOGCONFIG(TAG, "Setting up PMWCS3..."); }
55 
57 
59 
61  ESP_LOGCONFIG(TAG, "PMWCS3");
62  LOG_I2C_DEVICE(this);
63  if (this->is_failed()) {
64  ESP_LOGE(TAG, "Communication with PMWCS3 failed!");
65  }
66  ESP_LOGI(TAG, "%s", this->is_failed() ? "FAILED" : "OK");
67 
68  LOG_UPDATE_INTERVAL(this);
69  LOG_SENSOR(" ", "e25", this->e25_sensor_);
70  LOG_SENSOR(" ", "ec", this->ec_sensor_);
71  LOG_SENSOR(" ", "temperature", this->temperature_sensor_);
72  LOG_SENSOR(" ", "vwc", this->vwc_sensor_);
73 }
75  uint8_t data[8];
76  float e25, ec, temperature, vwc;
77 
79 
80  if (!this->write_bytes(PMWCS3_REG_READ_START, nullptr, 0)) {
81  this->status_set_warning();
82  ESP_LOGVV(TAG, "Failed to write into REG_READ_START register !!!");
83  return;
84  }
85  // NOLINT delay(100);
86 
87  if (!this->read_bytes(PMWCS3_REG_GET_DATA, (uint8_t *) &data, 8)) {
88  ESP_LOGVV(TAG, "Error reading PMWCS3_REG_GET_DATA registers");
89  this->mark_failed();
90  return;
91  }
92  if (this->e25_sensor_ != nullptr) {
93  e25 = ((data[1] << 8) | data[0]) / 100.0;
94  this->e25_sensor_->publish_state(e25);
95  ESP_LOGVV(TAG, "e25: data[0]=%d, data[1]=%d, result=%f", data[0], data[1], e25);
96  }
97  if (this->ec_sensor_ != nullptr) {
98  ec = ((data[3] << 8) | data[2]) / 10.0;
99  this->ec_sensor_->publish_state(ec);
100  ESP_LOGVV(TAG, "ec: data[2]=%d, data[3]=%d, result=%f", data[2], data[3], ec);
101  }
102  if (this->temperature_sensor_ != nullptr) {
103  temperature = ((data[5] << 8) | data[4]) / 100.0;
104  this->temperature_sensor_->publish_state(temperature);
105  ESP_LOGVV(TAG, "temp: data[4]=%d, data[5]=%d, result=%f", data[4], data[5], temperature);
106  }
107  if (this->vwc_sensor_ != nullptr) {
108  vwc = ((data[7] << 8) | data[6]) / 10.0;
109  this->vwc_sensor_->publish_state(vwc);
110  ESP_LOGVV(TAG, "vwc: data[6]=%d, data[7]=%d, result=%f", data[6], data[7], vwc);
111  }
112 }
113 
114 } // namespace pmwcs3
115 } // namespace esphome
sensor::Sensor * temperature_sensor_
Definition: pmwcs3.h:34
const float DATA
For components that import data from directly connected sensors like DHT.
Definition: component.cpp:19
float get_setup_priority() const override
Definition: pmwcs3.cpp:58
void status_set_warning(const char *message="unspecified")
Definition: component.cpp:151
bool read_bytes(uint8_t a_register, uint8_t *data, uint8_t len)
Compat APIs All methods below have been added for compatibility reasons.
Definition: i2c.h:212
void dump_config() override
Definition: pmwcs3.cpp:60
void new_i2c_address(uint8_t newaddress)
Definition: pmwcs3.cpp:26
sensor::Sensor * vwc_sensor_
Definition: pmwcs3.h:35
sensor::Sensor * e25_sensor_
Definition: pmwcs3.h:32
void status_clear_warning()
Definition: component.cpp:166
void publish_state(float state)
Publish a new state to the front-end.
Definition: sensor.cpp:39
sensor::Sensor * ec_sensor_
Definition: pmwcs3.h:33
uint16_t temperature
Definition: sun_gtil2.cpp:26
bool write_byte(uint8_t a_register, uint8_t data, bool stop=true)
Definition: i2c.h:262
virtual void mark_failed()
Mark this component as failed.
Definition: component.cpp:118
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7
void set_i2c_address(uint8_t address)
We store the address of the device on the bus.
Definition: i2c.h:140
bool write_bytes(uint8_t a_register, const uint8_t *data, uint8_t len, bool stop=true)
Definition: i2c.h:248