Making Raspberry Pi to work with MQTT.

step by step guide on mqtt

Pre-requisites:

  • Have Raspberry Pi.
  • Raspbian OS installed and WiFi / LAN cable connected to your router and have an IP address to connect it to. If you have not installed Raspbian, head over here and install it on SD Card and boot it.

Connect to your Raspberry Pi via Putty Client on Windows or Terminal on Mac / Linux

Let us see what is the debian version we have

cat /etc/debian_version
8.0

It just says the version, we need the codename so we use now /etc/os-release

cat /etc/os-release

PRETTY_NAME="Raspbian GNU/Linux 8 (jessie)"
NAME="Raspbian GNU/Linux"
VERSION_ID="8"
VERSION="8 (jessie)"
ID=raspbian
ID_LIKE=debian
HOME_URL="http://www.raspbian.org/"
SUPPORT_URL="http://www.raspbian.org/RaspbianForums"
BUG_REPORT_URL="http://www.raspbian.org/RaspbianBugs"

It is debian based with code name ‘jessie’

Change our directory to /etc/apt/sources.list.d

cd /etc/apt/sources.list.d

Download mosquitto-jessie repo

sudo wget http://repo.mosquitto.org/debian/mosquitto-jessie.list

Update:

apt-get update

Search for what are the packages available.

apt-cache search mosquitto

Instal mosquitto and mosquitto-clients

apt-get install mosquitto mosquitto-clients -y

Now we have installed out broker on Raspberry Pi, let us test this:

Go on to your workstation and subscribe to one or two topic:

mosquitto_sub -t "home/light" -t "home/fan" -h 192.168.8.9

-t represents the topics to subscribe to

there are two topics it subscribes to at the moment:

home/fan and home/light

It waits for topics to be published and displays if there is any.

To Publish a topic:

mosquitto_pub -t "home/lights" -m "Light is ON"

I did not mention a hostname with the flag -h since I am running this on my Raspberry Pi.

by default it takes localhost

Resources: