ESPHome 2026.2.1
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 static char buffer[MAX_LINE_LENGTH];
16 while (this->available() != 0) {
17 if (readline_(read(), buffer, MAX_LINE_LENGTH) > 0) {
18 int counts = (int) strtol((strtok(buffer, ",")), nullptr, 10);
19 float permittivity = (float) strtod((strtok(nullptr, ",")), nullptr);
20 float moisture = (float) strtod((strtok(nullptr, ",")), nullptr);
21 float temperature = (float) strtod((strtok(nullptr, ",")), nullptr);
22 float voltage = (float) strtod((strtok(nullptr, ",")), nullptr);
23
24 if (this->counts_sensor_ != nullptr) {
26 }
27
28 if (this->permittivity_sensor_ != nullptr) {
30 }
31
32 if (this->moisture_sensor_ != nullptr) {
34 }
35
36 if (this->temperature_sensor_ != nullptr) {
38 }
39
40 if (this->voltage_sensor_ != nullptr) {
42 }
43 }
44 }
45}
46
48 ESP_LOGCONFIG(TAG, "SMT100:");
49
50 LOG_SENSOR(TAG, "Counts", this->counts_sensor_);
51 LOG_SENSOR(TAG, "Permittivity", this->permittivity_sensor_);
52 LOG_SENSOR(TAG, "Temperature", this->temperature_sensor_);
53 LOG_SENSOR(TAG, "Moisture", this->moisture_sensor_);
54 LOG_UPDATE_INTERVAL(this);
55 this->check_uart_settings(9600);
56}
57
58int SMT100Component::readline_(int readch, char *buffer, int len) {
59 static int pos = 0;
60 int rpos;
61
62 if (readch > 0) {
63 switch (readch) {
64 case '\n': // Ignore new-lines
65 break;
66 case '\r': // Return on CR
67 rpos = pos;
68 pos = 0; // Reset position index ready for next time
69 return rpos;
70 default:
71 if (pos < len - 1) {
72 buffer[pos++] = readch;
73 buffer[pos] = 0;
74 }
75 }
76 }
77 // No end of line has been found, so return -1.
78 return -1;
79}
80
81} // namespace smt100
82} // 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:34
sensor::Sensor * counts_sensor_
Definition smt100.h:31
int readline_(int readch, char *buffer, int len)
Definition smt100.cpp:58
sensor::Sensor * voltage_sensor_
Definition smt100.h:35
sensor::Sensor * moisture_sensor_
Definition smt100.h:33
sensor::Sensor * permittivity_sensor_
Definition smt100.h:32
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:692
size_t size_t pos
Definition helpers.h:729
uint16_t temperature
Definition sun_gtil2.cpp:12