Raspberry Pi Talking to ESP8266 Using MQTT



Posted in TechnologyRaspberry-pi

Introduction: Raspberry Pi Talking to ESP8266 Using MQTT

About: Keen to know latest technologies

In this project, I will explain what the MQTT protocol is and how it is used to communicate between devices.Then, as a practical demonstration, I will demonstrate how to setup a client and brocker system, where an ESP8266 module as well as RPi talk to each other or send message when a button is pushed.

Material required

1. Raspberry Pi 3

2. NodeMCU

3. LED

4. Button

5. Resistors(10k,475 ohm)

Microcontroller Contest

This is an entry in the
Microcontroller Contest

Step 1: What Is MQTT and How It Works


MQTT is a machine-to-machine (M2M) data transfer protocol. MQTT was created with the goal of collecting data from many devices and then transporting that data to the IT infrastructure. It is lightweight, and therefore ideal for remote monitoring, especially in M2M connections that require a small code footprint or where network bandwidth is limited.

How MQTT work

MQTT is a publish/subscribe protocol that allows edge-of-network devices to publish to a broker. Clients connect to this broker, which then mediates communication between the two devices. Each device can subscribe, or register, to particular topics. When another client publishes a message on a subscribed topic, the broker forwards the message to any client that has subscribed.

MQTT is bidirectional, and maintains stateful session awareness. If an edge-of-network device loses connectivity, all subscribed clients will be notified with the “Last Will and Testament” feature of the MQTT server so that any authorized client in the system can publish a new value back to the edge-of-network device, maintaining bidirectional connectivity.

The project is divided in 3 parts

Firstly, we create MQTT server on RPi and install some libraries.

Secondly, we will install libraries in Arduino IDE for NodeMCU to be work with MQTT, upload the code and check whether server is working or not.

Finally, we create a script in Rpi ,upload the required code in NodeMCU and run the python script to control leds from both server and client side. Here, server is RPi and client is NodeMCU.

Step 2: Raspberry Pi

Steps for install latest MQTT server and client in RPi

1. To use the new repository you should first import the repository package signing key:

wget  http://repo.mosquitto.org/debian/mosquitto-repo.g...
sudo apt-key add mosquitto-repo.gpg.key

2.Then make the repository available to apt:

cd /etc/apt/sources.list.d/

3.Depending on which version of Debian you are using:

sudo wget  http://repo.mosquitto.org/debian/mosquitto-wheezy...
sudo wget http://repo.mosquitto.org/debian/mosquitto-jessie...
 sudo apt-get update

4.Then install Mosquitto server using.

sudo apt-get install mosquitto

If you are getting errors in installing Mosquitto like this.

The following packages have unmet dependencies:

mosquitto : Depends: libssl1.0.0 (>= 1.0.1) but it is not installable
Depends: libwebsockets3 (>= 1.2) but it is not installable
 E: Unable to correct problems, you have held broken packages.

Then use this command to fix issues.

sudo apt --fix-broken install

5. After, installing MQTT server install client using command.

sudo apt-get install mosquitto-clients

You can check services using command.

systemctl status mosquitto.service

Now, we check using subscribe and publish. For subscribe and publish you can check commands. you can visit website also.



We successfully installed MQTT server and client .
Required library ==> paho-mqtt ,you can install using command.

sudo apt-get update
sudo apt-get install python python-pip <br>
sudo pip install RPi.GPIO paho-mqtt


Step 3: How to Setup Static Ip Address

Go to directory cd /etc and open file dhcpcd.conf using any editor. At the end, write these four lines.

interface eth0 
static ip_address= // ip you want to use
interface wlan0 
static ip_address= 
static routers=                      // your Default gateway 
static domain_name_servers=

After that save it and reboot your pi.

Step 4: NodeMCU

Install required libraries in Arduino IDE for NodeMCU

1. Go to Sketch ==> Include library ==> Manage libraries.

2. Search for mqtt and install library by Adafruit or you can install any library.

3. It depend on sleepydog library so we need this library also.

Program is given above, just for checking whether its works or not. Here I have not created any script in RPi. We are just using commands to subscribe and publish. We will create script for controlling later on.

mosquitto_pub -h raspberrypi -t "/leds/pi" -m "ON"
mosquitto_pub -h raspberrypi -t "/leds/pi" -m "OFF"
mosquitto_pub -h raspberrypi -t "/leds/pi" -m "TOGGLE"
mosquitto_pub -h raspberrypi -t "/leds/esp8266" -m "ON"
mosquitto_pub -h raspberrypi -t "/leds/esp8266" -m "OFF"
mosquitto_pub -h raspberrypi -t "/leds/esp8266" -m "TOGGLE"

-h == > host name
-t == > topic

-m == > message

After checking the Mqtt_check program upload complete program in NodeMCU

Step 5: Python Script

As I discussed above we need python script for controlling leds using buttons .So, we are going to create script. Script is given above.

When you run the script your script should be look like as shown in image, if result code is not zero then their is an error you can check error on paho website.

Step 6: Connections and Circuit Diagram

Interfacing of button, LED with NodeMCU

NodeMCU === > Button
Gnd === > Gnd

3.3V === > PIN1

GPIO4 (D2) === > PIN2

NodeMCU === > LED

Gnd === > Cathode (-ve)

GPIO5 (D1) === > Anode(+ve)

Interfacing of button, LED with RPi

RPi === > Button
Gnd === > PIN1

GPIO 23 === > PIN2

RPi === > LED

Gnd == > Cathode(-ve)

GPIO 24 === > Anode(+ve)

Step 7: Result

Make sure you script is running otherwise it will not able to control led using buttons.

Step 8: Program



    • Spotless Contest

      Spotless Contest
    • Space Challenge

      Space Challenge
    • Science of Cooking

      Science of Cooking

    We have a be nice policy.
    Please be positive and constructive.