KNX with micropython

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
henfri
Posts: 4
Joined: Sun Oct 28, 2018 1:20 pm

KNX with micropython

Post by henfri » Sun Oct 28, 2018 1:29 pm

Hello,

KNX is an international standard for HomeAutomation.
I would like to use it with MicroPython. I found these libraries:

https://pypi.org/project/knx/
https://xknx.io/
https://github.com/usul27/homecontrol/tree/master/knx
https://github.com/knxd/PyKNyX
https://www.pknyx.org/

I am wondering whether one of these libraries is likely to work with micropython.
Especially this one (https://github.com/usul27/homecontrol) seems to have few requirements:
import socket
import threading
import SocketServer
import sys
import logging

But also this one has few requirements:
https://github.com/mfussenegger/knx
import asyncio as aio
import socket
import struct
from collections import namedtuple
from setuptools import setup

What's yor view?

Regards,
Hendrik

User avatar
WhiteHare
Posts: 129
Joined: Thu Oct 04, 2018 4:00 am

Re: KNX with micropython

Post by WhiteHare » Sun Oct 28, 2018 3:15 pm

My view is try it. Then you'll know for sure. ;)

If you don't already have a micropython platform, you could try it on the unix emulator (which exists somewhere, as pythoncoder refers to it on some forum threads here) to see if your libraries are likely to run.

dschwert
Posts: 12
Joined: Mon Sep 11, 2017 4:30 pm

Re: KNX with micropython

Post by dschwert » Sun Oct 28, 2018 5:09 pm

I'm using my own small KNX module (unfortunately I can't attach a file here). It's around 9k in size. It does not have any dependencies other than pyb and struct
It has been in use now for one year on a small board with NCN5120 and DS2482 to send the room temperature on the bus. The plan is to use it also for humidity measurements and window contacts. The window contacts can probably be handled by an interrupt.
The module will also work with the Pyboard and a Siemens BCU.

The main loop looks like this:

Code: Select all

while True:
    pyb.delay(1000)
    for sensor,GA in zip(temp_sensors, temp_GAs):
        sensor.convert_temp()
        pyb.delay(1000)
        T = sensor.read_temp()
        if T is not False:
            knx.send(GA, T, 9)
or for testing

Code: Select all

    knx = KNX(uart=4)  # PyBoardKNX is UART 4
    knx.send("2/6/130", 1, 1)

    last_state = None
    while True:
        received = knx.receive()
        if received:
            print(received, knx.error_counters)
        pyb.delay(1)

Regards,

Dietmar

User avatar
WhiteHare
Posts: 129
Joined: Thu Oct 04, 2018 4:00 am

Re: KNX with micropython

Post by WhiteHare » Sun Oct 28, 2018 5:17 pm

dschwert wrote:
Sun Oct 28, 2018 5:09 pm
I'm using my own small KNX module (unfortunately I can't attach a file here). It's around 9k in size.
You can probably attach it if you zip it. Otherwise, if that fails, you can post it to pastbin.com (free) and then post a link to that. Or, of course, github, which it also free.

Now you've both got me curious about KNX and what can be done with it.

dschwert
Posts: 12
Joined: Mon Sep 11, 2017 4:30 pm

Re: KNX with micropython

Post by dschwert » Sun Oct 28, 2018 5:22 pm

Thanks. See attached file.
Attachments
KNX.zip
KNX module
(2.64 KiB) Downloaded 417 times

kevinkk525
Posts: 969
Joined: Sat Feb 03, 2018 7:02 pm

Re: KNX with micropython

Post by kevinkk525 » Sun Oct 28, 2018 6:28 pm

would be great if you could upload your library to a github repository. That makes it a lot easier for other people to continue/contribute to development or adopting it for other platforms.
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode

henfri
Posts: 4
Joined: Sun Oct 28, 2018 1:20 pm

Re: KNX with micropython

Post by henfri » Sun Oct 28, 2018 7:04 pm

Hi Dietmar,

