We know that MQTT can be and is widely used for Internet of Things Applications. But do you know that mqtt can be a good choice of sending data in local networks too? Instead of juggling with TCP or HTTP request and running webserver, mqtt can be a super simple, yet reliable solution for local network data exchange between raspberry pi to raspberry pi or Raspberry pi to computer. You can use mqtt for any small requirement of sending data from one computer to another computer. or you can use mqtt to send data to PC from raspberry pi. You can also use mqtt to send data to raspberry pi from PC. If you want to know how to setup raspberry pi for the first time, you can read it here
In order to do any of these things, first thing is to understand how mqtt works? and what are the advantages over the conventional client-server communication
How Client-server communication Works?
In a very conventional sense, whenever we talk about 2 computers talking to each other, the general way of communication is Client -> Server Communication.
Lets say when 2 computers wants to talk to each other, unless we’re using a direct TCP communication, we often need server. The typical configuration will look like this
Typical Client server configuration
Now in above structure, what usually happens is when client 1 sends some data, the server has to save it. And when client 2 sends a request for that data then server will serve it. Lets take an example of IoT project and client 1 is sending temperature and client 2 wants to have that temperature. The communication goes like this
Client 1 reads temperature and sends it to Server
Server receives it and stores it somehow
Client 2 requests server about data
Server responds with the available data
client 2 gets requested data
How MQTT Works?
Now if the same communication is to be done using mqtt, lets see how it works.
First, in MQTT, there is no need to save the data at the server end. The data saving into any database on server is completely optional. Instead the server immediately sends that data back to the clients who need it by fetching from its own cache. Now how to know which client wants that data? In order to know which client wants which data and which client sends which data, mqtt uses something called topics. A topic is basically a virtual communication ID and is better understood in terms of internet of things. This is one of the reason why mqtt is used most in internet of things.
So talking about IoT, a topic can be a sensor value or a parameter which is being sent by one or more devices and which needs to be received by one or more devices. So topic names can be
Temperature
Humidity
Pressure
Presence sensor
LPG Sensor Value
and so on…
topic can also be the device control action. For example, in IoT you have to turn on cooler or heater or a fan or a motor or anything using a relay. Then topic names can be
Relay 1 or relay 2 or so on
LED
Fan
Motor
basically topic is a means of communication with which the clients will communicate each other.
Now the clients or individual computer in mqtt, can have one of the 2 jobs.
Sending Data
Receiving Data
Doing both
so depending on whether its sending data or receiving data, the client computers are called as
Publisher (who sends data)
Subscriber (who receives data)
Look at the generalized mqtt block diagram
how mqtt works
As you can see, the entities are now called as publisher and subscriber. As the storage is completely optional at the server, its now called as broker. The broker is nothing different than server in our previous case. It is a physical entity in your network, like a computer PC or raspberry pi. But saving data ont this computer is optional. The communication between devices happens over a particular topic. Lets assume there is just temperature data which needs to be exchanged. In this case, lets see how mqtt communication happens between 2 computers.
So the subscriber here, at the time of boot up or at the time of running the application code, has to subscribe to the topic it wants to read from the network. Lets assume the topic name here is temp. The publisher is supposed to publish to this topic to broker. And the subscriber has to stay subscribed to this topic from broker. The communication happens like this.
Publisher will read the temperature value and publish over the topic called temp
Broker receives the temperature value over a topic called temp and immediately distributes / sends the value to all the subscribers of the topic temp
All the subscribers of topic temp receives the value.
Its just pure simple and fun to make communication this way. The best part is, the publisher can publish to several topics and subscribe to several topics as well. The same case with subscriber.
How to use mqtt in python
Having understood the theory of how mqtt works, lets give it a quick spin. For this experiment, you’ll need at least 2 computers to have real fun and experience of understanding communication. One of our computer will act as broker and subscriber. One computer will act as publisher.
Installation and Running MQTT python program is a 2 part process.
We need a MQTT server who runs the service, technically, its called mqtt broker — Its Raspberry pi in this experiment.
We need a publisher, who just sends some data to or receives some data from the server, — Its PC in our case can be other RPi or Nodemcu or esp32 or any such thing. In current scene, its the PC
So what we need is an MQTT server (or broker) running on raspberry pi. mosquitto is one such server which can run on Raspberry pi. In this tutorial, we use mosquitto server on raspberry pi.
Now lets see installations to be done
Install Python MQTT Library On raspberry Pi
we need to install both the mqtt server as well as the python client library on raspberry pi so that we can not only receive data, but also be able to interpret it using python code. In short to receive the data in python codes.
so on raspberry pi, first install the mqtt server with below command. Make sure you type these commands in terminal of your raspberry pi computer.
Unknown language
sudo apt-get install -y mosquitto mosquitto-clients
this command will install the mosquitto MQTT broker and client libraries on raspberry pi and will run it automatically in the background. The broker is now installed on the raspberry pi. Now lets run the mqtt broker so that its operational. This can be done by
Unknown language
sudo systemctl enable mosquitto
Above line will run the broker on raspberry pi.
Next, we need to run the subscriber code on raspberry pi as well. This code will read the values published by the publisher. In this case, we’re making use of broker itself as a subscriber as well. To access the mqtt functionalities through python program, we need to install the mqtt python libraries on raspberry pi. Mosquitto here is the software which will act as broker meaning it will act as the receiver of data or sender too in some cases. To read the received data in python program, we need python mqtt library. To install python MQTT library, run below command on termianl of raspberry pi. The most popular mqtt library for python is paho-mqtt library which can be installed with this command
Unknown language
sudo pip install paho-mqtt
Above line will install the paho-mqtt python library with which we can write a code to read the data coming from a sender like PC or microcontroller.
If you’ve both python2 and python3 installed on your raspberry pi, which you obviously would have; then its better to explicitly specify that you want to install libraries for python3 with this command
Unknown language
sudo pip3 install paho-mqtt
Above command will install paho mqtt libraries for python3
MQTT Python Code
Now the Code, below python code is useful to receive data on raspberry pi mqtt broker on specified port address. As discussed before, MQTT needs a topic like temp to receive data, this is also called as channel, so whatever topic you’re specifying here, should be the same one used on the sending device (publisher). Look at the code below, this is a python code for raspeberry pi to receive mqtt data.
Python
import paho.mqtt.client as mqtt #import library
MQTT_SERVER = "localhost" #specify the broker address, it can be IP of raspberry pi or simply localhost
MQTT_PATH = "test_channel" #this is the name of topic, like temp
# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
# Subscribing in on_connect() means that if we lose the connection and
# reconnect then subscriptions will be renewed.
client.subscribe(MQTT_PATH)
# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
print(msg.topic+" "+str(msg.payload))
# more callbacks, etc
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect(MQTT_SERVER)
client.loop_forever()# use this line if you don't want to write any further code. It blocks the code forever to check for data
#client.loop_start() #use this line if you want to write any more code here
Install MQTT python libraries On PC
After installing everything required on Raspberry Pi, we’ve to install the things needed on PC
We only need the python mqtt libraries on PC for sending data to mqtt broker running on raspberry pi. It can be installed by running below command in command prompt of windows. Make sure you run the command prompt by right clicking and clicking on “Run as Administrator”
Unknown language
pip3 install paho-mqtt
make sure, you open the command prompt as “Administrator” otherwise it may cause issues in installation.
Now use below simple python code to send data to the test channel created in raspberry pi mqtt server. You need to specify the raspberry pi IP address as the mqtt server address in the below code. Run below code on PC
Python
import paho.mqtt.publish as publish
MQTT_SERVER = "192.168.1.5"
MQTT_PATH = "test_channel"
import time
while True:
publish.single(MQTT_PATH, "Hello World!", hostname=MQTT_SERVER) #send data continuously every 3 seconds
time.sleep(3)
And that is about it. whatever data instead of “hello world” you send, it’ll be sent and published to the mqtt broker. Now this is a very simple and crude sample and as you learn more, you can experiment more about it.
Interesting thing is, MQTT is a preferred way to send data from any kind of IoT Device to major cloud service providers like microsoft azure and aws.
Keep Experimenting!!!