2 dht22

#32

If you are able to get the readings and show them in the Serial Monitor you should be able to send them via a Cayenne write call. For example:

CAYENNE_OUT_DEFAULT()
{
    // Send temperature in Celsius
    Cayenne.virtualWrite(0, dht.readTemperature(true),  "temp", "c"); 
    // Send relative humidity in percent
    Cayenne.virtualWrite(1, dht.readHumidity(), "rel_hum", "p");
}

Then load up the dashboard site and you should see temporary widgets appear once data is sent. You can make those widgets permanent by clicking on the plus sign.

#33

Thank you very much, unfortunately I can not program, the code to read the values of the dht I got after a long series of ctrl -c and ctrl-v … :wink: will not be the state of the art programming but it works.
Thanks for the tips on how to send the values to the dashboard, I immediately start to try.

#34

Hi, I changed the code so I get an error report in CAYENNE_IN_DEFAULT () but I do not understand where I’m wrong.
Thank you

 /*
This example shows how to connect to Cayenne using a Serial USB connection and send/receive sample data.

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.

This requires the use of the Serial USB connection so you cannot use the Serial device for
printing messages. If you need to print you can use SoftwareSerial and connect another device
to read messages via the SoftwareSerial pins.

In order for this to work you must run the connection script on the machine the Arduino is connected to.
The scripts are located under the extras\scripts folder in the main library folder. This redirects the traffic
from the Arduino to the Cayenne server.

Steps:
1. Set the Cayenne authentication info to match the authentication info from the Dashboard.
2. Compile and upload this sketch.
3. Launch the connection script as described below for Windows or Linux/OSX.

Windows:
  1. Open the Windows command line (cmd.exe)
  2. Navigate to the scripts folder by typing "cd [path]", e.g.  "cd C:\Users\[YourUserName]\Documents\Arduino\libraries\CayenneMQTT\extras\scripts"
  3. Run the script by typing "cayenne-ser.bat -c COM4" (where COM4 is the Arduino serial port) and hitting Enter

Linux and OSX:
    ./cayenne-ser.sh (may need to run with sudo)
    
You can specify port, baud rate, and server endpoint like this:
    ./cayenne-ser.sh -c <serial port> -b <baud rate> -s <server address> -p <server port>

    For instance :
      ./cayenne-ser.sh -c /dev/ttyACM0 -b 9600 -s mqtt.mydevices.com -p 1883

    Run cayenne-ser.sh -h for more information

    Be sure to select the right serial port (there may be multiple).

ATTENTION!
  Do not use Serial to display any output in this sketch. It will interfere with the Serial
  USB connection. When uploading sketches the Arduino IDE may complain with "programmer is
  not responding" or "Access is denied." You will need to terminate the connection script
  before uploading new sketches since it blocks access to the Serial port. Also make sure 
  the Serial Monitor is disabled in the IDE since that can prevent the Arduino from 
  connecting to the Windows/Linux/OSX machine. If you use Visual Micro for Visual Studio make
  sure Automatic Debugging is disabled. Otherwise the Serial Monitor can interfere with the
  Serial connection.
*/

#include <CayenneMQTTSerial.h>
#include "DHT.h"

DHT dht(2,DHT22); //I define the pin to which the sensor and the type is connected
DHT dht2(3,DHT22);

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

void setup()
{
  //Baud rate can be specified by calling Cayenne.begin(username, password, clientID, 9600);
  Cayenne.begin(username, password, clientID);
}

void loop()
{
  float h = dht.readHumidity(); // Leggo il valore di umidità
  float t = dht.readTemperature(); // Leggo il valore di temperatura
  float h2 = dht2.readHumidity(); // Leggo il valore di umidità
  float t2 = dht2.readTemperature(); // Leggo il valore di temperatura
  if (isnan(t) || isnan(h)) // Se almeno uno dei due valori è NaN (Not a Number)
  if (isnan(t) || isnan(h)) // Se almeno uno dei due valori è NaN (Not a Number)
  {
    Serial.println("Impossibile leggere il sensore!");
  }
  else //altrimenti
  {
    Serial.print("Umidità_sensore1: ");
    Serial.print(h);
    Serial.print(" %\t");
    Serial.print("Temperatura_sensore1: ");
    Serial.print(t);
    Serial.println(" *C");
    Serial.print("Umidità_sensore2: ");
    Serial.print(h2);
    Serial.print(" %\t");
    Serial.print("Temperatura_sensore2: ");
    Serial.print(t2);
    Serial.println(" *C");
    delay(10000);
    
    
  }

// Default function for sending sensor data at intervals to Cayenne.
// You can also use functions for specific channels, e.g CAYENNE_OUT(1) for sending channel 1 data.
CAYENNE_OUT_DEFAULT()
{
    // Send temperature in Celsius
    Cayenne.virtualWrite(0, dht.readTemperature(true),  "temp_1_sensor", "c"); 
    Cayenne.virtualWrite(2, dht2.readTemperature(true),  "temp_2_sensor", "c"); 
    // Send relative humidity in percent
    Cayenne.virtualWrite(1, dht.readHumidity(), "rel_hum_1_sensor", "p");
    Cayenne.virtualWrite(3, dht2.readHumidity(), "rel_hum_2_sensor", "p");
}

// Default function for processing actuator commands from the Cayenne Dashboard.
// You can also use functions for specific channels, e.g CAYENNE_IN(1) for channel 1 commands.
CAYENNE_IN_DEFAULT()
{
  //Process message here. If there is an error set an error message using getValue.setError(), e.g getValue.setError("Error message");
}
#35

you were missing a } in void loop(). i have made the changes to your code.

#include <CayenneMQTTSerial.h>
#include "DHT.h"

DHT dht(2,DHT22); //I define the pin to which the sensor and the type is connected
DHT dht2(3,DHT22);

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

void setup()
{
  //Baud rate can be specified by calling Cayenne.begin(username, password, clientID, 9600);
  Cayenne.begin(username, password, clientID);
}

void loop()
{
  float h = dht.readHumidity(); // Leggo il valore di umidità
  float t = dht.readTemperature(); // Leggo il valore di temperatura
  float h2 = dht2.readHumidity(); // Leggo il valore di umidità
  float t2 = dht2.readTemperature(); // Leggo il valore di temperatura
  if (isnan(t) || isnan(h)) // Se almeno uno dei due valori è NaN (Not a Number)
  if (isnan(t) || isnan(h)) // Se almeno uno dei due valori è NaN (Not a Number)
  {
    Serial.println("Impossibile leggere il sensore!");
  }
  else //altrimenti
  {
    Serial.print("Umidità_sensore1: ");
    Serial.print(h);
    Serial.print(" %\t");
    Serial.print("Temperatura_sensore1: ");
    Serial.print(t);
    Serial.println(" *C");
    Serial.print("Umidità_sensore2: ");
    Serial.print(h2);
    Serial.print(" %\t");
    Serial.print("Temperatura_sensore2: ");
    Serial.print(t2);
    Serial.println(" *C");
    delay(10000);
    
    
  }
}
// Default function for sending sensor data at intervals to Cayenne.
// You can also use functions for specific channels, e.g CAYENNE_OUT(1) for sending channel 1 data.
CAYENNE_OUT_DEFAULT()
{
    // Send temperature in Celsius
    Cayenne.virtualWrite(0, dht.readTemperature(true),  "temp_1_sensor", "c"); 
    Cayenne.virtualWrite(2, dht2.readTemperature(true),  "temp_2_sensor", "c"); 
    // Send relative humidity in percent
    Cayenne.virtualWrite(1, dht.readHumidity(), "rel_hum_1_sensor", "p");
    Cayenne.virtualWrite(3, dht2.readHumidity(), "rel_hum_2_sensor", "p");
}

// Default function for processing actuator commands from the Cayenne Dashboard.
// You can also use functions for specific channels, e.g CAYENNE_IN(1) for channel 1 commands.
CAYENNE_IN_DEFAULT()
{
  //Process message here. If there is an error set an error message using getValue.setError(), e.g getValue.setError("Error message");
}

