6namespace uponor_smatrix {
8static const char *
const TAG =
"uponor_smatrix";
19 ESP_LOGCONFIG(TAG,
"Uponor Smatrix");
20 ESP_LOGCONFIG(TAG,
" System address: 0x%04X", this->
address_);
23 ESP_LOGCONFIG(TAG,
" Time synchronization: YES");
31 ESP_LOGCONFIG(TAG,
" Detected unknown device addresses:");
33 ESP_LOGCONFIG(TAG,
" 0x%04X", device_address);
42 if (!this->
rx_buffer_.empty() && (now - this->last_rx_ > 50)) {
43 ESP_LOGD(TAG,
"Discarding %d bytes of unparsed data", this->
rx_buffer_.size());
61 if (this->
rx_buffer_.empty() && (now - this->last_rx_ > 50) && (now - this->last_rx_ < 100) &&
62 (now - this->last_tx_ > 200)) {
70 auto packet = std::move(this->
tx_queue_.front());
83 const uint8_t *packet = this->
rx_buffer_.data();
91 uint16_t system_address =
encode_uint16(packet[0], packet[1]);
92 uint16_t device_address =
encode_uint16(packet[2], packet[3]);
93 uint16_t crc =
encode_uint16(packet[packet_len - 1], packet[packet_len - 2]);
95 uint16_t computed_crc =
crc16(packet, packet_len - 2);
96 if (crc != computed_crc) {
101 ESP_LOGV(TAG,
"Received packet: sys=%04X, dev=%04X, data=%s, crc=%04X", system_address, device_address,
102 format_hex(&packet[4], packet_len - 6).c_str(), crc);
106 ESP_LOGI(TAG,
"Using detected system address 0x%04X", system_address);
108 }
else if (this->
address_ != system_address) {
110 ESP_LOGW(TAG,
"Received packet from unknown system address 0x%04X", system_address);
115 size_t data_len = (packet_len - 6) / 3;
117 if (packet[4] == UPONOR_ID_REQUEST)
118 ESP_LOGVV(TAG,
"Ignoring request packet for device 0x%04X", device_address);
124 for (
int i = 0; i < data_len; i++) {
125 data[i].
id = packet[(i * 3) + 4];
135 bool found_temperature =
false;
136 bool found_time =
false;
137 for (
int i = 0; i < data_len; i++) {
138 if (data[i].
id == UPONOR_ID_ROOM_TEMP)
139 found_temperature =
true;
140 if (data[i].
id == UPONOR_ID_DATETIME1)
142 if (found_temperature && found_time) {
143 ESP_LOGI(TAG,
"Using detected time device address 0x%04X", device_address);
153 for (
auto *device : this->
devices_) {
154 if (device->address_ == device_address) {
156 device->on_device_data(data, data_len);
162 ESP_LOGI(TAG,
"Received packet for unknown device address 0x%04X ", device_address);
171 if (this->
address_ == 0 || device_address == 0 || data ==
nullptr || data_len == 0)
175 std::vector<uint8_t> packet;
176 packet.reserve(6 + 3 * data_len);
178 packet.push_back(this->
address_ >> 8);
179 packet.push_back(this->
address_ >> 0);
180 packet.push_back(device_address >> 8);
181 packet.push_back(device_address >> 0);
183 for (
int i = 0; i < data_len; i++) {
184 packet.push_back(data[i].
id);
185 packet.push_back(data[i].value >> 8);
186 packet.push_back(data[i].value >> 0);
189 auto crc =
crc16(packet.data(), packet.size());
190 packet.push_back(crc >> 0);
191 packet.push_back(crc >> 8);
215 uint16_t time1 = (
year & 0x7F) << 7 | (
month & 0x0F) << 3 | (day_of_week & 0x07);
216 uint16_t time2 = (day_of_month & 0x1F) << 11 | (
hour & 0x1F) << 6 | (
minute & 0x3F);
222 UponorSmatrixData data[] = {{UPONOR_ID_DATETIME1, time1}, {UPONOR_ID_DATETIME2, time2}, {UPONOR_ID_DATETIME3, time3}};
uint32_t IRAM_ATTR HOT get_loop_component_start_time() const
Get the cached time in milliseconds from when the current component started its loop execution.
void add_on_time_sync_callback(std::function< void()> callback)
ESPTime now()
Get the time in the currently defined timezone.
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...
bool read_byte(uint8_t *data)
void write_array(const uint8_t *data, size_t len)
bool send(uint16_t device_address, const UponorSmatrixData *data, size_t data_len)
bool parse_byte_(uint8_t byte)
uint16_t time_device_address_
std::vector< UponorSmatrixDevice * > devices_
std::set< uint16_t > unknown_devices_
void dump_config() override
time::RealTimeClock * time_id_
std::vector< uint8_t > rx_buffer_
std::queue< std::vector< uint8_t > > tx_queue_
bool send_time_requested_
Providing packet encoding functions for exchanging data with a remote host.
uint16_t crc16(const uint8_t *data, uint16_t len, uint16_t crc, uint16_t reverse_poly, bool refin, bool refout)
Calculate a CRC-16 checksum of data with size len.
std::string format_hex(const uint8_t *data, size_t length)
Format the byte array data of length len in lowercased hex.
constexpr uint16_t encode_uint16(uint8_t msb, uint8_t lsb)
Encode a 16-bit value given the most and least significant byte.
Application App
Global storage of Application pointer - only one Application can exist.
A more user-friendly version of struct tm from time.h.
uint8_t minute
minutes after the hour [0-59]
uint8_t second
seconds after the minute [0-60]
uint8_t hour
hours since midnight [0-23]
bool is_valid() const
Check if this ESPTime is valid (all fields in range and year is greater than 2018)
uint8_t day_of_month
day of the month [1-31]
uint8_t month
month; january=1 [1-12]
uint8_t day_of_week
day of the week; sunday=1 [1-7]