ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
smt100.cpp
Go to the documentation of this file.
1#include "smt100.h"
2#include "esphome/core/log.h"
3
4namespace esphome::smt100 {
5
6ESPHOME_LOG_TAG(TAG, "smt100");
7
9 ESP_LOGV(TAG, "Sending measurement request");
10 this->write_str("GetAllMeasurements!\r");
11}
12
14 this->read_available([this](uint8_t readch) {
15 if (readch == 0)
16 return;
17 char *line = this->line_reader_.feed(readch);
19 if (line != nullptr && line[0] != '\0') {
20 char *token = strtok(line, ",");
21 if (!token)
22 return;
23 int counts = (int) strtol(token, nullptr, 10);
24 token = strtok(nullptr, ",");
25 if (!token)
26 return;
27 float permittivity = (float) strtod(token, nullptr);
28 token = strtok(nullptr, ",");
29 if (!token)
30 return;
31 float moisture = (float) strtod(token, nullptr);
32 token = strtok(nullptr, ",");
33 if (!token)
34 return;
35 float temperature = (float) strtod(token, nullptr);
36 token = strtok(nullptr, ",");
37 if (!token)
38 return;
39 float voltage = (float) strtod(token, nullptr);
40
41 if (this->counts_sensor_ != nullptr) {
43 }
44
45 if (this->permittivity_sensor_ != nullptr) {
47 }
48
49 if (this->moisture_sensor_ != nullptr) {
51 }
52
53 if (this->temperature_sensor_ != nullptr) {
55 }
56
57 if (this->voltage_sensor_ != nullptr) {
59 }
60 }
61 });
62}
63
65 ESP_LOGCONFIG(TAG, "SMT100:");
66
67 LOG_SENSOR(TAG, "Counts", this->counts_sensor_);
68 LOG_SENSOR(TAG, "Permittivity", this->permittivity_sensor_);
69 LOG_SENSOR(TAG, "Temperature", this->temperature_sensor_);
70 LOG_SENSOR(TAG, "Moisture", this->moisture_sensor_);
71 LOG_UPDATE_INTERVAL(this);
72}
73
74} // namespace esphome::smt100
char * feed(uint8_t c)
Feed one byte; returns the completed line (empty lines included), valid until the next call,...
Definition line_reader.h:23
void log_if_overflowed(const char *tag) const
Log the shared "line dropped" warning when the last feed() overflowed.
Definition line_reader.h:69
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:65
LineReader< LINE_BUFFER_SIZE, '\r', '\n'> line_reader_
Definition smt100.h:30
sensor::Sensor * temperature_sensor_
Definition smt100.h:35
sensor::Sensor * counts_sensor_
Definition smt100.h:32
sensor::Sensor * voltage_sensor_
Definition smt100.h:36
sensor::Sensor * moisture_sensor_
Definition smt100.h:34
sensor::Sensor * permittivity_sensor_
Definition smt100.h:33
void write_str(const char *str)
Definition uart.h:37
bool read_available(F &&on_byte)
Same as read_available_chunks(), calling on_byte(uint8_t) once per byte.
Definition uart.h:77
ESPHOME_LOG_TAG(TAG, "smt100")
uint16_t temperature
Definition sun_gtil2.cpp:12