Saturday, December 21, 2024
How ToIoT PlatformsIoT ProtocolsTutorials/DIY

How to setup a Mosquitto MQTT Server and receive data from OwnTracks

Here you can find a tutorial “How to setup a Mosquitto MQTT Server and receive data from OwnTracks”.

Mosquitto is an open source (EPL/EDL licensed) message broker that implements the MQTT protocol versions 3.1 and 3.1.1. It is lightweight and is suitable for use on all devices from low power single board computers to full servers. The MQTT protocol provides a lightweight method of carrying out messaging using a publish/subscribe model. This makes it suitable for Internet of Things messaging such as with low power sensors or mobile devices such as phones, embedded computers or microcontrollers.

You may like also : What is Mosquitto ?

OwnTracks is an Open Source project which provides an iOS and an Android app with which your smartphone records its current location.

So OwnTracks is an open-source GPS location history logging service (the main components are apps for iPhone and Android.) OwnTracks takes care of sending the data, and recommends using Mosquitto as the framework on the receiving or “broker” side.

Know more about OwnTracks Visit this https://owntracks.org/booklet/

This is the process I went through to get a Mosquitto server up and receiving data on a DigitalOcean droplet, I customized the server for OwnTracks but most steps except config should apply for any use of Mosquitto server.

REQUIREMENTS:

It runs on Raspberry Pi, so I figured a 512MB Ubuntu 14.04 x64 Droplet would be sufficient.

Create user “mosquitto”

Mosquitto wants to run as user mosquitto, adduser mosquitto

adduser mosquitto

Install Mosquitto

SSH into the droplet, do an update, and then install mosquitto dependencies

apt-get update
apt-get install build-essential libwrap0-dev libssl-dev libc-ares-dev uuid-dev xsltproc

You could try installing via apt-get, it didn’t work for me so I downloaded the latest release of mosquitto listed here: http://mosquitto.org/download/

cd /home/mosquitto
wget http://mosquitto.org/files/source/mosquitto-1.4.8.tar.gz
tar xvzf mosquitto-1.4.8.tar.gz
cd mosquitto-1.4.8

Run make to compile and make install to install

make
make install

Setup Mosquitto

Create a mosquitto user/password: the command below will create a user owntracks, you can change

mosquitto_passwd -c /etc/mosquitto/pwfile owntracks

you will be prompted to enter a password.

Create the directory where persistence db files will be stored, change owner to mosquitto:

mkdir /var/lib/mosquitto/
chown mosquitto:mosquitto /var/lib/mosquitto/ -R

Create a config file by copying the example file:

cp /etc/mosquitto/mosquitto.conf.example /etc/mosquitto/mosquitto.conf
editor /etc/mosquitto/mosquitto.conf

end of the config file, add a block of all suggested config changes specific to OwnTracks (replace <yourIP> with the IP address of the droplet)

listener 8883 <yourIP>
persistence true
persistence_location /var/lib/mosquitto/
persistence_file mosquitto.db
log_dest syslog
log_dest stdout
log_dest topic
log_type error
log_type warning
log_type notice
log_type information
connection_messages true
log_timestamp true
allow_anonymous false
password_file /etc/mosquitto/pwfile

Finally be sure to run:

/sbin/ldconfig

Run/Test Mosquitto

Run the mosquitto server with this command:

mosquitto -c /etc/mosquitto/mosquitto.conf

It should start running without error, then in another window: Replace <YourIP> and <YourPassword> with your own stuff

mosquitto_sub -h <YourIP> -p 8883 -v -t 'owntracks/#' -u owntracks -P <YourPassword>

If everything went correctly you should see no errors executing this command, and in the window where mosquitto is running should acknowledge the connection. if so create an upstart file to autorun mosquitto:

vim /etc/init/mosquitto.conf

#THEN PASTE IN:

description "Mosquitto MQTT broker"
start on net-device-up
respawn
exec /usr/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf

Now You have Mosquitto broker working, your next step is to Setup OwnTracks on your phone to speak to your broker – The TL;DR is you need to install the App, go to preferences, select PRIVATE mode, and set the connection details to match the IP, user, and password specs you just setup.


Harshvardhan Mishra

Hi, I'm Harshvardhan Mishra. Tech enthusiast and IT professional with a B.Tech in IT, PG Diploma in IoT from CDAC, and 6 years of industry experience. Founder of HVM Smart Solutions, blending technology for real-world solutions. As a passionate technical author, I simplify complex concepts for diverse audiences. Let's connect and explore the tech world together! If you want to help support me on my journey, consider sharing my articles, or Buy me a Coffee! Thank you for reading my blog! Happy learning! Linkedin

Leave a Reply

Your email address will not be published. Required fields are marked *