ESPHome 2026.5.0b1
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::wl_134 {
8
9static const char *const TAG = "wl_134.sensor";
10static const uint8_t ASCII_CR = 0x0D;
11static const uint8_t ASCII_NBSP = 0xFF;
12static const int MAX_DATA_LENGTH_BYTES = 6;
13
15
17 while (this->available() >= RFID134_PACKET_SIZE) {
18 Wl134Component::Rfid134Error error = this->read_packet_();
19 if (error != RFID134_ERROR_NONE) {
20 ESP_LOGW(TAG, "Error: %d", error);
21 }
22 }
23}
24
25Wl134Component::Rfid134Error Wl134Component::read_packet_() {
26 uint8_t packet[RFID134_PACKET_SIZE];
27 packet[RFID134_PACKET_START_CODE] = this->read();
28
29 // check for the first byte being the packet start code
30 if (packet[RFID134_PACKET_START_CODE] != 0x02) {
31 // just out of sync, ignore until we are synced up
32 return RFID134_ERROR_NONE;
33 }
34
35 if (!this->read_array(&(packet[RFID134_PACKET_ID]), RFID134_PACKET_SIZE - 1)) {
37 }
38
39 if (packet[RFID134_PACKET_END_CODE] != 0x03) {
41 }
42
43 // calculate checksum
44 uint8_t checksum = 0;
45 for (uint8_t i = RFID134_PACKET_ID; i < RFID134_PACKET_CHECKSUM; i++) {
46 checksum = checksum ^ packet[i];
47 }
48
49 // test checksum
50 if (checksum != packet[RFID134_PACKET_CHECKSUM]) {
52 }
53
54 if (static_cast<uint8_t>(~checksum) != static_cast<uint8_t>(packet[RFID134_PACKET_CHECKSUM_INVERT])) {
56 }
57
58 Rfid134Reading reading;
59
60 // convert packet into the reading struct
61 reading.id = this->hex_lsb_ascii_to_uint64_(&(packet[RFID134_PACKET_ID]), RFID134_PACKET_COUNTRY - RFID134_PACKET_ID);
62 reading.country = this->hex_lsb_ascii_to_uint64_(&(packet[RFID134_PACKET_COUNTRY]),
63 RFID134_PACKET_DATA_FLAG - RFID134_PACKET_COUNTRY);
64 reading.isData = packet[RFID134_PACKET_DATA_FLAG] == '1';
65 reading.isAnimal = packet[RFID134_PACKET_ANIMAL_FLAG] == '1';
66 reading.reserved0 = this->hex_lsb_ascii_to_uint64_(&(packet[RFID134_PACKET_RESERVED0]),
67 RFID134_PACKET_RESERVED1 - RFID134_PACKET_RESERVED0);
68 reading.reserved1 = this->hex_lsb_ascii_to_uint64_(&(packet[RFID134_PACKET_RESERVED1]),
69 RFID134_PACKET_CHECKSUM - RFID134_PACKET_RESERVED1);
70
71 ESP_LOGV(TAG,
72 "RFID134 Tag:\n"
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 esphome::wl_134
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:510
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