ESPHome 2026.3.0
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 {
5namespace smt100 {
6
7static const char *const TAG = "smt100";
8
10 ESP_LOGV(TAG, "Sending measurement request");
11 this->write_str("GetAllMeasurements!\r");
12}
13
15 while (this->available() != 0) {
16 if (this->readline_(this->read(), this->readline_buffer_, MAX_LINE_LENGTH) > 0) {
17 char *token = strtok(this->readline_buffer_, ",");
18 if (!token)
19 continue;
20 int counts = (int) strtol(token, nullptr, 10);
21 token = strtok(nullptr, ",");
22 if (!token)
23 continue;
24 float permittivity = (float) strtod(token, nullptr);
25 token = strtok(nullptr, ",");
26 if (!token)
27 continue;
28 float moisture = (float) strtod(token, nullptr);
29 token = strtok(nullptr, ",");
30 if (!token)
31 continue;
32 float temperature = (float) strtod(token, nullptr);
33 token = strtok(nullptr, ",");
34 if (!token)
35 continue;
36 float voltage = (float) strtod(token, nullptr);
37
38 if (this->counts_sensor_ != nullptr) {
40 }
41
42 if (this->permittivity_sensor_ != nullptr) {
44 }
45
46 if (this->moisture_sensor_ != nullptr) {
48 }
49
50 if (this->temperature_sensor_ != nullptr) {
52 }
53
54 if (this->voltage_sensor_ != nullptr) {
56 }
57 }
58 }
59}
60
62 ESP_LOGCONFIG(TAG, "SMT100:");
63
64 LOG_SENSOR(TAG, "Counts", this->counts_sensor_);
65 LOG_SENSOR(TAG, "Permittivity", this->permittivity_sensor_);
66 LOG_SENSOR(TAG, "Temperature", this->temperature_sensor_);
67 LOG_SENSOR(TAG, "Moisture", this->moisture_sensor_);
68 LOG_UPDATE_INTERVAL(this);
69 this->check_uart_settings(9600);
70}
71
72int SMT100Component::readline_(int readch, char *buffer, int len) {
73 int rpos;
74
75 if (readch > 0) {
76 switch (readch) {
77 case '\n': // Ignore new-lines
78 break;
79 case '\r': // Return on CR
80 rpos = this->readline_pos_;
81 this->readline_pos_ = 0; // Reset position index ready for next time
82 return rpos;
83 default:
84 if (this->readline_pos_ < len - 1) {
85 buffer[this->readline_pos_++] = readch;
86 buffer[this->readline_pos_] = 0;
87 }
88 }
89 }
90 // No end of line has been found, so return -1.
91 return -1;
92}
93
94} // namespace smt100
95} // namespace esphome
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:65
sensor::Sensor * temperature_sensor_
Definition smt100.h:37
sensor::Sensor * counts_sensor_
Definition smt100.h:34
int readline_(int readch, char *buffer, int len)
Definition smt100.cpp:72
char readline_buffer_[MAX_LINE_LENGTH]
Definition smt100.h:31
sensor::Sensor * voltage_sensor_
Definition smt100.h:38
sensor::Sensor * moisture_sensor_
Definition smt100.h:36
sensor::Sensor * permittivity_sensor_
Definition smt100.h:35
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
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
std::string size_t len
Definition helpers.h:892
uint16_t temperature
Definition sun_gtil2.cpp:12