Publish DHT22 to Cloud [SOLVED]

Hello,

This is my first project on the Photon. and I’m trying to puclish DHT22 sensor data to the Particle Cloud. I am using a DHT22 sensor connected to my Photon to acquire temp and humidity data using the PietteTech_DHT/PietteTech_DHT.h library.

The code I’m using as a base is a straightforward copy from the library above:


   /*
 * FILE:        DHT_simple.ino
 * VERSION:     0.3
 * PURPOSE:     Example that uses DHT library with two sensors
 * LICENSE:     GPL v3 (http://www.gnu.org/licenses/gpl.html)
 *
 * Samples one sensor and monitors the results for long term
 * analysis.  It calls DHT.acquireAndWait
 *
 * Scott Piette (Piette Technologies) scott.piette@gmail.com
 *      January 2014        Original Spark Port
 *      October 2014        Added support for DHT21/22 sensors
 *                          Improved timing, moved FP math out of ISR
 */

#include "PietteTech_DHT/PietteTech_DHT.h"  // Uncomment if building in IDE
//#include "PietteTech_DHT.h"  // Uncommend if building using CLI

#define DHTTYPE  DHT22       // Sensor type DHT11/21/22/AM2301/AM2302
#define DHTPIN   3           // Digital pin for communications

//declaration
void dht_wrapper(); // must be declared before the lib initialization

// Lib instantiate
PietteTech_DHT DHT(DHTPIN, DHTTYPE, dht_wrapper);
int n;      // counter

void setup()
{
    
    Serial.begin(9600);
    while (!Serial.available()) {
        Serial.println("Press any key to start.");
        delay (1000);
    }
    Serial.println("DHT Example program using DHT.acquireAndWait");
    Serial.print("LIB version: ");
    Serial.println(DHTLIB_VERSION);
    Serial.println("---------------");
}

// This wrapper is in charge of calling
// must be defined like this for the lib work
void dht_wrapper() {
    DHT.isrCallback();
}

void loop()
{
    Serial.print("\n");
    Serial.print(n);
    Serial.print(": Retrieving information from sensor: ");
    Serial.print("Read sensor: ");
    //delay(100);
  
    int result = DHT.acquireAndWait();

    switch (result) {
        case DHTLIB_OK:
            Serial.println("OK");
            break;
        case DHTLIB_ERROR_CHECKSUM:
            Serial.println("Error\n\r\tChecksum error");
            break;
        case DHTLIB_ERROR_ISR_TIMEOUT:
            Serial.println("Error\n\r\tISR time out error");
            break;
        case DHTLIB_ERROR_RESPONSE_TIMEOUT:
            Serial.println("Error\n\r\tResponse time out error");
            break;
        case DHTLIB_ERROR_DATA_TIMEOUT:
            Serial.println("Error\n\r\tData time out error");
            break;
        case DHTLIB_ERROR_ACQUIRING:
            Serial.println("Error\n\r\tAcquiring");
            break;
        case DHTLIB_ERROR_DELTA:
            Serial.println("Error\n\r\tDelta time to small");
            break;
        case DHTLIB_ERROR_NOTSTARTED:
            Serial.println("Error\n\r\tNot started");
            break;
        default:
            Serial.println("Unknown error");
            break;
    }
    Serial.print("Humidity (%): ");
    Serial.println(DHT.getHumidity(), 2);

    Serial.print("Temperature (oC): ");
    Serial.println(DHT.getCelsius(), 2);

    Serial.print("Temperature (oF): ");
    Serial.println(DHT.getFahrenheit(), 2);

    Serial.print("Temperature (K): ");
    Serial.println(DHT.getKelvin(), 2);

    Serial.print("Dew Point (oC): ");
    Serial.println(DHT.getDewPoint());

    Serial.print("Dew Point Slow (oC): ");
    Serial.println(DHT.getDewPointSlow());

    n++;
    

  
    delay(5000);
}

I want to publish then temp in Celsius and the Humidity to the cloud. Appreciate your help

Hi , one of my project`s uses 2 DHT22 and posts the data to thingspeak

First , if you want to post real data so it can be logged etc , one change I made was remove the CASE and do a

  if ( result == DHTLIB_OK )
  {
    Hum =  DHT.getHumidity();
    temp =   DHT.getCelsius();
  }

That way if the sensor errors you don’t get a random result.
Don’t forget to declare hum and temp as floats at the top
Use Particle.publish() or some library to push Hum and temp to the cloud .

What do you mean Publish and what do you want to publish too ?

Unless you are going to use the terminal, remove these lines…

It may cause your code to hang.

Could you define what you understand by “push it to the cloud”. What exactly are you expecting the data to do? Do you want to be able to query it from somewhere, or does it actually have to be pushed?
For querying you could use variables, which you can find here: https://docs.particle.io/reference/firmware/photon/#particle-variable-
For pushing things, you could use publishes, which are to be found here: https://docs.particle.io/reference/firmware/photon/#particle-publish-

What have you tried so far, other than copying the example? If you do a quick search on the community, you’ll find plenty of projects doing similar things. Check it out :smile:

Hi all, thank you for your replies.

Sorry for the vague “publish to the cloud”. By this i meant to push the temp and humidity results to the particle dashboard.

So from what i can gather from your responses, the following would work?

a) remove the Serial “press any key to start” code

b) Remove the Case error handling code, and replace with

  if ( result == DHTLIB_OK )
  {
    Hum =  DHT.getHumidity();
    temp =   DHT.getCelsius();
  }

c) Use both particle.variable & particle.publish, like so:

Particle.variable("Temp", Temp);
Particle.publish("Temperature",  String(Temp) + "%", 60, PRIVATE);
 
Particle.variable("Hum", Hum);
Particle.publish("Humidity",  String(Hum) + "%", 60, PRIVATE);

d) The end result looks like this:

#include "PietteTech_DHT/PietteTech_DHT.h"

#define DHTTYPE  DHT22       // Sensor type DHT11/21/22/AM2301/AM2302
#define DHTPIN   3           // Digital pin for communications

float Temp =   DHT.getCelsius();
float Hum =  DHT.getHumidity();

//declaration
void dht_wrapper(); // must be declared before the lib initialization

// Lib instantiate
PietteTech_DHT DHT(DHTPIN, DHTTYPE, dht_wrapper);
int n;      // counter

void setup() {
    
    Serial.println("DHT Example program using DHT.acquireAndWait");
    Serial.print("LIB version: ");
    Serial.println(DHTLIB_VERSION);
    Serial.println("---------------");
}

// This wrapper is in charge of calling
// must be defined like this for the lib work
void dht_wrapper() {
    DHT.isrCallback();
}

void loop()
{
    Serial.print("\n");
    Serial.print(n);
    Serial.print(": Retrieving information from sensor: ");
    Serial.print("Read sensor: ");
    //delay(100);
  
    int result = DHT.acquireAndWait();

    if ( result == DHTLIB_OK )
  {
    Temp =   DHT.getCelsius();
    Hum =  DHT.getHumidity();
  }
    
    Serial.print("Humidity (%): ");
    Serial.println(DHT.getHumidity(), 2);

    Serial.print("Temperature (oC): ");
    Serial.println(DHT.getCelsius(), 2);

    Serial.print("Temperature (oF): ");
    Serial.println(DHT.getFahrenheit(), 2);

    Serial.print("Temperature (K): ");
    Serial.println(DHT.getKelvin(), 2);

    Serial.print("Dew Point (oC): ");
    Serial.println(DHT.getDewPoint());

    Serial.print("Dew Point Slow (oC): ");
    Serial.println(DHT.getDewPointSlow());

    n++;
    
  Particle.variable("Temp", Temp);
  Particle.publish("Temperature",  String(Temp) + "%", 60, PRIVATE);
 
  
  Particle.variable("Hum", Hum);
  Particle.publish("Humidity",  String(Hum) + "%", 60, PRIVATE);
  
    delay(60000);
}

However, when trying to compile in Particle IDE, I get the following errors:

dht_2sensorjk.cpp:22:16: error: 'DHT' was not declared in this scope
 //#include "PietteTech_DHT.h"  // Uncommend if building using CLI
                ^

dht_2sensorjk.cpp:23:14: error: 'DHT' was not declared in this scope
 
              ^

make[1]: *** [../build/target/user/platform-6dht_2sensorjk.o] Error 1
make: *** [user] Error 2

These two lines need to go into setup() and only get executed once.

And you can’t do this before you actually have your DHT object

BTW: In order to make sense of the line numbers in your error message you should post the code as it is built or state the line numbers the errors refer to as comments in your stripped down code - saves us the guessing :sunglasses:

1 Like

Thanks ScruffR! Works great. Appreciate you taking the time.

1 Like

One little point !!

if you do a .

float Temp = DHT.getCelsius(); // I guess this is line number 22
float Hum = DHT.getHumidity();

inside a fuction or if group it will be local to inside that fuction or if group .
So if you want access to your variable anywhere make it global eg top of your script .

Hmm, where does this come from, since they are global anyway?

Or is it due to the initialization?

What Im saying you cant do this :smile:

void loop() 
{

int result = 12;

if ( result == 12 )
{
    float temp = 12.12;
}
    Serial.println( temp );
}

or you will get oine of these

“error: ‘temp’ was not declared in this scope”

So what i’m trying to say is don`t declare / define / initialization your variable in side brackets if you want to use that variable outside the brackets.

eg

int result = DHT.acquireAndWait();
float Temp , Hum 
    if ( result == DHTLIB_OK )
  {
    Temp =   DHT.getCelsius();
    Hum =  DHT.getHumidity();
  }
Serial.print( Temp )

You can take a lock at Github, I have put my solution for the DHT22 with the Photon, to send the data to Spark cloud and Ubidots cloud
The Ubidots.cloud is free of chart, and there you can combined
the data to send alerts and further combination.

the link: