Introduction: Raspberry Pi Talking to ESP8266 Using MQTT

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)

Step 1: What Is MQTT and How It Works

MQTT

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

1. To install the latest MQTT server and client in RPi , to use the new repository you should first import the repository package signing key.

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

2. 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.list
sudo wget http://repo.mosquitto.org/debian/mosquitto-jessie.list
sudo wget http://repo.mosquitto.org/debian/mosquitto-stretch.list
sudo apt-get update

4. Install the Mosquitto server using command.

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

As our MQTT server and client is installed. Now, we can check it using subscribe and publish. For subscribe and publish you can check commands or visit website as given below.

Mosquitto Sub

Mosquitto Pub

To install paho-mqtt library use the command below.

sudo pip install paho-mqtt

Paho

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=192.168.1.100 // ip you want to use
interface wlan0 
static ip_address=192.168.1.68 
static routers=192.168.1.1                      // your Default gateway 
static domain_name_servers=192.168.1.1

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