Advertisement
Guest User

Untitled

a guest
Dec 27th, 2011
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.65 KB | None | 0 0
  1. /*
  2.  
  3. ThingSpeak Client to Update Channel Feeds
  4.  
  5. The ThingSpeak Client sketch is designed for the Arduino + Ethernet Shield.
  6. This sketch updates a channel feed with an analog input reading via the
  7. ThingSpeak API (http://community.thingspeak.com/documentation/)
  8. using HTTP POST.
  9.  
  10. Getting Started with ThingSpeak:
  11.  
  12. * Sign Up for New User Account - https://www.thingspeak.com/users/new
  13. * Create a New Channel by selecting Channels and then Create New Channel
  14. * Enter the Write API Key in this sketch under "ThingSpeak Settings"
  15.  
  16. Created: January 25, 2011 by Hans Scharler (http://www.iamshadowlord.com)
  17.  
  18. Additional Credits: Example sketches from Tom Igoe and David A. Mellis
  19.  
  20. */
  21.  
  22. #include <SPI.h>
  23. #include <Ethernet.h>
  24.  
  25. int sensorPin = 0;
  26.  
  27.  
  28.  
  29. // Local Network Settings
  30. byte mac[] = { 0xD4, 0x28, 0xB2, 0xFF, 0xA0, 0xA1 }; // Must be unique on local network
  31. byte ip[] = { 192, 168, 1, 140 }; // Must be unique on local network
  32. byte gateway[] = { 192, 168, 1, 1 };
  33. byte subnet[] = { 255, 255, 255, 0 };
  34.  
  35. // ThingSpeak Settings
  36. byte server[] = { 184, 106, 153, 149 }; // IP Address for the ThingSpeak API
  37. String writeAPIKey = "xxxxxxxxxxxxxx"; // Write API Key for a ThingSpeak Channel
  38. const int updateInterval = 10000; // Time interval in milliseconds to update ThingSpeak
  39. Client client(server, 80);
  40.  
  41. // Variable Setup
  42. long lastConnectionTime = 0;
  43. boolean lastConnected = false;
  44. int resetCounter = 0;
  45.  
  46. void setup()
  47. {
  48. Ethernet.begin(mac, ip, gateway, subnet);
  49. Serial.begin(9600);
  50. delay(1000);
  51. int reading = analogRead(sensorPin);
  52. float voltage = reading * 5.0;
  53. voltage /= 1024.0;
  54. int temperatureC = (voltage) * 100 ;
  55. }
  56.  
  57. void loop()
  58. {
  59. int reading = analogRead(sensorPin);
  60. float voltage = reading * 5.0;
  61. voltage /= 1024.0;
  62. long temperatureC = (voltage) * 100;
  63.  
  64. String temper = String(temperatureC, DEC);
  65. /*
  66. /Serial.print(temperatureC);
  67. delay (1000);
  68. */
  69.  
  70.  
  71. // Print Update Response to Serial Monitor
  72. if (client.available())
  73. {
  74. char c = client.read();
  75. Serial.print(c);
  76. }
  77.  
  78. // Disconnect from ThingSpeak
  79. if (!client.connected() && lastConnected)
  80. {
  81. Serial.println();
  82. Serial.println("...disconnected.");
  83. Serial.println();
  84.  
  85. client.stop();
  86. }
  87.  
  88. // Update ThingSpeak
  89. if(!client.connected() && (millis() - lastConnectionTime > updateInterval))
  90. {
  91. //updateThingSpeak("field1="+(temperatureC, DEC));
  92. updateThingSpeak("field1="+temper);
  93. //updateThingSpeak("field1="+(temper, DEC));
  94. }
  95.  
  96. lastConnected = client.connected();
  97. }
  98.  
  99. void updateThingSpeak(String tsData)
  100. {
  101. if (client.connect())
  102. {
  103. Serial.println("Connected to ThingSpeak...");
  104. Serial.println();
  105.  
  106. client.print("POST /update HTTP/1.1\n");
  107. client.print("Host: api.thingspeak.com\n");
  108. client.print("Connection: close\n");
  109. client.print("X-THINGSPEAKAPIKEY: "+writeAPIKey+"\n");
  110. client.print("Content-Type: application/x-www-form-urlencoded\n");
  111. client.print("Content-Length: ");
  112. client.print(tsData.length());
  113. client.print("\n\n");
  114.  
  115. client.print(tsData);
  116.  
  117. lastConnectionTime = millis();
  118.  
  119. resetCounter = 0;
  120.  
  121. }
  122. else
  123. {
  124. Serial.println("Connection Failed.");
  125. Serial.println();
  126.  
  127. resetCounter++;
  128.  
  129. if (resetCounter >=5 ) {resetEthernetShield();}
  130.  
  131. lastConnectionTime = millis();
  132. }
  133. }
  134.  
  135. void resetEthernetShield()
  136. {
  137. Serial.println("Resetting Ethernet Shield.");
  138. Serial.println();
  139.  
  140. client.stop();
  141. delay(1000);
  142.  
  143. Ethernet.begin(mac, ip, gateway, subnet);
  144. delay(1000);
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement