0Pricing
Arduino & IoT Academy · درس

نشر Topics المستشعرات

أرسل القراءات إلى Topics مسمّاة.

نشر Topics المستشعرات درس مجاني في Arduino & IoT Academy على CoddyKit. هذا هو الدرس 3 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Arduino & IoT Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Arduino & IoT Academy 4 دروس في المجموع.

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

Sending Data Is Publishing

Once connected, sharing a reading is one call. You publish a value to a topic and the broker delivers it to subscribers.

Choose a Clear Topic

Pick a descriptive topic path like home/garden/soil. Good names make a growing system easy to read and route.

home/garden/soil/moisture

The publish Call

The basic form takes a topic and a message. Both are simple strings, so MQTT does not care what is inside the payload.

client.publish("home/garden/temp", "23.5");

Payloads Are Just Text

A payload is a plain byte string. You decide its meaning, whether it is a number, a word, or structured data.

Numbers Need Converting

Sensor readings are often floats. Turn them into a string first with dtostrf or String() before you publish.

char buf[8];
dtostrf(temp, 4, 1, buf);
client.publish("home/temp", buf);

Send Structured Data as JSON

To bundle several values, format the payload as JSON. One message can carry temperature and humidity together.

client.publish("home/dht", "{\"t\":23.5,\"h\":55}");

Do Not Flood the Broker

Publishing thousands of times a second wastes power and bandwidth. Add a sensible interval so messages stay useful.

Time It with millis

Use millis() instead of delay to publish every few seconds. Your device stays responsive while it waits.

if (millis() - last > 5000) {
  last = millis();
  publishReading();
}

Retain the Latest Reading

Add a retain flag so the broker stores the most recent value. New dashboards then show data the instant they connect.

client.publish("home/temp", buf, true);

One Topic Per Quantity

Give each measurement its own topic, like .../temp and .../humidity. Clean separation keeps subscribers simple.

Confirm It Worked

The publish call returns true on success. Log it, or watch with a desktop MQTT client, to be sure data left the board.

Quick Check

You want a new dashboard to immediately show the last temperature, even before the sensor publishes again. What helps?

Recap

You learned to publish sensor data: convert values to strings, pick clear topics, time sends with millis, and retain the latest. 🎉

الأسئلة الشائعة

هل درس «نشر Topics المستشعرات» مجاني؟

نعم — نص درس «نشر Topics المستشعرات» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Arduino & IoT Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Arduino & IoT Academy 4 دروس في المجموع.

ماذا ستتعلم في «نشر Topics المستشعرات»؟

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

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

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

كم من الوقت يستغرق درس «نشر Topics المستشعرات»؟

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

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

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

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

  1. لماذا يناسب MQTT إنترنت الأشياء
  2. الاتصال بـ Broker
  3. نشر Topics المستشعرات
  4. الاشتراك في الأوامر وتنفيذها
← العودة إلى Arduino & IoT Academy