このページは機械翻訳を使用して翻訳されました。元の英語を参照するには、ここをクリックします。
MQTT 経由で Particle デバイス クライアントを使用してThingSpeakチャネルにパブリッシュする
この例では、ボロン、アルゴン、フォトン、電子などのパーティクル デバイスを使用して、MQTT 経由で測定値をThingSpeakチャネルに公開する方法を示します。ThingSpeakに送信する値が複数ある場合は、複数の値をチャネルフィードに公開できます。また、センサーが 1 つだけの場合は、単一の値をチャネル フィールドにパブリッシュできます。
設定
1) Collect Data in a New Channelに示すように、新しいチャネルを作成します。使用する予定のすべてのフィールドを必ず有効にしてください。
2) ThingSpeakページの上部にある [デバイス] > [MQTT] をクリックし、[デバイスの追加] をクリックして MQTT デバイスを作成します。デバイスをセットアップし、新しいチャネルを認証リストに追加するときに、«Download Credentials» > «Plain Text» をクリックします。以下の「コード」セクションで保存した認証情報を使用します。詳細については、 「ThingSpeak MQTT デバイスの作成」を参照してください。
3) ライブラリMQTT/MQTT.h
を Particle IDE に含めます。
コード
1) ThingSpeakとの通信に使用する変数を定義します。自分の ID と資格情報のコードを編集します。
// This #include statement is automatically added by the Particle IDE. #include <MQTT.h> const long channelId = YOUR_THINGSPEAK_CHANNEL_NUMBER; // Change this to your ThingSpeak channel number. String clientId = "MQTT_CLIENDID_FROM_THINGSPEAK"; String username = "MQTT_USERNAME_FROM_THINGSPEAK"; String password = "MQTT_PASSWORD_FROM_THINGSPEAK"; char server[] = "mqtt3.thingspeak.com";
2) 最終接続時間を追跡し、グローバル変数を使用してデータの公開時間間隔を定義します。MQTTクライアントを初期化します。
unsigned long lastConnectionTime = 0; const unsigned long postingInterval = 20L * 1000L; // Post data every 20 seconds. int strength = WiFi.RSSI(); int power = 10^(strength/10); MQTT client(server, 1883, callback); // Initialize the MQTT client.
3) MQTT クライアントのコールバック関数を定義します。この場合、 setup
関数は意図的に空になっています。
// Define a callback function to initialize the MQTT client. void callback(char* topic, byte* payload, unsigned int length) { } void setup() { }
4) MQTT 接続を確立し、メインのloop
関数で一定の時間間隔でデータをチャネルにパブリッシュします。
void loop() { // If MQTT client is not connected then reconnect. if (!client.isConnected()) { reconnect(); } // Call the loop continuously to establish connection to the server. client.loop(); if (millis() - lastConnectionTime > postingInterval) { mqttpublish(); } }
5) mqttpublish
メソッドを使用して、センサー データをThingSpeakチャネルに公開します。チャネル フィードにパブリッシュする場合、複数のフィールドに一度にパブリッシュできます。センサーが 1 つのみの場合は、単一のフィールドに直接パブリッシュできます。必要に応じて、次のコードのコメント行を変更します。
void mqttpublish() { //Get SSID signal strength strength = WiFi.RSSI(); //Power in milliwatts power = 10^(strength/10); // Create a data string to send data to ThingSpeak. // Use these lines to publish to a channel feed, which allows multiple fields to be updated simultaneously. // String data = String("field1=" + String(strength) + "&field2=" + String(power) ); // String topic = String("channels/"+String(channelId)+ "/publish"); // Use the next two to publish to a single channel field directly. String data = String(strength); String topic = String("channels/"+String(channelId)+ "/publish/fields/field1"); client.publish(topic,data); lastConnectionTime = millis(); }
6) 一意のクライアント ID を生成し、 reconnect
関数を使用してParticle Photon MQTT クライアントをThingSpeak MQTT ブローカーに接続します。
void reconnect(){ Particle.publish("Attempting MQTT connection"); // Connect to the ThingSpeak MQTT broker. if (client.connect(clientId,username,password)) { // Track the connection with particle console. Particle.publish("Conn:"+ String(server) + " cl: " + String(clientId)+ " Uname:" + String(username)); } else { Particle.publish("Failed to connect. Trying to reconnect in 2 seconds"); delay(2000); } }
参考
Publish to a Channel Field Feed | Publish to a Channel Feed
関連する例
- デスクトップ MQTT クライアントを使用したチャネルのパブリッシュとサブスクライブ
- セキュア MQTT を使用したThingSpeakチャネルの公開とサブスクライブ
- Raspberry Pi 上の Python で WebSocket を使用してパブリッシュする