Last active
May 22, 2017 11:10
-
-
Save krhoyt/50c381e913fc3c7a312d to your computer and use it in GitHub Desktop.
From Arduino Yun to Firebase (DHT22 temperature and humidity).
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
// Arduino libraries | |
#include <Bridge.h> | |
#include <Console.h> | |
#include <Process.h> | |
// Third-party library | |
// https://github.com/RobTillaart/Arduino | |
#include <dht.h> | |
// Literals | |
// #define DEBUG | |
#define DHT_PIN 5 | |
// Constants | |
const char *FIREBASE = "_YOUR_FIREBASE_URL_"; | |
const char *NODE = ".json"; | |
const int UPDATE_RATE = 1000; | |
// Leverage Yun Linux (curl) | |
Process process; | |
// DHT instance | |
dht DHT; | |
// Buffer for parameters | |
char buffer[80]; | |
// Setup | |
void setup() | |
{ | |
// Bridge communication | |
Bridge.begin(); | |
// Debugging | |
#ifdef DEBUG | |
Console.begin(); | |
while( !Console ) {;} | |
#endif | |
} | |
// Loop | |
void loop() | |
{ | |
// Checksum from DHT | |
int check; | |
// Read the DHT data | |
// Return from call is checksum | |
// Checks for correct communication | |
check = DHT.read22( DHT_PIN ); | |
// Check for errors | |
switch( check ) | |
{ | |
case DHTLIB_OK: | |
// Debugging | |
#ifdef DEBUG | |
Console.print( "OK," ); | |
#endif | |
// Update value on Firebase | |
request( ( DHT.temperature * 1.8 ) + 32, DHT.humidity ); | |
wait(); | |
response(); | |
break; | |
case DHTLIB_ERROR_CHECKSUM: | |
Console.println( "CHECKSUM," ); | |
break; | |
case DHTLIB_ERROR_TIMEOUT: | |
Console.println( "TIME_OUT" ); | |
break; | |
default: | |
Console.println( "UNKNOWN" ); | |
break; | |
} | |
// Rinse, wash, and repeat | |
delay( UPDATE_RATE ); | |
} | |
// Send the data to Parse.com | |
void request( double temperature, double relative ) | |
{ | |
// Buffer for string conversion | |
// The sprintf function does not like doubles | |
char fahrenheit[10]; | |
char humidity[10]; | |
// Temperature as character string | |
dtostrf( temperature, 3, 2, fahrenheit ); | |
// Humidity as character string | |
dtostrf( relative, 3, 2, humidity ); | |
// Build curl command line | |
// Includes HTTPS support | |
// PATCH | |
// Single value so no JSON | |
process.begin( "curl" ); | |
process.addParameter( "-k" ); | |
process.addParameter( "-X" ); | |
process.addParameter( "PATCH" ); | |
// Body | |
// Value to update | |
process.addParameter( "-d" ); | |
sprintf( | |
buffer, | |
"{\"fahrenheit\": %s, \"humidity\": %s}", | |
fahrenheit, | |
humidity | |
); | |
process.addParameter( buffer ); | |
// URI | |
sprintf( | |
buffer, | |
"%s%s", | |
FIREBASE, | |
NODE | |
); | |
process.addParameter( buffer ); | |
// Run the command | |
// Synchronous | |
process.run(); | |
} | |
// Response from Parse.com | |
void response() | |
{ | |
bool print = true; | |
char c; | |
// While there is data to read | |
while( process.available() ) | |
{ | |
// Get character | |
c = process.read(); | |
// Debugging | |
#ifdef DEBUG | |
Console.print( c ); | |
#endif | |
} | |
} | |
// Wait for a response from Parse.com | |
void wait() | |
{ | |
// Periodically check curl process | |
while( !process.available() ) | |
{ | |
delay( 100 ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment