Check Your Temperature and Humidity

0
1189

BACKGROUND

Sometimes you just want to keep tabs on the temperature and humidity … don’t you?  Well OK, maybe I’m a little bit weird that way.  I happen to be curious about the humidity in my basement.  That’s mostly because it floods when I get too much rain or the water heater leaks.  I also care about the temperature when I brew beer at home.  Then there’s my prized fig tree that I cover (and heat with Christmas lights) during the winter.  So you can probably see where I’m going with this.  If you want to remotely monitor temperature or humidity, I’ve got an app for that.

 

Hardware setup for temperature and humidity sensor

THE SOLUTION

To satisfy my quest for data, I’ve cobbled together an extremely simple circuit.  It consists of a Particle Photon, a temperature and humidity sensor and a resistor.  Most of the magic happens within the sensor, which in my case is a AM2302.  When prompted, this little device measures the temperature and humidity.  It then and sends back a digital reading to the Photon.

To capture and display the data I collect, I use a phone-based dashboard called Blynk.  (I’ll be doing a Blynk overview in a future article.)  The cool thing about this program is you don’t have to know anything about writing code for a mobile device.  Blynk allows you to easily create custom Android or iOS control panels.  You do this by dragging and dropping visual components right on your screen.  In the example below I’m displaying our two variables in two different formats.  At the top are “gauges” that show a speedometer style reading.  This lets me know what the current temperature and humidity.  Below those are historical charts that show a graphical record the same data.

 

My Blynk dashboard on an Android phone

 

If you’ve read my Building Blocks for DIY Projects article, you’ll notice that we’re using a number of the basic components.  These are shown by the green, shaded boxes below. Keep in mind, these building blocks are like Legos, and you can mix and match them however you like.  So let’s say you want to get notified if the temperature hits a certain threshold.  You can easily tweak the Blynk software and Photon code to send you an email or text message.  I’ve set this up in another project where I get text alerts if my basement humidity rises above 70%.  This is usually a sign that I have a problem.  In another example, I’ve used a “controller” building block to turn on a heater if the temperature drops below a certain level (e.g., 35 degrees).  So when it comes to building custom projects, your imagination is your only limit.

 

Block diagram of my temperature/humidity setup

 

HARDWARE

 

 

* Note – The resistor pack is great if you’re going to be building multiple projects.  If you just need a single resistor, I’ll send you one for the price of a stamp.  Just send me a note via the Contact Us page.

 

SOFTWARE / CLOUD SERVICES

 

 

THE CODE

 

// This #include statement was automatically added by the Particle IDE.
#define BLYNK_PRINT Serial
#include "blynk/blynk.h"


// This #include statement was automatically added by the Particle IDE.
#include "PietteTech_DHT/PietteTech_DHT.h"

// system defines
#define DHTTYPE  AM2302              // Sensor type DHT11/21/22/AM2301/AM2302
#define DHTPIN   4         	    // Digital pin for communications
#define DHT_SAMPLE_INTERVAL   60000  // Sample every minute

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

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

char auth[] = "[YOUR AUTH CODE HERE]";

double temp;
double humidity;
int n = 0;
String string_temp = "Not Initialized";
String string_humidity = "Not Initialized";



#define READ_INTERVAL 6000

void setup()
{

//  Blynk.begin(auth);
 
    Particle.variable("temp", &temp, DOUBLE);
    Particle.variable("humidity",&humidity, DOUBLE);
    delay(5000); // Allow board to settle
    Serial.begin(9600);
//    Spark.publish("Basement_Temp", "test1");

    Blynk.begin(auth);

}

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

void loop()
{

    Blynk.run();
    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 (%): ");
            humidity=DHT.getHumidity();
      Serial.println(humidity, 1);
            string_humidity = String(humidity, 1);

            Blynk.virtualWrite(V0, string_humidity);

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

      Serial.print("Temperature (oF): ");
            temp=DHT.getFahrenheit();
      Serial.println(temp, 1);
            string_temp = String(temp, 1);

            Blynk.virtualWrite(V1, string_temp);

//	    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());

//        string_temp = String(temp, 3);
        Spark.publish("Floating Temp", string_temp);


    n++;
    delay(10000);
}

 

Help support my work via Patreon!

LEAVE A REPLY

Please enter your comment!
Please enter your name here