Photocell & DHT 22 on Desktop

Made by Yuhan Wu

Using a DHT 22 (AM 2302) and photoresistor to acquire the temperature, humidity and brightness of the workplace at home. Applying if statement to trigger different blinking pattern for LED when it comes to different estimated limits.

Created: November 11th, 2023

0

Intention

With a window facing south and a height adjustable table in the living room, me and my roommates always work on the table. Light is always mild during daytime, making it an ideal workplace.

But the thing is, as it is still in an apartment, the environment is far from a passive cooling and heating place. The indoor environment is something that we should pay attention to.

0

Context

Because the window can't be opened, sometimes it may help to stop the heating when it's too hot during day time. We all need to pause from the heat, even when winter comes. We found that 25.5 Celsius is a comfortable temperature but your palm would kind of start sweeting because of the heat coming out. Which means we might want to turn the heat off when it is hotter than 25.5 Celsius.

At night, one of my roommates suffered from rhinitis and may be hard to fall asleep. Which means he will be really sensitive to the indoor humidity. According some researchers, a humidity of around 60% is more comfortable for people suffering from it. You might want to turn on the humidifier when humidity is below that value.

0

Process

First I built the test version to try on the DHT 22 sensor using the library example. The example in Adafruit_DHT failed to work on Argon but the one  by Piettetech_DHT worked. I doubted that it might be caused by different configuration and uses of the pins and asked Zhenfang about this question. 

He told me that he looked into the header files and main classes of Adafruit_DHT and Adafruit_DHT_Partice, and found that they should be used for different boards, the class member functions initialize the pin differently. (Thanks for the help by Zhenfang)

Next, I use the test to figure out the existing conditions of the workplace and using particle serial monitor to check the status. As can be seen in the CLI, indoor temperature is roughly 25 Celsius and humidity is around 45%. While the light reading is not using a standard unit of measurement, I tested the reading with the light and curtain off to simulate the conditions in which we are not working. It is a reading of photoresistor around 1250. Later I tested with them on and the reading is around or larger than 2500.

0

So I design a circuit that blink quickly when the humidity is low and light is dim, to inform us to adjust the humidity indoor for a better sleep. And blink slowly when temperature is higher than 25.5 and light is bright. To tell us when to stop the hear during day time, instead of using a timer.

0

Product

Blink quickly for 10 times when humidity is below 60% and light is dim. (See Pt.1) Blink slowly for 5 times when temperature is above 25.5 Celsius and light is bright.  (See Pt.2)  

The presumption here is that light is dim when we are not working and about to sleep and light is bright when we are working.

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

#define DHTTYPE  DHT22              // Sensor type DHT22 AM2302
#define DHTVcc   D2                 // Digital pin to power the sensor
#define DHTPIN   D3                 // Digital pin for communications
#define DHTGnd   D4                 // Digital pin for sensor GND
#define DHT_SAMPLE_INTERVAL   20000 // 20 seconds Pause between sample

void dht_wrapper();                 // must be declared before the lib initialization
PietteTech_DHT DHT(DHTPIN, DHTTYPE, dht_wrapper);
void dht_wrapper() {
  DHT.isrCallback();
}

bool bDHTstarted;		    // flag to indicate we started acquisition
int n;                              // counter

//data collector
float temperatureC = 0;
float humidity = 0;

int ledPin = D6;

int lightSensorPin = A0;
int lightReading = 0;

void setup() {
    
    Serial.begin(115200);

    pinMode( ledPin, OUTPUT );
    pinMode( lightSensorPin, INPUT );

    pinMode(DHTGnd, OUTPUT);
    pinResetFast(DHTGnd);
    pinMode(DHTVcc, OUTPUT);
    pinSetFast(DHTVcc);
  
    delay(100);

    Serial.println("DHT Simple program using DHT.acquire");
    Serial.printlnf("LIB version: %s", (const char*)DHTLIB_VERSION);
    Serial.println("---------------");

    DHT.begin();
    
}

void loop() {

  static uint32_t msLastSample = 0;
  if (millis() - msLastSample <  DHT_SAMPLE_INTERVAL) return; //start sample when meet the set sample interval

  if (!bDHTstarted) {               // start the sample
    Serial.printlnf("\r\n%d: Retrieving information from sensor: ", n);
    DHT.acquire();
    bDHTstarted = true;
    }

  if (!DHT.acquiring()) {           // has sample completed?
    int result = DHT.getStatus();
    
  if (result == DHTLIB_OK) {
    temperatureC = DHT.getCelsius();
    humidity = DHT.getHumidity();
    lightReading = analogRead( lightSensorPin );
    
    char data[64];
    snprintf(data, sizeof(data), "Temperature: %.2f°C, Humidity: %.2f%%,Light Reading : %.d ", temperatureC, humidity, lightReading );
    //f for float
    //d for int
    
    Particle.publish("DHT", data);
    
    Serial.printlnf("Humidity      : %5.2f %%", DHT.getHumidity());
    Serial.printlnf("Temperature   : %5.2f °C", DHT.getCelsius());
    Serial.printlnf("Light Reading : %.d ", analogRead( lightSensorPin ));
    
        if ( humidity < 60 && lightReading < 2000 ) {         
            //quick blink when air is dry and light is dim
            //light is dim when we are not working and about to sleep
            for( int i = 0; i < 10; i++ ){
            digitalWrite( ledPin, HIGH );
            delay( 500 );
            digitalWrite( ledPin, LOW );
            delay( 500 ); 
            }
        } else if ( temperatureC > 25.5 && lightReading > 2000 ) {
            //slow blink when hotter than a limit of 25.5 and light is bright
            //light is bright when we are working
            for( int i = 0; i < 5; i++ ){
            digitalWrite( ledPin, HIGH );
            delay( 2000 );
            digitalWrite( ledPin, LOW );
            delay( 2000 );
            } 
        }else{
            digitalWrite( ledPin, LOW );}
    
    }
    
    else {
    Serial.printlnf("error");    
    }
    
    n++;                    //counter
    bDHTstarted = false;    // reset the sample flag so we can take another
    msLastSample = millis();
    
    }
 }
Click to Expand
0
Skill Dev 2 Pt.1
DIOT-Max - https://youtu.be/1AMYqZ0SJEs
0
Skill Dev 2 Pt.2
DIOT-Max - https://youtu.be/onF3T3oc994
0

Reflection

In the coding it took me some time to realize that I initially misplaced one or two semicolons in wrong places. In coding with a for loop in an if command, it is really helpful to use indent and always checking by clicking on braces to see its boundary.

Another take away is that the library may not suit into every device so test and check it before. Trying out with examples in the library is always helpful.

The way of configuring pins in the piettetech_DHT library is also pretty amazing. It let me know that GPIO pins could also serve as a VCC or GND to power the sensors.

x
Share this Project

Courses

48-675 Designing for the Internet of Things

· 11 members

A hands-on introductory course exploring the Internet of Things and connected product experiences.


About

Using a DHT 22 (AM 2302) and photoresistor to acquire the temperature, humidity and brightness of the workplace at home.
Applying if statement to trigger different blinking pattern for LED when it comes to different estimated limits.