الاشتراك في الأوامر وتنفيذها
بدّل Relay عند وصول رسالة.
الاشتراك في الأوامر وتنفيذها درس مجاني في Arduino & IoT Academy على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Arduino & IoT Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Arduino & IoT Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Listening for Messages
Publishing sends data out; subscribing lets your device receive it. This is how the cloud sends commands back to hardware.
Subscribe to a Topic
Tell the broker which topic you want with subscribe. From then on, matching messages are pushed straight to your device.
client.subscribe("home/livingroom/light");Messages Arrive in a Callback
Incoming messages trigger a callback function you write. The broker calls it every time a subscribed topic gets a message.
The Callback Signature
Your callback receives the topic, the payload bytes, and a length. You register it once during setup.
void onMessage(char* topic, byte* payload, unsigned int len) {
}Register the Callback
Hook your function up with setCallback before connecting. The library then routes every message to it.
client.setCallback(onMessage);Read the Payload Safely
The payload is raw bytes, not a finished string. Build a String by looping over its length so you never read past the end.
String msg;
for (int i = 0; i < len; i++)
msg += (char)payload[i];Decide What It Means
Compare the message to known commands. A simple if check on the text is enough to map ON and OFF to actions.
if (msg == "ON") digitalWrite(RELAY, HIGH);Act on the Command
Now do something real: flip a relay, move a servo, or change a light. This closes the loop from cloud to physical world.
Check the Topic Too
If you subscribe to several topics, inspect the topic argument first. That tells you which device the command targets.
Re-Subscribe After Reconnect
A dropped link forgets your subscriptions. After every reconnect, call subscribe again or you will hear silence.
Keep Callbacks Short
Do quick work inside the callback and return fast. Long jobs there can stall the MQTT loop and miss new messages.
Quick Check
After a WiFi drop your device reconnects but stops responding to commands. What did the reconnect break?
Recap
You learned to subscribe, handle messages in a callback, read the payload, act on commands, and re-subscribe after reconnects. 🎉
الأسئلة الشائعة
هل درس «الاشتراك في الأوامر وتنفيذها» مجاني؟
نعم — نص درس «الاشتراك في الأوامر وتنفيذها» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Arduino & IoT Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Arduino & IoT Academy 4 دروس في المجموع.
ماذا ستتعلم في «الاشتراك في الأوامر وتنفيذها»؟
بدّل Relay عند وصول رسالة. تتمرن على Arduino & IoT Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Arduino & IoT Academy؟
لا تُشترط خبرة سابقة. Arduino & IoT Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.
كم من الوقت يستغرق درس «الاشتراك في الأوامر وتنفيذها»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Arduino & IoT Academy هذا؟
نعم. كل درس في Arduino & IoT Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- لماذا يناسب MQTT إنترنت الأشياء
- الاتصال بـ Broker
- نشر Topics المستشعرات
- الاشتراك في الأوامر وتنفيذها