Advertisement
mikecmills2

Arduino Ethernet - Ping Test

Dec 31st, 2013
399
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.69 KB | None | 0 0
  1. /*
  2.  
  3.  Ethernet Ping Test
  4.  
  5. */
  6.  
  7. // License:
  8. //  Copyright 2014 GroveStreams LLC.
  9. //  Licensed under the Apache License, Version 2.0 (the "License");
  10. //  you may not use this file except in compliance with the License.
  11. //  You may obtain a copy of the License at: http://www.apache.org/licenses/LICENSE-2.0
  12. //
  13. //  Unless required by applicable law or agreed to in writing, software
  14. //  distributed under the License is distributed on an "AS IS" BASIS,
  15. //  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. //  See the License for the specific language governing permissions and
  17. //  limitations under the License.
  18.  
  19. #include <SPI.h>
  20. #include <Ethernet.h>
  21. // Local Network Settings
  22. byte mac[] = {
  23.   0x90, 0xA2, 0xDA, 0x0E, 0x60, 0xA3 };   // Change this!!! Must be unique on local network.
  24.                                           // Look for a sticker on the back of your Ethernet shield.
  25.  
  26. char domain[] = "grovestreams.com";         //Domain for ping test
  27.  
  28. const int updateFrequency = 20 * 1000;    //Ping frequency
  29.  
  30. // Variable Setup
  31. long lastSuccessfulUploadTime = 0;    //Don't change.
  32. boolean lastConnected = false;        //Don't change. Used for Internet Connection Reset logic
  33. int failedCounter = 0;                //Don't change. Used for Internet Connection Reset logic
  34.  
  35. // Initialize Arduino Ethernet Client
  36. EthernetClient client;
  37.  
  38.  
  39. void setup()
  40. {
  41.   // Start Serial for debugging on the Serial Monitor
  42.   Serial.begin(9600);
  43.  
  44.   // Start Ethernet on Arduino
  45.   startEthernet();
  46. }
  47.  
  48. void loop()
  49. {
  50.  
  51.   // Print Update Response to Serial Monitor
  52.   if (client.available())
  53.   {
  54.     char c = client.read();
  55.     Serial.print(c);
  56.   }
  57.  
  58.   // Disconnect from domain
  59.   if (!client.connected() && lastConnected)
  60.   {
  61.     Serial.println("...disconnected");
  62.     Serial.println();
  63.  
  64.     client.stop();
  65.   }
  66.  
  67.   // Ping domain
  68.   if(!client.connected() && (millis() - lastSuccessfulUploadTime > updateFrequency))
  69.   {
  70.    
  71.     pingDomain();
  72.   }
  73.  
  74.   // Check if Arduino Ethernet needs to be restarted
  75.   if (failedCounter > 3 ) {
  76.    
  77.     //Too many failures. Restart Ethernet.
  78.     startEthernet();
  79.   }
  80.  
  81.   lastConnected = client.connected();
  82. }
  83.  
  84. void pingDomain()
  85. {
  86.   long connectAttemptTime = millis();
  87.  
  88.   if (client.connect(domain, 80))
  89.   {        
  90.    
  91.     client.println("GET HTTP/1.1");
  92.     client.println("Host: " + String(domain));
  93.     client.println("Connection: close");
  94.     client.println();
  95.    
  96.     if (client.available())
  97.     {
  98.       //Read the response and display in the the console
  99.       char c = client.read();
  100.       Serial.print(c);
  101.     }
  102.  
  103.     if (client.connected())
  104.     {
  105.       lastSuccessfulUploadTime = connectAttemptTime;
  106.       failedCounter = 0;
  107.     }
  108.     else
  109.     {
  110.       //Connection failed. Increase failed counter
  111.       failedCounter++;
  112.  
  113.       Serial.println("Connection to domain failed ("+String(failedCounter, DEC)+")");  
  114.       Serial.println();
  115.     }
  116.  
  117.   }
  118.   else
  119.   {
  120.      //Connection failed. Increase failed counter
  121.     failedCounter++;
  122.  
  123.     Serial.println("Connection to domain Failed ("+String(failedCounter, DEC)+")");  
  124.     Serial.println();
  125.   }
  126. }
  127.  
  128. void startEthernet()
  129. {
  130.   //Start or restart the Ethernet connection.
  131.   client.stop();
  132.  
  133.   Serial.println("Connecting Arduino to network...");
  134.   Serial.println();  
  135.  
  136.   //Wait for the connection to finish stopping
  137.   delay(2000);
  138.  
  139.   //Connect to the network and obtain an IP address using DHCP
  140.   if (Ethernet.begin(mac) == 0)
  141.   {
  142.     Serial.println("DHCP Failed, reset Arduino to try again");
  143.     Serial.println();
  144.   }
  145.   else
  146.   {
  147.     Serial.println("Arduino connected to network using DHCP");
  148.     Serial.println();
  149.   }
  150.  
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement