DS18B20 temp

Hi , can anyone help me with connecting multiple DS18B20 temp sensors using Cayenne ?

Connecting the one on its own is simple enough , but i cant seem to get 2 working using the latest arduino library MQTT version , i have connected them successfully using Blynk , but i prefer the Cayenne interface and data logging.

Dave

this is a code for multiple ds18b20 Can I connect more than one ds18b20 to the same arduino? - #6 by kreggly and just edit to work with MQTT version.

@shramik_salgaonkar,

Yeah, adding multiples is easy, but please review this app note because most of the problems we see with One Wire devices are wiring.

I’d also like to make the DS18B20
more fault tolerant as it gets stuck from time to time.

Cheers,

Craig

1 Like

Hi Craig .

Thanks for the reply.
I see you mentioned the wiring for these sensors, just to check ,with Cayenne do i need to wire them separately with a 4.7k resistor for each sensor connected to a individual Pin , or as with Blynk all wired in parallel with one common resistor back to one Pin and identified by the sensors mac address ?

I have one working with Cayenne using the arduino IDE Cayenne example MQTT DS18B20 ( below)
wired (red 5v) (yellow sig) ( black E ) resistor between red and yellow , works fine , just cant seem to add another sensor , they always both read the same value . i have tried adapting the the supplied sketch but i get constant errors or duplicate readings, it seem that sketch is for the older version that used tokens.

Thanks
Dave

/*
Cayenne DS18B20 Example

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

The CayenneMQTT 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 (OneWire Arduino Library, connecting 1-wire devices (DS18S20, etc) to Teensy) from the Arduino Library Manager.
  2. Install the DallasTemperature library (Miles Burton - Innovative engineering for pretty much anything) from the Arduino Library Manager.
  3. Attach a DS18B20 to a digital pin on your Arduino.
    Schematic:
    [Ground] – [DS18B20] – [4.7k resistor] – [5V]
    |______________|
    |
    Digital Pin
  4. Set the SENSOR_PIN value below to the pin number you used when connecting the sensor.
  5. Set the VIRTUAL_CHANNEL value below to a free virtual channel (or the virtual channel of a DS18B20 Sensor widget you have added) in the Dashboard.
  6. Set the Cayenne authentication info to match the authentication info from the Dashboard.
  7. Compile and upload this sketch.
  8. Once the Arduino connects to the Dashboard it should automatically create a temporary display widget (or update the DS18B20 Sensor widget you have added) with data.
    To make a temporary widget permanent click the plus sign on the widget.
    */

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

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username = “MQTT_USERNAME”;
char password = “MQTT_PASSWORD”;
char clientID = “CLIENT_ID”;

#define SENSOR_PIN 2 // Do not use digital pins 0 or 1 since those conflict with the use of Serial.
#define VIRTUAL_CHANNEL 1

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

void setup()
{
Serial.begin(9600);
Cayenne.begin(username, password, clientID);
sensors.begin();
}

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

// This function is called at intervals to send sensor data to Cayenne.
CAYENNE_OUT(VIRTUAL_CHANNEL)
{
// Send the command to get temperatures.
sensors.requestTemperatures();
// This command writes the temperature in Celsius to the Virtual Channel.
Cayenne.celsiusWrite(VIRTUAL_CHANNEL, sensors.getTempCByIndex(0));
// To send the temperature in Fahrenheit use the corresponding code below.
//Cayenne.fahrenheitWrite(VIRTUAL_CHANNEL, sensors.getTempFByIndex(0));
}

You are fine with the one pullup if you’re close. You are using the DS18B20 which is the powered sensors and not the parasitic ones which will draw from the bus power.

When you do the sensor request temperatures, an array is created, and all the values on the bus will be returned in an array.

To access the second sensor, you do this:

sensors.getTempCByIndex(1);

Cheers,

Craig

Thanks again Craig , so do i duplicate that code once with 0 once with 1 ?
when i change the figure i get the same temp on both sensors ie, 0 gives me 40 degrees on both, 1 gives me 25 degrees on both , i tried putting both but no luck .

Dave

Add this to your sketch for the second one.

CAYENNE_OUT(V2)
{
// Send the command to get temperatures.
sensors.requestTemperatures();
// This command writes the temperature in Celsius to the Virtual Channel.
Cayenne.celsiusWrite(V2, sensors.getTempCByIndex(1));
}

It will create a new widget on your dash with the second one.

You can have up to 32 channels. V1 = 1, V2=2, etc.

Cheers,

Craig

Sorry to keep coming back to you Graig , but im still getting nowhere , now my 2 temp widgets swap between the to readings every 5 seconds or so which is something i guess ,i have tried adding as you suggested but get numerous errors below is where im up to .

Dave

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

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username[] = "";
char password[] = "";
char clientID[] = "";

#define SENSOR_PIN 2 // Do not use digital pins 0 or 1 since those conflict with the use of Serial. 
#define VIRTUAL_CHANNEL 1
#define VIRTUAL_CHANNEL 2

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

void setup()
{
	Serial.begin(9600);
	Cayenne.begin(username, password, clientID);
	sensors.begin();
}

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

// This function is called at intervals to send sensor data to Cayenne.
CAYENNE_OUT(VIRTUAL_CHANNEL)
{
	// Send the command to get temperatures.
	sensors.requestTemperatures();
	// This command writes the temperature in Celsius to the Virtual Channel.
	Cayenne.celsiusWrite(VIRTUAL_CHANNEL , sensors.getTempCByIndex(1));

 
 Cayenne.celsiusWrite(VIRTUAL_CHANNEL , sensors.getTempCByIndex(0));

	// To send the temperature in Fahrenheit use the corresponding code below.
	//Cayenne.fahrenheitWrite(VIRTUAL_CHANNEL, sensors.getTempFByIndex(0));


}

My dude. Replace your defines and out functions with this.

I also recommend going through the Arduino tutorials. It helps. It really helps.

Cheers,

Craig

#define VIRTUAL_CHANNEL_1 1
#define VIRTUAL_CHANNEL_2 2

CAYENNE_OUT(VIRTUAL_CHANNEL_1)
{
// Send the command to get temperatures.
sensors.requestTemperatures();
// This command writes the temperature in Celsius to the Virtual Channel.
Cayenne.celsiusWrite(VIRTUAL_CHANNEL_1 ,sensors.getTempCByIndex(0));
}
CAYENNE_OUT(VIRTUAL_CHANNEL_2)
{
// Send the command to get temperatures.
sensors.requestTemperatures();
// This command writes the temperature in Celsius to the Virtual Channel.
Cayenne.celsiusWrite(VIRTUAL_CHANNEL_2 ,sensors.getTempCByIndex(1));
}

I have 7 sensors 10 ft long and definitely need a pull up for each sensor. Of course that’s an Rpi, never tried Arduino.

William

Hi William.

Do you have each of your sensors wired to their own GPIO or are they using a common input and wired in parallel ?

Dave

Try this blog. the diagram shows parallel connection

william