This is the 11th of my Blogs for the Bluetooth Unleashed Design Challenge
The other posts are here :-
Concept
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.
Hardware
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.
Mosquitto
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:
cd /etc/apt/sources.list.d/
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
just install:
sudo apt-get install mosquitto mosquitto-clients
You can check if it has installed and what version by typing
mosquitto --help
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.
Broker
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:
host: iot.eclipse.org
port: 1883
please post a keep-alive every minute
QoS: 1
Topic: element14_IoT
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
As is the case with many of these things, they are more like a reference document, than a beginners guide.
I did find a useful method of checking it works locally here
Installing MQTT Broker(Mosquitto) on Raspberry Pi
Opening two puTTy terminals
start mosquitto by typing in one
mosquitto -d
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.
eg https://www.dexterindustries.com/howto/run-a-program-on-your-raspberry-pi-at-startup/
The above link shows 5 different ways to start something, and while it was useful, it struck me as wrong.
In windows you drop it into startup and it runs, it's as simple as that.
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.
Paho-mqtt
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.
https://www.abelectronics.co.uk/kb/article/1085/io-pi-tutorial---mqtt-control
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
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.
Mark
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.
Top Comments