Created
January 31, 2014 22:01
-
-
Save ghing/8744161 to your computer and use it in GitHub Desktop.
Read temperature from a TMP102 sensor and push to Xively
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
// | |
// Read temperature from a TMP102 sensor and push to Xively | |
// | |
#include <SPI.h> | |
#include <WiFly.h> | |
#include <Wire.h> | |
const char ssid[] = "BigBabyBoy"; | |
const char wpaPassword[] = "mrjonesandme"; | |
const char apiUsername[] = "ghing"; | |
const char apiKey[] = "S9UpmnQPtEuPbXxtVKHzQFRYLyfEmUJ9Us8q8I3GqDFD3Ypx"; | |
const char feedId[] = "960752696"; | |
const int tmp102Address = 0x48; | |
WiFlyClient client("api.xively.com", 80); | |
void putXivelyData(const char *feedId, const char *apiKey, float temp) { | |
String feedPath = "/v2/feeds/" + String(feedId) + ".json"; | |
char tempBuf[14]; | |
String tempS; | |
String json; | |
dtostrf(temp, 7, 2, tempBuf); | |
tempS = String(tempBuf); | |
tempS.trim(); | |
json = "{\"datastreams\":[{\"id\":\"temperature\", \"current_value\":\"" + tempS + "\"}]}"; | |
Serial.println("connecting..."); | |
if (!client.connect()) { | |
Serial.println("connection failed"); | |
return; | |
} | |
Serial.println("connected"); | |
client.print("PUT "); | |
client.print(feedPath); | |
client.print(" HTTP/1.1\n"); | |
client.println("Host: api.xively.com"); | |
client.print("X-ApiKey: "); | |
client.println(apiKey); | |
client.print("Content-Type: application/json\n"); | |
client.print("Content-Length: "); | |
client.print(json.length()); | |
client.print("\n\n"); | |
client.println(json); | |
// Wait for response | |
delay(2000); | |
while (client.available()) { | |
Serial.print(client.readChar()); | |
} | |
Serial.println(); | |
if (client.connected()) { | |
Serial.println("disconnecting."); | |
client.stop(); | |
} | |
} | |
// From http://bildr.org/2011/01/tmp102-arduino/ | |
float getTemperature(){ | |
Wire.requestFrom(tmp102Address, 2); | |
byte MSB = Wire.read(); | |
byte LSB = Wire.read(); | |
// it's a 12bit int, using two's compliment for negative | |
int TemperatureSum = ((MSB << 8) | LSB) >> 4; | |
float celsius = TemperatureSum*0.0625; | |
return celsius; | |
} | |
void setup() { | |
Serial.begin(9600); | |
WiFly.begin(); | |
Wire.begin(); | |
if (!WiFly.join(ssid, wpaPassword)) { | |
Serial.print("Failed to connect to the wifi network "); | |
Serial.println(ssid); | |
while (1) { | |
// Hang on failure | |
} | |
} | |
Serial.print("Wifi connection to "); | |
Serial.print(ssid); | |
Serial.println(" successful"); | |
} | |
void loop() { | |
float celsius = getTemperature(); | |
Serial.print("Celsius: "); | |
Serial.println(celsius); | |
float fahrenheit = (1.8 * celsius) + 32; | |
Serial.print("Fahrenheit: "); | |
Serial.println(fahrenheit); | |
putXivelyData(feedId, apiKey, fahrenheit); | |
delay(5000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment