ESP8266 Publishing DHT22 Readings with MQTT to Raspberry Pi
437.ESP8266 Publishing DHT22 Readings with MQTT to Raspberry Pi This project uses a Raspberry Pi to display temperature and humidity readings from a DHT22 sensor. Using the MQTT protocol,…

Know the mission before touching a wire.
Understand what you are making, prepare the right tools, and make the workbench safe.
Project details
ESP8266 Publishing DHT22 Readings with MQTT to Raspberry Pi is a iot project. 437.ESP8266 Publishing DHT22 Readings with MQTT to Raspberry Pi This project uses a Raspberry Pi to display temperature and humidity readings from a DHT22 sensor. Using the MQTT protocol,…
- Source pages
- 1077-1083
- Named parts
- 5
- Build goal
- Working, tested prototype
Tools you need
- Digital multimeter
- Wire stripper and side cutters
- Soldering iron with a fine tip
- Current-limited bench supply
- Computer with a data-capable USB cable
Use eye protection, good lighting, and a clean insulated surface throughout the build.
Safety precautions
- Disconnect every power source before changing a connection.
- Check component polarity, pinout, and supply voltage twice.
- Use a current limit for the first power-up.
- Keep liquids, loose metal, and uninsulated wires away from the bench.
Gather, identify, and understand every part.
Use the standardized inventory, then open What's this? to learn each part's role, advantages, limitations, handling, and specifications.
What's this?Image, role, pros, cons, handling & specifications

x ESP8266 12E – read Best ESP8266 Wi-Fi Development Boards
A programmable controller that reads inputs, makes decisions, and drives the project's outputs.
What it does hereIt is the control centre and must use the documented board, pin map, supply, and logic level.
Buy / compare this part ↗Advantages
- Reprogrammable and reusable
- Large learning ecosystem
- Complex behaviour remains changeable
Limitations
- GPIO voltage and current are limited
- Some pins affect boot or communication
- Loads normally need a driver
Handling
- Disconnect power before rewiring
- Avoid static discharge
- Never power motors, relays, or pumps directly from GPIO
Specifications to verify
- Use the exact model, value, package, and rating listed for x ESP8266 12E – read Best ESP8266 Wi-Fi Development Boards; similar-looking parts are not always interchangeable.
- Confirm operating voltage, logic level, pinout, memory, USB interface, and maximum GPIO current.
What's this?Image, role, pros, cons, handling & specifications

x DHT22 Sensor
A sensor converts a physical condition into an electrical signal the circuit can measure.
What it does hereIt provides project input as an analogue, digital, resistive, frequency, or calibrated signal.
Buy / compare this part ↗Advantages
- Adds real-world awareness
- Can usually be tested independently
- Often supports calibration
Limitations
- Readings can drift
- Placement affects results
- Some sensors need warm-up or calibration
Handling
- Protect the sensing surface
- Observe supply voltage and polarity
- Keep signal leads away from noisy power wiring
Specifications to verify
- Use the exact model, value, package, and rating listed for x DHT22 Sensor; similar-looking parts are not always interchangeable.
- Confirm supply range, output type, measurement range, accuracy, response time, and pin order.
What's this?Image, role, pros, cons, handling & specifications
PASSIVE LEARNING VIEWx 4700 Ohm Resistor
A passive component sets current, voltage, timing, filtering, or signal behaviour without adding gain.
What it does hereIts exact value and tolerance determine how the surrounding stage behaves.
Buy / compare this part ↗Advantages
- Simple and dependable
- Low cost
- Easy to measure before installation
Limitations
- A wrong value can stop or damage the circuit
- Ratings must not be exceeded
- Polarized parts require correct orientation
Handling
- Measure unclear values
- Observe capacitor polarity
- Avoid overheating leads while soldering
Specifications to verify
- Use the exact model, value, package, and rating listed for x 4700 Ohm Resistor; similar-looking parts are not always interchangeable.
- Confirm value, tolerance, power or voltage rating, polarity, and package size.
What's this?Image, role, pros, cons, handling & specifications

x 470 Ohm resistors
A named project component whose exact role is defined by the source circuit and build guide.
What it does hereIt performs a documented electrical, control, interface, or construction function in this project.
Buy / compare this part ↗Advantages
- Selected for this project
- Can be checked independently
- Supports modular troubleshooting
Limitations
- Substitutes may differ
- Generic names can hide variants
- Pinouts and ratings vary
Handling
- Compare the received part with the source
- Keep it labelled
- Do not force connectors or adjusters
Specifications to verify
- Use the exact model, value, package, and rating listed for x 470 Ohm resistors; similar-looking parts are not always interchangeable.
- Confirm dimensions, ratings, connection method, polarity, and environmental limits.
What's this?Image, role, pros, cons, handling & specifications

x LEDs
A named project component whose exact role is defined by the source circuit and build guide.
What it does hereIt performs a documented electrical, control, interface, or construction function in this project.
Buy / compare this part ↗Advantages
- Selected for this project
- Can be checked independently
- Supports modular troubleshooting
Limitations
- Substitutes may differ
- Generic names can hide variants
- Pinouts and ratings vary
Handling
- Compare the received part with the source
- Keep it labelled
- Do not force connectors or adjusters
Specifications to verify
- Use the exact model, value, package, and rating listed for x LEDs; similar-looking parts are not always interchangeable.
- Confirm dimensions, ratings, connection method, polarity, and environmental limits.
Connect one verified path at a time.
Explore the named components and standardized signal flow, then use Source Check to verify exact physical pins, values, and topology before applying power.
Trace this circuit.
ESP8266 Publishing DHT22 Readings with MQTT to Raspberry Pi: interactive parts, standardized terminals, responsive anchored wires, student explanations, and the original circuit reference in one shared system.
Trace before wiring
Follow power, ground, inputs, processing, and outputs in that order. Never guess a pin from package shape alone.
Connect with power off
Make short, labelled connections and share a common ground only where the schematic requires it.
Inspect every joint
Check continuity, polarity, adjacent shorts, and loose connections before the first power-up.
Open all source diagrams and build views 3 visuals



