Advertisement
Guest User

thingspeak arduino sketch

a guest
Jul 26th, 2012
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.70 KB | None | 0 0
  1.  
  2. */
  3.  
  4. #include <SPI.h>
  5. #include <Ethernet.h>
  6.  
  7. // Give pin 13 a name  
  8. int led = 13;
  9.  
  10. // Local Network Settings
  11. byte mac[] = { 0xD4, 0x28, 0xB2, 0xFF, 0xA0, 0xA1 }; // Must be unique on local network
  12.  
  13. // ThingSpeak Settings
  14. char thingSpeakAddress[] = "api.thingspeak.com";
  15. String thingSpeakChannel = "2738";                // Channel number looking for twitter update
  16. String thingSpeakField = "1";                     // Field number of the CheerLights commands
  17. const int thingSpeakInterval = 16 * 1000; // Time interval in milliseconds to get data from ThingSpeak (number of seconds * 1000 = interval)    
  18.  
  19. // Variable Setup
  20. long lastConnectionTime = 0;
  21. String lastCommand = "";
  22. boolean lastConnected = false;
  23. int failedCounter = 0;
  24.  
  25. // Initialize Arduino Ethernet Client
  26. EthernetClient client;
  27.  
  28. void setup() {
  29.  
  30.   // initialize the digital pin as an output.
  31.   pinMode(led, OUTPUT);    
  32.  
  33.   // Setup Serial
  34.   Serial.begin(9600);
  35.   delay(100);
  36.  
  37.   Serial.flush();
  38.   delay(100);
  39.  
  40.   // Start Ethernet on Arduino
  41.   startEthernet();
  42. }
  43.  
  44. void loop() {
  45.   // if there's incoming data from the net connection.
  46.   // send it out the serial port.  This is for debugging
  47.   // purposes only:
  48.   if (client.available()) {
  49.     char c = client.read();
  50.     Serial.print(c);
  51.   }
  52.  
  53.   // if there's no net connection, but there was one last time
  54.   // through the loop, then stop the client:
  55.   if (!client.connected() && lastConnected) {
  56.     Serial.println();
  57.     Serial.println("disconnecting.");
  58.     client.stop();
  59.   }
  60.  
  61.  
  62.   // store the state of the connection for next time through
  63.   // the loop:
  64.   lastConnected = client.connected();
  65. }
  66.  
  67.  
  68. void subscribeToThingSpeak(String tsChannel, String tsField)
  69. {
  70.   if (client.connect(thingSpeakAddress, 80))
  71.   {        
  72.     Serial.println("Connecting to ThingSpeak...");
  73.     Serial.println();
  74.      
  75.     failedCounter = 0;
  76.    
  77.     client.println("GET /channels/"+tsChannel+"/field/"+tsField+"/last.txt HTTP/1.0");
  78.     client.println();
  79.    
  80.     lastConnectionTime = millis();
  81.   }
  82.   else
  83.   {
  84.     failedCounter++;
  85.    
  86.     Serial.println("Connection to ThingSpeak Failed ("+String(failedCounter, DEC)+")");  
  87.     Serial.println();
  88.    
  89.     lastConnectionTime = millis();
  90.   }
  91. }
  92.  
  93. void startEthernet()
  94. {
  95.  
  96.   client.stop();
  97.  
  98.   Serial.println("Connecting Arduino to network...");
  99.   Serial.println();  
  100.  
  101.   delay(1000);
  102.  
  103.   // Connect to network amd obtain an IP address using DHCP
  104.   if (Ethernet.begin(mac) == 0)
  105.   {
  106.     Serial.println("DHCP Failed, reset Arduino to try again");
  107.     Serial.println();
  108.   }
  109.   else
  110.   {
  111.     Serial.println("Arduino connected to network using DHCP");
  112.     Serial.println();
  113.   }
  114.  
  115.   delay(1000);
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement