ESPHome 2026.5.0b1
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
6static const char *const TAG = "smt100";
7
9 ESP_LOGV(TAG, "Sending measurement request");
10 this->write_str("GetAllMeasurements!\r");
11}
12
14 while (this->available() != 0) {
15 if (this->readline_(this->read(), this->readline_buffer_, MAX_LINE_LENGTH) > 0) {
16 char *token = strtok(this->readline_buffer_, ",");
17 if (!token)
18 continue;
19 int counts = (int) strtol(token, nullptr, 10);
20 token = strtok(nullptr, ",");
21 if (!token)
22 continue;
23 float permittivity = (float) strtod(token, nullptr);
24 token = strtok(nullptr, ",");
25 if (!token)
26 continue;
27 float moisture = (float) strtod(token, nullptr);
28 token = strtok(nullptr, ",");
29 if (!token)
30 continue;
31 float temperature = (float) strtod(token, nullptr);
32 token = strtok(nullptr, ",");
33 if (!token)
34 continue;
35 float voltage = (float) strtod(token, nullptr);
36
37 if (this->counts_sensor_ != nullptr) {
39 }
40
41 if (this->permittivity_sensor_ != nullptr) {
43 }
44
45 if (this->moisture_sensor_ != nullptr) {
47 }
48
49 if (this->temperature_sensor_ != nullptr) {
51 }
52
53 if (this->voltage_sensor_ != nullptr) {
55 }
56 }
57 }
58}
59
61 ESP_LOGCONFIG(TAG, "SMT100:");
62
63 LOG_SENSOR(TAG, "Counts", this->counts_sensor_);
64 LOG_SENSOR(TAG, "Permittivity", this->permittivity_sensor_);
65 LOG_SENSOR(TAG, "Temperature", this->temperature_sensor_);
66 LOG_SENSOR(TAG, "Moisture", this->moisture_sensor_);
67 LOG_UPDATE_INTERVAL(this);
68 this->check_uart_settings(9600);
69}
70
71int SMT100Component::readline_(int readch, char *buffer, int len) {
72 int rpos;
73
74 if (readch > 0) {
75 switch (readch) {
76 case '\n': // Ignore new-lines
77 break;
78 case '\r': // Return on CR
79 rpos = this->readline_pos_;
80 this->readline_pos_ = 0; // Reset position index ready for next time
81 return rpos;
82 default:
83 if (this->readline_pos_ < len - 1) {
84 buffer[this->readline_pos_++] = readch;
85 buffer[this->readline_pos_] = 0;
86 }
87 }
88 }
89 // No end of line has been found, so return -1.
90 return -1;
91}
92
93} // namespace esphome::smt100
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:68
sensor::Sensor * temperature_sensor_
Definition smt100.h:36
sensor::Sensor * counts_sensor_
Definition smt100.h:33
int readline_(int readch, char *buffer, int len)
Definition smt100.cpp:71
char readline_buffer_[MAX_LINE_LENGTH]
Definition smt100.h:30
sensor::Sensor * voltage_sensor_
Definition smt100.h:37
sensor::Sensor * moisture_sensor_
Definition smt100.h:35
sensor::Sensor * permittivity_sensor_
Definition smt100.h:34
void check_uart_settings(uint32_t baud_rate, uint8_t stop_bits=1, UARTParityOptions parity=UART_CONFIG_PARITY_NONE, uint8_t data_bits=8)
Check that the configuration of the UART bus matches the provided values and otherwise print a warnin...
Definition uart.cpp:16
void write_str(const char *str)
Definition uart.h:32
std::string size_t len
uint16_t temperature
Definition sun_gtil2.cpp:12