element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Bluetooth Unleashed Design Challenge
  • Challenges & Projects
  • Design Challenges
  • Bluetooth Unleashed Design Challenge
  • More
  • Cancel
Bluetooth Unleashed Design Challenge
Blog BT_Sentry : MQTT
  • Blog
  • Forum
  • Documents
  • Polls
  • Files
  • Events
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: mcb1
  • Date Created: 15 Jul 2018 3:50 AM Date Created
  • Views 1060 views
  • Likes 7 likes
  • Comments 3 comments
  • mqtt
  • openhab
  • broker
  • startup
  • raspberry pi 3 b+
  • pip
  • mosquitto
  • apt-get
  • openhab_mqtt
Related
Recommended

BT_Sentry : MQTT

mcb1
mcb1
15 Jul 2018

This is the 11th of my Blogs for the Bluetooth Unleashed Design Challenge

 

 

The other posts are here :-

BT_Sentry Introduction
BT_Sentry : Zero Emission Detection
BT_Sentry : Data Transmission
BT_Sentry : Sentry Power
BT_Sentry : Raspberry Pi
BT_Sentry : Bluetooth Options
BT_Sentry : Software
BT_Sentry : Bluetooth Sniffing 
BT_Sentry : OpenHAB
BT_Sentry : Notifications

 

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

image

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

image

 

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. image

 

 

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.

image

 

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

image

 

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.

image

 

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.image

 

 

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.

https://learn.adafruit.com/diy-esp8266-home-security-with-lua-and-mqtt/configuring-mqtt-on-the-raspberry-pi

 

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.

 

image

 

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

image

 

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.

  • Sign in to reply

Top Comments

  • genebren
    genebren over 7 years ago +1
    Great work on your design challenge project. It really does feel like you are getting close, just a few more details to glue down. Good luck on your 'understanding' and 'completing' of your project! G…
  • DAB
    DAB over 7 years ago +1
    Nice update Mark, There is clearly more to MQTT than is apparent. DAB
  • mcb1
    mcb1 over 7 years ago in reply to DAB

    thanks DAB

    It turns out there were some other issues that were caused by openhab starting a broker.

     

    See BT_Sentry : Connecting to MQTT Broker

     

     

    Mark

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • DAB
    DAB over 7 years ago

    Nice update Mark,

     

    There is clearly more to MQTT than is apparent.

     

    DAB

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • genebren
    genebren over 7 years ago

    Great work on your design challenge project.  It really does feel like you are getting close, just a few more details to glue down.

    Good luck on your 'understanding' and 'completing' of your project!

    Gene

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube