Particle Photon and DS18B20 Temp Sensor

I have a project where I want to use the Particle Photon with a DS18B20 temperature sensor. Has anyone tried to make this combination work with Cayenne? I found instructions to setup the Photon to work with Cayenne and that seems to work. When I add the DS18B20 sensor, the code it gives is for an Arduino Uno so some of that doesn’t work quite the same with Photon. Also I am bit confused on The photon using Blynk function instead of Cayenne but in the sketch for the sensor it uses Cayenne functions.

Thanks!

Welcome to Cayenne!

I actually haven’t tried the new native support for the ESP, so I guess I need to do that. The code for DS18B20 sensors should be the same for ESP and Arduino, but if you can you post what you have so far so we can make suggestions on what to change.

This is the sketch given Cyanne below. I load that on the Particle Photon and get tons or errors compiling. I tried using Particle’s versions of the libraries and that didn’t work. Any help or advice is appreciated.

/*
Cayenne DS18B20 Example

This sketch shows how to send temperature data to a DS18B20 Sensor in the Cayenne Dashboard.

The Cayenne Library is required to run this sketch. If you have not already done so you can install it from the Arduino IDE Library Manager.

Steps:
1. Install the OneWire library (http://www.pjrc.com/teensy/td_libs_OneWire.html) from the Arduino Library Manager.
2. Install the DallasTemperature library (http://milesburton.com/Main_Page?title=Dallas_Temperature_Control_Library#Code.2FLibrary) from the Arduino Library Manager.
3. In the Cayenne Dashboard add a new DS18B20 widget.
4. Set the widget to Value Display.
5. Select Virtual Pins and a virtual pin number.
6. Set VIRTUAL_PIN to the pin number you selected.
7. Attach a DS18B20 to an digital pin on your Arduino.
   Schematic:
   [Ground] -- [DS18B20] -- [4.7k resistor] -- [5V]
                   |______________|
                   |
              Digital Pin
8. Set the tmpPin variable to match the pin used to connect the DS18B20.
9. Set the token variable to match the Arduino token from the Dashboard.
10. Compile and upload this sketch.
11. Once the Arduino connects to the Dashboard it should automatically update the DS18B20 widget with data.
*/

#define CAYENNE_PRINT Serial  // Comment this out to disable prints and save space
#include <OneWire.h>
#include <DallasTemperature.h>

// If you're not using the Ethernet W5100 shield, change this to match your connection type. See Communications examples.
#include <CayenneEthernet.h>

// Virtual Pin of the DS18B20 widget.
#define VIRTUAL_PIN V1

// Digital pin the DS18B20 is connected to. Do not use digital pins 0 or 1 since those conflict with the use of Serial.
const int tmpPin = 2;

OneWire oneWire(tmpPin);
DallasTemperature sensors(&oneWire);

// Cayenne authentication token. This should be obtained from the Cayenne Dashboard.
char token[] = "t0ken4cayenne";

void setup()
{
	Serial.begin(9600);
	Cayenne.begin(token);
	sensors.begin();
}

void loop()
{
	Cayenne.run();
}

// This function is called when the Cayenne widget requests data for the Virtual Pin.
CAYENNE_OUT(VIRTUAL_PIN)
{
	// Send the command to get temperatures.
	sensors.requestTemperatures();
	// This command writes the temperature in Celsius to the Virtual Pin.
	Cayenne.celsiusWrite(VIRTUAL_PIN, sensors.getTempCByIndex(0));
	// To send the temperature in Fahrenheit use the corresponding code below.
	//Cayenne.fahrenheitWrite(VIRTUAL_PIN, sensors.getTempFByIndex(0));
}

Hmm not sure how/why I thought you were using an ESP, clearly says you are using a Particle Photon my fault. I actually don’t have a Particle Photon so I’ll order one tonight and give it a go unless someone else has one and can respond faster. Until then, try to combine the sketch in the link below with the blank default Cayenne sketch.

Thanks! I will check this out and see if it helps!

I just copied the code and “as-is” it will not compile in the Partical Web IDE. It references a “DS18.h” library that I can not find in the libraries and the Web IDE doesn’t let you upload or specify your own.

Hmm ok. I did order a Photon so I’ll give it a try in the next week or so.

1 Like

Hi Adam! Just curious if you were ever able to give this a go? Thx!

I did actually. I got my particle connected to the dashboard over the weekend, didn’t get to the DS18B20 part. I was planning on getting to that tonight.

1 Like

This works for me, DS18B20 is connected to D0:

#define CAYENNE_PRINT Serial
#include "cayenne-particle.h"
#include "DS18.h"

DS18 sensor(D0);

char auth[] = "xxxxxxxxx";

void setup()
{
    Serial.begin(9600);
    delay(5000); // Allow board to settle
    Blynk.begin(auth);
}

void loop()
{
    Blynk.run();
    if (sensor.read()) {
        // Do something cool with the temperature
        Serial.printf("Temperature %.2f C %.2f F ", sensor.celsius(), sensor.fahrenheit());
        Particle.publish("temperature", String(sensor.celsius()), PRIVATE);
        Blynk.virtualWrite(V2, sensor.celsius());
        
        // Additional info useful while debugging
        printDebugInfo();
        
        // If sensor.read() didn't return true you can try again later
        // This next block helps debug what's wrong.
        // It's not needed for the sensor to work properly
    } else {
        // Once all sensors have been read you'll get searchDone() == true
        // Next time read() is called the first sensor is read again
        if (sensor.searchDone()) {
            Serial.println("No more addresses.");
            // Avoid excessive printing when no sensors are connected
            delay(250);
        
        // Something went wrong
        } else {
          printDebugInfo();
        }
    }
    Serial.println();
}

