ESPHome 2026.2.1
Loading...
Searching...
No Matches
wl_134.cpp
Go to the documentation of this file.
1#include "wl_134.h"
3#include "esphome/core/log.h"
4
5#include <cinttypes>
6
7namespace esphome {
8namespace wl_134 {
9
10static const char *const TAG = "wl_134.sensor";
11static const uint8_t ASCII_CR = 0x0D;
12static const uint8_t ASCII_NBSP = 0xFF;
13static const int MAX_DATA_LENGTH_BYTES = 6;
14
16
18 while (this->available() >= RFID134_PACKET_SIZE) {
19 Wl134Component::Rfid134Error error = this->read_packet_();
20 if (error != RFID134_ERROR_NONE) {
21 ESP_LOGW(TAG, "Error: %d", error);
22 }
23 }
24}
25
26Wl134Component::Rfid134Error Wl134Component::read_packet_() {
27 uint8_t packet[RFID134_PACKET_SIZE];
28 packet[RFID134_PACKET_START_CODE] = this->read();
29
30 // check for the first byte being the packet start code
31 if (packet[RFID134_PACKET_START_CODE] != 0x02) {
32 // just out of sync, ignore until we are synced up
33 return RFID134_ERROR_NONE;
34 }
35
36 if (!this->read_array(&(packet[RFID134_PACKET_ID]), RFID134_PACKET_SIZE - 1)) {
38 }
39
40 if (packet[RFID134_PACKET_END_CODE] != 0x03) {
42 }
43
44 // calculate checksum
45 uint8_t checksum = 0;
46 for (uint8_t i = RFID134_PACKET_ID; i < RFID134_PACKET_CHECKSUM; i++) {
47 checksum = checksum ^ packet[i];
48 }
49
50 // test checksum
51 if (checksum != packet[RFID134_PACKET_CHECKSUM]) {
53 }
54
55 if (static_cast<uint8_t>(~checksum) != static_cast<uint8_t>(packet[RFID134_PACKET_CHECKSUM_INVERT])) {
57 }
58
59 Rfid134Reading reading;
60
61 // convert packet into the reading struct
62 reading.id = this->hex_lsb_ascii_to_uint64_(&(packet[RFID134_PACKET_ID]), RFID134_PACKET_COUNTRY - RFID134_PACKET_ID);
63 reading.country = this->hex_lsb_ascii_to_uint64_(&(packet[RFID134_PACKET_COUNTRY]),
64 RFID134_PACKET_DATA_FLAG - RFID134_PACKET_COUNTRY);
65 reading.isData = packet[RFID134_PACKET_DATA_FLAG] == '1';
66 reading.isAnimal = packet[RFID134_PACKET_ANIMAL_FLAG] == '1';
67 reading.reserved0 = this->hex_lsb_ascii_to_uint64_(&(packet[RFID134_PACKET_RESERVED0]),
68 RFID134_PACKET_RESERVED1 - RFID134_PACKET_RESERVED0);
69 reading.reserved1 = this->hex_lsb_ascii_to_uint64_(&(packet[RFID134_PACKET_RESERVED1]),
70 RFID134_PACKET_CHECKSUM - RFID134_PACKET_RESERVED1);
71
72 ESP_LOGV(TAG,
73 "Tag id: %012lld\n"
74 "Country: %03d\n"
75 "isData: %s\n"
76 "isAnimal: %s\n"
77 "Reserved0: %d\n"
78 "Reserved1: %" PRId32,
79 reading.id, reading.country, reading.isData ? "true" : "false", reading.isAnimal ? "true" : "false",
80 reading.reserved0, reading.reserved1);
81
82 char buf[20]; // "%03d" (3) + "%012" PRId64 (12) + null = 16 max
83 buf_append_printf(buf, sizeof(buf), 0, "%03d%012" PRId64, reading.country, reading.id);
84 this->publish_state(buf);
85 if (this->do_reset_) {
86 this->set_timeout(1000, [this]() { this->publish_state(""); });
87 }
88
89 return RFID134_ERROR_NONE;
90}
91
92uint64_t Wl134Component::hex_lsb_ascii_to_uint64_(const uint8_t *text, uint8_t text_size) {
93 uint64_t value = 0;
94 uint8_t i = text_size;
95 do {
96 i--;
97
98 uint8_t digit = text[i];
99 if (digit >= 'A') {
100 digit = digit - 'A' + 10;
101 } else {
102 digit = digit - '0';
103 }
104 value = (value << 4) + digit;
105 } while (i != 0);
106
107 return value;
108}
109
111 ESP_LOGCONFIG(TAG, "WL-134 Sensor:");
112 LOG_TEXT_SENSOR("", "Tag", this);
113 // As specified in the sensor's data sheet
115}
116} // namespace wl_134
117} // namespace esphome
uint8_t checksum
Definition bl0906.h:3
ESPDEPRECATED("Use const char* or uint32_t overload instead. Removed in 2026.7.0", "2026.1.0") void set_timeout(const std voi set_timeout)(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
Definition component.h:429
void publish_state(const std::string &state)
optional< std::array< uint8_t, N > > read_array()
Definition uart.h:38
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:16
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7