ESP32 - MQTT - Random Nerds Tutorials
ESP32 - MQTT - Random Nerds Tutorials
ESP32 MQTT –
Publish and
Subscribe with
Arduino IDE
This project shows how to
use MQTT communication
protocol with the ESP32 to
publish messages and
subscribe to topics. As an
example, we’ll publish
BME280 sensor readings to
the Node-RED Dashboard,
and control an ESP32
output. The ESP32 we’ll be
programmed using Arduino
IDE.
Project Overview
In this example, there’s a
Node-RED application that
controls ESP32 outputs and
receives sensor readings
from the ESP32 using
MQTT communication
protocol. The Node-RED
application is running on a
Raspberry Pi.
We’ll use the Mosquitto
broker installed on the
same Raspberry Pi. The
broker is responsible for
receiving all messages,
ltering the messages,
decide who is interested in
them and publishing the
messages to all subscribed
clients.
Prerequisites
You should be familiar with
the Raspberry Pi – read
Getting Started with
Raspberry Pi.
You should have the
Raspbian operating system
installed in your Raspberry
Pi – read Installing
Raspbian Lite, Enabling and
Connecting with SSH.
You need Node-RED
installed on your
Pi and Node-RED
Dashboard.
Learn what’s MQTT and
how it works.
Parts Required
These are the parts
required to build the
circuit (click the links below
to nd the best price at
Maker Advisor):
Introducing the
BME280 Sensor
Module
The BME280 sensor
module reads temperature,
humidity, and pressure.
Because pressure changes
with altitude, you can also
estimate altitude. However,
in this tutorial we’ll just
read temperature and
humidity. There are several
versions of this sensor
module, but we’re using
the one shown in the gure
below.
Schematic
We’re going to use I2C
communication with the
BME280 sensor module.
For that, wire the sensor to
the ESP32 SDA and SCL
pins, as shown in the
following schematic
diagram.
Windows instructions –
ESP32 Board in Arduino IDE
Mac and
Linux instructions – ESP32
Board in Arduino IDE
Installing the
PubSubClient Library
Important: PubSubClient
is not fully compatible with
the ESP32, but the example
provided in this tutorial is
working very reliably during
our tests.
Installing the
Adafruit_Sensor library
Uploading code
Now, you can upload the
following code to your
ESP32. The code is
commented on where you
need to make changes. You
need to edit the code
with your own SSID,
password and Raspberry
Pi IP address.
/*********
Rui Santos
Complete project details at https://randomnerdtutorials.com
*********/
#include <WiFi.h>
#include <PubSubClient.h>
#include <Wire.h>
#include <Adafruit_BME280.h>
#include <Adafruit_Sensor.h>
WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;
Subscribing to
MQTT topics
In the reconnect() function,
you can subscribe to MQTT
topics. In this case, the
ESP32 is only subscribed to
the esp32/output:
client.subscribe("esp32/output");
In the callback() function,
the ESP32 receives the
MQTT messages of the
subscribed topics.
According to the MQTT
topic and message, it turns
the LED on or o :
Publishing MQTT
messages
In the loop(), new readings
are being published every 5
seconds:
humidity = bme.readHumidity();
// Convert the value to a char array
char humString[8];
dtostrf(humidity, 1, 2, humString);
Serial.print("Humidity: ");
Serial.println(humString);
client.publish("esp32/humidity", humString);
Creating the
Node-RED ow
Before creating the ow,
you need to have installed
in your Raspberry Pi:
Node-RED
Node-RED Dashboard
Mosquitto Broker
Node-RED UI
Now, your Node-RED
application is ready. To
access Node-RED UI and
see how your application
looks, access any browser
in your local network and
type:
http://Your_RPi_IP_address:1880/ui
Wrapping Up
In summary, we’ve shown
you the basic concepts that
allow you to turn on lights
and monitor sensors with
your ESP32 using Node-
RED and the MQTT
communication protocol.
You can use this example
to integrate in your own
home automation system,
control more outputs, or
monitor other sensors.