diff --git a/firmware/PietteTech_DHT.cpp b/firmware/PietteTech_DHT.cpp index a0e2343..0e848c9 100644 --- a/firmware/PietteTech_DHT.cpp +++ b/firmware/PietteTech_DHT.cpp @@ -1,6 +1,6 @@ /* * FILE: PietteTech_DHT.cpp - * VERSION: 0.2 + * VERSION: 0.3 * PURPOSE: Spark Interrupt driven lib for DHT sensors * LICENSE: GPL v3 (http://www.gnu.org/licenses/gpl.html) * diff --git a/firmware/PietteTech_DHT.h b/firmware/PietteTech_DHT.h index db19928..1f4c2dd 100644 --- a/firmware/PietteTech_DHT.h +++ b/firmware/PietteTech_DHT.h @@ -1,6 +1,6 @@ /* * FILE: PietteTech_DHT.h - * VERSION: 0.2 + * VERSION: 0.3 * PURPOSE: Spark Interrupt driven lib for DHT sensors * LICENSE: GPL v3 (http://www.gnu.org/licenses/gpl.html) * diff --git a/firmware/examples/DHT_2sensor.ino b/firmware/examples/DHT_2sensor.ino index 491dc95..ccc888d 100644 --- a/firmware/examples/DHT_2sensor.ino +++ b/firmware/examples/DHT_2sensor.ino @@ -1,6 +1,6 @@ /* * FILE: DHT_2sensor.ino - * VERSION: 0.2 + * VERSION: 0.3 * PURPOSE: Example that uses DHT library with two sensors * LICENSE: GPL v3 (http://www.gnu.org/licenses/gpl.html) * diff --git a/firmware/examples/DHT_example.ino b/firmware/examples/DHT_example.ino index 638f5d2..e98ac22 100644 --- a/firmware/examples/DHT_example.ino +++ b/firmware/examples/DHT_example.ino @@ -1,11 +1,14 @@ /* * FILE: DHT_example.ino - * VERSION: 0.2 + * VERSION: 0.3 * PURPOSE: Example that uses DHT library with two sensors * LICENSE: GPL v3 (http://www.gnu.org/licenses/gpl.html) * - * Calls acquire on one sensor and monitors the results for long term - * analysis. It uses DHT.acquire and DHT.acquiring + * Example that start acquisition of DHT sensor and allows the + * loop to continue until the acquisition has completed + * It uses DHT.acquire and DHT.acquiring + * + * Change DHT_SAMPLE_TIME to vary the frequency of samples * * Scott Piette (Piette Technologies) scott.piette@gmail.com * January 2014 Original Spark Port @@ -15,15 +18,21 @@ #include "PietteTech_DHT/PietteTech_DHT.h" -#define DHTTYPE DHT22 // Sensor type DHT11/21/22/AM2301/AM2302 -#define DHTPIN 3 // Digital pin for communications +// system defines +#define DHTTYPE DHT22 // Sensor type DHT11/21/22/AM2301/AM2302 +#define DHTPIN 3 // Digital pin for communications +#define DHT_SAMPLE_INTERVAL 2000 // Sample every two seconds //declaration void dht_wrapper(); // must be declared before the lib initialization // Lib instantiate PietteTech_DHT DHT(DHTPIN, DHTTYPE, dht_wrapper); -int n; // counter + +// globals +unsigned int DHTnextSampleTime; // Next time we want to start sample +bool bDHTstarted; // flag to indicate we started acquisition +int n; // counter void setup() { @@ -36,8 +45,11 @@ void setup() Serial.print("LIB version: "); Serial.println(DHTLIB_VERSION); Serial.println("---------------"); + + DHTnextSampleTime = 0; // Start the first sample immediately } + // This wrapper is in charge of calling // mus be defined like this for the lib work void dht_wrapper() { @@ -46,64 +58,73 @@ void dht_wrapper() { void loop() { - Serial.print("\n"); - Serial.print(n); - Serial.print(": Retrieving information from sensor: "); - Serial.print("Read sensor: "); - //delay(100); - - DHT.acquire(); - while (DHT.acquiring()) ; - - int result = DHT.getStatus(); - - 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; + // Check if we need to start the next sample + if (millis() > DHTnextSampleTime) { + if (!bDHTstarted) { // start the sample + Serial.print("\n"); + Serial.print(n); + Serial.print(": Retrieving information from sensor: "); + DHT.acquire(); + bDHTstarted = true; + } + + if (!DHT.acquiring()) { // has sample completed? + + // get DHT status + int result = DHT.getStatus(); + + Serial.print("Read sensor: "); + 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; + } + + Serial.print("Humidity (%): "); + Serial.println(DHT.getHumidity(), 2); + + Serial.print("Temperature (oC): "); + Serial.println(DHT.getCelsius(), 2); + + Serial.print("Temperature (oF): "); + Serial.println(DHT.getFahrenheit(), 2); + + Serial.print("Temperature (K): "); + Serial.println(DHT.getKelvin(), 2); + + Serial.print("Dew Point (oC): "); + Serial.println(DHT.getDewPoint()); + + Serial.print("Dew Point Slow (oC): "); + Serial.println(DHT.getDewPointSlow()); + + n++; // increment counter + bDHTstarted = false; // reset the sample flag so we can take another + DHTnextSampleTime = millis() + DHT_SAMPLE_INTERVAL; // set the time for next sample + } } - Serial.print("Humidity (%): "); - Serial.println(DHT.getHumidity(), 2); - - Serial.print("Temperature (oC): "); - Serial.println(DHT.getCelsius(), 2); - - Serial.print("Temperature (oF): "); - Serial.println(DHT.getFahrenheit(), 2); - - Serial.print("Temperature (K): "); - Serial.println(DHT.getKelvin(), 2); - - Serial.print("Dew Point (oC): "); - Serial.println(DHT.getDewPoint()); - - Serial.print("Dew Point Slow (oC): "); - Serial.println(DHT.getDewPointSlow()); - - n++; - delay(2500); } diff --git a/firmware/examples/DHT_simple.ino b/firmware/examples/DHT_simple.ino index df55103..ee01bb9 100644 --- a/firmware/examples/DHT_simple.ino +++ b/firmware/examples/DHT_simple.ino @@ -1,6 +1,6 @@ /* * FILE: DHT_simple.ino - * VERSION: 0.2 + * VERSION: 0.3 * PURPOSE: Example that uses DHT library with two sensors * LICENSE: GPL v3 (http://www.gnu.org/licenses/gpl.html) * diff --git a/spark.json b/spark.json index 46a0230..292bba7 100644 --- a/spark.json +++ b/spark.json @@ -1,6 +1,6 @@ { "name": "PietteTech_DHT", - "version": "0.0.2", + "version": "0.0.3", "author": "Scott Piette scott.piette@gmail.com", "license": "GPL v3 (http://www.gnu.org/licenses/gpl.html)", "description": "Interrupt driven DHT11/21/22 Sensor library"