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

 找回密码
 立即注册
查看: 428|回复: 11

[技术探讨] 智能电表通过rs485走modbus协议接入ha

[复制链接]

2

主题

15

帖子

62

积分

注册会员

Rank: 2

积分
62
金钱
47
HASS币
0
发表于 4 天前 | 显示全部楼层 |阅读模式
本帖最后由 402096806 于 2025-2-18 13:21 编辑

ESP8266 MODBUS TCP转RTU
https://bbs.hassbian.com/thread-28615-1-1.html
(出处: 『瀚思彼岸』» 智能家居技术论坛)
结合上一个贴,我们做一个 MODBUS TCP转RTU的设备,

                               
登录/注册后可看大图

                               
登录/注册后可看大图
在这里用到3个设备:

1、永诺yn20e智能电表,来自咸鱼10块钱

2、esp01s一个3元

3.ttl转485模块一个2元

yn20e有485接口,支持modbus协议。用esp8266做一个modbus rtu tcp透传设备(源码已经分享)通过modbus tcp 传输到ha。
————————————————
configuration.yaml代码复制就可以使用
modbus:
  - type: tcp
    host: 192.168.1.15
    port: 502
    name: "ddsu666"
    sensors:
      - name: "A相电压"
        address: 0x0046
        scale: 0.1
        slave: 2
        precision: 2
        unit_of_measurement: V
        data_type: int16
        unique_id: "2023062821"
        device_class: voltage
      - name: "A相电流"
        address: 0x004C
        slave: 2
        precision: 2
        scale: 0.01
        unit_of_measurement: A
        data_type: int16
        unique_id: "2023062822"
        device_class: current
      - name: "A相有功 功率"
        address: 0x004F
        slave: 2
        precision: 2
        scale: 1
        unit_of_measurement: W
        state_class: total
        data_type: int16
        unique_id: "2023062823"
        device_class: energy
      - name: "A相无功 功率"
        address: 0x0053
        slave: 2
        precision: 2
        scale: 1
        unit_of_measurement: Var
        data_type: int16
        unique_id: "2023062824"
        device_class: current
      
      - name: "A相视功 功率"
        address: 0x0057
        slave: 2
        precision: 2
        scale: 1
        unit_of_measurement: VA
        state_class: total
        data_type: int16
        unique_id: "2023062825"
        device_class: energy
      - name: "A相功率因素"
        address: 0x005B
        slave: 2
        precision: 2
        scale: 0.001
        unit_of_measurement: F
        data_type: int16
        unique_id: "2023062826"
        device_class: current  
      - name: "频率"
        address: 0x005F
        slave: 2
        precision: 2
        scale: 0.01
        unit_of_measurement: HZ
        data_type: int16
        unique_id: "2023062827"
        device_class: current    
      - name: "当前总有功电能"
        address: 0x0063
        slave: 2
        precision: 2
        scale: 0.01
        unit_of_measurement: kWh
        state_class: total
        data_type: int32
        unique_id: "2023062828"
        device_class: energy  
      - name: "当前总有功电能1"
        address: 0x2000
        slave: 2
        precision: 2
        scale: 0.01
        unit_of_measurement: kWh
        state_class: total
        data_type: int32
        unique_id: "2023062829"
        device_class: energy    
      - name: "波特率"
        address: 0x0211
        slave: 2
        precision: 2
        scale: 1
        unit_of_measurement: HZ
        data_type: int16
        unique_id: "2023062830"
        device_class: current

                               
登录/注册后可看大图


永诺yn20e电表

永诺yn20e电表

485

485
微信图片_20250218131915.jpg
微信图片_202502161354451.jpg
微信图片编辑_20250216131410.jpg
回复

使用道具 举报

162

主题

2602

帖子

8040

积分

元老级技术达人

积分
8040
金钱
5433
HASS币
30
发表于 4 天前 | 显示全部楼层
怎么给8266供电的,外接usb数据线吗
回复

使用道具 举报

55

主题

1410

帖子

6080

积分

论坛DIY达人

积分
6080
金钱
4670
HASS币
30
发表于 4 天前 | 显示全部楼层
好像使用这种方式接入HA,不是显示为单个设备,而是多个零散的实体,很不爽
回复

使用道具 举报

2

主题

15

帖子

62

积分

注册会员

Rank: 2

积分
62
金钱
47
HASS币
0
 楼主| 发表于 4 天前 | 显示全部楼层
sorrypqa 发表于 2025-2-17 12:56
好像使用这种方式接入HA,不是显示为单个设备,而是多个零散的实体,很不爽 ...

是的啊
回复

使用道具 举报

50

主题

1916

帖子

7532

积分

论坛元老

Rank: 8Rank: 8

积分
7532
金钱
5606
HASS币
40
发表于 4 天前 | 显示全部楼层
sorrypqa 发表于 2025-2-17 12:56
好像使用这种方式接入HA,不是显示为单个设备,而是多个零散的实体,很不爽 ...

为何不在esphome处理,而是透传到ha
折腾精神永存,感恩感谢论坛每一位愿意分享和帮助过我的大佬,论坛有你更精彩
回复

使用道具 举报

2

主题

15

帖子

62

积分

注册会员

Rank: 2

积分
62
金钱
47
HASS币
0
 楼主| 发表于 4 天前 | 显示全部楼层
本帖最后由 402096806 于 2025-2-17 18:25 编辑
web_server:  
  port: 80
 
uart:
  id: mbus
  rx_pin: 2
  tx_pin: 0
  baud_rate: 9600
  data_bits: 8
  stop_bits: 1
 
modbus:
  id: modbus1
  uart_id: mbus
  send_wait_time: 200ms
 
modbus_controller:
  - id: modbus_device
    ## the Modbus device addr
    modbus_id: modbus1
    address: 3
    update_interval: 5s
 
switch:
  - platform: modbus_controller
    modbus_controller_id: modbus_device
    register_type: coil
    address: 0x0000
    name: "light1"
 
  - platform: modbus_controller
    modbus_controller_id: modbus_device
    register_type: coil
    address: 0x0001
    name: "light2"
 
  - platform: modbus_controller
    modbus_controller_id: modbus_device
    register_type: coil
    address: 0x0002
    name: "light3"
binary_sensor:
- platform: modbus_controller
  modbus_controller_id: modbus_device
  name: "开关"
  register_type: discrete_input
  address: 0x0008        

jjcs 发表于 2025-2-17 14:52
为何不在esphome处理,而是透传到ha


esphome也用了,感觉都不行啊,半个小时或者一个小时才更新一次数据
回复

使用道具 举报

50

主题

1916

帖子

7532

积分

论坛元老

Rank: 8Rank: 8

积分
7532
金钱
5606
HASS币
40
发表于 4 天前 | 显示全部楼层
402096806 发表于 2025-2-17 18:22
jjcs 发表于 2025-2-17 14:52
为何不在esphome处理,而是透传到ha

不可能把,你透传轮询也是一样的逻辑,只是一个底层处理一个应用层处理
折腾精神永存,感恩感谢论坛每一位愿意分享和帮助过我的大佬,论坛有你更精彩
回复

使用道具 举报

2

主题

15

帖子

62

积分

注册会员

Rank: 2

积分
62
金钱
47
HASS币
0
 楼主| 发表于 4 天前 | 显示全部楼层
jjcs 发表于 2025-2-17 18:38
不可能把,你透传轮询也是一样的逻辑,只是一个底层处理一个应用层处理 ...

esphome我也用了啊,你看上面的代码
回复

使用道具 举报

2

主题

15

帖子

62

积分

注册会员

Rank: 2

积分
62
金钱
47
HASS币
0
 楼主| 发表于 4 天前 | 显示全部楼层
jjcs 发表于 2025-2-17 18:38
不可能把,你透传轮询也是一样的逻辑,只是一个底层处理一个应用层处理 ...

esp怎么连接2个或者三个从站啊,我试了多次都不行
回复

使用道具 举报

2

主题

15

帖子

62

积分

注册会员

Rank: 2

积分
62
金钱
47
HASS币
0
 楼主| 发表于 3 天前 | 显示全部楼层
bugensui 发表于 2025-2-17 11:33
怎么给8266供电的,外接usb数据线吗

用的电表的3.3v
回复

使用道具 举报

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

本版积分规则

Archiver|手机版|小黑屋|Hassbian

GMT+8, 2025-2-21 20:58 , Processed in 0.160547 second(s), 34 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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