Wire & Code the Sensor Node
Read climate data and publish it over MQTT.
Wire & Code the Sensor Node is a free Arduino & IoT Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Arduino & IoT Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Build the Node
Now you turn the plan into hardware. You will wire the DHT22 to the ESP32 and write code that reads climate data and publishes it. 🔌
Wire Power and Ground
Connect the DHT22's VCC to 3.3V and GND to ground on the ESP32. Shared ground is what lets the two parts agree on signals.
Wire the Data Pin
Run the sensor's data line to a GPIO pin and add a pull-up resistor so the idle line stays clean and reads reliably.
Include Your Libraries
At the top of the sketch, pull in the tools you need: the DHT library for the sensor and PubSubClient for MQTT.
#include <DHT.h>
#include <WiFi.h>
#include <PubSubClient.h>Create the Sensor Object
Tell the library which pin and sensor type you used by creating a DHT object. This becomes your handle for every reading.
#define DHTPIN 4
DHT dht(DHTPIN, DHT22);Start Things in setup()
In setup() you open Serial, start the sensor with dht.begin(), and connect to WiFi so the node is ready before the loop runs.
void setup() {
Serial.begin(115200);
dht.begin();
}Read Temperature & Humidity
Each cycle, call readTemperature() and readHumidity() to grab fresh numbers from the room into two variables.
float t = dht.readTemperature();
float h = dht.readHumidity();Guard Against Bad Reads
Sensors sometimes return NaN. Check isnan() and skip publishing that round so you never send junk to the cloud.
if (isnan(t) || isnan(h)) {
return;
}Connect to the Broker
Point PubSubClient at your broker's address and port, then keep a reconnect helper so a dropped link is rejoined automatically.
client.setServer(BROKER, 1883);Publish a Reading
Send each value to its topic with client.publish(). The cloud now receives a fresh temperature every cycle.
client.publish("home/livingroom/temp", String(t).c_str());Pace the Loop
Don't flood the network. Use millis() timing to publish about once every few seconds, which is plenty for room climate.
Quick Check
Test your sensor-node code.
Recap
You wired the DHT22, read clean values, guarded bad reads, and published them over MQTT. Your sensor node is alive and talking. 📡
Frequently asked questions
Is the “Wire & Code the Sensor Node” lesson free?
Yes — the full text of “Wire & Code the Sensor Node” is free to read here on the web, and the Arduino & IoT Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Arduino & IoT Academy course, upgrade to CoddyKit PRO.
What will I learn in “Wire & Code the Sensor Node”?
Read climate data and publish it over MQTT. You practise Arduino & IoT Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Arduino & IoT Academy?
No prior experience is required. Arduino & IoT Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Wire & Code the Sensor Node” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Arduino & IoT Academy lesson?
Yes. Every Arduino & IoT Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Plan the End-to-End Architecture
- Wire & Code the Sensor Node
- Build the Cloud Dashboard & Alerts
- Deploy, Monitor & Update in the Field