thanks. I am thrilled how quickly I got help here.
Don't you think, it would be great if the library could also work via IP instead of only via Serial/TPUART?
This way, one would create KNX devices rapidly with virtually no cost.

Let me know if you need help setting up a repository in github.

Greetings,
Hendrik

dschwert
Posts: 12
Joined: Mon Sep 11, 2017 4:30 pm

Re: KNX with micropython

Post by dschwert » Sun Oct 28, 2018 7:26 pm

I will do so in the next days.

dschwert
Posts: 12
Joined: Mon Sep 11, 2017 4:30 pm

Re: KNX with micropython

Post by dschwert » Sun Oct 28, 2018 8:06 pm

henfri wrote:
Sun Oct 28, 2018 7:04 pm
Don't you think, it would be great if the library could also work via IP instead of only via Serial/TPUART?
This way, one would create KNX devices rapidly with virtually no cost.
What hardware do you think of? Do you want to use e.g. an ESP and actually connect via WLAN instead of KNX?
My focus was on hardware with a TPUART on board. My own hardware has the same form factor than the Konnekting Multi interface. For a STM32F405 without SD card, you have only around 95 kB Flash memory so I need to keep the libraries small.

On my Raspberry with eibd I'm using the attached KNX library that is using asyncio.open_connection to connect to the eibd running on either the same or a different computer. The library has a mainloop as asyncio.coroutine, a send method and
The size of the library is around 26k. The dependencies are collections, asyncio, time, struct. I would think that it could be adapted to MicroPython but that's certainly not something I will do.
When I did the library, I had a look at Smarthome.py and found that it's easier for me to write my own code than to learn setting up Smarthome.py.
I don't know how it compares to the other available options, but it might work for you as I always try to avoid dependencies.


Some (not very useful) code snippets from the main program that is using the library:

Code: Select all

import KNX
knx = KNX.KNX("192.168.0.17", try_local=LINUX, monitor_eibd=LINUX, log_messages=log_message)
#knx.register_raw_handler( logger.log )

knx.register("0/5/1", 1, "Winter", read_cached=True) # Sommer / Winter (0/1)
knx.register_handler("0/5/1", on_winter_summer)

asyncio.async( knx.mainloop() )

loop = asyncio.get_event_loop()
try:
    loop.run_forever()
finally:
    loop.close()


Depending on what you want to accomplish, things could even be simpler. My first code to write someting to the bus looked like this:

Code: Select all

import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(1.0)
s.connect( ("192.168.0.17", 6720) )

import time
s.send(build_telegram( "HHB", [EIB_OPEN_GROUPCON,0,0] ));time.sleep(1.0); print "OPEN_GROUPCON", repr(s.recv(20))

last_value = 0
GA = "2/6/070"
GA = decode_GA_string(GA)

while True:
    raw_input("Hit Enter")
    last_value = 1-last_value
    s.send(build_telegram( "HHBB", [EIB_GROUP_PACKET,GA,0, KNXWRITE | last_value] ))
Regards,
Dietmar
Attachments
KNX.zip
(6.75 KiB) Downloaded 293 times

henfri
Posts: 4
Joined: Sun Oct 28, 2018 1:20 pm

Re: KNX with micropython

Post by henfri » Sun Oct 28, 2018 8:25 pm

Hello Dietmar,

thanks!
Yes, I want to use and ESP8266 and directly read/write to the bus.
So, if I see that correctly, you have psted two options:
a) use TPUART
b) use KNXD/eibd
for option
c) direct access to KNX
there is no option yet.
I am a bit hesitant to rely on KNXd, but it's an option.
In the meanwhile, I have checked the beforementioned libraries.
None of them works out of the box.

Here the missing libararies:
https://github.com/mfussenegger/knx/blob/master/knx.py
asyncio as aio
struct
collections

XKNX:
asyncio
logging
signal

https://github.com/usul27/homecontrol/tree/master/knx
threading
SocketServer
logging

https://github.com/knxd/PyKNyX/tree/master/pyknyx
Too many files to test.

Greetings,
Hendrik

Post Reply