void printDebugInfo() {
  // If there's an electrical error on the 1-Wire bus you'll get a CRC error
  // Just ignore the temperature measurement and try again
  if (sensor.crcError()) {
    Serial.print("CRC Error ");
  }

  // Print the sensor type
  const char *type;
  switch(sensor.type()) {
    case WIRE_DS1820: type = "DS1820"; break;
    case WIRE_DS18B20: type = "DS18B20"; break;
    case WIRE_DS1822: type = "DS1822"; break;
    case WIRE_DS2438: type = "DS2438"; break;
    default: type = "UNKNOWN"; break;
  }
  Serial.print(type);

  // Print the ROM (sensor type and unique ID)
  uint8_t addr[8];
  sensor.addr(addr);
  Serial.printf(
    " ROM=%02X%02X%02X%02X%02X%02X%02X%02X",
    addr[0], addr[1], addr[2], addr[3], addr[4], addr[5], addr[6], addr[7]
  );

  // Print the raw sensor data
  uint8_t data[9];
  sensor.data(data);
  Serial.printf(
    " data=%02X%02X%02X%02X%02X%02X%02X%02X%02X",
    data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7], data[8]
  );
}
2 Likes

This works!!! I have the Photon at my office with the DS18B20 wired up to it and I flashed the code in the particle IDE with my auth code. I added the sensor back in the Cayenne dashboard and viola!
I think part of my problem was the sketch given by Cayenne and other’s I found in the Particle community referenced the OneWire and DallasTemp libraries so I included them and they are always tripping up errors. And some of the differenced between the Cayenne and Blynk libraries. I read somewhere that Cayenne was relying on Blynk for Arduino but they were going to be moving away from it.

Anyhow this works great and I really appreciate your help! The fact that you actually bought a Photon to help me out is going above and beyond. I wish I could repay you somehow.

1 Like

Haha don’t worry about it. That’s what I’m here for. As long as someone finds it useful I’m happy. The photon looked pretty cool so it gave me an excuse to get one. It’s really a cool device but I have the say the web IDE really needs some work.

1 Like

Yes I agree the web IDE does need some work for sure. They have a desktop version you can install too. Which did you use? For the code, did you start off with the initial code to get the photon connected with Cayenne and then merge in a Blynk example for the ds18b20 sensor? Or is that all from scratch? Just curious.

Again, Thank you very much! This helps me out immensely!

I wasn’t aware there was a desktop version of the IDE, but I did not look very hard either. To come up with the code I used the Cayenne example to make sure I could get connected to the dashboard, then I tried 2 or 3 DS18B20 libraries until I found one that actually worked then just merged the two together. The printDebugInfo() function at the bottom really isn’t necessary, I just included it in case you had any problems. Technically the way I threw that sketch together isn’t ideal but it works. I’ll make it right and post back when I have it done.

@tech3 Here’s a better version. I stripped out everything unnecessary and it only sends data to Cayenne when requested. The sensor is connected to D0 and I’m using V2 for the virtual pin. The DS18B20 library I used is Particle Web IDE Cayenne library Particle Web IDE

#define CAYENNE_PRINT Serial
#include "cayenne-particle.h"
#include "DS18.h"

DS18 sensor(D0);

char auth[] = "xxxxxxxxxxx";

void setup()
{
    Serial.begin(9600);
    delay(5000); // Allow board to settle
    Blynk.begin(auth);
}

void loop()
{
    Blynk.run();
}

BLYNK_OUT(V2)
{
    if (sensor.read()) {
        // Do something cool with the temperature
        Serial.printf("Temperature %.2f C %.2f F ", sensor.celsius(), sensor.fahrenheit());
        Particle.publish("temperature", String(sensor.celsius()), PRIVATE);
        Blynk.virtualWrite(V2, sensor.celsius());
        
        // If sensor.read() didn't return true you can try again later
        // This next block helps debug what's wrong.
        // It's not needed for the sensor to work properly
    } else {
        // Once all sensors have been read you'll get searchDone() == true
        // Next time read() is called the first sensor is read again
        if (sensor.searchDone()) {
            Serial.println("No more addresses.");
            // Avoid excessive printing when no sensors are connected
            delay(250);
        }
    }
}

Hey thanks! I was going to go in and do the same thing at some point. Take out the unnecessary stuff but you saved me the trouble. So far its still working great.

On an off-topic, I have an Asus Tinker Board and I’m trying to get Cayenne running on it but its not installing. I’m going to start a new community thread on it. Just wanted to mention it to you incase you might have any thoughts.

Thanks!

Tim

Tag me in that thread. I actually have one and I can help out with that too.

Hello Adam,

I’m trying to setup my Particle Photon to connect to cayenne, looking at the code you provided, however I’m unable to make the correlation from my cayenne mqtt username and password to the authtoken. Can you point me in the right direction?

Thanks!

That’s was actually an old unsupported connection method. I’m not sure if there are any examples here to connect with mqtt. I’m not at my computer right now but I’ll see what I can come up with this weekend. @shramik_salgaonkar before I go digging in drawers for my photon do you know of example code?

I never had my hands on particle photon. Let me check it with the team. Give it a try and see if you can get it working.

1 Like