Use Particle Photon Client to Subscribe to Channel Updates

This example shows how to use a Particle Photon board to subscribe to channel updates from the CheerLights channel. The program reads a color from the channel and displays it using 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) Create a new Photon app.

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

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

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) Messages relayed from the MQTT broker invoke the callback function to handle the message.

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

2) Use the setup function to enable LED control and start the MQTT connection and subscription.

void setup() {
    
  // Set up the onboard LED.
  RGB.control(true);
   
  // Connect to the server.
  subscribeMQTT();
  
}

3) In the loop function, check for a connection and reconnect and subscribe if the connection is lost. Specify that the device check the time. For this example, set the device to sleep mode between 11 p.m. and 4 a.m.

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

}

4) 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