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