Software Development

SweetHomeHub: Home Control with Raspberry Pi and MQTT – Part 1

Since quite a long time I am working on my universal Raspberry Pi based Intertechno-Remote (see former posts 1 2 3 4):

intertechno-remote

I tried different approaches to trigger/control my remote control service via a custom HTTPServer/-Handler and a simple Vert.x verticle. Since MQTT v3.1.1 turns out as on of the de-facto standard protocols for the IoT I also implemented an MQTT client.

This MQTT client basically follows two design patterns:

  1. One topic for each device

    For each device a topic is defined. Its state can be controlled by publishing a message with payload “ON” or “OFF”.

    Pro:

    • the user must not know about the address code of the Intertechno device
    • changes of the address must not be published
    • the message is simply “ON” or “OFF to control the device

    Contra:

    • the user must know the topic for each device
    • the user can only control configured devices
  2. One topic for a JSON message

    Pro:

    • very flexible to control the devices

    Contra:

    • the user must know about the syntax of the JSON and the coding of devices

Solution:

Provide both options

One topic for each device

topics_table

My configuration is very simple

On start-up the Client is searching for sweethomehub-config.xml in the users home directory which is then unmarshalled from JAXB.

This configuration contains the codes and the topic for each device and the MQTT settings for the broker connection:

01
02
03
04
05
06
07
08
09
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
<configuration>
    <devices>
        <device>
            <houseCode>a</houseCode>
            <groupId>1</groupId>
            <deviceId>1</deviceId>
            <name>Light Front-Door</name>
            <mqttTopic>front/lights/door</mqttTopic>
        </device>
        <device>
            <houseCode>a</houseCode>
            <groupId>1</groupId>
            <deviceId>2</deviceId>
            <name>Light Terrace</name>
            <mqttTopic>garden/lights/terrace</mqttTopic>
        </device>
        <device>
            <houseCode>a</houseCode>
            <groupId>1</groupId>
            <deviceId>3</deviceId>
            <name>Fountain</name>
            <mqttTopic>garden/devices/fountain</mqttTopic>
        </device>
        <device>
            <houseCode>a</houseCode>
            <groupId>1</groupId>
            <deviceId>4</deviceId>
            <name>Light Garden</name>
            <mqttTopic>garden/lights/ambiente</mqttTopic>
        </device>
        <device>
            <houseCode>a</houseCode>
            <groupId>1</groupId>
            <deviceId>3</deviceId>
            <name>Light Living Room</name>
            <mqttTopic>livingroom/lights/ambiente</mqttTopic>
        </device>
    </devices>
    <mqttClientConfiguration>
        <mqttClientId>SweethoemMQTTClientId</mqttClientId>
        <mqttBrokerAddress>sweethome</mqttBrokerAddress>
        <mqttBrokerPort>1883</mqttBrokerPort>
        <mqttMessagesBaseTopic>sweethome</mqttMessagesBaseTopic>
    </mqttClientConfiguration>
</configuration>

And there is one additional topic awaiting the JSON commands: sweethome/devices/jsoncommand

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
{
  "devices":[
    {
      "device":{
        "name": "Light Front-Door",
        "houseCode": "a",
        "groupId": "1",
        "deviceId": "1"
      },
      "command":"ON"
    },
    {
      "device":{
        "name": "Light Terrace",
        "houseCode": "a",
        "groupId": "1",
        "deviceId": "2"
      },
      "command":"ON"
    },
    {
      "device":{
        "name": "Light Living Room",
        "houseCode": "a",
        "groupId": "1",
        "deviceId": "3"
      },
      "command":"ON"
    }
  ]
}

The central method to handle arrived messages:

MqttMessageReceiver.messageArrived

The JsonDeviceCommandProcessor:

JsonCommandProcessor

And the doSwitch methods:

DeviceControl

MQTT Client running on the Raspberry Pi waiting for messages:

mqtt-receiver1

… and receiving command messages:

mqtt-receiver-commands

Testing the receiver with MQTT.fx

mqtt-fx

Complete code can be found at BitBucket.

Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy

Jens Deters

Jens Deters is a Senior Software Developer working in the domain of Aviation Authorities. His main objectives are RIA and Desktop Applications also he loves to play with IoT related stuff.
Subscribe
Notify of
guest


This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button