Sydney Curran
Published

Lane Tech PCL -- Sensing Environmental Changes

Am I going crazy or has the temperature/humidity changed in this room? Let's find out using a DHT11 sensor and a Particle Argon!

BeginnerFull instructions provided201
Lane Tech PCL -- Sensing Environmental Changes

Things used in this project

Hardware components

DHT11 Temperature & Humidity Sensor (4 pins)
DHT11 Temperature & Humidity Sensor (4 pins)
×1
Argon
Particle Argon
×1
Common Cathode RGB LED
×1
Resistor 220 ohm
Resistor 220 ohm
×1
Solderless Breadboard Half Size
Solderless Breadboard Half Size
×2
Breadboard (generic)
Breadboard (generic)
×1
Solderless Breadboard Full Size
Solderless Breadboard Full Size
×1

Software apps and online services

Particle Build Web IDE
Particle Build Web IDE
Arduino IDE
Arduino IDE
Used the Arduino IDE to run a serial monitor.
IFTTT
IFTTT Pro is used for this project.

Story

Read more

Schematics

Circuit Diagram

Image made using Fritzing. Some wire colors here are different from those on my physical circuit due to supply availability.

Code

DHT11 and Obstacle Avoidance Sensor Code

C/C++
// This #include statement was automatically added by the Particle IDE.
#include "variables.h"

// This #include statement was automatically added by the Particle IDE.
#include <Adafruit_DHT.h>

DHT dht(DHT11_PIN, DHTTYPE); //setup the DHT sensor


void setup() {
  //setup all other connected pins
  pinMode(avoidPin, INPUT);
  pinMode(bPin, OUTPUT);
  pinMode(rPin, OUTPUT);
  pinMode(gPin, OUTPUT);
  Serial.begin(9600); 
  dht.begin();
  //set the initial temperature to be checked against
  float hi = dht.getHeatIndex();
  if (isnan(hi) || hi > 100){ //make sure there were no reading errors
     Serial.println("Error!!");
  }
  else{
     initialRoomTemp = (int)hi;
   }
  //do the same for the humidity
  float humI = dht.getHumidity();
  if (isnan(humI) || humI > 100){
     Serial.println("Error!!");
  }
  else{
     initialRoomHum = (int)humI;
  }
  Serial.print("Initial temp: ");
  Serial.println(initialRoomTemp);
  Serial.print("Inital hum: ");
  Serial.println(initialRoomHum);
  //color the RGB LED with red for the temperature
  int rVal = map(initialRoomTemp, 0, 50, 0, 255);
  analogWrite(rPin, rVal);
  //color the RGB LED with blue for the humidity
  int bVal = map(initialRoomHum, 0, 100, 0, 255);
  analogWrite(bPin, bVal);
  //the green value is used to show the change in humidity, 
  //so it is not lit during the setup
  //create cloud variables to share data with IFTTT
  Particle.variable("Delta T", tempDiff);
  Particle.variable("Delta H", humDiff);
}


void loop() {
   //check the temperature and humidity every the loop is run
   float tem1 = dht.getHeatIndex();
   if (isnan(tem1) || tem1 > 100){ 
     Serial.println("Error!!");
     return;
     //try again if there is an error
   }
   else{
     Temp = (int)tem1;
   }
   float hum1 = dht.getHumidity();
   if (isnan(hum1) || hum1 > 100){
     Serial.println("Error!!");
     return;
     //try again if there is an error
   }
   else{
     Hum = (int)hum1;
   }

  int reading = digitalRead(avoidPin);

  if (reading != lastAvoidState) {
    lastDebounceTime = millis();
  }

  //debounce the avoidance sensor
  if ((millis() - lastDebounceTime) > debounceDelay) {
    if (reading != avoidState) { //when the avoidance sensor detects a change
      avoidState = reading;
      isTriggered = !isTriggered;
      if(reading != 1){ //only react when the avoidance sensor has been triggered on
         //find the difference in the temperature and humidity at the requested time
         tempDiff = Temp - initialRoomTemp;
         humDiff = Hum - initialRoomHum;
         //print the info to the serial monitor
         Serial.print("Temp Diff: ");
         Serial.println(tempDiff);
         Serial.print("Hum Diff: ");
         Serial.println(humDiff);
         Serial.print("Temp: ");
         Serial.println(Temp);
         Serial.print("Hum: ");
         Serial.println(Hum);
         //color the RGB LED with red representing the temperature
         int rVal = map(Temp, 0, 50, 0, 255);
         analogWrite(rPin, rVal);
         //blue for the humidity
         int bVal = map(Hum, 0, 100, 0, 255);
         analogWrite(bPin, bVal);
         //and green for the change in humidity
         int deltaHum = abs(humDiff);
         int gVal = map(deltaHum, 0, 100, 0, 255);
         analogWrite(gPin, gVal);
         //print the values of the color to the serial monitor
         Serial.print("Red: ");
         Serial.println(rVal);
         Serial.print("Green: ");
         Serial.println(gVal);
         Serial.print("Blue: ");
         Serial.println(bVal);
         //publish an event to trigger IFTTT
         Particle.publish("Print info!", "IFTTT");
     }
    }
  }
  lastAvoidState = reading;
  lastTrigger = isTriggered;
}

variables

C Header File
#define DHTTYPE DHT11 //I am using the DHT 11 sensor
#define DHT11_PIN 2 //And it is connected to Pin 2

//Variables to store the temperature recorded during setup
int initialRoomTemp;
int initialRoomHum;

//Variables for checking the temperature during the loop
int Temp;
int Hum;

int tempDiff;
int humDiff;


#define avoidPin 6 //The avoidance sensor is connected to Pin 6

//Variables for debouncing the avoidance sensor
int avoidState;
int lastAvoidState = LOW;
int isTriggered = LOW;
int lastTrigger = LOW;

unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 200;

//Pins to light the colors of the RGB LED
#define bPin 4
#define rPin 5
#define gPin 8

Credits

Sydney Curran

Sydney Curran

3 projects • 2 followers

Comments

Add projectSign up / Login