Advertisement
Guest User

gumball v2

a guest
Aug 13th, 2012
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.15 KB | None | 0 0
  1. #include <SPI.h>
  2. #include <Ethernet.h>
  3.  
  4. // Give pin 13 a name  
  5. int led = 13;
  6.  
  7. // Local Network Settings
  8. byte mac[] = { 0xD4, 0x28, 0xB2, 0xFF, 0xA0, 0xA1 }; // Must be unique on local network
  9.  
  10. // ThingSpeak Settings
  11. char thingSpeakAddress[]       = "api.thingspeak.com";
  12. String thingSpeakChannel       = "2738";                // Channel number looking for twitter update
  13. String thingSpeakField         = "1";                   // Field number of the CheerLights commands
  14. const int thingSpeakInterval   = 16 * 1000;             // Time interval in milliseconds to get data from ThingSpeak (number of seconds * 1000 = interval)    
  15.  
  16. // Variable Setup
  17. long lastConnectionTime  = 0;
  18. String lastCommand       = "";
  19. boolean lastConnected    = false;
  20. int failedCounter        = 0;
  21.  
  22. // Initialize Arduino Ethernet Client
  23. EthernetClient client;
  24.  
  25. void setup()
  26. {
  27.  
  28.   // initialize the digital pin as an output.
  29.   pinMode(led, OUTPUT);    
  30.  
  31.   // Setup Serial
  32.   Serial.begin(9600);
  33.   delay(100);
  34.  
  35.   Serial.flush();
  36.   delay(100);
  37.  
  38.   // Start Ethernet on Arduino
  39.   startEthernet();
  40. }
  41.  
  42. void loop()
  43. {
  44.  
  45.   if (client.available() > 0)
  46.   {
  47.     delay(100);
  48.    
  49.     String response;
  50.     char charIn;
  51.    
  52.     do
  53.     {
  54.       charIn = client.read();
  55.       response += charIn;  
  56.     }
  57.    
  58.     while(client.available() > 0);
  59.    
  60.     if (response.indexOf("gumball") > 0)
  61.     {  
  62.         lastCommand = "gumball";
  63.     }
  64.    
  65.      // Echo command
  66.     delay(200);
  67.     Serial.println("CheerLight Command Received: "+lastCommand);
  68.     Serial.println();
  69.     delay(200);
  70.    
  71.   }
  72.  
  73.   if (!client.connected() && lastConnected)
  74.   {
  75.     Serial.println();
  76.     Serial.println("disconnecting.");
  77.     client.stop();
  78.   }
  79.  
  80.    // Subscribe to ThingSpeak Channel and Field
  81.   if(!client.connected() && (millis() - lastConnectionTime > thingSpeakInterval))
  82.   {
  83.     subscribeToThingSpeak(thingSpeakChannel, thingSpeakField);
  84.   }
  85.  
  86.   // Check if Arduino Ethernet needs to be restarted
  87.   if (failedCounter > 3 )
  88.   {
  89.     startEthernet();
  90.   }
  91.  
  92.   lastConnected = client.connected();
  93. }
  94.  
  95.  
  96. void subscribeToThingSpeak(String tsChannel, String tsField)
  97. {
  98.   if (client.connect(thingSpeakAddress, 80))
  99.   {        
  100.     Serial.println("Connecting to ThingSpeak...");
  101.     Serial.println();
  102.      
  103.     failedCounter = 0;
  104.    
  105.     client.println("GET /channels/"+tsChannel+"/field/"+tsField+"/last.txt HTTP/1.0");
  106.     client.println();
  107.    
  108.     lastConnectionTime = millis();
  109.   }
  110.   else
  111.   {
  112.     failedCounter++;
  113.    
  114.     Serial.println("Connection to ThingSpeak Failed ("+String(failedCounter, DEC)+")");  
  115.     Serial.println();
  116.    
  117.     lastConnectionTime = millis();
  118.   }
  119. }
  120.  
  121. void startEthernet()
  122. {
  123.  
  124.   client.stop();
  125.  
  126.   Serial.println("Connecting Arduino to network...");
  127.   Serial.println();  
  128.  
  129.   delay(1000);
  130.  
  131.   // Connect to network amd obtain an IP address using DHCP
  132.   if (Ethernet.begin(mac) == 0)
  133.   {
  134.     Serial.println("DHCP Failed, reset Arduino to try again");
  135.     Serial.println();
  136.   }
  137.   else
  138.   {
  139.     Serial.println("Arduino connected to network using DHCP");
  140.     Serial.println();
  141.   }
  142.  
  143.   delay(1000);
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement