找回密码
 立即注册
查看: 223|回复: 4

[技术讨论] 分享个DIY气象站接入HA

[复制链接]

13

主题

127

回帖

892

积分

高级会员

积分
892
金钱
752
HASS币
0
发表于 4 天前 | 显示全部楼层 |阅读模式
1.jpg 2.jpg


esphome:
  name: weather-station
  friendly_name: Weather Station
esp8266:
  board: nodemcuv2

# 开启日志功能
#logger:

# 设置API
api:

time:
  - platform: homeassistant
    id: homeassistant_time
    on_time:
      # 每天午夜清除累计降雨量
      - seconds: 0
        minutes: 0
        hours: 0
        then:
          - pulse_counter.set_total_pulses:
              id: rainfall_counter
              value: 0  # 直接设置清零值
# 设置OTA
ota:
  - platform: esphome
    password: !secret ota_password  

#设置WIFI
wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  fast_connect: True  #不扫描直接连接,隐藏SSID必须启用
#  use_address: 192.168.88.57
  # 设置AP名称与密码
  ap:
    ssid: "WS"
    password: "12345678"

# 如果wifi连接失败,启动热点
captive_portal:

button:
  - platform: restart
    name: "Restart"

  - platform: template
    name: "Reset Rainfall"
    on_press:
      then:
        - pulse_counter.set_total_pulses:
            id: rainfall_counter
            value: 0  # 直接设置清零值

i2c:
  - id: bus_i2c
    sda: D1
    scl: D2

uart:
  tx_pin: D6
  rx_pin: D7
  baud_rate: 4800
  stop_bits: 1

modbus:
  send_wait_time: 200ms
  id: mod_bus

modbus_controller:
  - id: wdmc
    address: 0x01                 ##风向传感器的地址码
    modbus_id: mod_bus
    command_throttle: 0ms
    setup_priority: -10
    update_interval: 1min

  - id: wsmc
    address: 0x02                 ##风速传感器的地址码
    modbus_id: mod_bus
    command_throttle: 0ms
    setup_priority: -10
    update_interval: 1min

  - id: rsmc
    address: 0x03                 ##B-RS-L30 光照度传感器
    modbus_id: mod_bus
    command_throttle: 0ms
    setup_priority: -10
    update_interval: 1min

  - id: htmc
    address: 0x04                 ## B-HT-RS30 温湿度传感器
    modbus_id: mod_bus
    command_throttle: 0ms
    setup_priority: -10
    update_interval: 1min



binary_sensor:
  - platform: template
    id: rainy_state
    device_class: MOISTURE
    name: "Rainy State"
    lambda: |-
      if (id(rainy_state_voltage_sensor).state > 3.1) { //当ADC检测到电压低于3.1V返回false,否则返回true;
        return false; //返回false表示OFF
      } else {
        return true;  //返回true表示ON
      }


sensor:

# ESP8266仅内置1个10位ADC通道,该通道通过寄存器配置可选择测量外部电压(A0引脚)或系统电源电压(VCC)‌
#  - platform: adc
#    pin: VCC
#    name: "VCC Voltage"

  - platform: adc
    pin: A0
    name: "Rainy State Voltage"
    id: rainy_state_voltage_sensor
