Real-time temperature monitoring with LavinMQ: build an IoT dashboard using ESP32 and MQTT
This guide walks through building a real-time temperature and humidity monitoring system using an ESP32-C6, a DHT11 sensor, MQTT, and LavinMQ. The sensor connects to an ESP32-C6, which publishes readings to a managed LavinMQ broker on CloudAMQP. The data appears in a Python terminal and a live web dashboard.
Along the way, the publish/subscribe model of MQTT becomes clear: IoT devices connect to applications without requiring them to communicate directly.
The complete source code is available in the IoT demo GitHub repository.
How the system works
The ESP32-C6 reads temperature and humidity values from the DHT11 sensor and publishes them to LavinMQ over MQTT. LavinMQ then delivers the messages to applications subscribed to the corresponding topics.
The ESP32 acts as the publisher, LavinMQ as the message broker, and the Python application and dashboard as subscribers.

The device publishes each reading once. LavinMQ distributes it to all interested subscribers, allowing new applications to be added without changing the firmware.
Requirements
This guide uses the following hardware and software:
| Hardware | Software and services |
|---|---|
| ESP32-C6 microcontroller | Arduino IDE |
| DHT11 temperature and humidity sensor | ESP32 board package by Espressif |
| Three female-to-female jumper wires | Adafruit DHT sensor library |
| USB-C data cable | PubSubClient library |
| Computer and 2.4 GHz Wi-Fi | Python 3 and a CloudAMQP account |
Other MQTT-capable microcontrollers and sensors also work. Adapt the wiring and firmware to the specific hardware; the MQTT publishing and LavinMQ subscriber architecture remains the same.
Contact marketing@cloudamqp.com to request the LavinMQ IoT starter kit, or purchase compatible components separately.
Received an IoT kit from us? The sensor is already connected and the firmware is preinstalled. Skip Step 1 and start directly from Step 2.
Step 1: Connect the sensor and upload the firmware
Connect the DHT11 sensor to the ESP32-C6 using three jumper wires.
| ESP32-C6 pin | DHT11 pin | Purpose | Cable color |
|---|---|---|---|
| GPIO 19 | Left | Data | Yellow |
| 3V3 | Middle | Power | Purple |
| GND | Right | Ground | Gray |
These connections apply to the three-pin DHT11 module used in this project. Check the sensor’s pin labels before connecting, since other modules may have different layouts.
The final wired setup should look like this:

Upload the firmware
-
Install Arduino IDE.
Setting up Arduino IDE? Go to File → Preferences (or Arduino IDE → Settings on macOS), find Additional Boards Manager URLs, and add:
https://espressif.github.io/arduino-esp32/package_esp32_index.jsonThen open Boards Manager, search for
esp32, and install esp32 by Espressif Systems. - Open Boards Manager, search for
esp32, and install the Espressif Systems board package. - Open Library Manager and install the Adafruit DHT sensor library and PubSubClient by Nick O’Leary, including any required dependencies.
-
Clone the project:
git clone https://github.com/cloudamqp/iot-demo-page.git cd iot-demo-page - Open
esp32_c6/esp32_c6.inoin Arduino IDE. Keep the other files in theesp32_c6directory alongside the sketch. - Select the ESP32-C6 board and its USB port.
- Click Upload.
After uploading, open Tools → Serial Monitor and set the baud rate to 115200. Startup messages should appear, indicating that the device has initialized the sensor and opened its configuration portal.
Checkpoint: The firmware uploads successfully and the ESP32 starts.
Step 2: Create a LavinMQ instance on CloudAMQP
LavinMQ supports both MQTT and AMQP. This guide uses MQTT to transfer readings from the ESP32 to the broker.
CloudAMQP provides managed LavinMQ instances, eliminating the need for local broker installation and maintenance.
- Log in to the CloudAMQP control panel.
- Select Create New Instance and choose LavinMQ.
- Choose a plan. A free plan works well for this project.
- Name the instance
iot-temperature-project. - Select a data center region and create the instance.
Once the instance is running, locate its MQTT connection details:
| Setting | Value |
|---|---|
| Hostname | The CloudAMQP MQTT hostname |
| Port | 8883 |
| Username | The exact MQTT username from CloudAMQP, including any vhost prefix (for example, vhost:username) |
| Password | The broker password |
Port 8883 is used for MQTT over TLS. Keep these credentials available for the next step.
Checkpoint: The LavinMQ instance is running and its connection details are available.
Step 3: Configure Wi-Fi and MQTT on the ESP32
The firmware provides a temporary Wi-Fi configuration portal, eliminating the need to hardcode credentials into the sketch.
- Power on the ESP32.
-
Connect a computer or phone to the Wi-Fi network named
demowifi-<device-id>.
The configuration network is available for approximately one minute. Accessing the portal extends the configuration window. If the network disappears, restart the device and reconnect.
-
Open a browser and navigate to
http://192.168.4.1.
Configure the MQTT connection
In the MQTT connection section, enter the broker credentials:
Host: <your-mqtt-hostname>
Port: 8883
Username: <your-username>
Password: <your-password>

