Created
October 14, 2014 06:37
-
-
Save aznel/10cf0690f7cd6168828e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This #include statement was automatically added by the Spark IDE. | |
#include "idDHT22/idDHT22.h" | |
#define DHTPIN D4 // Digital pin D2 | |
#define WORKING_LED D7 | |
void dht22_wrapper(); // must be declared before the lib initialization | |
// DHT instantiate | |
idDHT22 DHT22(DHTPIN, dht22_wrapper); | |
// This wrapper is in charge of calling | |
// mus be defined like this for the lib work | |
void dht22_wrapper() { | |
DHT22.isrCallback(); | |
} | |
float h; // humidity | |
float t; // temperature | |
double tempC = 0; | |
double humidity = 0; | |
int f = 0; // failed? | |
void setup() { | |
Serial.begin(9600); | |
// dim the main LED | |
RGB.control(true); | |
RGB.brightness(32); | |
RGB.control(false); | |
// Turn on control of status LEDs | |
pinMode(WORKING_LED, OUTPUT); | |
// Flash the working LED to show we are running start | |
for(int i=0; i<5; i++) { | |
digitalWrite(WORKING_LED, HIGH); | |
delay(75); | |
digitalWrite(WORKING_LED, LOW); | |
delay(75); | |
} | |
Spark.variable("tempC", &tempC, DOUBLE); | |
Spark.variable("humidity", &humidity, DOUBLE); | |
} | |
bool readDHT22() | |
{ | |
Serial.print("Retrieving information from DHT22 sensor: "); | |
DHT22.acquire(); | |
while (DHT22.acquiring()) | |
; | |
int result = DHT22.getStatus(); | |
switch (result) | |
{ | |
case IDDHTLIB_OK: | |
Serial.println("OK"); | |
return true; | |
case IDDHTLIB_ERROR_CHECKSUM: | |
Serial.println("Checksum error"); | |
break; | |
case IDDHTLIB_ERROR_ISR_TIMEOUT: | |
Serial.println("ISR Time out error"); | |
break; | |
case IDDHTLIB_ERROR_RESPONSE_TIMEOUT: | |
Serial.println("Response time out error"); | |
break; | |
case IDDHTLIB_ERROR_DATA_TIMEOUT: | |
Serial.println("Data time out error"); | |
break; | |
case IDDHTLIB_ERROR_ACQUIRING: | |
Serial.println("Error Acquiring"); | |
break; | |
case IDDHTLIB_ERROR_DELTA: | |
Serial.println("Error Delta time to small"); | |
break; | |
case IDDHTLIB_ERROR_NOTSTARTED: | |
Serial.println("Error Not started"); | |
break; | |
default: | |
Serial.println("Unknown Error"); | |
break; | |
} | |
return false; | |
} | |
void loop() { | |
Serial.println("START"); | |
digitalWrite(WORKING_LED, LOW); | |
if (readDHT22()) | |
{ | |
h = DHT22.getHumidity(); | |
t = DHT22.getCelsius(); | |
f = 0; | |
} | |
else | |
{ | |
t = h = 999; | |
f = 1; | |
} | |
if (!f) | |
//Update the temp | |
Serial.print(" tempC: "); | |
Serial.println(t); | |
tempC = (double)t; | |
//Update the humidity | |
Serial.print(" humidity: "); | |
Serial.println(h); | |
humidity = (double)h; | |
digitalWrite(WORKING_LED, HIGH); | |
delay(6000); | |
Serial.println("END"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment