Particle Photon and DS18B20 Temp Sensor

@shramik_salgaonkar I tried to connect my Particle but it’s not happening. Do you know anyone that has one in the Cayenne team? I’m not sure how to tell why it’s not working. There are no errors that I can see, it just doesn’t connect. Here is the code I tried:

// This #include statement was automatically added by the Particle IDE.
#include <DS18B20.h>
//DS18B20 ds18b20(D0);

// This #include statement was automatically added by the Particle IDE.
#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() {
    Serial.begin(9600);
    delay(5000); // Allow board to settle
    client.connect("cayenne", "username", "password");
    //Particle.publish("setup finished");
}

int counter = 0;
void loop() {
    //Particle.publish("loop");
    counter = counter++;
    
    if (client.isConnected()){
        Particle.publish("connected");
        client.loop();
    
        String mesasge = "temp,f=" + counter;
    
        client.publish("v1/username/things/clientid/data/1", mesasge);
    }
    
    delay(1000);
}

thank you @adam for giving it a try. We currently do not have an official support for the photon.

Guess I’ll try to connect to a local MQTT server to see if it’s a problem with Cayenne, the library, or the device. The photon is a cool easy to use device, I think it would be wise to get official support on it. @jburhenn

1 Like

The (unsupported) library that someone ingenuously forked from Blynk to allow Particle to work with Cayenne no longer works. Ever since having to go MQTT. I have spent hours trying to use the Arduino-MQTT library for Cayenne and make it work with Particle and so far no luck. I have made an official request for MyDevices to get together with Particle and work on Integration. With Particle releasing thier new Mesh products I think there is going to be more of a demand for Cayenne users wanting to use Particle devices with their projects. I have a number of projects I have had to switch up to using a NodeMCU instead of Photon for this reason. I love the Photon!

@Tim_G_Tech photon support is on the roadmap, there are a lot of tasks to be completed before that and it will take time to release.

I figured it was. I know you guys have a heavy workload already. Was just throwing something out there. I will be happy to do some beta testing when that time comes. Keep up the good work! :slight_smile:

we let you know when we release and more than happy if you could do some testing on it :slightly_smiling_face:

@Tim_G_Tech I set up a local MQTT server to mimic the Cayenne MQTT server (same user/pass/clientID) and I’m able to send data locally so I don’t believe it’s the device or MQTT library. I can even see that the photon is connecting (or it at least thinks it is) to the Cayenne server but as soon as it sends any data it’s being disconnected and the last data received on the dashboard doesn’t update. I pinged the slack group, waiting to hear back what’s going on at the server.

Which library are you using specifically? I would like to try it. Thanks.

It’s just called mqtt Particle Web IDE I think that link should work

You can use my code up a few posts if you want to test it.

1 Like

I took a loot at this MQTT library and compared with the Cayenne-Arduino-MQTT library downloaded from GitHub. It looks like there is a ton of stuff missing. Mostly a lot of other supporting libraries to make it all work properly.
I may attempt to fork it if I can make the time.
Here’s the link to the GitHub repo: GitHub - myDevicesIoT/Cayenne-MQTT-Arduino: Cayenne MQTT Arduino Library

The only things missing will be the cayenne.virtual write, etc. You can still send properly formatted payloads. Have a look at my project here

1 Like

@Tim_G_Tech I found my mistake. I was putting in the wrong value for client ID. Try this code, it will just increment a temp widget.

// This #include statement was automatically added by the Particle IDE.
#include <DS18B20.h>
//DS18B20 ds18b20(D0);
// This #include statement was automatically added by the Particle IDE.
#include <MQTT.h>
void callback(char* topic, byte* payload, unsigned int length);
String username = "";
String password = "";
String clientID = "";
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() {
    Serial.begin(9600);
    delay(5000); // Allow board to settle
    client.connect(clientID, username, password);
}
int counter = 0;
void loop() {
    counter = counter + 1;
    
    if (client.isConnected()){
        Particle.publish("connected");
        client.loop();
    
        String mesasge = "temp,f=" + String(counter);
    
        client.publish("v1/" + username + "/things/" + clientID + "/data/1", mesasge);
    }
    
    delay(2000);
}
2 Likes

Interesting. I’m glad you got it to work and thanks for sharing! I am going to try it out. I’ll post back my outcome.

@adam you got your particle photon connected to cayenne and is sending data?

Yep! With help from @ecoqba

2 Likes

Nice :+1:

@ryan.fasenmyer I got my Particle connected. If I have time this weekend I’ll hook up my DS18B20 and send you the code.

2 Likes

Thanks!

I can confirm it does work. I used @adam’s example with mine and was ale to send data to the dashboard. So kudos to @adam! Now what I need is to be able to send actuator commands to the device. I toyed around with client.subscribe for a bit and didn’t have any luck. Just trying to use an on/off widget to tun an LED on or off.

1 Like