『瀚思彼岸』» 智能家居技术论坛

 找回密码
 立即注册
查看: 1027|回复: 2

[技术探讨] 蓝牙电子秤接入HA(需要先连接蓝牙设备)

[复制链接]

1

主题

40

帖子

467

积分

中级会员

Rank: 3Rank: 3

积分
467
金钱
427
HASS币
0
发表于 2024-9-10 00:24:53 | 显示全部楼层 |阅读模式
看了XCray的帖子之后就一直在研究自己的云麦电子秤,结果我的不是直接读广播数据,而是先连接蓝牙后才能读取数据,不能直接抄作业了。研究了半天有人在github上传过一个Yunmai-Scale-ESP32-BLE-to-MQTT 改了改只接入阻值和体重两个数据,已经稳定运行了几个月还可以,但是总觉得买个ESP32只接入一个体重秤实在有点浪费,就一直想研究通过ESPHOME接入,苦于无奈只会python,实在玩不转ESPHOME,熬了几个晚上最后还是通过AI一点一点拼凑出来了,现在分享给大家一下。
esphome:
  name: esp32-c3
  friendly_name: esp32
  platformio_options:
    board_build.flash_mode: dio
    board_build.mcu: esp32c3

esp32:
  board: esp32-c3-devkitm-1
  framework:
    type: arduino
    version: 'recommended'
    platform_version: '6.8.1'

# Enable logging
logger:

globals:
  - id: last_ble_notification
    type: uint64_t
# Enable Home Assistant API
api:
  encryption:
    key: "填自己的"

ota:
  - platform: esphome
    password: "填自己的"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Blell Fallback Hotspot"
    password: "puSeBVB5Izk7"

captive_portal:

web_server:
  port: 80

bluetooth_proxy:
  active: true

# Define the BLE Client
ble_client:
  - mac_address: "64:cf:d9:09:fa:40"  # Replace with your scale's MAC address
    id: my_ble_client
time:
  - platform: sntp
    id: sntp_time
    timezone: 'Asia/Shanghai'  # 例如 'America/New_York'
    servers:
      - ntp1.aliyun.com

sensor:
  - platform: template
    id: weight_sensor
    name: "Weight Sensor"
    unit_of_measurement: 'kg'
    accuracy_decimals: 2
    icon: "mdi:weight-kilogram"
  - platform: template
    id: resistance_sensor
    name: "Resistance Sensor"
    unit_of_measurement: 'Ω'
    accuracy_decimals: 0
    icon: "mdi:resistor"
  - platform: ble_client
    type: characteristic
    ble_client_id: my_ble_client
    name: "UPDATE"
    unit_of_measurement: 'sec'
    service_uuid: "ffe0"
    characteristic_uuid: "ffe4"
    notify: True
    lambda: |-
      auto now = id(sntp_time).now().timestamp;
      auto data = x;
      if (id(last_ble_notification) == 0 || (now - id(last_ble_notification))>= 4) {
          if (data.size() >= 20) {
              uint16_t weight_raw = (static_cast<uint8_t>(data[13]) << 8) | static_cast<uint8_t>(data[14]);
              float weight = weight_raw * 0.01f;
              float resistance_raw = (static_cast<uint8_t>(data[15]) << 8) | static_cast<uint8_t>(data[16]);
              ESP_LOGD("ble_client", "Weight: %.2f kg, Resistance: %u", weight, resistance_raw);
              id(weight_sensor).publish_state(weight);
              id(resistance_sensor).publish_state(resistance_raw);
              id(last_ble_notification)=now;
          }
          return id(last_ble_notification);
      } else {
          id(last_ble_notification)=now;
          ESP_LOGD("ble_client", "Ignoring notification, too soon after last one");
          return id(last_ble_notification);
      }
text_sensor:
  - platform: wifi_info
    ssid:
      name: Connected SSID
      icon: "mdi:signal-variant"
    ip_address:
      name: IP Address
      icon: "mdi:ip-network"
    mac_address:
      name: Mac Address
      icon: "mdi:wifi-marker"
    


评分

参与人数 1金钱 +12 收起 理由
隔壁的王叔叔 + 12 感谢楼主分享!

查看全部评分

回复

使用道具 举报

0

主题

13

帖子

316

积分

中级会员

Rank: 3Rank: 3

积分
316
金钱
303
HASS币
0
发表于 2024-9-10 08:34:40 | 显示全部楼层
十分感谢分享~我也有一个蓝牙设备。同事通过微信小程序连接蓝牙控制的。应该和楼主的电子秤类似。
回复

使用道具 举报

2

主题

28

帖子

660

积分

高级会员

Rank: 4

积分
660
金钱
632
HASS币
0
发表于 2024-9-13 13:30:46 | 显示全部楼层
这个只能节体重秤吗
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|Hassbian

GMT+8, 2024-11-24 00:23 , Processed in 0.382476 second(s), 26 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表