Site icon High Voltages

ESP8266 and Raspberry pi communication using MQTT

esp8266 mqtt

esp8266 mqtt

Esp8266 and Raspberry pi communication using MQTT – is a blog in which we will learn to use the ESP8266 or NodeMCU as an MQTT client. In the last blog, we made the cheapest MQTT broker using Raspberry pi and test it using MQTT-explorer from PC on a local network. We are going to use the same local broker which we created in the last blog and ESP8266 as an MQTT publish-subscribe client. This tutorial focuses on how to use MQTT in esp8266, we will follow the following steps,

  1. We will install the libraries of the MQTT protocol in nodemcu.
  2. We will write the code to publish DHT11 sensor data as an MQTT message from esp8266 to nodemcu.
  3. Then we will subscribe to a topic to receive the MQTT message sent from raspberry pi to esp8266 and depending on that message we will control the LED.
  4. We will use the shell terminal in raspberry pi to publish-subscribe MQTT messages.

ESP8266 and Raspberry pi communication using MQTT
ESP8266 and Raspberry pi communication using MQTT

Buying guide

Development boardAmazon (US)Ali express
ESP32ESP8266 and Raspberry pi communication using MQTTESP8266 and Raspberry pi communication using MQTT
Raspberry pi zeroESP8266 and Raspberry pi communication using MQTTESP8266 and Raspberry pi communication using MQTT

Raspberry pi 4
ESP8266 and Raspberry pi communication using MQTTESP8266 and Raspberry pi communication using MQTT
ESP8266ESP8266 and Raspberry pi communication using MQTTESP8266 and Raspberry pi communication using MQTT
DHT11ESP8266 and Raspberry pi communication using MQTTESP8266 and Raspberry pi communication using MQTT
Buying guide for How to MQTT series

ESP8266 as an MQTT Client

NodeMCU or ESP8266 is the most popular boards for the Internet of things (IoT) development and we are using it as an MQTT publish-subscribe client. NodeMcu is a microcontroller with builtin WiFi that gives us access to the internet quickly. Apart from WiFi functionality, it has some GPIOs that help connect sensors and actuators, and with the help of these two functionalities, we can make smart things. 

ESP8266 MQTT
ESP8266 MQTT

NodeMcu or esp8266 IoT applications are vast due to its compact size and numerous features. This little 5$ board is very common in home automation, smart homes, and weather stations.  Many students use this to make different prototypes such as smart cities and using the different iot platforms there is a number of possibilities.

Hackster has a number of projects related to this mini IoT board. In this project, our esp8266 will be talking to the raspberry pi using the local MQTT broker we set up on the pi.

DHT11 Temperature/Humidity sensor

For the temperature and humidity reading, we will be using the DHT11 sensor, which is a very common sensor and readily available. You can operate this sensor on 3.3V-5V. The more details of the sensor and ESP8266 is available on components101 website. We are using this sensor because of esp8266 publishing dht11 readings with MQTT to the raspberry pi.

raspberry pi esp8266 mqtt
Raspberry pi esp8266 DHT

Raspberry pi as MQTT broker

Pi is a mini-computer and another popular board for IoT development. Pi has a processor and with the help of basic machine learning algorithms, it can make our application look more practical. You can insert a memory card in a raspberry pi that works as the storage of this device, and that storage can be helpful for storing and sharing data.

 In order to use the raspberry pi as MQTT broker, we installed the mosquitto MQTT broker which is an open-source broker in raspberry pi. You can secure the broker by providing credentials such as username and password, which we did in the last blog.

ESP8266 and Raspberry pi communication using MQTT video

Raspberry Pi as a MQTT Broker | Cheap MQTT Broker

ESP8266 Raspberry pi MQTT

So how these two boards will connect to each other? And how to use MQTT in esp8266 and raspberry pi? what programming languages will be used and how esp8266 publish-subscribe MQTT messages? 

We are going to discuss the above questions in this section in detail. So I am answering all these questions in sequence,

  1. Raspberry pi and esp8266 both have wifi capabilities and using that both devices will be connected to our home network, it can be a router or any dongle device. So, in short, raspberry pi, esp8266 will be communicating with each other using the home network. So, that will be wireless communication.
  2. We will be using the pubsub client library for esp8266 in order to use MQTT. For the raspberry pi, we will be using the terminal which, we already installed the local MQTT broker in the last blog.
  3. We can program esp8266 in multiple ways and in multiple languages, but for this specific tutorial, we will be using Arduino ide and C++. Whereas raspberry pi will be publishing and subscribing from the command line. But in the next tutorials, we will learn how we can use NodeRed and python on the raspberry pi to publish and subscribe to the MQTT messages.

Circuit Diagram

DHT sensor has three terminals or four terminals. The four terminals DHT11 mostly have 1 terminal NC or Not connected. The other 3 terminals are VCC, SIGNAL/DATA, and GND.

  1. The VCC will be connected to 3V of esp8266.
  2. The GND will be connected to esp8266 GND.
  3. The SIGNAL/DATA will be connected to the D1 pin of ESP8266.

The LED has two terminals, cathode, and Anode. 

  1. Connect Anode with D2 of ESP8266.
  2. NodeMCU GND connects with LED cathode.

We do not need to connect raspberry pi to ESP8266 as it will be already connected to ESP using the home network. Here is the circuit diagram,

Nodemcu DHT circuit diagram
Nodemcu DHT circuit diagram

You can get the PCB of a circuit from NextPCB, which is the sponsor of this project. They are providing 10% off on all PCB orders. Make sure to check their offer.

Code

#include <ESP8266WiFi.h>          //https://github.com/esp8266/Arduino

//needed for library
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h>         //https://github.com/tzapu/WiFiManager
#include <DHT.h>
#include <PubSubClient.h>

#define DHTPIN 5 // Digital pin connected to the DHT sensor
#define LED D2
// Uncomment the type of sensor in use:
#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT22 // DHT 22 (AM2302)
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
DHT dht(DHTPIN, DHTTYPE);

unsigned long previousMillis = 0;
unsigned long interval = 10000;

const char* mqttServer = "192.168.0.110";
const int mqttPort = 1883;

void callback(char* topic, byte* payload, unsigned int length) {

  Serial.print("Message arrived in topic: ");
  Serial.println(topic);

  Serial.print("Message:");
    Serial.println();
  
  String message = "";
  for (int i = 0; i < length; i++) {
    Serial.print((char)payload[i]);
    message+=(char)payload[i];
  }
  Serial.println("-----------------------");
  if(String(topic)=="LED"){
    if(message=="LED ON"){
      digitalWrite(LED,HIGH);
      Serial.println("LED IS ON");
    }
    else{
      digitalWrite(LED,LOW);
    }
  }
    
}

WiFiClient espClient;
PubSubClient client(mqttServer, mqttPort, callback, espClient);


void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  pinMode(LED,OUTPUT);
  digitalWrite(LED,LOW);
  //WiFiManager
  //Local intialization. Once its business is done, there is no need to keep it around
  WiFiManager wifiManager;
  //reset saved settings
// wifiManager.resetSettings();

  //set custom ip for portal
  //wifiManager.setAPStaticIPConfig(IPAddress(10,0,1,1), IPAddress(10,0,1,1), IPAddress(255,255,255,0));

  //fetches ssid and pass from eeprom and tries to connect
  //if it does not connect it starts an access point with the specified name
  //here  "AutoConnectAP"
  //and goes into a blocking loop awaiting configuration
  wifiManager.autoConnect("AutoConnectAP");
  //or use this for auto generated name ESP + ChipID
  //wifiManager.autoConnect();


  //if you get here you have connected to the WiFi
  Serial.println("connected...yeey :)");
  dht.begin();
  client.setServer(mqttServer, mqttPort);
  client.setCallback(callback);
    while (!client.connected()) {
    Serial.println("Connecting to MQTT...");

    if (client.connect("ESP8266Client" )) {
      client.subscribe("LED");
      Serial.println("connected");

    } else {

      Serial.print("failed with state ");
      Serial.print(client.state());
      delay(2000);

    }
}
}

void loop() {
    client.loop();
unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval) {
    // save the last time you updated the DHT values
    previousMillis = currentMillis;
    float t = dht.readTemperature();
    float h = dht.readHumidity();
    if(!isnan(h)&&!isnan(t)){
      Serial.println("Temperature : " +String(t));
      Serial.println("Humidity : " +String(h));
      String toSend = String(t) + "," +String(h);
      client.publish("data",toSend.c_str());
    }

  }
}

Libraries used:

Wifi Manager:

Wifi Manager is a library that helps you connect the esp8266/esp32 with wifi without using the hardcoded credentials. I have explained this in a very detailed tutorial – Nodemcu connects WIFI without hard coding credentials (SSID, Password, etc). We can also use this library to change the MQTT parameters without uploading the code again using the HTTP server provided by this library.

Esp8266 connect WIFI without hard coding credentials (SSID, Password)

Pubsubclient Library:

ESP8266, ESP32 and some other boards support the pubsubclient library. This library helps you to connect to the MQTT connections and allows you to publish a message on MQTT broker and subscribing to a topic.

Before uploading the code make sure to add the esp8266 board on Arduino ide. While uploading the code make sure you have selected the right board and COM PORT.

Code and project description

In the following video, I have explained the each line of code and you can watch the build of this project in the video. Make sure to subscribe to me on youtube so you can follow the complete How to MQTT series.

ESP8266 (NodeMcu) Raspberry Pi MQTT | ESP8266 Publish/Subscribe

Conclusion

In this blog we learned to use MQTT protocol in esp8266 and raspberry pi, We used the local MQTT broker to control the LED from a local network and also published the DHT sensor data. 

In the upcoming tutorials, we will discuss how we can use MQTT in NodeRed and python programming language in Raspberry pi. 

Cheers!

Exit mobile version