ESPHome 2026.11.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
12ESPHOME_LOG_TAG(TAG, "wts01");
13
15 this->read_available([this](uint8_t c) { this->handle_char_(c); });
16}
17
18void WTS01Sensor::dump_config() { LOG_SENSOR("", "WTS01 Sensor", this); }
19
21 // State machine for processing the header. Reset if something doesn't match.
22 if (this->buffer_pos_ == 0 && c != HEADER_1) {
23 return;
24 }
25
26 if (this->buffer_pos_ == 1 && c != HEADER_2) {
27 this->buffer_pos_ = 0;
28 return;
29 }
30
31 if (this->buffer_pos_ == 2 && c != HEADER_3) {
32 this->buffer_pos_ = 0;
33 return;
34 }
35
36 if (this->buffer_pos_ == 3 && c != HEADER_4) {
37 this->buffer_pos_ = 0;
38 return;
39 }
40
41 // Add byte to buffer
42 this->buffer_[this->buffer_pos_++] = c;
43
44 // Process complete packet
45 if (this->buffer_pos_ >= PACKET_SIZE) {
46 this->process_packet_();
47 this->buffer_pos_ = 0;
48 }
49}
50
52 // Based on Tasmota implementation
53 // Format: 55 01 01 04 01 11 16 12 95
54 // header T Td Ck - T = Temperature, Td = Temperature decimal, Ck = Checksum
55 const uint8_t calculated_checksum = sum_bytes(this->buffer_, PACKET_SIZE - 1);
56
57 uint8_t received_checksum = this->buffer_[PACKET_SIZE - 1];
58 if (calculated_checksum != received_checksum) {
59 ESP_LOGW(TAG, "WTS01 Checksum doesn't match: 0x%02X != 0x%02X", received_checksum, calculated_checksum);
60 return;
61 }
62
63 // Extract temperature value
64 const uint8_t raw = this->buffer_[6];
65
66 // WTS01 encodes sign in bit 7, magnitude in bits 0-6
67 const bool negative = (raw & 0x80) != 0;
68 const uint8_t magnitude = raw & 0x7F;
69
70 const float decimal = static_cast<float>(this->buffer_[7]) / 100.0f;
71
72 float temperature = static_cast<float>(magnitude) + decimal;
73
74 if (negative) {
76 }
77
78 ESP_LOGV(TAG, "Received new temperature: %.2f°C", temperature);
79
80 this->publish_state(temperature);
81}
82
83} // 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:65
bool read_available(F &&on_byte)
Same as read_available_chunks(), calling on_byte(uint8_t) once per byte.
Definition uart.h:77
void handle_char_(uint8_t c)
Definition wts01.cpp:20
uint8_t buffer_[PACKET_SIZE]
Definition wts01.h:17
void dump_config() override
Definition wts01.cpp:18
void loop() override
Definition wts01.cpp:14
ESPHOME_LOG_TAG(TAG, "wts01")
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
constexpr uint8_t sum_bytes(const uint8_t *data, size_t len, uint8_t sum=0)
Add up the bytes of data with size len, truncated to 8 bits: the checksum many serial and IR protocol...
Definition helpers.h:894
uint16_t temperature
Definition sun_gtil2.cpp:12