Advertisement
Guest User

Spark Core Xivley Feed

a guest
Feb 7th, 2014
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.45 KB | None | 0 0
  1. #define FEED_ID "1234" //note: fake id here..
  2. #define XIVELY_API_KEY "1234" //note: fake key here
  3.  
  4. TCPClient client;
  5.  
  6. int reading = 0;
  7. int ledD = D7;
  8.  
  9. int count = 0;
  10. int total_temp = 0;
  11. int temp_calc = 0;
  12. unsigned long LastUpTime = 0;
  13. unsigned long LastCloudCheck = 0;
  14. char whichApp[64] = "READ TEMPERATURE with XIVELY";
  15.  
  16. // This routine runs only once upon reset
  17. void setup()
  18. {
  19. //Register our Spark function here
  20. Spark.variable("whichapp", &whichApp, STRING);
  21. Spark.variable("reading", &reading, INT);
  22. Spark.function("degres", tempCalculation);
  23. Spark.function("volt", analogReading);
  24. pinMode(A7, INPUT);
  25. pinMode(ledD, OUTPUT);
  26. ledStatus(5, 100); //Blink
  27. }
  28.  
  29. void loop()
  30. {
  31. reading = analogRead(A7);
  32. temp_calc = (reading*3.3/4095)*100 - 50;
  33.  
  34. if (millis()-LastUpTime>1000)
  35. {
  36. if (count <= 5) {
  37. total_temp += temp_calc;
  38. count++;
  39. }
  40. else {
  41. xivelyTemp(total_temp/count); //Send the average of the last 5 readings
  42. count = 0;
  43. total_temp = 0;
  44. }
  45. LastUpTime = millis();
  46. }
  47.  
  48. if (millis()-LastCloudCheck > 1000*60*5) { //check every 5 min to see if the connection still exists
  49. if(!Spark.connected()) Spark.connect();
  50. LastCloudCheck = millis();
  51. }
  52. }
  53.  
  54. void xivelyTemp(int temperature) {
  55.  
  56. ledStatus(5, 100);
  57. //Serial.println("Connecting to server...");
  58. if (client.connect("api.xively.com", 8081))
  59. {
  60. // Connection succesful, update datastreams
  61. client.print("{");
  62. client.print(" \"method\" : \"put\",");
  63. client.print(" \"resource\" : \"/feeds/");
  64. client.print(FEED_ID);
  65. client.print("\",");
  66. client.print(" \"params\" : {},");
  67. client.print(" \"headers\" : {\"X-ApiKey\":\"");
  68. client.print(XIVELY_API_KEY);
  69. client.print("\"},");
  70. client.print(" \"body\" :");
  71. client.print(" {");
  72. client.print(" \"version\" : \"1.0.0\",");
  73. client.print(" \"datastreams\" : [");
  74. client.print(" {");
  75. client.print(" \"id\" : \"bedroom_temp\",");
  76. client.print(" \"current_value\" : \"");
  77. client.print(temperature-8); //adjustment for some weird reason..
  78. client.print("\"");
  79. client.print(" }");
  80. client.print(" ]");
  81. client.print(" },");
  82. client.print(" \"token\" : \"0x123abc\"");
  83. client.print("}");
  84. client.println();
  85.  
  86. ledStatus(3, 1000);
  87. }
  88. else
  89. {
  90. // Connection failed
  91. //Serial.println("connection failed");
  92. ledStatus(3, 2000);
  93. }
  94.  
  95.  
  96. if (client.available())
  97. {
  98. // Read response
  99. //char c = client.read();
  100. //Serial.print(c);
  101. }
  102.  
  103. if (!client.connected())
  104. {
  105. //Serial.println();
  106. //Serial.println("disconnecting.");
  107. client.stop();
  108. }
  109.  
  110. client.flush();
  111. client.stop();
  112. }
  113.  
  114.  
  115. void ledStatus(int x, int t)
  116. {
  117. for (int j = 0; j <= x-1; j++)
  118. {
  119. digitalWrite(ledD, HIGH);
  120. delay(t);
  121. digitalWrite(ledD, LOW);
  122. delay(t);
  123. }
  124. }
  125.  
  126. int tempCalculation(String command) {
  127. int tempCalc = (reading*3.3/4095)*100 - 50;
  128. return tempCalc;
  129. }
  130.  
  131. int analogReading(String command) {
  132. return reading;
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement