MQTT Based Server

Project Overview:
In this Project we will be using multiple ESP8266 Modules (each equipped with different sensors) which are connected wirelessly to a Raspberry Pi which acts as a Broker. The data from the Broker is then viewed with the help of a Mobile Application. In this tutorial we have used two ESP8266 modules (2 clients)

ESP-1 - Humidity and Temperature sensor(dht11), 4 Channel Relay
ESP-2 - Soil moisture sensor
Raspberry Pi 3B+ ( MQTT-broker)

The code for ESP8266 and Raspberry Pi commands are provided at the end of this page
What if many devices are connected in a home? 

The solution will be using all the devices under the same host. Using local host is the most secure way,on the other hand if we use global host these devices can be accessed anywhere from any part of the world. But it’s highly insecure. MQTT is a light-weight protocol designed for the purpose of communication between multiple IOT devices. In this project we will be connecting multiple IOT devices under Raspberry - pi using Mosquitto server / Mosquitto broker. Monitoring the house using IOT devices is an easy task, which can be achieved using ESP8266 modules and connecting multiple sensors to it.

MQTT-Protocol:

MQTT stands for Message Queuing Telemetry Transport. It is a publish/subscribe, extremely simple and lightweight messaging protocol, designed for constrained devices and low-bandwidth, high-latency or unreliable networks. The design principles are to minimise network bandwidth and device resource requirements whilst also attempting to ensure reliability and some degree of assurance of delivery. These principles also turn out to make the protocol ideal of the emerging “machine-to-machine” (M2M) or “Internet of Things” world of connected devices, and for mobile applications where bandwidth and battery power are at a premium.

There are a few terms associated with MQTT

Subscribe and Publish: Subscribe means to receive the data from other device, and publish means to send the data to other device.

Message: Messages are the information that we are sending and receiving.In Our case, the data from the sensors
 
MQTT-Dashboard (Android App): 

You can download the app here

The MQTT dashboard is responsible for displaying all the values from the client to the phone through a broker. In order to display the values the clients hast to publish the data whereas the phone has to be subscribed to the topic or the feed from the publisher. This makes it hassle free to connect multiple clients together. ADAFRUIT mqtt library is used for the purpose of subscribing and publishing data on to the dashboard. Note that All the devices must be connected to the same WiFi network

How does this system work?
In order for this system to work our server that is the broker , clients and your host must be connected to the same network. Rasberry Pi can be interfaced with your laptop using VNC viewer. Inorder to provide a static ip for your Rasberry pi just replace the sketch with few commands. So whenever the Pi is connected to different networks the IP address remains the same which allows to access the Pi through any network. Mqtt protocol sends the data depending on the topic which we are subscribed . MQTT pings for every 3 seconds which makes sure that the clients are connected. If people do prefer global network it can be replaced  with a cloud service like thingspeak, slack,adafruit io etc.

Hardware Setup: 
Client-1(ESP8266)
  • Power up all the sensors using 3v3 and ground pin of ESP8266
  • Connect the input pins IN1 IN2 IN3 IN4 of the 4 Channel Relay to D1 D2 D6 D7 pins of ESP Respectively
  • Connect the data pin of DHT11 Sensor to pin D4
  • The code is well commented on where changes has to be made
  • Upload the sketch
Client-2(ESP8266)
  • Power up the sensor using 3v3 and ground pin of ESP8266
  • Connect A0 of the sensor to A0 pin of ESP8266
  • The code is well commented on where changes has to be made
  • Upload the sketch
(Broker)Raspberry Pi:
Run the following commands to install Mosquitto Broker

sudo apt update 
sudo apt install -y mosquitto mosquitto-clients
Wait for the Installation to complete,
and then type the following command to start the broker
sudo systemctl enable mosquitto.service
LINK TO CODE: https://github.com/techxaperture/MQTTbroker
Now open the Android application and fill in the required details. Thats it! 

Comments