As a beginner you should avoid using the CayenneMQTTSerial.h library and replace with CayenneMQTTEthernet.h. it will help you in debuging in future,

#36

Thanks for your help.:pray:

#37

does not work. I give up my skills are too scarce for these things.
Thank you all for your help.

#38

this code works for me without any problem. it is currently using a single DHT22, is if it works for you.

/*
This example shows how to connect to Cayenne using an Ethernet W5100 shield and send/receive sample data.

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. Set the Cayenne authentication info to match the authentication info from the Dashboard.
2. Compile and upload the sketch.
3. A temporary widget will be automatically generated in the Cayenne Dashboard. To make the widget permanent click the plus sign on the widget.
*/

#define CAYENNE_DEBUG       // Uncomment to show debug messages
#define CAYENNE_PRINT Serial  // Comment this out to disable prints and save space
#include <CayenneMQTTEthernet.h>

//DTH
#include "DHT.h"
#define DHTPIN 2
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
#define DTH_hum_Virtual_Channel 1
#define DTH_tempC_Virtual_Channel 2
#define DTH_tempF_Virtual_Channel 3
#define DTH_hic_Virtual_Channel 4
#define DTH_hif_Virtual_Channel 5
unsigned long lastMillis_dht = 0;

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

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

void loop() {
	Cayenne.loop();
  //DTH
  if (millis() - lastMillis_dht > 10000) {
    lastMillis_dht = millis();
    // Reading temperature or humidity takes about 250 milliseconds!
    // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
    float h = dht.readHumidity();
    // Read temperature as Celsius (the default)
    float t = dht.readTemperature();
    // Read temperature as Fahrenheit (isFahrenheit = true)
    float f = dht.readTemperature(true);

    // Check if any reads failed and exit early (to try again).
    if (isnan(h) || isnan(t) || isnan(f)) {
      Serial.println("Failed to read from DHT sensor!");
      return;
    }

    // Compute heat index in Fahrenheit (the default)
    float hif = dht.computeHeatIndex(f, h);
    // Compute heat index in Celsius (isFahreheit = false)
    float hic = dht.computeHeatIndex(t, h, false);
    Serial.print("Humidity: ");
    Serial.print(h);
    Serial.print(" %\t");
    Serial.print("Temperature: ");
    Serial.print(t);
    Serial.print(" *C ");
    Serial.print(f);
    Serial.print(" *F\t");
    Serial.print("Heat index: ");
    Serial.print(hic);
    Serial.print(" *C ");
    Serial.print(hif);
    Serial.println(" *F");
    Cayenne.virtualWrite(DTH_hum_Virtual_Channel, h, "rel_hum", "p");
    Cayenne.virtualWrite(DTH_tempC_Virtual_Channel, t, "temp", "c");
    Cayenne.virtualWrite(DTH_tempF_Virtual_Channel, f, "temp", "f");
    Cayenne.virtualWrite(DTH_hif_Virtual_Channel, hif, "temp", "f");
    Cayenne.virtualWrite(DTH_hic_Virtual_Channel, hic, "temp", "c");

  }
}
#39

this code works for me without any problem. it is currently using a single DHT22, is if it works for you.

Thanks

#40

When you get reading from one dht22, just duplicate for second DHT.

#41

I was able to duplicate (only partially) the reading of the second dht. Below the screen shot of my dashboard and then that of the serial monitor.
I have not yet figured out which widgets I need to insert and how to connect them to the second dht.
I tried to copy the settings of the widgets that appeared automatically in the dashboard, but I did not understand anything :slight_smile:

Schermata 2018-07-17 alle 15.28.38

[820439] Publish: topic 1, channel 4, value 20.776, subkey c, key temp
[821519] Connection ok
Humidity: 43.10 % Temperature: 21.20 *C 70.16 *F Heat index: 20.50 *C 68.90 *F
[830852] Publish: topic 1, channel 1, value 43.100, subkey p, key rel_hum
[830930] Publish: topic 1, channel 2, value 21.200, subkey c, key temp
[831005] Publish: topic 1, channel 3, value 70.160, subkey f, key temp
[831080] Publish: topic 1, channel 5, value 68.902, subkey f, key temp
[831156] Publish: topic 1, channel 4, value 20.501, subkey c, key temp
Humidity2: 45.20 % Temperature2: 21.40 *C 70.52 *F Heat index: 20.78 *C 69.40 *F
[831520] Publish: topic 1, channel 1, value 45.200, subkey p, key rel_hum
[831600] Publish: topic 1, channel 2, value 21.400, subkey c, key temp
[831675] Publish: topic 1, channel 3, value 70.520, subkey f, key temp
[831750] Publish: topic 1, channel 5, value 69.396, subkey f, key temp
[831824] Publish: topic 1, channel 4, value 20.776, subkey c, key temp
[832904] Connection ok

#42

this code reads 5 parameters from the DTH22 namely

  1. humidity using float h = dht.readHumidity();
  2. temperature in Celsius using float t = dht.readTemperature();
  3. temperature in Fahrenheit using float f = dht.readTemperature(true);
  4. heat index in Celsius using float hif = dht.computeHeatIndex(f, h);
  5. heat index in Fahrenheit, using float hic = dht.computeHeatIndex(t, h, false);

Next this value are published/send to cayenne using:

Cayenne.virtualWrite(DTH_hum_Virtual_Channel, h, "rel_hum", "p");
Cayenne.virtualWrite(DTH_tempC_Virtual_Channel, t, "temp", "c");
Cayenne.virtualWrite(DTH_tempF_Virtual_Channel, f, "temp", "f");
Cayenne.virtualWrite(DTH_hif_Virtual_Channel, hif, "temp", "f");
Cayenne.virtualWrite(DTH_hic_Virtual_Channel, hic, "temp", "c");

Now remove whichever you don’t want from the code.

#43

Thank you! I duplicated the part of code for reading the sensors, but I had not seen this part that I did not duplicate.

#define DTH_hum_Virtual_Channel 1
#define DTH_tempC_Virtual_Channel 2
#define DTH_tempF_Virtual_Channel 3
#define DTH_hic_Virtual_Channel 4
#define DTH_hif_Virtual_Channel 5

thanks for your patience…

#44

you also need to duplicate this part with different name and channel number.

#45

It works!!! thank you very much to all the people who have had the desire and the patience to help me.
image![image|663x404]

Grazie

1 Like
#46

you can change the name of the widget by clicking on the clog wheel for setting on the widget.

1 Like
#47

Hello everyone, my sketch for two dht22 has stopped going, have you changed something that prevents you from using 2 sensors on the same arduino?
Thank you

#48

add #define CAYENNE_DEBUG in your code and check the issue in your serial monitor.

#49

Thanks for the answer, I have two dht22 and if I change the pins the result does not change I only see the pin 2, But the really strange thing is that suddenly it stopped working, so I thought you had taken away the possibility of having two sensors on the same board.

/*
This example shows how to connect to Cayenne using an Ethernet W5100 shield and send/receive sample data.

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. Set the Cayenne authentication info to match the authentication info from the Dashboard.
2. Compile and upload the sketch.
3. A temporary widget will be automatically generated in the Cayenne Dashboard. To make the widget permanent click the plus sign on the widget.
*/

#define CAYENNE_DEBUG       // Uncomment to show debug messages
//#define CAYENNE_PRINT Serial  // Comment this out to disable prints and save space
#include <CayenneMQTTEthernet.h>

//DTH
#include "DHT.h"
//#define DHTPIN 2//
//#define DHTTYPE DHT22
DHT dht(2, DHT22);
DHT dht7(7,DHT22);
#define DTH_hum_Virtual_Channel 1
#define DTH_tempC_Virtual_Channel 2
#define DTH_tempF_Virtual_Channel 3
#define DTH_hic_Virtual_Channel 4
#define DTH_hif_Virtual_Channel 5
#define DTH7_hum_Virtual_Channel 6
#define DTH7_tempC_Virtual_Channel 7
#define DTH7_tempF_Virtual_Channel 8
#define DTH7_hic_Virtual_Channel 9
#define DTH7_hif_Virtual_Channel 10
unsigned long lastMillis_dht = 0;

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


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

