ESPHome 2026.11.0-dev
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
23ESPHOME_LOG_TAG(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 LOG_UPDATE_INTERVAL(this);
59 LOG_SENSOR(" ", "e25", this->e25_sensor_);
60 LOG_SENSOR(" ", "ec", this->ec_sensor_);
61 LOG_SENSOR(" ", "temperature", this->temperature_sensor_);
62 LOG_SENSOR(" ", "vwc", this->vwc_sensor_);
63}
66 if (!this->write_bytes(PMWCS3_REG_READ_START, nullptr, 0)) {
67 this->status_set_warning();
68 ESP_LOGVV(TAG, "Writing REG_READ_START failed");
69 return;
70 }
71
72 // Wait for the sensor to be ready.
73 // 80ms empirically determined (conservative).
74 this->set_timeout(80, [this] {
75 uint16_t data[4];
76 float e25, ec, temperature, vwc;
77 if (!this->read_bytes_16_le(PMWCS3_REG_GET_DATA, data, 4)) {
78 ESP_LOGVV(TAG, "Reading PMWCS3_REG_GET_DATA failed");
79 this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
80 return;
81 }
82 if (this->e25_sensor_ != nullptr) {
83 e25 = data[0] / 100.0;
84 this->e25_sensor_->publish_state(e25);
85 ESP_LOGVV(TAG, "e25: raw=%u, result=%f", data[0], e25);
86 }
87 if (this->ec_sensor_ != nullptr) {
88 ec = data[1] / 10.0;
89 this->ec_sensor_->publish_state(ec);
90 ESP_LOGVV(TAG, "ec: raw=%u, result=%f", data[1], ec);
91 }
92 if (this->temperature_sensor_ != nullptr) {
93 temperature = data[2] / 100.0;
94 this->temperature_sensor_->publish_state(temperature);
95 ESP_LOGVV(TAG, "temp: raw=%u, result=%f", data[2], temperature);
96 }
97 if (this->vwc_sensor_ != nullptr) {
98 vwc = data[3] / 10.0;
99 this->vwc_sensor_->publish_state(vwc);
100 ESP_LOGVV(TAG, "vwc: raw=%u, result=%f", data[3], vwc);
101 }
102 });
103}
104
105} // namespace esphome::pmwcs3
uint8_t address
Definition bl0906.h:4
void mark_failed()
Mark this component as failed.
void set_timeout(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a const char* name.
Definition component.cpp:96
void status_clear_warning()
Definition component.h:296
bool read_bytes_16_le(uint8_t a_register, uint16_t *data, uint8_t len)
Read len 16-bit values stored least significant byte first; no conversion is compiled on little-endia...
Definition i2c.h:284
bool write_byte(uint8_t a_register, uint8_t data) const
Definition i2c.h:312
void set_i2c_address(uint8_t address)
We store the address of the device on the bus.
Definition i2c.h:151
bool write_bytes(uint8_t a_register, const uint8_t *data, uint8_t len) const
Definition i2c.h:298
sensor::Sensor * e25_sensor_
Definition pmwcs3.h:28
sensor::Sensor * ec_sensor_
Definition pmwcs3.h:29
sensor::Sensor * vwc_sensor_
Definition pmwcs3.h:31
sensor::Sensor * temperature_sensor_
Definition pmwcs3.h:30
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:65
ESPHOME_LOG_TAG(TAG, "pmwcs3")
uint16_t temperature
Definition sun_gtil2.cpp:12