by Ponlakshmi
Over the past few years, MQTT has begun to establish itself as the most commonly used messaging protocol for IoT projects. This document provides developers with a complete guide on how to set up and run an MQTT Broker on Raspberry Pi in just a few minutes. You will also see how easily you can add intelligence to your Edge device using ESP8266. Once you have gained the knowledge of the MQTT Broker and how to use it, you will be able to make your own IoT projects.
The steps involved in setting up a MQTT Broker on Raspberry Pi are
MQTT is a lightweight messaging protocol that allows you to send arbitrary messages across a network to any interested device. It uses the publish/subscribe method to exchange data among clients and the server.
We are going to use Bevywise MQTT Broker and the Raspberry Pi 3 running the latest version of Raspbian Jessie in this tutorial.
To install the Bevywise MQTT Broker and its client packages follow the below steps
Then use
Check process MQTT broker, with pidfile Bevywise/MQTTRoute/Broker.pid.
start program = “Bevywise/MQTTRoute/bin/runbroker.sh” with timeout 2 seconds. stop program = “Bevywise/MQTTRoute/bin/stopbroker.sh” with timeout 2 seconds.
sudo monit reload
sudo monit start MQTTRoute
The below steps explains how to flash a client library in ESP8266 by using the Arduino IDE.
ESPMQTT_Client
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
const char* ssid = “wifi name”;
const char* password = “wifi password”;
const char* mqtt_server = “localhost”;
WiFiClient espClient;
PubSubClient client(espClient);
int RECV_PIN = 5;
void reconnect()
{
while (!client.connected())
{
Serial.print(“Attempting MQTT connection…”);
if(client.connect(“ESP8266Client”))
{
Serial.println(“connected”);
client.subscribe(“ESP8266/LED”);
client.publish(“ESP8266/LED”,”on”);
}
else
{
Serial.print(“failed, rc=”);
Serial.print(client.state());
Serial.println(” try again in 5 seconds”);
delay(5000);
}
}
}
void setup()
{
pinMode(RECV_PIN,OUTPUT);
Serial.begin(115200);
digitalWrite(RECV_PIN,HIGH);
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
reconnect();
}
void setup_wifi()
{
delay(10);
Serial.print(“Connecting to “);
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(“.”);
}
Serial.println(“WiFi connected”);
Serial.println(“IP address: “);
Serial.println(WiFi.localIP());
}
void callback(char* topic, byte* payload, unsigned int length)
{
Serial.print(“Message arrived [“);
Serial.print(topic);
Serial.print(“] “);
for (int i = 0; i < length; i++)
{
Serial.print((char)payload[i]);
}
if((char)payload[0] == ‘o’ && (char)payload[1] == ‘n’)
{
digitalWrite(RECV_PIN,LOW);
}
}
void loop()
{
if (!client.connected())
{
reconnect();
}
client.loop();
}
Below we have explained the code to help you understand what is going on.
Here, we have configured WIFI:
const char* ssid = “wifi name”;
const char* password = “wifi password”;
Then the MQTT Configuration:
const char* mqtt_server = “localhost”;
client.setServer(mqtt_server, 1883);
Now, we have created the MQTT and WiFi stacks:
WiFiClient espClient;
PubSubClient client(espClient);
Arduino Setup Function:
void setup()
{
pinMode(RECV_PIN,OUTPUT);
Serial.begin(115200);
digitalWrite(RECV_PIN,HIGH);
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
reconnect();
}
Setting up an MQTT Publish/Subscribe:
void reconnect()
{
while (!client.connected())
{
Serial.print(“Attempting MQTT connection…”);
if(client.connect(“ESP8266Client”))
{
Serial.println(“connected”);
client.subscribe(“ESP8266/LED”);
client.publish(“ESP8266/LED”,”on”);
}
else
{
Serial.print(“failed, rc=”);
Serial.print(client.state());
Serial.println(” try again in 5 seconds”);
delay(5000);
}
}
}
Circuit Diagram for Flashing the program in ESP-12E
Refer our Broker help documentation for more information about our MQTT Broker User Interface. To know more about the features, visit the MQTT Broker page.
Note : Are you the technology person looking to automate your house on a Raspbery pi box? Here is your FOREVER FREE MQTT Broker with all the widgets and dashboard needed to monitor and control your MQTT devices.