void loop() {
  Cayenne.loop();
  //DTH
  if (millis() - lastMillis_dht > 10000) {
    lastMillis_dht = millis();
    // Reading temperature or humidity takes about 250 milliseconds!
    // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
    float h = dht.readHumidity();
    // Read temperature as Celsius (the default)
    float t = dht.readTemperature();
    // Read temperature as Fahrenheit (isFahrenheit = true)
    float f = dht.readTemperature(true);

    // Check if any reads failed and exit early (to try again).
    if (isnan(h) || isnan(t) || isnan(f)) {
      Serial.println("Failed to read from DHT sensor!");
      return;
    }

    // Compute heat index in Fahrenheit (the default)
    float hif = dht.computeHeatIndex(f, h);
    // Compute heat index in Celsius (isFahreheit = false)
    float hic = dht.computeHeatIndex(t, h, false);
    Serial.print("Humidity: ");
    Serial.print(h);
    Serial.print(" %\t");
    Serial.print("Temperature: ");
    Serial.print(t);
    Serial.print(" *C ");
    Serial.print(f);
    Serial.print(" *F\t");
    Serial.print("Heat index: ");
    Serial.print(hic);
    Serial.print(" *C ");
    Serial.print(hif);
    Serial.println(" *F");
    Cayenne.virtualWrite(DTH_hum_Virtual_Channel, h, "rel_hum", "p");
    Cayenne.virtualWrite(DTH_tempC_Virtual_Channel, t, "temp", "c");
    Cayenne.virtualWrite(DTH_tempF_Virtual_Channel, f, "temp", "f");
    Cayenne.virtualWrite(DTH_hif_Virtual_Channel, hif, "temp", "f");
    Cayenne.virtualWrite(DTH_hic_Virtual_Channel, hic, "temp", "c");

    //dht7

    // Reading temperature or humidity takes about 250 milliseconds!
    // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
    float h7 = dht7.readHumidity();
    // Read temperature as Celsius (the default)
    float t7 = dht7.readTemperature();
    // Read temperature as Fahrenheit (isFahrenheit = true)
    float f7 = dht7.readTemperature(true);

    // Check if any reads failed and exit early (to try again).
    if (isnan(h7) || isnan(t7) || isnan(f7)) {
      Serial.println("Failed to read from DHT sensor!");
      return;
    }

    // Compute heat index in Fahrenheit (the default)
    float hif7 = dht7.computeHeatIndex(f, h);
    // Compute heat index in Celsius (isFahreheit = false)
    float hic7 = dht7.computeHeatIndex(t, h, false);
    Serial.print("Humidity: ");
    Serial.print(h7);
    Serial.print(" %\t");
    Serial.print("Temperature: ");
    Serial.print(t7);
    Serial.print(" *C ");
    //Serial.print(f7);
    Serial.print(" *F\t");
    Serial.print("Heat index: ");
    Serial.print(hic7);
    Serial.print(" *C ");
    Serial.print(hif7);
    Serial.println(" *F");
    Cayenne.virtualWrite(DTH7_hum_Virtual_Channel, h, "rel_hum", "p");
    Cayenne.virtualWrite(DTH7_tempC_Virtual_Channel, t, "temp", "c");
    Cayenne.virtualWrite(DTH7_tempF_Virtual_Channel, f, "temp", "f");
    Cayenne.virtualWrite(DTH7_hif_Virtual_Channel, hif, "temp", "f");
    Cayenne.virtualWrite(DTH7_hic_Virtual_Channel, hic, "temp", "c");

  }
}
Double ds18b20
#50

add dht7.begin(); in void setup()

1 Like
#51

Thank you so much! It remains to be seen why that line has been deleted!