After getting our hardware set up in part 1 we would now like to get us ready on the software side.
Intalling the required software
Here we want to install an MQTT Broker which acts as a hub to dispatch MQTT messages and we need a MQTT client to connect to the broker.
In this example I would like to use Mosquitto from the Eclipse foundation as the broker and Mosquitto Paho as the MQTT client.
Setting Mosquitto is fairly straight forward if you are running a Debian based Linux distribution (like Raspbian, Ubuntu etc.) as it is already available in the repostory and can be installed.
sudo apt-get intall mosquitto sudo apt-get intall mosquitto-clients
will do the trick. In most cases this will also install Mosquitto as dameaon so it will come up automatically after each reboot
systemctl status mosquitto
will show you if it was installed this way.
For the client I chose to use the python client Paho which requires Python 3 to be installed. Most Linux distributions will come with Python 2 installed and it is not advisable to just do an upgrade – we rather want to try an side by side installation.
We also need pip to be installed (a kind of python based package delivery)
sudo apt-get install python3 sudo apt-get install python3-pip
Now we can install Paho via
sudo pip3 install paho-mqtt
We should now have all the necessary software installed and it is time for a first quick test.
We use the Mosquitto client tools to check if Mosquitto is working correctly
This test can be performed on one or two systems. Just make sure that you have two seperate consoles open so you can see the imediate effect of your commands.
On the first session we start a subsription to a MQTT channel called “test” we want to listen to all topic in this cannel (so we subsribe to test/#)
mosquitto_sub -t 'test/#' -v -u user -P password
the shell will now sit and wait for messages that we now want to send via the second shell
Here we start
mosquitto_pub -t 'test/abc' -m 'foo'
Which will send the message “foo” to the channel “test/abc”
On our listening shell we will now see:
So Mosquitto is set up and running fine.
Hardening the system
Currently the broker is wide open and each and every client is allowed to subscribe and read/write to any channel. This is obviously dangerous as the curious mischief will be able to do all sorts of manipulation.
So we will harden the system a bit to make username+password mandatory and add a few of these.
new username/password combinations can be added to the system via:
cd /etc/mosquitto/ mosquitto_passwd /etc/mosquitto/passwd username
Then you will be prompted to provide a password for the new user.
(this is obviously only minimal security which is fine for our test case but should be reconsidered in productive scenarios)
Next we will need to edit the Mosquitto configuration file: /etc/mosquitto/conf.d/hcp.conf
Mosquitto will allow you to store individual configurations via a new file in the path /conf.d/*.conf
We will need to disallow anonymous access and provide the location to the password file.
Then we will test again if the configuration works with the subsciber: You can use the same user for both commands
mosquitto_sub -t 'test/#' -v -u username -P password
And the sender
mosquitto_pub -t 'test/abc' -m 'foo' -u username -P password
Now we can confirm that the username +pw setup is working as expected.
Automatically query the senosors and publish the data to the MQTT broker
I have modified the python script from part 1 to publish to the mqtt broker via paho.
#!/usr/bin/python3 # -*- coding: utf-8 -*- #The header is important make sure you use pyhton3 or paho will not work # Import the Modules import sys import os import time import paho.mqtt.client as mqtt def on_connect(client, userdata, flags, rc): print("Connected with result code " + str(rc)) client = mqtt.Client() #Set username and pw here client.username_pw_set("username",password="password") client.on_connect = on_connect #add the url or ip to your mqtt broker port 1883 is the default non encrypted port client.connect("192.168.0.xxx", 1883, 60) # read 1-Wire Slave-list file = open('/sys/devices/w1_bus_master1/w1_master_slaves') w1_slaves = file.readlines() file.close() # print temperature for each 1-Wire Slave for line in w1_slaves: # extract 1-wire Slave w1_slave = line.split("\n") # 1-wire Slave read file file = open('/sys/bus/w1/devices/' + str(w1_slave) + '/w1_slave') filecontent = file.read() file.close() # Read values and convert it to a floating point number in celsius stringvalue = filecontent.split("\n").split(" ") temperature = float(stringvalue[2:]) / 1000 #publish the values to mqtt broker client.publish("test/temperature/%s" % w1_slave, "%s" % temperature) # print temperature #print(str(w1_slave) + ': %s °C' % temperature) sys.exit(0)
I have added the script to the crontab and execute it every mintue to get a sensor reading.
crontab -e add */1 * * * * /opt/openhab/configurations/scripts/heizung.py > /dev/null
When we run a the subscriber script now we get the sensor reading for each temperature sensor:
In Part 3 we will learn how to connect the SCP IoT Service to our broker to receive our readings.