Skip to content

Commit

Permalink
Updated DHT_example
Browse files Browse the repository at this point in the history
Updated DHT_example to remove while loop
Increased version numbers
  • Loading branch information
mtnscott committed Oct 22, 2014
1 parent b5ab916 commit 0657b80
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 70 deletions.
2 changes: 1 addition & 1 deletion firmware/PietteTech_DHT.cpp
Original file line number Diff line number Diff line change
@@ -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)
*
Expand Down
2 changes: 1 addition & 1 deletion firmware/PietteTech_DHT.h
Original file line number Diff line number Diff line change
@@ -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)
*
Expand Down
2 changes: 1 addition & 1 deletion firmware/examples/DHT_2sensor.ino
Original file line number Diff line number Diff line change
@@ -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)
*
Expand Down
151 changes: 86 additions & 65 deletions firmware/examples/DHT_example.ino
Original file line number Diff line number Diff line change
@@ -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) [email protected]
* January 2014 Original Spark Port
Expand All @@ -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()
{
Expand All @@ -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() {
Expand All @@ -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);
}
2 changes: 1 addition & 1 deletion firmware/examples/DHT_simple.ino
Original file line number Diff line number Diff line change
@@ -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)
*
Expand Down
2 changes: 1 addition & 1 deletion spark.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "PietteTech_DHT",
"version": "0.0.2",
"version": "0.0.3",
"author": "Scott Piette [email protected]",
"license": "GPL v3 (http://www.gnu.org/licenses/gpl.html)",
"description": "Interrupt driven DHT11/21/22 Sensor library"
Expand Down

0 comments on commit 0657b80

Please sign in to comment.