Bookmark

[OpenHAB] Công tơ điện tử với PZEM004T và ESPHome trên ESP32-C3.

Post img header

Thông tin và yêu cầu

  • OpenHAB version: 4.3.3
  • Đã cài đặt MQTT binding
  • PZEM004T v1 (bản xuất khẩu) or V3 nội địa - tham khảo giá trên shopee
  • ESP32, ESP32-C3 SuperMini ... nếu là ESP32-C3 SuperMini thì nên mua bản có anten (tham khảo ở shopee )

Hướng dẫn build sử dụng ESPHome

  • Tham khảo dự án ESPHome ở đây
  • Cài đặt ESPHome vào PC theo hướng dẫn ở đây
  • Dưới đây là code mình đang sử dụng để gửi MQTT đến server (MQTT Broker) mỗi 2s
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
esphome:
  name: pzem004t-mqtt
  friendly_name: PZEM004T to MQTT
  min_version: 2025.3.2
  name_add_mac_suffix: true
  platformio_options:
    board_build.f_flash: 40000000L
    board_build.flash_mode: dio
    board_build.flash_size: 4MB
    
esp32:
  board: esp32-c3-devkitm-1
  framework:
    type: esp-idf
    
uart:
  rx_pin: 8
  tx_pin: 9
  baud_rate: 9600

modbus:

mqtt:
  broker: "192.168.0.4"
  username: "user"
  password: "password"
  discovery: false
  discover_ip: false

wifi:
  ssid: "WIFI SSID"
  password: "********"

ota:
  - platform: esphome
    password: "******"

logger:
  level: INFO

sensor:
  - platform: pzemac
    current:
      name: "PZEM-004T Current"
    voltage:
      name: "PZEM-004T Voltage"
    energy:
      # Nhân Energy với 0.001 để quy đổi đơn vị từ Wh sang kWh
      name: "PZEM-004T Energy"
      filters:
        - multiply: 0.001
    power:
      name: "PZEM-004T Power"
    frequency:
      name: "PZEM-004T Frequency"
    power_factor:
      name: "PZEM-004T Power Factor"
    update_interval: 2s
    
  - platform: wifi_signal
    name: "WiFi Signal dB"
    id: wifi_signal_db
    update_interval: 300s

  - platform: copy
    source_id: wifi_signal_db
    name: "WiFi Signal Percent"
    filters:
      - lambda: return min(max(2 * (x + 100.0), 0.0), 100.0);
    unit_of_measurement: "Signal %"
  • Dòng 17-18 cấu hình chân RX-TX trên ESP32-C3
  • Dòng 24-26 cấu hình MQTT Broker
  • Dòng 31-32 cấu hình WIFI
  • Dòng 36 cấu hình mật khẩu khi update OTA
  • Dòng 58 tần suất cập nhật số liệu lên MQTT Broker

Kiểm tra MQTT Broker xem đã nhận số liệu chưa

Cấu hình Things trên OpenHAB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
UID: mqtt:topic:17653025f5:3574a13d85
label: PZEM004T Power Monitor
thingTypeUID: mqtt:topic
configuration: {}
bridgeUID: mqtt:broker:17653025f5
location: House
channels:
  - id: PZEM004T_House_Voltage
    channelTypeUID: mqtt:number
    label: PZEM004T House Voltage
    description: ""
    configuration:
      stateTopic: pzem004t-mqtt-be5d84/sensor/pzem-004t_voltage/state
  - id: PZEM004T_House_Power
    channelTypeUID: mqtt:number
    label: PZEM004T House Power
    description: ""
    configuration:
      stateTopic: pzem004t-mqtt-be5d84/sensor/pzem-004t_power/state
  - id: PZEM004T_House_Current
    channelTypeUID: mqtt:number
    label: PZEM004T House Current
    description: ""
    configuration:
      stateTopic: pzem004t-mqtt-be5d84/sensor/pzem-004t_current/state
  - id: PZEM004T_House_Energy
    channelTypeUID: mqtt:number
    label: PZEM004T House Energy
    description: ""
    configuration:
      stateTopic: pzem004t-mqtt-be5d84/sensor/pzem-004t_energy/state
  - id: PZEM004T_House_Freq
    channelTypeUID: mqtt:number
    label: PZEM004T House Frequency
    description: ""
    configuration:
      stateTopic: pzem004t-mqtt-be5d84/sensor/pzem-004t_frequency/state
  - id: PZEM004T_House_PowerFactor
    channelTypeUID: mqtt:number
    label: PZEM004T House PowerFactor
    description: ""
    configuration:
      stateTopic: pzem004t-mqtt-be5d84/sensor/pzem-004t_power_factor/state
  - id: PZEM004T_House_Sensor_Status
    channelTypeUID: mqtt:string
    label: PZEM004T House Sensor Status
    description: ""
    configuration:
      stateTopic: pzem004t-mqtt/status
  - id: PZEM004T_House_Wifi_Sginal_Level_dB
    channelTypeUID: mqtt:number
    label: PZEM004T House Wifi Sginal Level dB
    description: ""
    configuration:
      stateTopic: pzem004t-mqtt-be5d84/sensor/wifi_signal_db/state
  - id: PZEM004T_House_Wifi_Sginal_Level_Percent
    channelTypeUID: mqtt:number
    label: PZEM004T House Wifi Sginal Level Percent
    description: ""
    configuration:
      stateTopic: pzem004t-mqtt-be5d84/sensor/wifi_signal_percent/state
  • BONUS DSL Script tính số điện sử dụng trong ngày,tháng, khoảng thời gian
    • Dữ liệu sẽ update vào 3 item là: Energy_Consumption_Day, Energy_Consumption_Month, Energy_Consumption_Period.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
    logInfo("EnergyConsumption", "Current Energy: " + PZEM004T_House_Energy.state.toString + " - " + now.toString)

    val startOfDay = now.with(LocalTime.MIDNIGHT)
    val startOfMonth = now.withDayOfMonth(1).with(LocalTime.MIDNIGHT)
    val startOfPeriod = now.withMonth(4).withDayOfMonth(1).with(LocalTime.MIDNIGHT) // Bắt đầu vào 1 tháng 4
    
    val dayAgoValue = PZEM004T_House_Energy.persistedState(startOfDay)
    val monthAgoValue = PZEM004T_House_Energy.persistedState(startOfMonth)
    val periodAgoValue = PZEM004T_House_Energy.persistedState(startOfPeriod)
      
    val currentEnergy = PZEM004T_House_Energy.state as QuantityType<Number>
    val dayEnergy = dayAgoValue.state as QuantityType<Number>
    val monthEnergy = monthAgoValue.state as QuantityType<Number>
    val periodEnergy = periodAgoValue.state as QuantityType<Number>

    val dailyConsumption = (currentEnergy.subtract(dayEnergy)).doubleValue
    val monthlyConsumption = (currentEnergy.subtract(monthEnergy)).doubleValue
    val periodConsumption = (currentEnergy.subtract(periodEnergy)).doubleValue

    // Update Items with calculated values
    Energy_Consumption_Day.postUpdate(dailyConsumption)
    Energy_Consumption_Month.postUpdate(monthlyConsumption)
    Energy_Consumption_Period.postUpdate(periodConsumption)

0 Bình luận

Góp Ý / Bình Luận / Đánh giá