الاتصال بـ Broker
استخدم PubSubClient للوصول إلى خادم MQTT.
الاتصال بـ Broker درس مجاني في Arduino & IoT Academy على CoddyKit. هذا هو الدرس 2 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Arduino & IoT Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Arduino & IoT Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
You Need a Broker First
Before any messages flow, your device must reach a broker. It is the server that accepts connections and routes every message.
Public or Private Brokers
You can use a free public broker like test.mosquitto.org for learning, or run your own private one for real projects.
The PubSubClient Library
On ESP boards the popular choice is the PubSubClient library. Install it once, then include it at the top of your sketch.
#include <PubSubClient.h>It Rides on a WiFi Client
PubSubClient needs a network client to ride on. You hand it a WiFiClient that has already joined your network.
WiFiClient espClient;
PubSubClient client(espClient);Point It at the Broker
Tell the client where the broker lives with its address and port. Plain MQTT uses port 1883 by default.
client.setServer("test.mosquitto.org", 1883);Give Each Device a Client ID
Every connection needs a unique client ID. If two devices share one, the broker kicks the older connection off.
Make the Connection
Call connect with your client ID to log in. It returns true when the broker accepts you and false if something is wrong.
client.connect("esp32-sensor-01");Reconnect When It Drops
Connections die on real networks. Wrap your connect in a loop so the device keeps retrying until it is back online.
Keep the Link Alive
You must call client.loop() often inside loop(). It handles keepalive pings and incoming messages behind the scenes.
void loop() {
client.loop();
}Authenticate When Needed
Private brokers usually want a username and password. Pass them to connect so only trusted devices get in.
client.connect("id", "user", "pass");Watch the Connection State
Print the result of connect or check client.state() to debug. Clear logs save you from guessing why nothing arrives.
Quick Check
Your ESP32 sketch compiles but never receives messages, even though it connected once. What is the likely cause?
Recap
You connected to a broker with PubSubClient: set the server and port, used a unique client ID, and kept the link alive. 🎉
الأسئلة الشائعة
هل درس «الاتصال بـ Broker» مجاني؟
نعم — نص درس «الاتصال بـ Broker» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Arduino & IoT Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Arduino & IoT Academy 4 دروس في المجموع.
ماذا ستتعلم في «الاتصال بـ Broker»؟
استخدم PubSubClient للوصول إلى خادم MQTT. تتمرن على Arduino & IoT Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Arduino & IoT Academy؟
لا تُشترط خبرة سابقة. Arduino & IoT Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 2 من أصل 4.
كم من الوقت يستغرق درس «الاتصال بـ Broker»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Arduino & IoT Academy هذا؟
نعم. كل درس في Arduino & IoT Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.