#    internal: True       ##不在前端显示
    update_interval: 2s  # 采样间隔
    unit_of_measurement: "V"
    accuracy_decimals: 2  # 保留两位小数
    filters:
      - calibrate_linear:
          # 分压公式校准(关键配置)
          # ADC原始值 0.0(0V)对应实际 0V
          # ADC原始值 1.0(1V输入时)对应实际 5V
          - 0.0 -> 0.0
          - 1.0 -> 3.2  # (220K+100K)/100K = 3.2倍分压
      - sliding_window_moving_average:  # 噪声滤波
          window_size: 15
          send_every: 5

  - platform: wifi_signal
    name: "WiFi Signal"
    update_interval: 10min 

  - platform: uptime
    name: Uptime
    id: id_sensor_uptime_raw
  #  update_interval: 10min
  #  internal: true
    type: timestamp


  - platform: bmp280_i2c
    i2c_id: bus_i2c  # 明确指定使用的 I2C 总线   
    address: 0x77
    update_interval: 60s
    temperature:
      name: "BMP280 temperature"
      oversampling: 16x
    pressure:
      name: "Air Pressure"

  - platform: modbus_controller
    modbus_controller_id: rsmc
    id: illuminance
    icon: mdi:brightness-5
    name: "Illuminance"            #B-RS-L30 光照度传感器
    address: 0x02                  #寄存器地址 2:光照度
    register_type: holding
    value_type: U_DWORD  # 32位无符号整型
    register_count: 2    # 读取连续2个寄存器(4字节)
    unit_of_measurement: "lx"
    accuracy_decimals: 0
    filters:
      - lambda: |-
          // 原始数据转换为浮点数并除以1000
          return (x / 1000.0f);

  - platform: modbus_controller
    modbus_controller_id: htmc
    id: HT_RS30_humidity
    icon: mdi:water-percent
    name: "Humidity"               #B-HT-RS30  温湿度传感器
    address: 0x00                  #寄存器地址 0:湿度
    #register_count: 2
    unit_of_measurement: "%"
    register_type: holding
    value_type: U_WORD
    accuracy_decimals: 1
    filters:
      - multiply: 0.1

  - platform: modbus_controller
    modbus_controller_id: htmc
    id: HT_RS30_temperature
    icon: mdi:home-thermometer-outline
    name: "Temperature"            #B-HT-RS30  温湿度传感器
    address: 0x01                  #寄存器地址 1:温度
    #register_count: 2
    unit_of_measurement: "°C"
    register_type: holding
    value_type: U_WORD
    accuracy_decimals: 1
    filters:
      - multiply: 0.1


  - platform: absolute_humidity
    name: "Absolute Humidity"   ##绝对湿度
    temperature: HT_RS30_temperature
    humidity: HT_RS30_humidity

  - platform: template
    name: "Dew Point"  ##露点计算
    lambda: |-
      return (243.5*(log(id(HT_RS30_humidity).state/100)+((17.67*id(HT_RS30_temperature).state)/
      (243.5+id(HT_RS30_temperature).state)))/(17.67-log(id(HT_RS30_humidity).state/100)-
      ((17.67*id(HT_RS30_temperature).state)/(243.5+id(HT_RS30_temperature).state))));
    unit_of_measurement: °C
    icon: 'mdi:thermometer-alert'



  - platform: modbus_controller   ##Modbus控制器平台;
    modbus_controller_id: wdmc    ##ModbusID;
    id: wind_direction            ##传感器ID;
    icon: mdi:sign-direction      ##实体图标;
    name: "Wind direction 360°"   ##实体名称 风向标;
    address: 0                    ##寄存器地址 0:风向(0-360°) 1:风向(0-15档);
    #register_count: 2
    unit_of_measurement: "°"      ##单位符号;
    register_type: holding
    value_type: U_WORD
    accuracy_decimals: 1          ##保留小数位数;
    filters:
      - multiply: 0.1             ##结果 x 0.1 ; 

  - platform: modbus_controller
    modbus_controller_id: wsmc
    id: wind_speed
    icon: mdi:wind-power
    name: "Wind speed"            #风速
    address: 0                    #寄存器地址 0:瞬时风速 
    #register_count: 2
    unit_of_measurement: "m/s"
    register_type: holding
    value_type: U_WORD
    accuracy_decimals: 1
    filters:
      - multiply: 0.1


##直接调用风速传感器值进行判断风力等级;
  - platform: modbus_controller
    modbus_controller_id: wsmc
    id: wind_scale
    icon: mdi:wind-power
    name: "Wind scale"   #风力等级
    address: 0                    #寄存器地址 0:瞬时风速 
    unit_of_measurement: "级风"
    register_type: holding
    value_type: U_WORD
    filters:
    lambda: |-
      if ( x <2 ) {id(wind_scale_text).publish_state("无风");return 0;}
      else if ( x <15 ){id(wind_scale_text).publish_state("软风");return 1;}
      else if ( x <33 ){id(wind_scale_text).publish_state("轻风");return 2;}
      else if ( x <54 ){id(wind_scale_text).publish_state("微风");return 3;}
      else if ( x <79 ){id(wind_scale_text).publish_state("和风");return 4;}
      else if ( x <107 ){id(wind_scale_text).publish_state("劲风");return 5;}
      else if ( x <138 ){id(wind_scale_text).publish_state("强风");return 6;}
      else if ( x <171 ){id(wind_scale_text).publish_state("疾风");return 7;}
      else if ( x <207 ){id(wind_scale_text).publish_state("大风");return 8;}
      else if ( x <244 ){id(wind_scale_text).publish_state("烈风");return 9;}
      else if ( x <284 ){id(wind_scale_text).publish_state("狂风");return 10;}
      else if ( x <326 ){id(wind_scale_text).publish_state("暴风");return 11;}
      else if ( x <369 ){id(wind_scale_text).publish_state("飓风");return 12;}
      else if ( x <414 ){id(wind_scale_text).publish_state("飓风");return 13;}
      else if ( x <461 ){id(wind_scale_text).publish_state("飓风");return 14;}
      else if ( x <509 ){id(wind_scale_text).publish_state("飓风");return 15;}
      else if ( x <560 ){id(wind_scale_text).publish_state("飓风");return 16;}
      else if ( x <612 ){id(wind_scale_text).publish_state("飓风");return 17;}
      else {return 99;}


