このページは機械翻訳を使用して翻訳されました。最新版の英語を参照するには、ここをクリックします。
Particle Photonクライアントを使用してチャネルの更新を購読する
この例では、Particle Photonボードを使用して CheerLightsチャネルからのチャネル更新をサブスクライブする方法を示します。プログラムはチャネルから色を読み取り、Photon ボードの内蔵 LED を使用して色を表示します。この例に示すように、チャネルフィードに登録することも、CheerLightsチャネルのカラー フィールドに直接登録することもできます。
セットアップ
1) CheerLightsチャネル用の MQTT デバイスを作成します。
a) ThingSpeak ページの上部にある [Devices] > [MQTT] をクリックし、[Add a new device] をクリックします。
b) デバイスの名前と説明を入力します。
c) チャネルのアクセスを許可する の下で、Select a Channel > Enter a channel ID below をクリックします。
d) CheerLightsチャネル番号 1417 を入力し、チャネルの追加 をクリックします。

e) 承認済みチャネル リストに CheerLights (1417) が含まれるようになりました。デバイスの追加をクリックします。
f) 「新しいデバイスが追加されました」ダイアログが表示されます。[Download Credentials] > [Plain Text]をクリックします。完了をクリックします。以下のコード セクションで保存した資格情報を使用します。
2) Particle IDE にライブラリ MQTT/MQTT.h を含めます。
3) Web IDE を使用して、新しい Photon アプリを作成します。
4) MQTT ライブラリを追加します。ライブラリを追加すると、コードの先頭に #include <MQTT.h> という行が追加されます。
5) ここに表示されているコードを貼り付けます。必要に応じて、channelID,、username、および password を変更します。
コード
1) まず、ThingSpeak ™ と通信するための変数を宣言します。CheerLights のフィールド 1 (チャネル1417) を購読します。プログラムには、これらの行の先頭のスペースに MQTT ライブラリが組み込まれています。
// This #include statement is automatically added by the Particle IDE. #include <MQTT.h> const long channelID = 1417; String subscribeTopic = "channels/" + String( channelID ) + "/subscribe/fields/field1"; char clientId[] = "MQTT_CLIENDID_FROM_THINGSPEAK"; //clientID returned when adding this device on ThingSpeak char username[] = "MQTT_USERNAME_FROM_THINGSPEAK"; //username returned when adding this device on ThingSpeak char password[] = "MQTT_PASSWORD_FROM_THINGSPEAK"; //password returned when adding this device on ThingSpeak char server[] = "mqtt3.thingspeak.com"; MQTT client(server, 1883, callback); // Initialize the MQTT client
2) MQTT ブローカーから中継されたメッセージは、メッセージを処理するために callback 関数を呼び出します。
void callback(char* topic, byte* payload, unsigned int length) {
char p[length + 1]; // Leave an extra space to null terminate the string.
memcpy(p, payload, length);
p[length] = NULL; // Terminate the string.
Particle.publish(p);
if (!strncmp(p, "red", 3)) {
RGB.color(255, 0, 0);
}
else if (!strncmp(p, "green", 5)) {
RGB.color(0, 255, 0);
}
else if (!strncmp(p, "blue", 4)) {
RGB.color(0, 0, 255);
}
else if(!strncmp(p, "yellow", 6)) {
RGB.color(255, 255, 0);
}
else if(!strncmp(p, "orange", 5)) {
RGB.color(255, 165, 0);
}
else if(!strncmp(p, "magenta", 5)) {
RGB.color(255, 0, 255);
}
else if(!strncmp(p, "cyan", 5)) {
RGB.color(0, 255, 255);
}
else if(!strncmp(p, "white", 5)) {
RGB.color(255, 255, 255);
}
else if(!strncmp(p, "oldlace", 5)) {
RGB.color(253, 245, 230);
}
else if(!strncmp(p, "purple", 5)) {
RGB.color(128, 0, 128);
}
else if(!strncmp(p, "pink", 5)) {
RGB.color(255, 192, 203);
}
else {
RGB.color(255, 255, 255);
}
}
3) setup 関数を使用して LED 制御を有効にし、MQTT 接続とサブスクリプションを開始します。
void setup() {
// Set up the onboard LED.
RGB.control(true);
// Connect to the server.
subscribeMQTT();
}
4) loop 関数で接続を確認し、接続が失われた場合は再接続してサブスクライブします。デバイスが時間をチェックするように指定します。この例では、午後 11 時から午前 4 時の間、デバイスをスリープ モードに設定します。
void loop() {
int timeHour = Time.hour();
if (client.isConnected()) {
client.loop();
} else {
subscribeMQTT();
}
if ((timeHour > 23) or (timeHour < 4)) {
Particle.publish("Sleep");
System.sleep(SLEEP_MODE_DEEP, 7200);
}
delay(1);
}
5) subscribeMQTT 関数を使用してブローカーとの接続を確立し、チャネルフィールドをサブスクライブします。
void subscribeMQTT() {
if (!client.isConnected()) {
Particle.publish("Connecting");
client.connect(clientId, username, password, NULL, MQTT::QOS0, 0, NULL, true);
delay(2000);
if (client.isConnected()) {
client.subscribe(subscribeTopic);
Particle.publish("Subscribed");
} else {
Particle.publish("Not connected");
}
}
}
コードをテスト
コードをテストには、 <ahref="https://cheerlights.com/growing-the-community-new-cheerlights-discord-bot/">https://cheerlights.com/growing-the-community-new-cheerlights-discord-bot/</a> にある Cheer Lights Discord サーバーを使用して、CheerLights の色を変更します。
参考
Subscribe to a Channel Feed | Subscribe to a Channel Field Feed