STEP 1 / 6ESP + IOT

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,…

Circuit Atlas themed schematic for ESP8266 Publishing DHT22 Readings with MQTT to Raspberry Pi
PROJECT#437
TRACKIoT
PARTS05
STAGES06
STEP 1 / 6 · Overview

Know the mission before touching a wire.

Understand what you are making, prepare the right tools, and make the workbench safe.

01

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
02

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.

03

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.
Ready to continue?
STEP 2 / 6 · Parts library

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.

NAMED PROJECT INVENTORY5 PART LINES
PARTTYPEQTYREADY
Mx ESP8266 12E – read Best ESP8266 Wi-Fi Development BoardsMODULE1
What's this?Image, role, pros, cons, handling & specifications
Circuit Atlas themed schematic for ESP8266 Publishing DHT22 Readings with MQTT to Raspberry PiMODULE LEARNING VIEW

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 here

It 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.
Mx DHT22 SensorMODULE1
What's this?Image, role, pros, cons, handling & specifications
Circuit Atlas themed schematic for ESP8266 Publishing DHT22 Readings with MQTT to Raspberry PiMODULE LEARNING VIEW

x DHT22 Sensor

A sensor converts a physical condition into an electrical signal the circuit can measure.

What it does here

It 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.
Px 4700 Ohm ResistorPASSIVE1
What's this?Image, role, pros, cons, handling & specifications
Axial through-hole resistorPASSIVE LEARNING VIEW

x 4700 Ohm Resistor

A passive component sets current, voltage, timing, filtering, or signal behaviour without adding gain.

What it does here

Its 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.
Px 470 Ohm resistorsPART1
What's this?Image, role, pros, cons, handling & specifications
Circuit Atlas themed schematic for ESP8266 Publishing DHT22 Readings with MQTT to Raspberry PiPART LEARNING VIEW

x 470 Ohm resistors

A named project component whose exact role is defined by the source circuit and build guide.

What it does here

It 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.
Px LEDsPART1
What's this?Image, role, pros, cons, handling & specifications
Circuit Atlas themed schematic for ESP8266 Publishing DHT22 Readings with MQTT to Raspberry PiPART LEARNING VIEW

x LEDs

A named project component whose exact role is defined by the source circuit and build guide.

What it does here

It 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.
Ready to continue?
STEP 4 / 6 · Source code

Confirm the hardware-only control path.

This project does not include firmware in the source. The circuit itself provides the required behaviour.

01

How to connect

  1. Match every controller label to the circuit view and source pin map.
  2. Join grounds before signal wires when separate low-voltage supplies are used.
  3. 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.

02

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.

03

How to upload code

The original design is implemented entirely in hardware, so proceed after verifying the circuit and supply.

Ready to continue?
STEP 5 / 6 · Build

Assemble, deploy, test, and troubleshoot.

Use the complete source notes in build order, then pass the final checks before calling the project finished.

ASSEMBLY

Build in functional stages

  • Power and regulation
  • Controller or processing stage
  • Inputs and sensors
  • Outputs and loads
  • Enclosure and strain relief
TEST

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
TROUBLESHOOT

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
PROJECT-SPECIFIC BUILD NOTES

Follow the documented instructions.

These notes come from this project's source and remain in their original order.

01

Project overview

Project build note

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, 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

02

running in the background

Project build note

Installing 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

03

Part Required

Project build note

1x ESP8266 12E – read Best ESP8266 Wi-Fi Development Boards 1x DHT22 Sensor 1x 4700 Ohm Resistor 2x 470 Ohm resistors 2x LEDs

04

Circuit Diagram

Project build note

It 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

05

the folder that houses the app.py file

Project build note

pi@raspberrypi:/web-server/templates $ cd.. ;;;;;;;;;;;;;;;;;;;;;;;;;;;

06

Execute the following command after that

Project build note

pi@raspberrypi:/web-server $ sudo python app.py and then press enter.

Your web server needs to start up right away on port 8181!

07

Demonstration

Project build note

If 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.

Ready to continue?
PROJECT ACHIEVED

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.

ESP8266-Based Do-It-Yourself Wi-Fi RGB LED Mood Light for $10 (Step by Step) project thumbnail featuring x RGB LED Strip, x Power Supply Device with 12V Output Capable of Reducing, Voltage to 5V Alternative: LM7805 with Heat Sink
NEXT IOT ADVENTURE

ESP8266-Based Do-It-Yourself Wi-Fi RGB LED Mood Light for $10 (Step by Step)

Ready to reuse what you learned in another project from the same track?

Start this project
Browse all 500 projects