728x90
ETS๋ฅผ ์ด์ฉํด์ device์ group address๊ฐ ์ ๋ถ ์ค์ ๋ ์ดํ read/write ํ๋ ๊ฒ์ด ๋ชฉํ
1. knx ๊ฐ์ฒด ์ ์
from xknx import XKNX
from xknx.io import ConnectionConfig, ConnectionType
aysnc def main():
xknx = XKNX(
connection_config=ConnectionConfig(
gateway_ip="knx net/ip address",
gateway_port=3671,
connection_type=ConnectionType.ROUTING,
),
)
- router๋ฅผ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ(EX. ABB ips)
ConnectionType
์ Routing์ผ๋ก, TUNNELING๋ ์ค์ ๊ฐ๋ฅ - device๋ค์ด ์ด ๊ฐ์ฒด ํ์๋ก ๋ฌถ์ธ๋ค.
- ๋น๋๊ธฐ ์ฒ๋ฆฌ ํ์
2. device ์ ์
- xknx๊ฐ ์ง์ํ๋ ๋๋ฐ์ด์ค ๋ชฉ๋ก์ (https://xknx.io/devices) ์ฐธ๊ณ .
- ์ง์ํ์ง ์๋ ๋๋ฐ์ด์ค๋ return value ๋ฑ์ ๋ฐ๋ผ์
Sensor
,Binary Sensor
,Climate
๋ฑ์ผ๋ก ์ฌ์ฉ ๊ฐ๋ฅํ ์๋ ์์ ์๋ ์์async with xknx: temperature = Climate(xknx, "temp value", group_address_temperature="0/0/8") humidity = Sensor( xknx, "humidity value", group_address_state="0/0/11", sync_state=True, value_type="percent", )
- ๊ฐ ๋๋ฐ์ด์ค ๊ฐ์ฒด๋ง๋ค ํ์๋ก ์ง์ ํด์ค์ผํ๋ ๊ฐ์ด ์์ผ๋ฏ๋ก ์ฝ๋๋ฅผ ์ ์ฝ์ด๋ณด๊ณ ์ค์ ํ ๊ฒ.
3. value ์ฝ๊ธฐ
await temperature.sync(wait_for_result=True)
print("- Temperature: ", temperature.temperature.value)
device.sync
๋กgroup_address
์ ์๋ ๊ฐ์ ์ฝ์ด์ด- device์ ๋ฐ๋ผ ๊ฐ์ด ๋ค์ด๊ฐ์๋ ์์น๊ฐ ๋ค๋ฆ
- ex) climate ์จ๋ -> device.temperature.value
- ex) Sensor -> device.sensor_value.value
- ex) Binary Sensor -> device.remote_value.value
- ex) Switch -> device.state
4. value ์ฐ๊ธฐ(on/off)
- on/off ์ ์ด๊ฐ ์๋ ๊ฒฝ์ฐ
set_target_temperature
๊ณผ ๊ฐ์ด ๊ฐ์ผ๋ก ์ค์ ํ ์ ์๋ ๊ฒ์ผ๋ก ๋ณด์. - ๊ทธ๋ฌ๋ ํด๋ณด์ง ์์์ผ๋ฏ๋ก ๋ค๋ฃจ์ง๋ ์๊ฒ ์... ๋๋ ค๋์ ์์ ์ด ์์ด์ ์คํ ์ ํจ ํํ
heating = Switch(xknx, "heating", group_address="0/0/6") onoff = int(input()) if onoff == 1: await heating.set_on() else: await heating.set_off()
- Switch์ Light ๊ฐ์ฒด ๋ชจ๋
set_on()
๊ณผset_off()
ํจ์๋ก ๋๊ณ ํฌ ์ ์์
5. asyncio.gather๋ฅผ ์ฌ์ฉํ ๋์ ์คํ
async def sync_value(device):
await device.sync(wait_for_result=True)
print(f"{device.name}: Value = {device.temperature.value if device.name=='temp value' else device.sensor_value.value}")
await xknx.start()
await gather(*[sync_value(device) for device in devices])
await xknx.stop()
async with xknx
๋ฅผ ์ฐ๊ณ ์ถ์ง ์๋ค๋ฉดxknx.start
๋ฅผ ํด๋๊ณ ์๋์ ๋์์ํจ ๋ค์xknx.stop
ํด์ค๋ ๋จ- ๋ค๋ง SIGINT ๋ฐ์ ์ ๊น์ง ๋ฉ์ถ์ง ์์
- OPC ์ฒ๋ผ List ํํ๋ก ํ ๋ฒ์ ํต์ ์ผ๋ก ๋ณด๋ด๊ณ ํ ๋ฒ์ ํต์ ์ผ๋ก ๋ฐ์์ค๋๋ก ํ ์ ์์
- ๊ฐ๊ฐ์ device๊ฐ ๋ชจ๋ ๊ณ ์ ์ฃผ์๋ฅผ ๊ฐ์ง๊ณ ์๊ธฐ ๋๋ฌธ์
6. ์ํ ๋ณํ ์ ์๋ฆผ
- xknx ์ ์ธ ์์
device_update_cb
์ ์ฝ๋ฐฑ ํจ์๋ฅผ ์ค์ ํด์ฃผ๋ฉด xknx ๊ฐ์ฒด ์๋์ ๋ฌ๋ฆฐ device์ ์ํ๋ณํ๊ฐ ์์ ์์ ์๋ ค์ค - windows platform์ด ์๋๋ผ๋ฉด
daemon_mode=True
์ต์ ์ ํ์ฑํํด์ฃผ๋ฉด ๋จ - ์๋ ๊ฒฝ์ฐ์๋
xknx.start() - xknx.stop()
์ ํ์ฉ
import asyncio
from xknx import XKNX
from xknx.devices import Climate, Sensor, BinarySensor
from xknx.io import ConnectionConfig, ConnectionType
async def device_updated_cb(device):
print(f"Callback received from {device.name}")
if device.name == "heating fb":
value = device.remote_value.value
elif device.name == "temp value":
value = device.temperature.value
else:
value = device.sensor_value.value
print(f"+ {device.name} State Changed: ", value)
async def main():
xknx = XKNX(
connection_config=ConnectionConfig(
gateway_ip="knx net ip address",
gateway_port=3671,
connection_type=ConnectionType.ROUTING,
),
device_updated_cb=device_updated_cb,
# daemon_mode=True, # not supported on win32 platform
)
async with xknx:
heating_state = BinarySensor(xknx, "heating fb", group_address_state="0/0/7")
await heating_state.sync(wait_for_result=True)
print("- Heating State: ", heating_state.remote_value.value)
temperature = Climate(xknx, "temp value", group_address_temperature="0/0/8")
await temperature.sync(wait_for_result=True)
print("- Temperature: ", temperature.temperature.value)
await xknx.start()
# Wait until Ctrl-C was pressed
await xknx.stop()
- ์์ ๊ฐ์ด ๋ฑ๋กํด๋๊ณ on/off๊ฐ ๊ฐ๋ฅํ ๋๋ฐ์ด์ค๋ฅผ ๊ป๋ค ์ผฐ๋ค ํ๋ฉด
- ์ด๋ ๊ฒ ์ ๋์จ๋ค
728x90
'๐ etc.' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Obsidian] ํ๋ฌ๊ทธ์ธ_์ถ์ฒ_๋ฐ_10๊ฐ์_์ฌ์ฉํ๊ธฐ.md (3) | 2024.08.05 |
---|---|
[Protocol] KNX Overview (1) | 2023.12.15 |
[Obsidian] ์ปค๋ฎค๋ํฐ ํ๋ฌ๊ทธ์ธ ์ถ์ฒ ๋ฐ Git์ ํ์ฉํ ์ฐ๋๋ฒ (0) | 2023.10.20 |
[Obsidian] Notion vs Obsidian, ๋ด๊ฐ Obsidian์ผ๋ก ์ด์ฌํ ์ด์ (3) | 2023.10.19 |
[NVIDIA] ์๋์ฐ์์ ๊ฐ์๋จธ์ (WSL)์ผ๋ก Jetson AGX Xavier ๋ถํ ํ๊ธฐ (18) | 2023.04.14 |