Two widgets one virtual pin


#1

Hi, I’m new here. I have made a simple nodeMCU + ds18b20 temperature device that sends data to Cayenne. I haven’t found so far a way to have both chart and digit value of a single pin, is it possible?

Thanks!


#2

Hi @kurjak, welcome to the Cayenne Community.

Cayenne won’t let you have two widgets with the same pin setting, but this shouldn’t be a big deal in this case. You just need to duplicate in your sketch file sending the same data to two different virtual pins, and then you can create 2 DS18B20 widgets pointing to two different virtual pins with the same data, one a value and one a graph.

Assuming the example Arduino DS18B20 sketch file, you need to make 2 unique VIRTUAL_PIN defines. For example, assuming you want virtual pins 2 and 3:

#define VIRTUAL_PIN V2
#define VIRTUAL_PINB V3

Notice there is no need to duplicate the physical tmpPin since its the same physical sensor. Then you need two CAYENNE_OUT requests, one for each 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));
}
CAYENNE_OUT(VIRTUAL_PINB)
{
	// Send the command to get temperatures.
	sensors.requestTemperatures();
	// This command writes the temperature in Celsius to the Virtual Pin.
	Cayenne.celsiusWrite(VIRTUAL_PINB, sensors.getTempCByIndex(0));
	// To send the temperature in Fahrenheit use the corresponding code below.
	//Cayenne.fahrenheitWrite(VIRTUAL_PINB, sensors.getTempFByIndex(0));
}

#3

Actually, using this method the reading could vary slightly because they are 2 separate reads of the sensor. I would use a timer to read the sensor at a regular interval and then just use the associated variable for both outputs to Cayenne.
In setup:
timer.setInterval(5000L, getData);

In main loop:
timer.run();

Functions:

void getData(){
  sensors.requestTemperatures(); // Send the command to get temperatures
  dal_temp = sensors.getTempCByIndex(0);
}

CAYENNE_OUT(VIRTUAL_PIN){
	// This command writes the temperature in Celsius to the Virtual Pin.
	Cayenne.celsiusWrite(VIRTUAL_PIN, dal_temp);
	// To send the temperature in Fahrenheit use the corresponding code below.
	//Cayenne.fahrenheitWrite(VIRTUAL_PIN, dal_temp);
}

CAYENNE_OUT(VIRTUAL_PINB){
	// This command writes the temperature in Celsius to the Virtual Pin.
	Cayenne.celsiusWrite(VIRTUAL_PINB, dal_temp);
	// To send the temperature in Fahrenheit use the corresponding code below.
	//Cayenne.fahrenheitWrite(VIRTUAL_PINB, dal_temp);
}

#4

Something I hadn’t considered, thank you for the idea @andrewawise! :slight_smile:


#5

Hey whatever works! :+1: :slight_smile:


#6

Hey, a nice explanation… I’m putting it in my Cayenne how-to article…


#7

Thanks everyone, very valuelable response!


#8

Thank you, this was really helpful!


#9

Using MQTT there is no need for this code, i have single pin reading on 2 widgets, reads and updates without problem, only good thing i had from switching from Cayenne Arduino to Cayenne mqtt lib, everything else fails.
23-29-23