Click Save MQTT.
Configure Wi-Fi
In the Wi-Fi network section, select a 2.4 GHz network, enter the password, and click Save Wi-Fi.

After the configuration portal closes, the ESP32 connects to the Wi-Fi network and LavinMQ. The settings are stored on the device for future restarts.
Note: The board supports only the 2.4 GHz band. Most modern smartphones create hotspots on the 5 GHz band by default, which this board cannot connect to.
Verify the connection
Open the Arduino Serial Monitor. A successful connection produces output similar to:
[WiFi] Connected. IP: ...
[MQTT] Connecting to ...:8883 ... connected
[MQTT] publish OK
The firmware publishes temperature and humidity readings approximately every two seconds to these topics:
| MQTT topic | Example payload |
|---|---|
lavinmq/home/temperature |
Current Temperature: 22.5 |
lavinmq/home/humidity |
Current Humidity: 48.00 |
These are plain-text messages, not JSON. The values depend on the sensor’s measurements.
Verify that data is arriving by opening the LavinMQ management interface:

Checkpoint: The ESP32 connects to LavinMQ and publishes sensor readings.
Step 4: Receive sensor readings with Python
Subscribe to the measurements using Python. The repository includes a Python MQTT subscriber that listens to both temperature and humidity topics.
From the project root, navigate to the subscriber directory:
cd subscribers
Create a Python virtual environment and install the MQTT client:
python3 -m venv .venv
source .venv/bin/activate
python -m pip install "paho-mqtt>=2,<3"
On Windows, activate the environment using .venv\Scripts\activate instead.
Open MQTT_subscriber.py and update the connection settings:
BROKER = "<your-mqtt-hostname>"
USERNAME = "<your-username>"
PASSWORD = "<your-password>"
PORT = 8883
The Python subscriber in the repository defaults to port 1883, which does not enable TLS. To use encrypted MQTT with certificate verification, add this line immediately after client.username_pw_set(USERNAME, PASSWORD):
client.tls_set()
Run the subscriber:
python MQTT_subscriber.py
When the ESP32 publishes readings, the terminal displays output similar to:
Connected to broker.
Subscribed to: lavinmq/home/temperature, lavinmq/home/humidity
[lavinmq/home/temperature] Current Temperature: 22.5
[lavinmq/home/humidity] Current Humidity: 48.00
Stop the subscriber with Ctrl+C.
The repository also contains JavaScript, Ruby, and C# examples for other language preferences.
Checkpoint: The Python subscriber receives temperature and humidity messages from LavinMQ.
Step 5: Display live readings in a web dashboard
The terminal confirms that messages are arriving. The next step visualizes them in a browser.
The project includes a dashboard that subscribes to LavinMQ using MQTT over WebSockets, allowing the browser to receive new measurements without repeatedly refreshing the page.
Configure the dashboard
Open landingpage/app.js.
At the beginning of the file:
const MQTT_URL = "wss://<your-websocket-host>/mqtt";
const USERNAME = "";
const PASSWORD = "";
Replace these values with the broker’s MQTT-over-WebSocket endpoint and credentials:
const MQTT_URL = "wss://<your-websocket-host>/mqtt";
const USERNAME = "<your-username>";
const PASSWORD = "<your-password>";
The WebSocket hostname and path must match the endpoint exposed by the LavinMQ deployment. The TCP MQTT port 8883 is not a WebSocket URL.
The dashboard subscribes to the correct topics:
const TOPIC_TEMP = "lavinmq/home/temperature";
const TOPIC_HUM = "lavinmq/home/humidity";
Security note: Credentials placed in browser-side JavaScript are visible to page visitors. Use a dedicated, minimally privileged account for this local demonstration. For production deployments, use an architecture that avoids exposing shared broker credentials to browsers.
Start the dashboard
From the project root, navigate to the dashboard directory and start a local server:
cd landingpage
python3 -m http.server 8000
Open http://localhost:8000/index.html in Firefox or Safari. Chrome may not display the data due to WebSocket security restrictions.
The dashboard displays the latest temperature and humidity values, along with their trends as new messages arrive.
To test the system, gently warm the sensor by hand. The readings change as the ESP32 publishes new measurements.
Checkpoint: The Python subscriber and web dashboard both receive data from the same ESP32.
Why use LavinMQ for IoT?
A small IoT application could send sensor data directly to a web server, but this approach becomes limiting when multiple applications need the same measurements.
With LavinMQ, the ESP32 publishes once and the broker distributes the data to all interested subscribers. The project extends naturally to include monitoring dashboards, temperature alerts, historical data storage, or analytics services without changing the device’s publishing logic.
LavinMQ also supports AMQP, allowing backend applications to use queues, acknowledgements, and other messaging features alongside MQTT-connected devices. For example, a temperature sensor could publish readings over MQTT while a backend service processes alerts asynchronously using AMQP.
This separation becomes especially useful as the system grows from one sensor to multiple devices and services.
Troubleshooting
If the project fails at any point, identify which part of the system is not working:
| Problem | What to check |
|---|---|
| ESP32 not detected | USB data cable, selected board, and USB port |
| Configuration Wi-Fi missing | Restart the ESP32 and reconnect during the portal window |
| Wi-Fi connection fails | Password and 2.4 GHz compatibility |
| MQTT connection fails | Broker hostname, port, credentials, and network connectivity |
| Python subscriber receives nothing | Topic names, permissions, and whether the ESP32 is publishing |
| Dashboard doesn’t update | WebSocket endpoint, credentials, topic subscription, and browser console |
| Invalid sensor readings | DHT11 wiring and GPIO configuration |
If the subscriber connects successfully but receives no messages, wait for the next sensor reading. The firmware publishes sensor measurements without the MQTT retain flag, so earlier readings are not automatically replayed to new subscribers.
Frequently asked questions
Can a different ESP32 board be used?
Yes. The board selection, GPIO pins, and firmware configuration may need adjustment. This guide uses the ESP32-C6 and DHT11.
Can multiple applications receive the same sensor readings?
Yes. Multiple MQTT subscribers can receive messages published to the same topic, provided their subscriptions match and they have the required permissions.
Can LavinMQ be used without CloudAMQP?
Yes. LavinMQ can be self-hosted. This guide uses CloudAMQP to simplify broker setup and management.
What’s next?
This guide covered building a real-time temperature monitoring system that connects a physical sensor to multiple applications through LavinMQ.
The same approach supports additional sensors, monitoring dashboards, alerts, and event-driven backend services. For example, the same architecture can monitor temperature, humidity, motion, and equipment conditions across a facility, with each device publishing measurements to LavinMQ.
Explore the complete IoT demo on GitHub or create a LavinMQ instance on CloudAMQP to start building with connected devices.
Enjoyed this article?
Join our newsletter today to get exclusive updates, in-depth technical guides, and early access to our future plans, delivered straight to your inbox.
Lovisa Johansson