Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Manufacturing Technology >> Manufacturing process

Seamless MQTT Integration: NodeMCU DHT22 Sensors with Raspberry Pi 3 B+ as Broker

Deploy a scalable IoT solution that couples NodeMCU microcontrollers with DHT22 temperature/humidity sensors to a Raspberry Pi 3 B+ acting as an MQTT broker. Data is visualized in real‑time on the IoT MQTT Panel App.

In this guide I walk through the hardware connections, software setup, and code structure that lets you add or remove sensor nodes simply by editing MQTT topics. When the network drops, each NodeMCU continues to publish locally; you can later reconnect to the broker to recover the buffered data.

1. Network Architecture

The typical layout places the Raspberry Pi on the LAN and each NodeMCU behind the same Wi‑Fi router. Sensors read values every 10 seconds and publish to distinct topics such as /room1/temperature and /room1/humidity. The broker listens on port 1883 and forwards messages to the MQTT Panel App.

2. Software Prerequisites

2.1 Arduino IDE Libraries

2.2 Raspberry Pi Python Dependencies

3. Uploading Firmware

Flash the NodeMCU with the provided sketch, then run the Python script on the Raspberry Pi to subscribe to the topics and store the data.

4. NodeMCU Sketch Overview

#include <ESP8266WiFi.h>          // ESP8266 Wi‑Fi library
#include <PubSubClient.h>           // MQTT client
#include "DHT.h"                    // DHT sensor library

const char* mqtt_server = "Broker_IP_Address"; // Replace with Pi IP
const char* clientID = "room1";                 // Node identifier
const char* topicT = "/room1/temperature";      // Temperature topic
const char* topicH = "/room1/humidity";        // Humidity topic
const char* willTopic = "/room1/status";       // Will topic
const char* willMessage = "0";                  // 0 = disconnected
int willQoS = 0;
boolean willRetain = true;
int counter = 0;
const char* swversion = "1.0";

WiFiClient wifiClient;
PubSubClient client(mqtt_server, 1883, wifiClient);

DHT dhtA(2, DHT22); // DHT22 on NodeMCU pin D4 (GPIO2)

void setup() {
  Serial.begin(9600);
  Serial.print(swversion);
  dhtA.begin();
  delay(2000); // Allow Wi‑Fi to settle
  if (client.connect(clientID, "", "", willTopic, willQoS, willRetain, willMessage, true)) {
    Serial.println("Connected to MQTT broker");
  }
}

Replace Broker_IP_Address with the Pi’s local IP (e.g., 192.168.1.200). To find it, run ifconfig wlan0 on the Pi.

The willTopic and willMessage enable the broker to detect when a node disconnects unexpectedly, publishing a retained “0” status that can be visualized in the panel.

5. Raspberry Pi Subscriber Script

The Python script creates a persistent connection to the broker, subscribes to the relevant topics, and writes incoming data to a CSV file for later analysis.

6. Scaling the System

To add a new room, simply duplicate the sketch, change clientID, topicT, topicH, and willTopic to the new room name, and upload to another NodeMCU. The MQTT Panel App will automatically detect the new topics if you add them in the UI.

When network connectivity is lost, the NodeMCU keeps publishing to its local broker queue. Once the connection is restored, the messages are flushed, ensuring no data loss.

Follow me for updates, and stay tuned for a demo video of the entire system in action.

Manufacturing process

  1. MQTT vs. DDS: Choosing the Right M2M Protocol for IoT
  2. Build a Multi‑Sensor Temperature & Light Monitoring System with Raspberry Pi & DS18B20
  3. Mastering Raspberry Pi Sensor & Actuator Control: Accelerometer, Servo, and Data Streaming
  4. Python 3 to Arduino UNO: Easy Command Control and LED Demo
  5. Build a Raspberry Pi 3 & Arduino Laptop: Step‑by‑Step Guide
  6. Welding vs Brazing: Key Differences Explained for Better Metal Joining
  7. Up vs Down Milling: Key Differences Explained
  8. Soldering vs. Brazing: Understanding the Critical Differences
  9. Brazing vs Soldering: Key Differences Explained
  10. 2‑Stroke vs 4‑Stroke Engines: Key Differences Explained