Back to Parent

#include "PietteTech_DHT.h"  // Uncomment if building in IDE
#define DHTTYPE  DHT22       // Sensor type DHT11/21/22/AM2301/AM2302
#define DHTPIN   6           // Digital pin for communications

PietteTech_DHT DHT(DHTPIN, DHTTYPE);
int n;      // counter
int numPins = 5; // how many led we have
int ledPins[ 5 ] = { D0, D1, D2, D3, D4 }; //assign the pin of led
int buttonled = D7;
int check = 0;
int buttonPin = D5; // Button Pin
int buttonsend = 0; // tell the button status on cloud
double humidity;      // store the value of humidity

void setup()
{
    Serial.begin(9600);
    for( int i = 0 ; i < numPins; i++ ){
      pinMode(ledPins[ i ], OUTPUT);
    }
    pinMode(buttonPin, INPUT_PULLUP); // sets pin as input
    pinMode(buttonled, OUTPUT);
    Particle.variable("Button", &buttonsend, INT);
}

void loop()
{
    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;
    }

    humidity = DHT.getHumidity(); // grab the value of humidity at this moment
    humidity = round(humidity*10)/10; //round the digit into 2 decimals
    if (humidity >= 0){
      displayhumiditylevel();
      Particle.variable("Humidity", &humidity, DOUBLE); //Send data to cloud

      int buttonState = digitalRead( buttonPin );
      if( buttonState == LOW ){
        buttonsend = 1;
        digitalWrite(buttonled, HIGH); //Make D7 as a LED indicator that we enter this if.
        String wordhumidity = String(humidity); //Make double into string so that can be published
        Particle.publish("ButtonPushed", wordhumidity);
        delay(1000);
      }else{
        buttonsend = 0;
        digitalWrite(buttonled, LOW);
      }
      delay(500);
    }

}

void displayhumiditylevel(){ //display the humidity with LED lights
  int level = 10;
  for( int i = 0 ; i < numPins; i++ ){
    int led = ledPins[ i ];
    int desiredLevel = level * (i+1);
    if( humidity >= desiredLevel ){
        digitalWrite(led, HIGH);
    }else{
        digitalWrite(led, LOW);;
    }
  }
}
Click to Expand

Content Rating

Is this a good/useful/informative piece of content to include in the project? Have your say!

0