センサーノードを配線してコードを書く
環境データを読み取り、MQTTでパブリッシュします。
「センサーノードを配線してコードを書く」はCoddyKit上の無料Arduino & IoT Academyレッスンです。 これはレッスン2/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはArduino & IoT Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Arduino & IoT Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
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. 📡
よくある質問
「センサーノードを配線してコードを書く」レッスンは無料ですか?
はい。「センサーノードを配線してコードを書く」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Arduino & IoT Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Arduino & IoT Academyコースには全4レッスンが含まれています。
「センサーノードを配線してコードを書く」で何を学びますか?
環境データを読み取り、MQTTでパブリッシュします。 ブラウザで直接実行するハンズオンコードでArduino & IoT Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Arduino & IoT Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのArduino & IoT Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン2/4です。
「センサーノードを配線してコードを書く」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このArduino & IoT Academyレッスンでコードを書いて実行できますか?
はい。すべてのArduino & IoT Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- エンドツーエンドのアーキテクチャを計画する
- センサーノードを配線してコードを書く
- クラウドダッシュボードとアラートを作る
- 現場に展開・監視・更新する