# 脉冲计数传感器,IO14连接翻斗式雨量计,实测一个脉冲0.275mm
  - platform: pulse_counter
    id: rainfall_counter
    name: "Rainfall Pulse" #降雨量脉冲计数
    icon: "mdi:weather-pouring"
    pin:
      number: GPIO14
      mode: INPUT_PULLUP  # 启用内部上拉电阻
      inverted: true      # 信号低电平有效(干簧管闭合触发)
    unit_of_measurement: "pps"
    internal_filter: 10ms  # 防抖动滤波(10毫秒)
    count_mode:
      rising_edge: DISABLE  # 仅下降沿触发(干簧管闭合)
      falling_edge: INCREMENT
    accuracy_decimals: 0   # 脉冲计数取整数

    total:
      unit_of_measurement: 'mm'
      name: "Rainfall" #累计降雨量
      accuracy_decimals: 2    # 降雨量显示精度(0.28mm)
      icon: "mdi:weather-rainy"
      filters:
        - multiply: 0.275  # 脉冲转降雨量系数


text_sensor:
  - platform: wifi_info
    ip_address:
      name: "IP"
      id: id_sensor_ip
      icon: mdi:ip

############ 风力输出中文名称 ############
  - platform: template
    name: "Wind scale name"
    id: wind_scale_text
    icon: mdi:wind-power

############ 风向输出中文方位 ############
  - platform: modbus_controller
    modbus_controller_id: wdmc
    id: wind_direction_text
    bitmask: 0
    register_type: holding
    address: 1
    raw_encode: HEXBYTES
    name: "Wind direction 16Bit"
    icon: mdi:sign-direction
    filters:
      - map:
        - 0000 -> 北
        - 0001 -> 北东北
        - 0002 -> 东北
        - 0003 -> 东东北
        - 0004 -> 东
        - 0005 -> 东东南
        - 0006 -> 东南
        - 0007 -> 南东南
        - 0008 -> 南
        - 0009 -> 南西南
        - 000a -> 西南
        - 000b -> 西西南
        - 000c -> 西
        - 000d -> 西西北
        - 000e -> 西北
        - 000f -> 北西北


评分

参与人数 1金钱 +10 收起 理由
kaka0992 + 10 感谢楼主分享!

查看全部评分

回复

使用道具 举报

168

主题

2521

回帖

8376

积分

元老级技术达人

积分
8376
金钱
5682
HASS币
30
发表于 4 天前 | 显示全部楼层
产品图有没有。看看你放着在哪里的,我想不到合适的位置放,阳台窗户外?问题是供电麻烦
回复

使用道具 举报

13

主题

127

回帖

892

积分

高级会员

积分
892
金钱
752
HASS币
0
 楼主| 发表于 4 天前 | 显示全部楼层
bugensui 发表于 2025-4-27 14:08
产品图有没有。看看你放着在哪里的,我想不到合适的位置放,阳台窗户外?问题是供电麻烦 ...

这里有,https://bbs.hassbian.com/thread-24797-1-1.html
回复

使用道具 举报

24

主题

278

回帖

2150

积分

金牌会员

积分
2150
金钱
1848
HASS币
0
发表于 4 天前 | 显示全部楼层
qxdnzx 发表于 2025-4-27 17:46
这里有,https://bbs.hassbian.com/thread-24797-1-1.html

硬件能详细说下就完美了
回复

使用道具 举报

1

主题

27

回帖

374

积分

中级会员

积分
374
金钱
346
HASS币
0
发表于 前天 13:01 | 显示全部楼层
我最近也在搞,刚接好风速和雨雪传感器
回复

使用道具 举报

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

本版积分规则

Archiver|手机版|小黑屋|Hassbian

GMT+8, 2025-5-1 01:56 , Processed in 0.456256 second(s), 27 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

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