#include "PietteTech_DHT.h" // Uncomment if building in IDE
// system defines
#define DHTTYPE DHT22 // Sensor type DHT11/21/22/AM2301/AM2302
#define DHTPIN D4 // Digital pin for communications
#define DHT_SAMPLE_INTERVAL 2000 // Sample every two seconds
int ledPin1 = D2;
int ledPin2 = D0;
int buzzer = D3;
int buttonPin = D1;
bool isMediating = false;
long mediationStartTime = -1;
double temperature;
double humidity;
int tempReading;
// globals
unsigned int DHTnextSampleTime; // Next time we want to start sample
bool bDHTstarted; // flag to indicate we started acquisition
int n; // counter
void dht_wrapper(); // must be declared before the lib initialization
PietteTech_DHT DHT(DHTPIN, DHTTYPE, dht_wrapper);
void dht_wrapper() {
DHT.isrCallback();
}
void setup()
{
interrupts(); // added to make recognize DHT library
pinMode( buttonPin , INPUT_PULLUP); // sets pin as input
pinMode( ledPin1 , OUTPUT ); // sets pin as output
pinMode( ledPin2 , OUTPUT );
pinMode(buzzer, OUTPUT);
digitalWrite( ledPin1, LOW);
digitalWrite(ledPin2, LOW);
//tone(buzzer, 1000, 500);
// temperature sensor setup
WiFi.on();
Particle.connect();
// Variables Exposed to Cloud
Particle.variable("temperature", temperature);
Particle.publish("message: ", "I am meditating!");
Serial.begin(9600);
DHTnextSampleTime = 0; // Start the first sample immediately
}
void loop()
{
// find out if the button is pushed or not by reading from it.
int buttonState = digitalRead( buttonPin );
// switch LED color to signify timer starting
if( isMediating == false && buttonState == LOW )
{
// read temp of dht and display
tempReading = analogRead(DHTPIN);
// blink blue LED to signify starting timer
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin1, HIGH);
delay(500);
digitalWrite(ledPin1, LOW);
delay(500);
digitalWrite(ledPin1,HIGH);
delay(500);
digitalWrite(ledPin1,LOW);
// get temperature, publish temperature and I am meditating string
Particle.variable("temperature", temperature);
temperature = DHT.getCelsius();
Serial.printlnf("Humidity\t: %.2f %%", humidity);
Serial.printlnf("Temperature\t: %.2f °C", temperature);
Particle.publish("Temperature", String(temperature) + " °C");
Particle.publish("message: ", "I am meditating");
// start the mediation cycle...
isMediating = true;
// keep a track of when we started meditating
mediationStartTime = millis();
}else{
// otherwise turn white led on during meditation session
digitalWrite( ledPin2, HIGH);
}
// signal buzzer and flash blue light to signify end of meditation session (10 minutes is up)
if( isMediating == true and mediationStartTime + 600000 < millis() ) {
tone(buzzer, 1000, 500);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin1, HIGH);
delay(500);
digitalWrite(ledPin1, LOW);
delay(500);
digitalWrite(ledPin1,HIGH);
delay(500);
digitalWrite(ledPin1,LOW);
// update the start
isMediating = false;
mediationStartTime = -1;
}
// turn off both lights
if ( isMediating == false ) {
digitalWrite(ledPin1,LOW);
digitalWrite(ledPin2, LOW);
}
// anything less than 10 minutes, white light is on
else {
digitalWrite(ledPin2, HIGH);
}
}
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. .