ESPHome 2026.5.0b1
Loading...
Searching...
No Matches
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
5namespace esphome::pmwcs3 {
6
7static const uint8_t PMWCS3_I2C_ADDRESS = 0x63;
8static const uint8_t PMWCS3_REG_READ_START = 0x01;
9static const uint8_t PMWCS3_REG_READ_E25 = 0x02;
10static const uint8_t PMWCS3_REG_READ_EC = 0x03;
11static const uint8_t PMWCS3_REG_READ_TEMP = 0x04;
12static const uint8_t PMWCS3_REG_READ_VWC = 0x05;
13static const uint8_t PMWCS3_REG_CALIBRATE_AIR = 0x06;
14static const uint8_t PMWCS3_REG_CALIBRATE_WATER = 0x07;
15static const uint8_t PMWCS3_SET_I2C_ADDRESS = 0x08;
16static const uint8_t PMWCS3_REG_GET_DATA = 0x09;
17static const uint8_t PMWCS3_REG_CALIBRATE_EC = 0x10;
18static const uint8_t PMWCS3_REG_CAP = 0x0A;
19static const uint8_t PMWCS3_REG_RES = 0x0B;
20static const uint8_t PMWCS3_REG_RC = 0x0C;
21static const uint8_t PMWCS3_REG_RT = 0x0D;
22
23static const char *const TAG = "pmwcs3";
24
26 if (!this->write_byte(PMWCS3_SET_I2C_ADDRESS, address)) {
27 this->status_set_warning();
28 ESP_LOGW(TAG, "Setting I2C address failed (%d)", address);
29 return;
30 }
31 this->set_i2c_address(address); // Allows device to continue working until new firmware is written with new address.
32 ESP_LOGVV(TAG, "Set I2C address to %d", address);
34}
35
37 if (!this->write_bytes(PMWCS3_REG_CALIBRATE_AIR, nullptr, 0)) {
38 this->status_set_warning();
39 ESP_LOGW(TAG, "Starting air calibration failed");
40 return;
41 }
42 ESP_LOGW(TAG, "Running air calibration for 300s");
43}
45 if (!this->write_bytes(PMWCS3_REG_CALIBRATE_WATER, nullptr, 0)) {
46 this->status_set_warning();
47 ESP_LOGW(TAG, "Starting water calibration failed");
48 return;
49 }
50 ESP_LOGW(TAG, "Running water calibration for 300s");
51}
52
54
56 ESP_LOGCONFIG(TAG, "PMWCS3");
57 LOG_I2C_DEVICE(this);
58 if (this->is_failed()) {
59 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
60 }
61 LOG_UPDATE_INTERVAL(this);
62 LOG_SENSOR(" ", "e25", this->e25_sensor_);
63 LOG_SENSOR(" ", "ec", this->ec_sensor_);
64 LOG_SENSOR(" ", "temperature", this->temperature_sensor_);
65 LOG_SENSOR(" ", "vwc", this->vwc_sensor_);
66}
69 if (!this->write_bytes(PMWCS3_REG_READ_START, nullptr, 0)) {
70 this->status_set_warning();
71 ESP_LOGVV(TAG, "Writing REG_READ_START failed");
72 return;
73 }
74
75 // Wait for the sensor to be ready.
76 // 80ms empirically determined (conservative).
77 this->set_timeout(80, [this] {
78 uint8_t data[8];
79 float e25, ec, temperature, vwc;
80 if (!this->read_bytes(PMWCS3_REG_GET_DATA, (uint8_t *) &data, 8)) {
81 ESP_LOGVV(TAG, "Reading PMWCS3_REG_GET_DATA failed");
82 this->mark_failed();
83 return;
84 }
85 if (this->e25_sensor_ != nullptr) {
86 e25 = ((data[1] << 8) | data[0]) / 100.0;
87 this->e25_sensor_->publish_state(e25);
88 ESP_LOGVV(TAG, "e25: data[0]=%d, data[1]=%d, result=%f", data[0], data[1], e25);
89 }
90 if (this->ec_sensor_ != nullptr) {
91 ec = ((data[3] << 8) | data[2]) / 10.0;
92 this->ec_sensor_->publish_state(ec);
93 ESP_LOGVV(TAG, "ec: data[2]=%d, data[3]=%d, result=%f", data[2], data[3], ec);
94 }
95 if (this->temperature_sensor_ != nullptr) {
96 temperature = ((data[5] << 8) | data[4]) / 100.0;
97 this->temperature_sensor_->publish_state(temperature);
98 ESP_LOGVV(TAG, "temp: data[4]=%d, data[5]=%d, result=%f", data[4], data[5], temperature);
99 }
100 if (this->vwc_sensor_ != nullptr) {
101 vwc = ((data[7] << 8) | data[6]) / 10.0;
102 this->vwc_sensor_->publish_state(vwc);
103 ESP_LOGVV(TAG, "vwc: data[6]=%d, data[7]=%d, result=%f", data[6], data[7], vwc);
104 }
105 });
106}
107
108} // namespace esphome::pmwcs3
uint8_t address
Definition bl0906.h:4
void mark_failed()
Mark this component as failed.
bool is_failed() const
Definition component.h:284
ESPDEPRECATED("Use const char* or uint32_t overload instead. Removed in 2026.7.0", "2026.1.0") void set_timeout(const std voi set_timeout)(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
Definition component.h:510
void status_clear_warning()
Definition component.h:306
bool write_byte(uint8_t a_register, uint8_t data) const
Definition i2c.h:265
void set_i2c_address(uint8_t address)
We store the address of the device on the bus.
Definition i2c.h:139
bool write_bytes(uint8_t a_register, const uint8_t *data, uint8_t len) const
Definition i2c.h:251
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:217
sensor::Sensor * e25_sensor_
Definition pmwcs3.h:29
sensor::Sensor * ec_sensor_
Definition pmwcs3.h:30
sensor::Sensor * vwc_sensor_
Definition pmwcs3.h:32
sensor::Sensor * temperature_sensor_
Definition pmwcs3.h:31
void new_i2c_address(uint8_t newaddress)
Definition pmwcs3.cpp:25
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:68
uint16_t temperature
Definition sun_gtil2.cpp:12