Introducing QtKnx, the smart home library that translates your wishes into KNX protocol.

August 14, 2017 Lucie Gérard

QtKnx is the first major step to bring home automation at the finger tips of Qt users.
Multi-platform, elegant, flexible and easy to write software for smart homes will now be possible.
All it takes is the new QtKnx library, and the already existing functionalities of Qt.

KNX is the European leader for smart home and building protocol (https://www.knx.org), with its own PC based software for programming and controlling installations. The QtKnx library offers an open source and commercially deployable alternative for this technology.

As a first step, QtKnx can be used to build the KNX client implementation that discovers KNX servers, and then controls and manages the installation behind the server. Let’s have a look on how to do that.

Test case

We are testing the QtKnx functionalities on a small KNX installation, with a board and a server (see picture below). The server is connected to our intranet.

img_20170710_120035197

Discovering

Here is how we can discover this server using the QtKnx library:

  • get a discovery agent object (QKnxNetIpServerDiscoveryAgent agent)

  • set it up (optional)

  • ask the agent to start discovering (agent.start())

  • ask the agent for the list of discovered servers and the services they provide (agent.discoveredServers()).

    QKnxNetIpServerDiscoveryAgent agent;
    //To have the server's response sent only to the provided local IP address as opposed to the multicast one.
    agent.setResponseType(QKnxNetIpServerDiscoveryAgent::ResponseType::Unicast);
    //The local IP address the server's response shall be sent to.
    agent.setLocalAddress(QHostAddress("192.168.1.1"));
    agent.start();
    const auto servers = agent.discoveredServers();
    ...
    for (auto server : servers) {
        const auto serverAddress = server.controlEndpointAddress();
        const auto serverPort = server.controlEndpointPort();
        ...
        const auto serverServices = server.services();
        for (auto it = services.constBegin(); it != services.constEnd(); ++it) {
            qInfo().noquote() << QString::fromLatin1("      KNXnet/IP %1, Version: %2")
                        .arg(familieToString(it.key())).arg(it.value());
                }
    }

In our case, since we are working behind a firewall, only the server connected to the intranet was discovered. Here is what we get back 
using the above piece of code: 

1 server(s) found on the network.
  Server: KNX IP BAOS 777
      Individual address: 1.2.2
      Server control endpoint: 10.9.78.35:3671
    Supported services:
      KNXnet/IP Core, Version: 1
      KNXnet/IP Device Management, Version: 2
      KNXnet/IP Tunnel, Version: 1

Connecting to a KNX installation (e.g. using Tunneling services)

Once the server’s control endpoint address and port are retrieved from the discovery agent, we can establish a connection with the server in order to access the devices behind it. A tunneling connection allows, among other things, the sending of simple packages to, say, turn a light on and off.

To open this tunneling connection:

  • get a tunnel connection object (QKnxNetIpTunnelConnection tunnel)

  • set the local IP address. It will be passed to the server so it knows where to send its responses.

  • connect to the server using the previously discovered server’s control endpoint address and port (tunnel.connectToHost(serverIpAddress, serverPort))

  • use the tunnel to turn a light on and off, sending instructions to the KNX devices behind the server (tunnel.sendTunnelFrame(frame)). The control commands are encapsulated into Cemi frames. Fully automated and explicit frame building functions will be available soon.

  • when we’re done, we disconnect from the server (tunnel.disconnectFromHost())

    const auto servers = agent.discoveredServers();
    QHostAddress serverIpAddress = servers[0].controlEndpointAddress();
    quint16 serverPort = servers[0].controlEndpointPort();
    
    QKnxNetIpTunnelConnection tunnel;
    tunnel.setLocalAddress(QHostAddress("192.168.1.1"));
    // Connecting to the previously discovered server
    tunnel.connectToHost(serverIpAddress, serverPort);
    QKnxCemiFrame frame = ... ;
    tunnel.sendTunnelFrame(frame);
    tunnel.disconnectFromHost();

UI example

Of course, being a part of Qt, inserting QtKnx functionalities into your own UI is painless. Here is how our example UI and the QtCreator environment look like.

screenshot-from-2017-07-06-14-25-49

Brought to you soon: all basic functionalities needed by a KNX client implementation, to easily control and manage the installation, with no prerequisite knowledge of KNX protocol or high level packaging style necessary.

Brought to you later: functionalities to build a KNX server, and be able to program your installation with your own Qt written software.

Did this post raise your interest? In case it did, feel free to get in touch with us to gain access to a pre-release version.

Learn more about our offerings towards the Automation sector here and read our blog post.

The post Introducing QtKnx, the smart home library that translates your wishes into KNX protocol. appeared first on Qt Blog.

Previous
What “Automation” Means to Us
What “Automation” Means to Us

Hi! As you probably know, our mission has always been to provide the frameworks and tools to help developer...

Next Article
Introducing QtMqtt
Introducing QtMqtt

Recently, we talked about how we’re broadening our offering towards the automation sector. In case you miss...