ESPHome 2026.11.0b6
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
7ESPHOME_LOG_TAG(TAG, "wts01");
8
10 this->read_available([this](uint8_t c) {
11 if (const uint8_t *packet = this->reader_.feed(c)) {
12 this->process_packet_(packet);
13 }
14 });
15}
16
17void WTS01Sensor::dump_config() { LOG_SENSOR("", "WTS01 Sensor", this); }
18
19void WTS01Sensor::process_packet_(const uint8_t *packet) {
20 // Based on Tasmota implementation
21 // Format: 55 01 01 04 01 11 16 12 95
22 // header T Td Ck - T = Temperature, Td = Temperature decimal, Ck = Checksum
23 const uint8_t calculated_checksum = sum_bytes(packet, PACKET_SIZE - 1);
24
25 uint8_t received_checksum = packet[PACKET_SIZE - 1];
26 if (calculated_checksum != received_checksum) {
27 ESP_LOGW(TAG, "WTS01 Checksum doesn't match: 0x%02X != 0x%02X", received_checksum, calculated_checksum);
28 return;
29 }
30
31 // Extract temperature value
32 const uint8_t raw = packet[6];
33
34 // WTS01 encodes sign in bit 7, magnitude in bits 0-6
35 const bool negative = (raw & 0x80) != 0;
36 const uint8_t magnitude = raw & 0x7F;
37
38 const float decimal = static_cast<float>(packet[7]) / 100.0f;
39
40 float temperature = static_cast<float>(magnitude) + decimal;
41
42 if (negative) {
44 }
45
46 ESP_LOGV(TAG, "Received new temperature: %.2f°C", temperature);
47
48 this->publish_state(temperature);
49}
50
51} // namespace esphome::wts01
uint8_t raw[35]
Definition bl0939.h:0
const uint8_t * feed(uint8_t c)
Feed one byte; returns the completed frame, header included, valid until the next call,...
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 process_packet_(const uint8_t *packet)
Definition wts01.cpp:19
FrameReader< PACKET_SIZE, 0x55, 0x01, 0x01, 0x04 > reader_
Definition wts01.h:20
void dump_config() override
Definition wts01.cpp:17
void loop() override
Definition wts01.cpp:9
ESPHOME_LOG_TAG(TAG, "wts01")
constexpr uint8_t PACKET_SIZE
Definition wts01.h:10
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:939
uint16_t temperature
Definition sun_gtil2.cpp:12