ESPHome 2025.10.0-dev
Loading...
Searching...
No Matches
wts01.cpp
Go to the documentation of this file.
1#include "wts01.h"
2#include "esphome/core/log.h"
3#include <cmath>
4
5namespace esphome {
6namespace wts01 {
7
8constexpr uint8_t HEADER_1 = 0x55;
9constexpr uint8_t HEADER_2 = 0x01;
10constexpr uint8_t HEADER_3 = 0x01;
11constexpr uint8_t HEADER_4 = 0x04;
12
13static const char *const TAG = "wts01";
14
16 // Process all available data at once
17 while (this->available()) {
18 uint8_t c;
19 if (this->read_byte(&c)) {
20 this->handle_char_(c);
21 }
22 }
23}
24
25void WTS01Sensor::dump_config() { LOG_SENSOR("", "WTS01 Sensor", this); }
26
28 // State machine for processing the header. Reset if something doesn't match.
29 if (this->buffer_pos_ == 0 && c != HEADER_1) {
30 return;
31 }
32
33 if (this->buffer_pos_ == 1 && c != HEADER_2) {
34 this->buffer_pos_ = 0;
35 return;
36 }
37
38 if (this->buffer_pos_ == 2 && c != HEADER_3) {
39 this->buffer_pos_ = 0;
40 return;
41 }
42
43 if (this->buffer_pos_ == 3 && c != HEADER_4) {
44 this->buffer_pos_ = 0;
45 return;
46 }
47
48 // Add byte to buffer
49 this->buffer_[this->buffer_pos_++] = c;
50
51 // Process complete packet
52 if (this->buffer_pos_ >= PACKET_SIZE) {
53 this->process_packet_();
54 this->buffer_pos_ = 0;
55 }
56}
57
59 // Based on Tasmota implementation
60 // Format: 55 01 01 04 01 11 16 12 95
61 // header T Td Ck - T = Temperature, Td = Temperature decimal, Ck = Checksum
62 uint8_t calculated_checksum = 0;
63 for (uint8_t i = 0; i < PACKET_SIZE - 1; i++) {
64 calculated_checksum += this->buffer_[i];
65 }
66
67 uint8_t received_checksum = this->buffer_[PACKET_SIZE - 1];
68 if (calculated_checksum != received_checksum) {
69 ESP_LOGW(TAG, "WTS01 Checksum doesn't match: 0x%02X != 0x%02X", received_checksum, calculated_checksum);
70 return;
71 }
72
73 // Extract temperature value
74 int8_t temp = this->buffer_[6];
75 int32_t sign = 1;
76
77 // Handle negative temperatures
78 if (temp < 0) {
79 sign = -1;
80 }
81
82 // Calculate temperature (temp + decimal/100)
83 float temperature = static_cast<float>(temp) + (sign * static_cast<float>(this->buffer_[7]) / 100.0f);
84
85 ESP_LOGV(TAG, "Received new temperature: %.2f°C", temperature);
86
87 this->publish_state(temperature);
88}
89
90} // namespace wts01
91} // namespace esphome
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:73
bool read_byte(uint8_t *data)
Definition uart.h:35
void handle_char_(uint8_t c)
Definition wts01.cpp:27
uint8_t buffer_[PACKET_SIZE]
Definition wts01.h:19
void dump_config() override
Definition wts01.cpp:25
void loop() override
Definition wts01.cpp:15
constexpr uint8_t HEADER_4
Definition wts01.cpp:11
constexpr uint8_t HEADER_3
Definition wts01.cpp:10
constexpr uint8_t HEADER_2
Definition wts01.cpp:9
constexpr uint8_t HEADER_1
Definition wts01.cpp:8
constexpr uint8_t PACKET_SIZE
Definition wts01.h:10
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
uint16_t temperature
Definition sun_gtil2.cpp:12