In this week I have managed to make a big step forward with setting up the Command Center. With this post I finalize the basic components in the RPI3 so hope to work on the features - those that will benefit us like the presence emulator, etc - in the coming weeks. Specifically, the post covers installing and configuring the openHAB that provides a web user-interface with interesting add-ons for home automation, Mosquitto that will "glue" all the code in the Command Center and the openHAB server, and the RF24 module to enable communication with the remote nodes via RF 2.4Ghz.
But first, the links to my previous posts and the project status.
Previous Posts
Project Status
Mosquitto - MQTT installation
Some projects in the Challenge are leveraging the MQTT so this time I will not go deep into what it is. Why am I using mosquitto? In the Command Center - RPI3 based - I will be running a number of processes that need to exchange information among them. For example, a C-module will be in charge of interacting with the 2.4Ghz comms, openHAB will be interacting with the Dashboard, also in the future maybe other scripts will be getting data from sensors in the RPI, etc. Mosquitto helps to "glue" all of them so that they can exchange information and actions in a seamlessly manner.
Installation
Caterina has written a nice post on how she did install Mosquitto: [Pi IoT] Smart Competition Home #4: Smarthome II - Sensors Node & other clients . I will just put here the command lines I executed without mentioning any caveats - these can be found in her post, thanks clazarom and rhe123 for your comments there!
sudo wget http://repo.mosquitto.org/debian/mosquitto-repo.gpg.key sudo apt-key add mosquitto-repo.gpg.key rm mosquitto-repo.gpg.key cd /etc/apt/sources.list.d/ sudo wget http://repo.mosquitto.org/debian/mosquitto-jessie.list sudo apt-get update sudo apt-get install mosquitto mosquitto-clients
There are, however, some additional packages that I will require to monitor the MQTT channels with the RF24 and Arduino. These are based on libmosquitto. There is however some sort of incompatibility and I need to tweak a bit the process. First I execute sudo apt-get update . When finished, in the file /etc/apt/sources.list I replace the following line
deb http://mirrordirector.raspbian.org/raspbian/ jessie main contrib non-free rpi
by this other one
deb http://mirrordirector.raspbian.org/raspbian/ wheezy main contrib non-free rpi
This change allows the RPI to successfully download the packages with this command
sudo apt-get install libmosquitto0-dev libmosquittopp0-dev
Once installed, I just undo the change in the sources.list file by replacing the wheezy word by jessie and finally rerun "sudo apt-get update". Now we have the libmosquito packages from the previous version and running in jessie. To find this out, it has take me... long hours... with a previous RPI2 based in wheezy I did not encounter this problem and it has caught me by surprise.. but there is some sort of satisfaction when you overcome it . You can find additional details on the installation here and on the compatibility problems here.
Testing
Let´s test whether the Mosquitto is correctly installed and working. To do so, I run a subscriber in my current SSH session (you can do it in a Terminal window):
mosquitto_sub -d -t hello/world
and in a separate SSH session (or Terminal window) I execute the publisher
mosquitto_pub -d -t hello/world -m "If you can read me, Mosquitto is correctly set up :)"
And, voila, here you have a screenshot of the successful communication between both processes using mosquitto:
We are now set for the next step, openHAB!
OpenHAB
According to themselves, openHAB is "a vendor and technology agnostic open source automation software for your home.". The cool thing in itself is that it can integrate lots of different systems almost off the shelf. For DomPi, I will be leveraging several features of it:
- ability to subscribe and publish into mosquitto channels, I will be able to communicate openHAB with any Python or C code and share status, information and actions
- webserver with an user interface that can be accessed by any browser
- a configurable sitemaps and item-maps that will allow a quick deployment of how to present info from the rooms into the webserver
- allows further improvements on the user interface and "look and feel" so that I can make it nicer with time
- applications that support the design of the sitemaps and the configuration, I will not be using this for DomPi at this stage, but when it becomes more complex, it will be of great help
So all in all, it is very scalable and fits my needs of quick delivery and future improvements in features and visual look
Installation of openHAB
The first thing would be to check what is the current version of openHAB, I´m just going to their download page. In the first section called openHAB Runtime I just mouse over the first download button and see that the url looks like https://bintray.com/artifact/download/openhab/bin/distribution-1.8.3-runtime.zip meaning it is the version 1.8.3 at time of writing the post. Note that there is a beta version 2 and will provide enhanced possibilities, since it is still beta, I will not leverage it at this point. To install openHAB I execute these commands:
sudo mkdir /opt/openhab cd /opt/openhab sudo wget https://bintray.com/artifact/download/openhab/bin/distribution-1.8.3-runtime.zip sudo unzip distribution-1.8.3-runtime.zip sudo rm distribution-1.8.3-runtime.zip
note that I put 1.8.3 as it is the current version, you may want to replace it for the relevant version you want to install.
OpenHAB works with add-ons and bindings. This allows you to install only those that you require for your project. For DomPi for example we will need the MQTT binding to enable the communication via mosquitto. I will just download all of the add-ons and then just copy the mqtt binding into the addons file of openHAB. This way, I am just installing this binding, avoiding to overload the RPI with features DomPi does not require. I execute these commands:
sudo mkdir addons_repo cd addons_repo sudo wget https://bintray.com/artifact/download/openhab/bin/distribution-1.8.3-addons.zip sudo unzip distribution-1.8.3-addons.zip sudo rm distribution-1.8.3-addons.zip cd /opt/openhab sudo cp addons_repo/org.openhab.binding.mqtt-1.8.3.jar addons/org.openhab.binding.mqtt-1.8.3.jar
just remember the earlier comments about the current version.
Configuration of openHAB
The distribution comes with a default config file that I will be using and modifying for DomPi. The first step is to make this file our config file by just copying it as follows:
sudo cp /opt/openhab/configurations/openhab_default.cfg /opt/openhab/configurations/openhab.cfg
Let´s now edit the file and make some changes. I´m using nano (sudo nano /opt/openhab/configurations/openhab.cfg). Now I locate the section called "Transport configurations", you really need to scroll down… I am replacing the line #mqtt:<broker>.url=tcp://<host>:1883 with my own line mqtt:dompimqtt.url=tcp://localhost:1883 - remember to remove the # which makes the line just a comment. Scrolling down a bit more, I modify this line #mqtt:<broker>.retain=<retain> by this other one mqtt:dompimqtt.retain=true This makes the broker to retain messages sent over the channels so that if a new node appears, the node will get the last of all of the channels' messages - this is good if your nodes may come up at different moments, you ensure all of them have the latest status of the channels of their interest
Now I'm ready to create the default_items file. This file contains the elements that openHAB will interact with, like for example the humidity in the main bedroom. The file gives a name to this element as well as the mqtt channel that openHAB should listen to to get the updates of the humidity. Another example, this file will have an element to turn on and off the light in the living room, in the file it will appear what needs openHAB to publish and in which mqtt channel to send the command.
sudo nano /opt/openhab/configurations/items/default.items
You can find my current file in the attachments, let me just comment on some of the lines included in the default.items file
Group All Group gPiso (All) Group P_Salon "Salon" <video> (gPiso)
This creates a main group called "All" and the ground floor called gPiso that belongs to All. Additionally, it creates a last group called P_Salon that will include the elements in my living room. In the complete file you can find an additional group per room I want to control at this point. After creating the groups, let´s create some items. Let me just pick again two lines of the file:
Number Nodo03Temperatura "Temperatura [%.1f C]" <temperature> (P_Salon) { mqtt="<[dompimqtt:casa/salon/temperatura:state:default] "} Switch Lampara_3 "Luz" { mqtt=">[dompimqtt:casa/salon/luz:command:ON:1],>[dompimqtt:casa/salon/luz:command:OFF:0]"}
The first line creates an item that will read the temperature of the living room. As a reminder of the architecture, the Arduino in the living room transmits via RF 2.4Ghz the temperature to the Command Center, in the Command Center there is some C code that reads the RF message and publish it as a mosquitto message, in this case, it will use the channel: casa/salon/temperatura. Then the openHAB application will receive the temperature via the mqtt message and will update the Node03Temperature item with the value. Coming back to the syntaxis, the "Number" tag defines the type of the value, Nodo03Temperatura is the name of the item, "Temperatura" is the name that will be displayed in the webserver, [%.1f C] defines the number to have 1 decimal digit and will put a C for Celsius. <temperature> is an icon that will appear close to the item in the webserver showing a thermometer. (P_Salon) is the group this item belongs to and the last part { mqtt="<[dompimqtt:casa/salon/temperatura:state:default] "} indicates where it will take the data from, note that the "<" means openHAB will read the data from this channel.
The second line creates a type switch so that you can move it left or right to activate or deactivate it, it is called Lampara_3 (translation Lamp_3), it will display just "Luz" in the webserver (translation: light). The final part { mqtt=">[dompimqtt:casa/salon/luz:command:ON:1],>[dompimqtt:casa/salon/luz:command:OFF:0]"} states what to do if the switch is in our position or the other. Basically the ">" command means that openHAB will write into the casa/salon/luz mosquitto channel and it will send the command 1, which will be shown as ON in the web interface
There are a long number of items that you can use in openHAB and you can find several that will meet your requirements.
Now that we have the items, let´s configure the Sitemap, or how they will be shown on the web interface. I start by creating the relevant file:
sudo nano /opt/openhab/configurations/sitemaps/default.sitemap
My file looks like this:
sitemap default label="DomPi - My Domek" { Frame label="Salon" { Switch item=Lampara_3 Text item=Nodo03Temperatura Text item=Nodo03Humedad Text item=Nodo03Luminosidad Text item=Nodo03Movimiento Switch item=Forzar_tx } Frame label="Dormitorio" { Switch item=Lampara_2 Text item=Nodo02Temperatura Text item=Nodo02Humedad Text item=Nodo02Luminosidad } Frame label="Habitacion Bebes" { Switch item=Lampara_1 Text item=Nodo01Temperatura Text item=Nodo01Humedad Text item=Nodo01Luminosidad } Frame label="Pasillo" { Switch item=Luces_Casa Text item=Nodo04Temperatura Text item=Nodo04Humedad Text item=Nodo04Luminosidad } }
The first line defines the sitemap I want to create and show in the web. Within it there are several frames, each representing on of the rooms or spaces I want to display to the user. Taking the example of the Salon (living room) you can find there a switch item for the lamp, four items displaying the temperature
, humidity, luminosity and movement detection and finally yet another switch to force the node (the Arduinos) to update us on the sensor status.
Let´s have a quick look at the web interface and see how this is getting all together. Just type:
http://192.168.xx.xx:8080/openhab.app
replacing the xx´s with my IP. On the right hand side you can see the screenshot so far.
As you can note there is no data as there has been nothing published into the mosquitto channels that the openHAB is listening to - we don't yet have any communication set up in the RPI between the command Center and the remote nodes. Let´s start solving this limitation! Next step, the RF24 module.
RF 2.4GHz Comms HW and libraries
As discussed in previous posts, the remote nodes - living room, bedrooms, garage and garden, Arduino based - will communicate with the Command Center RPI3 based. This communication is based on the RF 2.4GHz modules. I already connected the board to the Arduino's in previous weeks, let´s now connect the RF board to the RPI3. As with the Arduinos, although the RF board has 8 pins, the pin for the IRQ is not required for DomPi, so I only need to connect 7 as per this table:
NRF24L01+ Radio module | RPI3 GPIO pin (header pinout here) |
---|---|
MISO | MISO (21) |
MOSI | MOSI (19) |
SCK | SCKL (23) |
CSN | GPIO8 (24) |
CE | GPIO22 (15) |
VCC | Any 3.3v pin |
GND | Any GND pin |
IRQ | (none - not required) |
More details here.
Libraries
The same as for the Arduinos, I´m leveraging the library TMRh20. To install it in the Command Center I run these commands
mkdir RF24 cd RF24 wget http://tmrh20.github.io/RF24Installer/RPi/install.sh chmod +x install.sh ./install.sh rm install.sh
Now, all is set to start the communication between the RPI3 and the Arduino modules using the RF24 comms. The openHAB is ready and the link between the RF24 and the openHAB will be based on mosquitto.
Nodes´Dashboard
More development hopefully next week!
Note - I have intensively used this webpage as a guide to several installations and configurations: The Project - Home Automation For Geeks
Top Comments