Main Content

このページの翻訳は最新ではありません。ここをクリックして、英語の最新版を参照してください。

Particle Photon クライアントを使用したチャネル更新のサブスクライブ

この例では、Particle Photon ボードを使用して CheerLights チャネルのチャネル更新をサブスクライブする方法を説明します。プログラムは、Photon ボードの組み込み LED のチャネルから読み取った色を表示します。この例で示すように、チャネル フィードをサブスクライブするか、または CheerLights チャネルの色のフィールドを直接サブスクライブすることができます。

  1. 新しい Photon アプリを起動します。

  2. MQTT ライブラリを追加します。ライブラリの追加により、コードの一番上に #include <MQTT.h> の行が含められます。

  3. ここに示すコードを貼り付けます。channelIDMQTTAPIKey を変更します。

     完全なコードはここをクリック

    1. はじめに、ThingSpeak™ と通信するための変数を宣言します。チャネル 1417 である CheerLights の Field 1 を、Subscribe to a Channel Field Feedで説明されているトピックの形式を使用してサブスクライブします。プログラムは、これらの行よりも上のスペースで MQTT ライブラリをインクルードします。

      const long channelID = 1417;
      String subscribeTopic = "channels/" + String( channelID ) + "/subscribe/fields/field1";
      String MQTTAPIKey = "XXXXXXXXXXXXXXXX";
      
      void callback( char* topic, byte* payload, unsigned int length );
      
      MQTT client( "mqtt.thingspeak.com" , 1883 , callback );
      
    2. 関数 callback で接続される MQTT ブローカーからの、中継されたメッセージを処理します。

      // This function processes the messages relayed by the MQTT broker
      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.
      
          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 on board LED  
        RGB.control(true);
         
        // Connect to the server
        subscribeMQTT();
        
      }
    4. loop メソッドで接続を確認し、接続が切断されている場合には再接続してサブスクライブします。時間をチェックして、午後 11 時以降や、起動するには時間が早すぎる場合はスリープ状態になります。

      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()) {
                  client.connect( " PhotonSubscribeXX" , "Username" , MQTTAPIKey , NULL , MQTT::QOS0 , 0 , NULL , true );
                  Particle.publish( " Connect " );
                  delay( 1000 );
                  if ( client.isConnected())   {
            
                      //client.subscribe("channels/739/subscribe/fields/field2");
                      client.subscribe( subscribeTopic );
                      Particle.publish( "subs" );
                      }
           }
      }

参考

| | | |

関連する例

詳細