Confirm the hardware-only control path.
This project does not include firmware in the source. The circuit itself provides the required behaviour.
How to connect
- Match every controller label to the circuit view and source pin map.
- Join grounds before signal wires when separate low-voltage supplies are used.
- Keep motors, relays, pumps, and other loads on a suitable driver and external supply.
Common mistakes
Reversed VCC/GND, board-label versus GPIO-number confusion, missing common ground, and charge-only USB cables.
Troubleshoot
Disconnect loads, continuity-test one path at a time, then test with a current limit.
Software preparation
No IDE, board package, library, or firmware upload is required for this project.
If you add a programmable controller as an extension, document its pin map separately.
How to upload code
The original design is implemented entirely in hardware, so proceed after verifying the circuit and supply.
Assemble, deploy, test, and troubleshoot.
Use the complete source notes in build order, then pass the final checks before calling the project finished.
Build in functional stages
- Power and regulation
- Controller or processing stage
- Inputs and sensors
- Outputs and loads
- Enclosure and strain relief
Power up safely
- Inspect unpowered continuity first
- Apply the lowest safe current limit
- Measure supply rails before signals
- Add one load at a time
- Record expected and actual results
Work from simple to complex
- Confirm power, ground, polarity, and orientation
- Compare each pin with the source
- Test inputs separately from outputs
- Replace only one variable at a time
- Power off before every correction
Follow the documented instructions.
These notes come from this project's source and remain in their original order.
Project overview
Project build note437.ESP8266 Publishing DHT22 Readings with MQTT to Raspberry Pi
This project uses a Raspberry Pi to display temperature and humidity readings from a DHT22 sensor. Using the MQTT protocol, it is also possible to control two outputs coming from an ESP8266. Flask is the name of the Python microframework that you will be utilizing in order to create the web server. The following is an overview of the system at a high level: Initial Configuration of a Raspberry Pi Before you proceed with reading about this project, check to see that the Raspbian operating system has been installed on your Raspberry Pi. You can install Raspbian and finish the basic setup by reading my guide to getting started with the Raspberry Pi. Run and install Mosquitto broker The MQTT protocol is going to be used to facilitate communication between the Raspberry Pi and the ESP8266. In addition to having Mosquitto broker installed on your system, you are required to have Mosquitto broker actively
running in the background
Project build noteInstalling Flask For the purpose of transforming the Raspberry Pi into a web server, we will be utilizing a Python microframework known as Flask. You will need to have pip already installed in order to install Flask. To bring your Raspberry Pi up to date and install pip, run the following commands: pi@raspberrypi ~ $ sudo apt-get update
pi@raspberrypi ~ $ sudo apt-get upgrade pi@raspberrypi:$ sudo apt-get install python-flask git-core python-pip git- core The next step is to install Flask and Paho MQTT by utilizing pip: pi@raspberrypi ~ $ sudo pip install flask pi@raspberrypi ~ $ sudo pip install paho-mqtt pi@raspberrypi ~ $ sudo pip install paho-mqtt Installing SocketIO This project makes use of SocketIO, which makes it possible for you to create a web page in Python Flask that can be updated asynchronously by the Python Flask application you've created. This indicates that you do not need to refresh the web page in order to view the most recent readings because they are immediately updated. Installing the Flask SocketIO Python package is going to be your next step. pi@raspberrypi ~ $ sudo pip install flask-socketio Developing the Script Using Python The essential script for our application can be found here. When these buttons are pressed, it publishes a MQTT message to the ESP8266 and then sets up the web server. In addition, it has readings for temperature and humidity MQTT topics subscribed to so that it can receive them.
Programming an ESP8266 module Installing the PubSubClient library on the Raspberry Pi is necessary in order for the ESP8266 to communicate with the web server running on the Pi. This
library offers a client that is capable of carrying out straightforward publish/subscribe messaging with a server that supports MQTT (basically allows your ESP8266 to talk with Python web server). Installing the PubSubClient library 1. To download the PubSubClient library, please click on the link provided. Within your Downloads folder, there should be a folder labeled ".zip." 2. After you have unzipped the.zip folder, you should see a folder named pubsubclient-master. 3. Rename your folder's current name, which is pubsubclient-master, to just pubsubclient. 4. Transfer the pubsubclient folder to the libraries folder that was created during the installation of your Arduino IDE. The library includes several different examples in the form of sketches. Within the Arduino IDE software, navigate to the File menu and select Examples before selecting PubSubClient. Installing the DHT sensor library Your ESP8266 or Arduino boards will be able to read temperature and humidity with ease when you use the DHT sensor library. This library makes it simple to use any DHT sensor. 1. To download the DHT sensor library, click on the link provided. Within your Downloads folder, there should be a folder labeled ".zip." 2. After you have unzipped the.zip folder, you should have access to the DHT-sensor-library-master folder.
3. Rename your folder's current name, which is DHT-sensor-library- master, to simply DHT. 4. Transfer the DHT folder to the libraries folder that was created during the installation of your Arduino IDE. 5. Next, reopen the Arduino IDE on your computer. Uploading sketch At this point, you are ready to finish the process by uploading the complete sketch to your ESP8266 (replace with your SSID, password, and RPi IP
Part Required
Project build note1x ESP8266 12E – read Best ESP8266 Wi-Fi Development Boards 1x DHT22 Sensor 1x 4700 Ohm Resistor 2x 470 Ohm resistors 2x LEDs
Circuit Diagram
Project build noteIt is imperative that you use the Vin pin that outputs 5V on your ESP8266, as the DHT sensor works correctly only when it receives the required 5V voltage to do so. Activating and Starting the Web Server In order to initiate the launch of your Raspberry Pi web server, navigate to
the folder that houses the app.py file
Project build notepi@raspberrypi:/web-server/templates $ cd.. ;;;;;;;;;;;;;;;;;;;;;;;;;;;
Execute the following command after that
Project build notepi@raspberrypi:/web-server $ sudo python app.py and then press enter.
Your web server needs to start up right away on port 8181!
Demonstration
Project build noteIf you want to access the address of your Raspberry Pi, open your browser and enter its IP address. In my case, that address is http://192.168.1.98:8181. Please note that you will need to enter your IP address followed by the number 8181.
You built ESP8266 Publishing DHT22 Readings with MQTT to Raspberry Pi.
You followed the full workflow from understanding the mission to testing the finished project. That is a real engineering achievement - well done.
Browse all 500 projects