Created
April 23, 2015 19:46
-
-
Save harrisonhjones/e1dc35ebfe130b731fcf to your computer and use it in GitHub Desktop.
Spark - DHT22 Logger to Webhook
This file contains hidden or 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
/* Includes ------------------------------------------------------------------*/ | |
//#include "application.h" | |
#include "PietteTech_DHT.h" // From: https://github.com/piettetech/PietteTech_DHT | |
/* Function prototypes -------------------------------------------------------*/ | |
void dht_wrapper(); // must be declared before the lib initialization | |
// Explicit Declaration of System Mode | |
SYSTEM_MODE(AUTOMATIC); | |
/* PRE-Processor Defined -----------------------------------------------------*/ | |
#define DHTTYPE DHT22 // Sensor type DHT11/21/22/AM2301/AM2302 | |
#define DHTPIN 3 // Digital pin for communications | |
#define DHT_SAMPLE_INTERVAL 4000 // Sample every two seconds | |
/* Constants -----------------------------------------------------------------*/ | |
const char c_ubidots_temp_var_id[] = "EXAMPLE1"; | |
const char c_ubidots_humidity_var_id[] = "EXAMPLE2"; | |
/* Objects -------------------------------------------------------------------*/ | |
PietteTech_DHT DHT(DHTPIN, DHTTYPE, dht_wrapper); | |
/* Global Variables ----------------------------------------------------------*/ | |
unsigned long ulDHTLastUpdate; | |
bool bDHTstarted; // flag to indicate we started acquisition | |
char message[180]; | |
/* Setup Function - Run One @ Startup ----------------------------------------*/ | |
void setup() | |
{ | |
/*Serial.begin(9600); | |
while (!Serial.available()) { | |
Serial.println("Press any key to start."); | |
delay (1000); | |
}*/ | |
/*Serial.println("DHT Example program using DHT.acquire and DHT.aquiring"); | |
Serial.print("LIB version: "); | |
Serial.println(DHTLIB_VERSION); | |
Serial.println("---------------");*/ | |
Spark.publish("DHTLIB_VERSION", DHTLIB_VERSION); | |
ulDHTLastUpdate = millis(); | |
} | |
/* Loop Function - Run Indefinitely ----------------------------------------*/ | |
void loop() | |
{ | |
// Check if we need to start the next sample | |
if(((millis() - ulDHTLastUpdate) > DHT_SAMPLE_INTERVAL) || (bDHTstarted)) | |
{ | |
// Start a new sample if we haven't yet | |
if (!bDHTstarted) | |
{ | |
/*Serial.print("\n"); | |
Serial.print(n); | |
Serial.print(": Retrieving information from sensor: ");*/ | |
DHT.acquire(); | |
bDHTstarted = true; | |
} | |
// Once we are done aquiring handle the result | |
if (!DHT.acquiring()) | |
{ | |
// get DHT status | |
int result = DHT.getStatus(); | |
if(result != DHTLIB_OK) | |
{ | |
sprintf(message, "{\"error\":\"%d\"}", result); | |
//Spark.publish("DHTLIB_ERROR", message); | |
Spark.publish("dhtlibError", message); | |
} | |
else | |
{ | |
// Grab the readings we are interested in | |
double tempF = (double) DHT.getFahrenheit(); | |
double humidity = (double) DHT.getHumidity(); | |
// Generate the temperature message: | |
sprintf(message, "{\"vid\":\"%s\", \"vval\":\"%f\"}", c_ubidots_temp_var_id, tempF); | |
Spark.publish("ubidots", message); | |
Serial.println(message); | |
// Generate the humidity message | |
sprintf(message, "{\"vid\":\"%s\", \"vval\":\"%f\"}", c_ubidots_humidity_var_id, humidity); | |
Spark.publish("ubidots", message); | |
Serial.println(message); | |
} | |
bDHTstarted = false; // reset the sample flag so we can take another | |
ulDHTLastUpdate = millis(); | |
} | |
} | |
} | |
// This wrapper is in charge of calling | |
// mus be defined like this for the lib work | |
void dht_wrapper() { | |
DHT.isrCallback(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment