Main Content

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.

Set Up

1) Create an MQTT device for the CheerLights channel.

a) Click Devices > MQTT at the top of the ThingSpeak page, then click Add a new device.

b) Enter a name and description for your device.

c) Under Authorize channels to access, click Select a Channel > Enter a channel ID below.

d) Enter the CheerLights channel number 1417 and click Add Channel.

e) Your Authorized Channel list now includes CheerLights (1417). Click Add Device.

f) The New Device Added dialog appears. Click Download Credentials > Plain Text. Click Done. Use the saved credentials in the Code section below.

2) Include the library MQTT/MQTT.h in your Particle IDE.

3) Using the Web IDE, create a new Photon app.

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

5) Paste the code shown here. Modify channelID, and username, and password as appropriate.

Code

1) Start by declaring the variables to communicate with ThingSpeak™. Subscribe to field 1 of CheerLights, which is channel 1417. The program includes the MQTT library in the space at the top of these lines.

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

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

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

5) Use the subscribeMQTT function to establish the connection with the broker and then subscribe to the channel field.

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

Test Your Code

To test your code, change the CheerLights color using the cheer lights discord server at https://cheerlights.com/growing-the-community-new-cheerlights-discord-bot/.

See Also

|

Related Examples

More About