0Pricing
Arduino & IoT Academy · レッスン

コマンドを購読して実行する

メッセージが届いたらリレーを切り替えます。

「コマンドを購読して実行する」はCoddyKit上の無料Arduino & IoT Academyレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これは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時間対応のAIチューター)、Arduino & IoT Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Arduino & IoT Academyコースには全4レッスンが含まれています。

「コマンドを購読して実行する」で何を学びますか?

メッセージが届いたらリレーを切り替えます。 ブラウザで直接実行するハンズオンコードでArduino & IoT Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Arduino & IoT Academyを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのArduino & IoT Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。

「コマンドを購読して実行する」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このArduino & IoT Academyレッスンでコードを書いて実行できますか?

はい。すべてのArduino & IoT Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. MQTTがIoTに適する理由
  2. ブローカーに接続する
  3. センサーのトピックをパブリッシュする
  4. コマンドを購読して実行する
← Arduino & IoT Academyに戻る