ThingSpeak

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

Subscribe to Channel Updates Using Particle Photon Client

This example shows how to use a Particle Photon board to subscribe to channel updates from the CheerLights channel. The program displays the color read from the channel on the built-in LED on the Photon board. You can subscribe to the channel feed or directly to the color field on the CheerLights channel as shown in this example.

  1. Start a new Photon App.

  2. Add the MQTT library. Adding the library includes the line #include <MQTT.h> to the top of your code.

  3. Paste the code shown here. Modify the channelID and MQTTAPIKey.

     Full Code

    1. Start by declaring the variables to communicate with ThingSpeak™. Subscribe to field 1 of CheerLights, which is channel 1417, using the topic format described in Subscribe to a Channel Field Feed. The program includes the MQTT library in the space above these lines.

      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. Handle messages relayed from the MQTT broker that you connect with the callback function.

      // 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. Use the setup method to enable the LED control and start the MQTT connection and subscription.

      void setup() {
          
        // Set up the on board LED  
        RGB.control(true);
         
        // Connect to the server
        subscribeMQTT();
        
      }
    4. In the loop method, check for a connection and reconnect and subscribe if the connection has been lost. Check the time and go to sleep after 11 pm or if it is too early to get up.

      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. Use the subscribeMQTT function to establish the connection with the broker and then subscribe to the channel field.

      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" );
                      }
           }
      }

See Also

| | |

Related Examples

More About

Was this topic helpful?