เริ่มต้นเนื้อหา
YAML configuration สำหรับใช้ Micromation Lite V3 ในระบบ Home Assistant
เนื้อหาต่อไปนี้เป็นตัวอย่าง YAML configuration สำหรับใช้ Micromation Lite V3 ต่อกับ BGT-WSD2 RS485 Temperature & Humidity Sensor เข้าเป็นอุปกรณ์ในระบบ Home Assistant โดยใช้โปรแกรมเสริม ESPHome
วัตถุประสงค์
เพื่อให้ผู้ใช้บอร์ด Micromation Lite V3 สามารถนำโค้ดตัวอย่างในเนื้อหานี้ ไปใช้ในการสร้าง Device บนระบบ Home Assistant เพื่อควบคุมอุปกรณ์บนบอร์ดผ่าน Dashboard ของ Home Assistant ได้ตามต้องการ
ฮาร์ดแวร์ที่เชื่อมต่อ
- RS-485
- BGT-WSD2 Sensor
- Buzzer
- Custom Switch
- RELAYS 1-4
- OLDE Display
Environment ที่ใช้ทดสอบ
- Home Assistant 2023.5.3
- Supervisor 2023.04.1
- Operating System 10.1
- Frontend 20230503.3 – latest
- ติดตั้งบน Raspberry Pi4
อุปกรณ์
ชุดบอร์ด Micromation Dev Board V3 Lite ESP32 Relay board 4 Channel | X 1 | ||
Atmospheric Temperature & Humidity Sensor Modbus RS485 เซ็นเซอร์วัดอุณหภูมิและความชื้นอากาศภายนอก | X 1 | ||
Raspberry Pi4 | X 1 |
ซอฟแวร์ที่เกี่ยวข้อง
Home Assistant | |
ESPHome |
อธิบายโค้ด
RS-485
uart:
id: mod_bus
tx_pin: 17
rx_pin: 16
baud_rate: 9600
stop_bits: 1
BGT-WSD2
#BGT-WSD2 RS-485Temperature & Humidity Sensor
modbus:
send_wait_time: 200ms
id: mod_bus_bgtwsd2
modbus_controller:
- id: bgtwsd2
## the Modbus device addr
address: 0x1
modbus_id: mod_bus_bgtwsd2
command_throttle: 200ms
setup_priority: -10
update_interval: 5s
sensor:
#TEMP sensor
- platform: modbus_controller
modbus_controller_id: bgtwsd2
id: temperature
name: "TEMP"
device_class: TEMPERATURE
address: 0x001
unit_of_measurement: "°C"
register_type: holding
value_type: U_WORD
accuracy_decimals: 2
filters:
- multiply: 0.1
#HUMI sensor
- platform: modbus_controller
modbus_controller_id: bgtwsd2
id: humidity
name: "HUMI"
device_class: HUMIDITY
address: 0x000
unit_of_measurement: "%"
register_type: holding
value_type: U_WORD
accuracy_decimals: 2
filters:
- multiply: 0.1
Buzzer
# Buzzer
output:
- platform: ledc
pin: 5
id: buzzer
rtttl:
output: buzzer
Custom Switch
# Custom Switch
binary_sensor:
- platform: gpio
pin:
number: 4
mode:
input: true
pulldown: true
name: Custom button
id: custom_button
device_class: window
# When button press play sound to buzzer:
icon: "mdi:emoticon-outline"
on_press:
- logger.log: "Button pressed"
- output.turn_on: buzzer
- output.ledc.set_frequency:
id: buzzer
frequency: "1000Hz"
- output.set_level:
id: buzzer
level: "50%"
on_release:
- logger.log: "Button released"
- output.turn_off: buzzer
RELAYS
#RELAYS
switch:
- platform: gpio
name: "Relay1"
pin: 23
- platform: gpio
name: "Relay2"
pin: 19
- platform: gpio
name: "Relay3"
pin: 18
- platform: gpio
name: "Relay4"
pin: 32
OLDE Display
# OLDE Display
i2c:
sda: 21
scl: 22
font:
# gfonts://family[@weight]
- file: "gfonts://Roboto"
id: roboto
size: 20
- file:
type: gfonts
family: Roboto
weight: 900
id: font2
size: 16
display:
- platform: ssd1306_i2c
model: "SSD1306 128x64"
reset_pin: 0
address: 0x3C
id: my_display
pages:
- id: page1
lambda: |-
// Print time in HH:MM format
it.strftime(64, 0, id(roboto), TextAlign::TOP_CENTER, "%H:%M", id(esptime).now());
// Print temperature
if (id(temperature).has_state()) {
it.printf(0, 23, id(roboto), TextAlign::TOP_LEFT , "TEMP");
it.printf(127, 23, id(roboto), TextAlign::TOP_RIGHT , "%.1f°", id(temperature).state);
}
// Print humidity
if (id(humidity).has_state()) {
it.printf(0, 60, id(roboto), TextAlign::BASELINE_LEFT , "\%HUMI");
it.printf(127, 60, id(roboto), TextAlign::BASELINE_RIGHT , "%.1f", id(humidity).state);
}
#- id: page2
# lambda: |-
# it.print(0, 10, id(roboto), "This is page 2!");
รวมโค้ด
แหล่งอ้างอิง
- เรียนรู้การติดตั้ง Home Assistant บน Raspberry-Pi >> https://www.home-assistant.io/installation/raspberrypi
- เรียนรู้การเขียน Configuration ด้วย ESPHome >> https://esphome.io/