esphome:
name: scales
friendly_name: Scales
esp32:
board: esp32dev
framework:
type: arduino
logger:
api:
encryption:
key: "P4LyRccQlamwbhSyExhGYwxI4rpadMs3G96DNEH211U="
ota:
password: "e683b53ecf4cfa24deef44dcaef4186e"
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
ap:
ssid: "Scales Fallback Hotspot"
password: "vsGnPD6LQ9CZ"
captive_portal:
uart:
id: uart_scales
tx_pin: GPIO17
rx_pin: GPIO16
baud_rate: 9600
sensor:
- platform: template
name: "Scales Weight"
id: scales_weight
unit_of_measurement: "kg"
accuracy_decimals: 3
update_interval: 5s
binary_sensor:
- platform: template
name: "Scales Stability"
id: stability_binary_sensor
- platform: template
name: "Scales Overload"
id: overload_binary_sensor
script:
- id: read_scales
mode: queued
then:
- lambda: |
static float last_weight = 0.0;
static bool is_stable = false;
static bool is_overload = false;
id(uart_scales).write_byte(0x05);
unsigned long start_time = millis();
bool got_ack = false;
uint8_t c; // Исправлено: uint8_t вместо char
while (millis() - start_time < 3000) {
if (id(uart_scales).read_byte(&c) && c == 0x06) {
got_ack = true;
break;
}
}
if (!got_ack) {
ESP_LOGD("scales", "No ACK received");
return;
}
id(uart_scales).write_byte(0x11);
String response = "";
start_time = millis();
while (millis() - start_time < 1000) {
if (id(uart_scales).read_byte(&c)) {
response += (char)c; // Исправлено: приведение к char при добавлении в строку
if (c == 0x04) break;
}
}
if (response.length() < 14) {
ESP_LOGD("scales", "Invalid data packet - too short");
return;
}
char stability = response[2];
char sign = response[3];
String weight_str = response.substring(4, 10);
is_stable = (stability == 'S');
is_overload = (sign == 'F');
if (!is_overload) {
float weight = weight_str.toFloat();
if (sign == '-') weight = -weight;
last_weight = weight;
id(scales_weight).publish_state(weight);
}
id(stability_binary_sensor).publish_state(is_stable);
id(overload_binary_sensor).publish_state(is_overload);
interval:
- interval: 5s
then:
- script.execute: read_scales