// 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
Content Rating
Is this a good/useful/informative piece of content to include in the project? Have your say!
You must login before you can post a comment. .