ESPHome  2024.10.2
smt100.cpp
Go to the documentation of this file.
1 #include "smt100.h"
2 #include "esphome/core/log.h"
3 
4 namespace esphome {
5 namespace smt100 {
6 
7 static 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 dielectric_constant = (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->dielectric_constant_sensor_ != nullptr) {
29  dielectric_constant_sensor_->publish_state(dielectric_constant);
30  }
31 
32  if (this->moisture_sensor_ != nullptr) {
34  }
35 
36  if (this->temperature_sensor_ != nullptr) {
37  temperature_sensor_->publish_state(temperature);
38  }
39 
40  if (this->voltage_sensor_ != nullptr) {
42  }
43  }
44  }
45 }
46 
48 
50  ESP_LOGCONFIG(TAG, "SMT100:");
51 
52  LOG_SENSOR(TAG, "Counts", this->temperature_sensor_);
53  LOG_SENSOR(TAG, "Dielectric Constant", this->temperature_sensor_);
54  LOG_SENSOR(TAG, "Temperature", this->temperature_sensor_);
55  LOG_SENSOR(TAG, "Moisture", this->moisture_sensor_);
56  LOG_UPDATE_INTERVAL(this);
57  this->check_uart_settings(9600);
58 }
59 
60 int SMT100Component::readline_(int readch, char *buffer, int len) {
61  static int pos = 0;
62  int rpos;
63 
64  if (readch > 0) {
65  switch (readch) {
66  case '\n': // Ignore new-lines
67  break;
68  case '\r': // Return on CR
69  rpos = pos;
70  pos = 0; // Reset position index ready for next time
71  return rpos;
72  default:
73  if (pos < len - 1) {
74  buffer[pos++] = readch;
75  buffer[pos] = 0;
76  }
77  }
78  }
79  // No end of line has been found, so return -1.
80  return -1;
81 }
82 
83 } // namespace smt100
84 } // namespace esphome
void write_str(const char *str)
Definition: uart.h:27
const float DATA
For components that import data from directly connected sensors like DHT.
Definition: component.cpp:19
void dump_config() override
Definition: smt100.cpp:49
sensor::Sensor * dielectric_constant_sensor_
Definition: smt100.h:34
sensor::Sensor * moisture_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:13
sensor::Sensor * temperature_sensor_
Definition: smt100.h:36
void publish_state(float state)
Publish a new state to the front-end.
Definition: sensor.cpp:39
float get_setup_priority() const override
Definition: smt100.cpp:47
uint16_t temperature
Definition: sun_gtil2.cpp:26
std::string size_t len
Definition: helpers.h:292
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
sensor::Sensor * counts_sensor_
Definition: smt100.h:33
sensor::Sensor * voltage_sensor_
Definition: smt100.h:37
int readline_(int readch, char *buffer, int len)
Definition: smt100.cpp:60