0Pricing
Arduino & IoT Academy · درس

توصيل عقدة المستشعر وبرمجتها

اقرأ بيانات المناخ وانشرها عبر MQTT.

توصيل عقدة المستشعر وبرمجتها درس مجاني في Arduino & IoT Academy على CoddyKit. هذا هو الدرس 2 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في 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/7) وفتح باقي دورة Arduino & IoT Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Arduino & IoT Academy 4 دروس في المجموع.

ماذا ستتعلم في «توصيل عقدة المستشعر وبرمجتها»؟

اقرأ بيانات المناخ وانشرها عبر MQTT. تتمرن على Arduino & IoT Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Arduino & IoT Academy؟

لا تُشترط خبرة سابقة. Arduino & IoT Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 2 من أصل 4.

كم من الوقت يستغرق درس «توصيل عقدة المستشعر وبرمجتها»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Arduino & IoT Academy هذا؟

نعم. كل درس في Arduino & IoT Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. تخطيط البنية من البداية إلى النهاية
  2. توصيل عقدة المستشعر وبرمجتها
  3. بناء لوحة السحابة والتنبيهات
  4. النشر والمراقبة والتحديث ميدانيًا
← العودة إلى Arduino & IoT Academy