2 sensors with the same device


#1

Hey, can we add 2 different sensors which are connecting to the same device to Cayenne platform? Thank you


#2

welcome to the cayenne community @sara.loukili.lasri. yes we can connect. can you provide more detail about the sensors and device.


#3

Thank you for your answer, I tried to connect DHT11 and luminosity sensors with NODEMCU 8266, the values of the luminosity sensor are displayed but the other of DHT11 aren’t. when I disconnect the luminosity sensor the values of DHT11 are displayed. I don’t know where is the problem.


#4

can you share the code you are using.


#5
#include<CayenneMQTTESP8266.h>
#include<DHT.h>

#define CAYENNE_DEBUG 
#define CAYENNE_PRINT Serial 
int lightPin = A0;  //define a pin for Photo resistor
int ledPin=13;


char ssid[] = "";
char password[] = "";

char username[]="";
char mqtt_password[]="";
char client_id[]="";

#define DHTPIN 4
#define DHTTYPE DHT11   
DHT dht(DHTPIN, DHTTYPE);

void setup() {
  
  Cayenne.begin(username, mqtt_password, client_id,ssid,password);
  pinMode(2,OUTPUT);//the plue led in esp8266
  digitalWrite(2,HIGH);// this digital write to set off the led
   //pinMode( ledPin, OUTPUT );

}

void loop() {
  
 Cayenne.loop();
 float temp = dht.readTemperature(true);
 float hum = dht.readHumidity();
 int sensorValue = analogRead(lightPin);
 float voltage = sensorValue * (5.0 / 1023.0);// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V)

 Cayenne.virtualWrite(1,temp, TYPE_TEMPERATURE, UNIT_FAHRENHEIT);
 Cayenne.virtualWrite(2,hum, TYPE_RELATIVE_HUMIDITY, UNIT_PERCENT);
  Cayenne.virtualWrite(4,voltage, TYPE_TEMPERATURE,UNIT_FAHRENHEIT );

}

CAYENNE_IN(0)
{
  digitalWrite(2, !getValue.asInt());
}

#6

delete all the widgets from the dashboard and Give the below code a try.

#include<CayenneMQTTESP8266.h>
#include<DHT.h>

#define CAYENNE_DEBUG 
#define CAYENNE_PRINT Serial 
int lightPin = A0;  //define a pin for Photo resistor
int ledPin=13;


char ssid[] = "";
char password[] = "";

char username[]="";
char mqtt_password[]="";
char client_id[]="";

#define DHTPIN 4
#define DHTTYPE DHT11   
DHT dht(DHTPIN, DHTTYPE);
unsigned long lastMillis = 0;

void setup() {
  
  Cayenne.begin(username, mqtt_password, client_id,ssid,password);
  pinMode(2,OUTPUT);//the plue led in esp8266
  digitalWrite(2,HIGH);// this digital write to set off the led
   //pinMode( ledPin, OUTPUT );

}

void loop() {
  
 Cayenne.loop();
 float temp = dht.readTemperature(true);
 float hum = dht.readHumidity();
 int sensorValue = analogRead(lightPin);
 float voltage = sensorValue * (5.0 / 1023.0);// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V)
  if (millis() - lastMillis > 10000) {
    lastMillis = millis();
 Cayenne.virtualWrite(1,temp, "temp", "f");
 Cayenne.virtualWrite(2,hum, "rel_hum", "p");
  Cayenne.virtualWrite(4,voltage, "voltage","v");
}
}

CAYENNE_IN(0)
{
  digitalWrite(2, !getValue.asInt());
}

#7

Oh, I’m so grateful, It’s working, what are the supported parameters of the virtualWrite function: the last 2 parameters (the type of sensor and unit?). Thank you for your help.


#8

#9

Thank you so much for your help.