Created
December 2, 2014 21:31
-
-
Save krhoyt/de480cfe7cfe81c76257 to your computer and use it in GitHub Desktop.
Spark Core to PubNub
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
// Libraries | |
#include <math.h> | |
// Literals | |
#define THERMISTOR A0 | |
// Constants | |
// Thermistor computation | |
const float ANALOG_STEPS = 4095.0; | |
const float B_THERM = 3977.0; | |
const float T_THERM = 298.15; | |
const float RESISTOR = 10000.0; | |
const float THERMISTOR_OHM = 10000.0; | |
const float VOLTAGE = 3.3; | |
// Constants | |
// PubNub | |
const char *CHANNEL = "spark_core"; | |
const char *PUB_KEY = "_YOUR_PUBLISH_KEY_"; | |
const char *PUBNUB_URL = "pubsub.pubnub.com"; | |
const char *SECRET_KEY = "_YOUR_SECRET_KEY_"; | |
const char *SUB_KEY = "_YOUR_SUBSCRIBE_KEY_"; | |
const int PUBNUB_PORT = 80; | |
const int UPDATE_RATE = 5000; | |
// TCP connection | |
TCPClient client; | |
// Buffer for fomatting | |
char buffer[100]; | |
// Setup | |
void setup() | |
{ | |
// Serial output | |
Serial.begin( 9600 ); | |
} | |
// Loop | |
void loop() | |
{ | |
double temperature = 0.0; | |
int analog = 0; | |
// Analog reading | |
analog = analogRead( THERMISTOR ); | |
// Thermistor reading as celcius | |
temperature = thermistor( analog ); | |
// Store data in cloud | |
if( client.connect( PUBNUB_URL, PUBNUB_PORT ) ) | |
{ | |
request( temperature ); | |
wait(); | |
response(); | |
} | |
// Wait for next sample | |
delay( UPDATE_RATE ); | |
} | |
void request( double value ) | |
{ | |
// Buffers for string conversion | |
char celcius[10]; | |
char farenheit[10]; | |
// Farenheit as character string | |
sprintf( celcius, "%2.2f", value ); | |
// Convert to celcius as character string | |
sprintf( farenheit, "%2.2f", value * 1.80 + 32 ); | |
// Message | |
sprintf( | |
buffer, | |
"{\"fahrenheit\":\"%s\",\"celcius\":\"%s\"}", | |
farenheit, | |
celcius | |
); | |
client.print( "GET /publish/" ); | |
client.print( PUB_KEY ); | |
client.print( "/" ); | |
client.print( SUB_KEY ); | |
client.print( "/" ); | |
client.print( SECRET_KEY ); | |
client.print( "/" ); | |
client.print( CHANNEL ); | |
client.print( "/0/" ); | |
client.print( buffer ); | |
client.print( "?uuid=" ); | |
client.print( millis() ); | |
client.println( " HTTP/1.1" ); | |
client.println( "Host: pubsub.pubnub.com" ); | |
client.println( "User-Agent: Spark Core" ); | |
client.println( "Content-Length: 0" ); | |
client.println(); | |
} | |
// Response from proxy | |
void response() | |
{ | |
char c; | |
// While there is data to read | |
while( client.available() ) | |
{ | |
// Get character | |
c = client.read(); | |
Serial.print( c ); | |
} | |
client.stop(); | |
} | |
// Calculate celcius temperature | |
// For a 10k resistor at 3.3V | |
// Using a 10k thermistor | |
double thermistor( int analog ) | |
{ | |
double kelvin = 0.0; | |
double l_therm = 0.0; | |
double r_therm = 0.0; | |
double v_out = 0.0; | |
// Thermistor calculations | |
v_out = VOLTAGE * analog / ANALOG_STEPS; | |
r_therm = ( RESISTOR * VOLTAGE / v_out ) - RESISTOR; | |
l_therm = log( THERMISTOR_OHM / r_therm ); | |
kelvin = ( T_THERM * B_THERM ) / ( B_THERM - T_THERM * l_therm ); | |
// Celcius value | |
return kelvin - 273.15; | |
} | |
// Wait for a response from proxy | |
void wait() | |
{ | |
// Periodically check curl process | |
while( !client.available() ) | |
{ | |
Serial.println( "Waiting ..." ); | |
delay( 100 ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment