#define FEED_ID "1234" //note: fake id here.. #define XIVELY_API_KEY "1234" //note: fake key here TCPClient client; int reading = 0; int ledD = D7; int count = 0; int total_temp = 0; int temp_calc = 0; unsigned long lastReset = 0; unsigned long LastUpTime = 0; unsigned long LastCloudCheck = 0; char whichApp[64] = "READ TEMPERATURE with XIVELY"; // This routine runs only once upon reset void setup() { lastReset = millis(); // We just powered up //Register our Spark function here Spark.variable("whichapp", &whichApp, STRING); Spark.variable("reading", &reading, INT); Spark.function("degres", tempCalculation); Spark.function("volt", analogReading); pinMode(A7, INPUT); pinMode(ledD, OUTPUT); ledStatus(5, 100); //Blink } void loop() { reading = analogRead(A7); temp_calc = (reading*3.3/4095)*100 - 50; if (millis()-LastUpTime>1000) { if (count <= 5) { total_temp += temp_calc; count++; } else { xivelyTemp(total_temp/count); //Send the average of the last 5 readings count = 0; total_temp = 0; } LastUpTime = millis(); } if (millis()-LastCloudCheck > 1000*60*5) { //check every 5 min to see if the connection still exists if(!Spark.connected()) Spark.connect(); LastCloudCheck = millis(); } // Is it time to reset? if( tenMinutesElapsed() ) { // Software Reset NVIC_SystemReset(); } } void xivelyTemp(int temperature) { ledStatus(5, 100); //Serial.println("Connecting to server..."); if (client.connect("api.xively.com", 8081)) { // Connection succesful, update datastreams client.print("{"); client.print(" \"method\" : \"put\","); client.print(" \"resource\" : \"/feeds/"); client.print(FEED_ID); client.print("\","); client.print(" \"params\" : {},"); client.print(" \"headers\" : {\"X-ApiKey\":\""); client.print(XIVELY_API_KEY); client.print("\"},"); client.print(" \"body\" :"); client.print(" {"); client.print(" \"version\" : \"1.0.0\","); client.print(" \"datastreams\" : ["); client.print(" {"); client.print(" \"id\" : \"bedroom_temp\","); client.print(" \"current_value\" : \""); client.print(temperature-8); //adjustment for some weird reason.. client.print("\""); client.print(" }"); client.print(" ]"); client.print(" },"); client.print(" \"token\" : \"0x123abc\""); client.print("}"); client.println(); ledStatus(3, 1000); } else { // Connection failed //Serial.println("connection failed"); ledStatus(3, 2000); } if (client.available()) { // Read response //char c = client.read(); //Serial.print(c); } if (!client.connected()) { //Serial.println(); //Serial.println("disconnecting."); client.stop(); } client.flush(); client.stop(); } void ledStatus(int x, int t) { for (int j = 0; j <= x-1; j++) { digitalWrite(ledD, HIGH); delay(t); digitalWrite(ledD, LOW); delay(t); } } int tempCalculation(String command) { int tempCalc = (reading*3.3/4095)*100 - 50; return tempCalc; } int analogReading(String command) { return reading; } uint8_t tenMinutesElapsed() { if( (millis()-lastReset) > (10*60*1000) ) { lastReset = millis(); return 1; // 10 minutes has elapsed } else { return 0; // nope, not yet be patient! } }