Particle Photon

I am working on adding a particle photon to cayenne but looks like the cayenne-particle library is out of date. The library requires an auth token instead of username, password and client id. Am i missing something? If anyone has a particle photon communicating with cayenne, i would like to hear your suggestions.

Thanks,
-Dalibor

Hi @sherkon18 ,

Are you referring to this library? If so, yes, that’s deprecated. Technically you can try using any MQTT client library available for Photon and they should work.
Such as: GitHub - hirotakaster/MQTT: MQTT for Photon, Spark Core

Thanks

1 Like

This is good info. I am trying to use this library and have had no success. Wondering if i am using it incorrectly?

 #include "application.h"
#include "MQTT.h"


void callback(char* topic, byte* payload, unsigned int length);

MQTT client("mqtt.mydevices.com", 1883, callback);

// recieve message
void callback(char* topic, byte* payload, unsigned int length) {
    char p[length + 1];
    memcpy(p, payload, length);
    p[length] = NULL;

    if (!strcmp(p, "RED"))
        RGB.color(255, 0, 0);
    else if (!strcmp(p, "GREEN"))
        RGB.color(0, 255, 0);
    else if (!strcmp(p, "BLUE"))
        RGB.color(0, 0, 255);
    else
        RGB.color(255, 255, 255);
    delay(1000);
}


void setup() {
    RGB.control(true);

    client.connect("client id goes here", "mqtt user name goes here", "mqtt password goes here");

    // publish/subscribe
    if (client.isConnected()) {
        client.publish("miningRigTempHum","hello world");
        client.subscribe("miningRigTempHum/message");
    }
}

void loop() {
    if (client.isConnected())
        client.loop();
}

As long as you are using the right credentials provided by the dashboard, the only thing you should focus on is the MQTT topic. To send data publish to: v1/USERNAME/things/CLIENT_ID/data/CHANNEL

CHANNEL can be any number from 1-99

As @asanchezdelc said, at a quick glance it looks fine to me. If it is not working let me know. I have one somewhere that I can test your code with.

Thats my code below, even with suggested topic format by @asanchezdelc i am not able to connect. Keep the suggestion coming.

#include “application.h”
#include “MQTT.h”

char USR[] = "user id # goes here";
char PSS[] ="password # goes here";
char CLIENT_ID[] = "client id goes here";


void callback(char* topic, byte* payload, unsigned int length);

MQTT client("mqtt.mydevices.com", 1883, callback);

// recieve message
void callback(char* topic, byte* payload, unsigned int length) {
    char p[length + 1];
    memcpy(p, payload, length);
    p[length] = NULL;

    if (!strcmp(p, "RED"))
        RGB.color(255, 0, 0);
    else if (!strcmp(p, "GREEN"))
        RGB.color(0, 255, 0);
    else if (!strcmp(p, "BLUE"))
        RGB.color(0, 0, 255);
    else
        RGB.color(255, 255, 255);
    delay(1000);
}


void setup() {
    RGB.control(true);

    client.connect(CLIENT_ID,USR,PSS);

    // publish/subscribe
    if (client.isConnected()) {
        client.publish("vi/CLIENT ID GOES HERE/things/USERNAME GOES HERE/data/1","hello world");
       
    }
}

void loop() {
    if (client.isConnected())
        client.loop();
}

Seems fine your code. I slightly modified the right payload format and turn the LED green once its connected. Also you had vi instead of v1.

char USR[] = "user id # goes here";
char PSS[] ="password # goes here";
char CLIENT_ID[] = "client id goes here";


void callback(char* topic, byte* payload, unsigned int length);

MQTT client("mqtt.mydevices.com", 1883, callback);

// recieve message
void callback(char* topic, byte* payload, unsigned int length) {
    char p[length + 1];
    memcpy(p, payload, length);
    p[length] = NULL;

    if (!strcmp(p, "RED"))
        RGB.color(255, 0, 0);
    else if (!strcmp(p, "GREEN"))
        RGB.color(0, 255, 0);
    else if (!strcmp(p, "BLUE"))
        RGB.color(0, 0, 255);
    else
        RGB.color(255, 255, 255);
    delay(1000);
}


void setup() {
    RGB.control(true);

    client.connect(CLIENT_ID,USR,PSS);

    // publish/subscribe
    if (client.isConnected()) {
         RGB.color(0, 255, 0);
        client.publish("v1/CLIENT ID GOES HERE/things/USERNAME GOES HERE/data/1","temp,c=19");
       
    }
}

void loop() {
    if (client.isConnected())
        client.loop();
}

Hi Adrien
I have tried to use the library and your code, but my photon blocks fixed green.
Any idea what might be happening?
Thanks in advance!
pedro

Do you mean the LED is not turning green? Is the device showing connected on the dashboard?

Not connected, LED does light green, but no connection
Here’s the code the full code.

#include "MQTT.h"

char USR[] = "b260eXXXXXXXXXXXXXXffc576d47cde";
char PSS[] ="faXXXXXXXXXXXXXXXXXXXXX";
char CLIENT_ID[] = "7XXXXXXXXXXXXXXX";

void callback(char* topic, byte* payload, unsigned int length);

/**
 * if want to use IP address,
 * byte server[] = { XXX,XXX,XXX,XXX };
 * MQTT client(server, 1883, callback);
 * want to use domain name,
 * exp) iot.eclipse.org is Eclipse Open MQTT Broker: https://iot.eclipse.org/getting-started
 * MQTT client("iot.eclipse.org", 1883, callback);
 **/
MQTT client("mqtt.mydevices.com", 1883, callback);

// recieve message
void callback(char* topic, byte* payload, unsigned int length) {
    char p[length + 1];
    memcpy(p, payload, length);
    p[length] = NULL;

    if (!strcmp(p, "RED"))
        RGB.color(255, 0, 0);
    else if (!strcmp(p, "GREEN"))
        RGB.color(0, 255, 0);
    else if (!strcmp(p, "BLUE"))
        RGB.color(0, 0, 255);
    else
        RGB.color(255, 255, 255);
    delay(1000);
}


void setup() {
    RGB.control(true);

    // connect to the server
    client.connect(CLIENT_ID,USR,PSS);

    // publish/subscribe
    if (client.isConnected()) {
        RGB.color(0, 255, 0);
       client.publish("v1/CLIENT_ID/things/USR/data/1","temp,c=19");
        client.subscribe("inTopic/message");
    }
}


void loop() {
    if (client.isConnected())
        client.loop();
}

Thanks for your help!

if your green light is ON it means that the connection is established.

It is on, but on cayenne dashboard the photon is offline.
I don’t understand, maybe some syntax error

can you give the code from here a try:

It worked all right!

Thank you so much

2 Likes