0Pricing
Arduino & IoT Academy · レッスン

ブローカーに接続する

PubSubClientを使ってMQTTサーバーに接続します。

「ブローカーに接続する」はCoddyKit上の無料Arduino & IoT Academyレッスンです。 これはレッスン2/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これは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. 🎉

よくある質問

「ブローカーに接続する」レッスンは無料ですか?

はい。「ブローカーに接続する」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Arduino & IoT Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Arduino & IoT Academyコースには全4レッスンが含まれています。

「ブローカーに接続する」で何を学びますか?

PubSubClientを使ってMQTTサーバーに接続します。 ブラウザで直接実行するハンズオンコードでArduino & IoT Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

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

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

「ブローカーに接続する」レッスンにはどのくらい時間がかかりますか?

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

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

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

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

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