This is the 11th of my Blogs for the Bluetooth Unleashed Design Challenge
The other posts are here :-
The idea is to detect the bluetooth transmitted from the vehicle and signal other Home Automation functions.
If the vehicle is known then it can open the garage door, and inform the home owner that xx is home.
The detection point needs to be at the start of the driveway, and because there is no power source, this will need to be low power with solar charging.
The PSOC range seems a very good fit, but because of the timeline and my need to upskill, the inital design will be Arduino based and some form of RF transmitter/transceiver.
Adding a vehicle detection loop or beam is necessary to ensure those vehicles without bluetooth will also trigger the system.
In order to send simple messages back and forth, the MQTT protocol was developed.
Just to be sure this should allow you to install mosquitto on Raspbian Stretch. Mosquitto Debian repository | Eclipse Mosquitto
To use the new repository you should first import the repository package signing key:
wget http://repo.mosquitto.org/debian/mosquitto-repo.gpg.key sudo apt-key add mosquitto-repo.gpg.key
Then make the repository available to apt:
Then one of the following, depending on which version of debian you are using:
sudo wget http://repo.mosquitto.org/debian/mosquitto-stretch.list
Then update apt information:
sudo apt-get update
sudo apt-get install mosquitto mosquitto-clients
You can check if it has installed and what version by typing
In this case it is version 1.5
So now that it's installed, it's time to see if it's running.
MQTT defaults to using port 1883 so typing
netstat -l -t
This shows that it is indeed listening.
If it isn't then typing mosquitto -d will cause it to start listening.
I made comment in this post BT_Sentry : Notifications there was an open invitation
Internet of Holiday Lights: Join the secret IoT Service - open for all
MQTT broker info:
please post a keep-alive every minute
In this instance the broker used was iot.eclipse.org , and the Topic is element14_IoT
While this approach works, I prefer to keep it within my own Home Network, and this means configuring a broker, and pointing at the Raspberry Pi rather than something out in the wild.
You can see the default configuration
cat /etc/mosquitto/mosquitto.conf # Place your local configuration in /etc/mosquitto/conf.d/ # # A full description of the configuration file is at # /usr/share/doc/mosquitto/examples/mosquitto.conf.example pid_file /var/run/mosquitto.pid persistence true persistence_location /var/lib/mosquitto/ log_dest file /var/log/mosquitto/mosquitto.log include_dir /etc/mosquitto/conf.d
The mosquitto.conf.example file mentioned at line 06 is compressed, so you need to uncompress it first.
cd /usr/share/doc/mosquitto/examples sudo gunzip mosquitto.conf.gz
However IMO it is not useful.
There is a text based version with more detail here https://mosquitto.org/man/mosquitto-conf-5.html
I did find a useful method of checking it works locally here
Opening two puTTy terminals
start mosquitto by typing in one
which starts it in the background. (see the above mosquitto --help results)
then subscribe to a message with
mosquitto_sub -d -t OpenHAB_mqtt
in the second terminal window publish a message
mosquitto_pub -d -t OpenHAB_mqtt -m "Unknown Visitor"
You can see the results in the snapshot below.
So in this case I have used OpenHAB_mqtt as the topic
I passed a message Unknown Visitor to whomever was listening (ie subscribed), but I can also pass Known Visitor as well
There is more imformation available at
mosquitto_sub --help and mosquitto_pub --help
so in the above test
-d is enable debug messages
-t mqtt topic to subscribe to. May be repeated multiple times
-m message payload to send
While this configuration defaults to using the default host, I need to allow other devices to subscribe.
A quick check shows that
-h mqtt host to connect to. Defaults to localhost.
so adding this to our test example
mosquitto_sub -d -h xxx.xxx.x.xxx -t OpenHAB_mqtt mosquitto_pub -d -h xxx.xxx.x.xxx -t OpenHAB_mqtt -m "Known Visitor"
where xxx.xxx.x.xxx represents the Raspberry Pi address works fine.
Start MQTT on restart
Obviously we needed to start Mosquitto before the test.
If I am going to use this it needs to restart whenever the Raspberry Pi reboots.
I found mixed messages about this and it seems there has been a change when they went to version 1.5.
So the trick is to check if port 1883 is active and listening.
netstat -l -t
As you can see on the 2nd to last line there is a port listening on 1883.
But if I try my mosquitto_sub I get a connection refused.
I did some more searching for ways to start something after a reboot.
I discovered a whole bunch of different methods involving services, scripts and there seemed to no common simple method.
The above link shows 5 different ways to start something, and while it was useful, it struck me as wrong.
I searched and searched and kept finding methods that involved creating a script, BUT if you type mosquitto -d in a terminal, it will run.
So it struck me that there is no need for another script, to call another script/instruction.
In the end I edited /etc/mosquitto/conf.d/mosquitto.conf
sudo nano /etc/mosquitto/conf.d/mosquitto.conf
by adding mosquitto -d at the end
This is the edited version with the start at the bottom
This my modified version after the issues I identified in BT_Sentry : Connecting to MQTT Broker
# user mosquitto max_queued_messages 200 message_size_limit 0 allow_zero_length_clientid true # allow_duplicate_messages false bind_address xxx.xxx.xxx.xxx cafile /etc/mosquitto/ca_certificates/ca.crt certfile /etc/mosquitto/certs/home.gerdakloos.crt keyfile /etc/mosquitto/certs/home.gerdakloos.key autosave_interval 900 autosave_on_changes false persistence true persistence_file mosquitto.db allow_anonymous true
On a reboot now it will start listening on port 1883.
The default configuration uses the standard port, no username, no password, in other words pretty much zero security.
While this might be okay in my situation here in the countryside, it might not be suitable for anyone replicating this, so feel free to alter it to suit your needs.
Somewhere during this trip, I thought I needed to install paho-mqtt.
The first part is important to update the packages
sudo apt-get update sudo apt-get upgrade sudo apt-get dist-upgrade
The next three packages were
sudo apt-get install mosquitto mosquitto-clients python-mosquitto
While the first two installed fine, the last wouldn't.
Internet search to the rescue and some more things to istall.
sudo apt-get install python3-pip
After some time and lots of lines, it was installed.
sudo pip3 install paho-mqtt
The pip3 function is like apt-get but the feedback is less than user friendly.
I hate having a blank prompt and not knowing what's going on.
However a minute or so later and it came back with
So now that I have a mqtt broker that runs and listens, I'm not sure if I need this or not.
I was going to delete it, but then it may be handy for someone, and it's only consumming 1's and 0's, so I've left it for reference.
Time is marching on, and I'm trying to get the parts glued together.
I'm more interested on completing all the parts and understanding how they all combine, to get what I want.
As I eluded, the parts and interaction is there in my head, but translating it to the hardware is still a WIP.
The last challenge where we used OpenHAB, all the hardware was supplied ready to go out the box.
We simply configured OpenHAB, added a few extra parts, and tweaked some other bits.
My return to it shows that the understanding wasn't really there, so for this challenge I decided that I should better understand it rather than simply complete it.
I have the desire to expand on it, and already the mqtt does lend itself to publishing the temperature that comes from the gateway.
A small eINK display might make a great display.
I have one more update before the summary, and hopefully it shows the full detection to indoor indication works as I want.
July 16 16:12 NZDT Editied the configuration file as it was incorrect.
See the next blog regarding issues with mqtt broker being started by openhab2 rather than mosquitto.