Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Ethernet Ping Test
- */
- // License:
- // Copyright 2014 GroveStreams LLC.
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at: http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- #include <SPI.h>
- #include <Ethernet.h>
- // Local Network Settings
- byte mac[] = {
- 0x90, 0xA2, 0xDA, 0x0E, 0x60, 0xA3 }; // Change this!!! Must be unique on local network.
- // Look for a sticker on the back of your Ethernet shield.
- char domain[] = "grovestreams.com"; //Domain for ping test
- const int updateFrequency = 20 * 1000; //Ping frequency
- // Variable Setup
- long lastSuccessfulUploadTime = 0; //Don't change.
- boolean lastConnected = false; //Don't change. Used for Internet Connection Reset logic
- int failedCounter = 0; //Don't change. Used for Internet Connection Reset logic
- // Initialize Arduino Ethernet Client
- EthernetClient client;
- void setup()
- {
- // Start Serial for debugging on the Serial Monitor
- Serial.begin(9600);
- // Start Ethernet on Arduino
- startEthernet();
- }
- void loop()
- {
- // Print Update Response to Serial Monitor
- if (client.available())
- {
- char c = client.read();
- Serial.print(c);
- }
- // Disconnect from domain
- if (!client.connected() && lastConnected)
- {
- Serial.println("...disconnected");
- Serial.println();
- client.stop();
- }
- // Ping domain
- if(!client.connected() && (millis() - lastSuccessfulUploadTime > updateFrequency))
- {
- pingDomain();
- }
- // Check if Arduino Ethernet needs to be restarted
- if (failedCounter > 3 ) {
- //Too many failures. Restart Ethernet.
- startEthernet();
- }
- lastConnected = client.connected();
- }
- void pingDomain()
- {
- long connectAttemptTime = millis();
- if (client.connect(domain, 80))
- {
- client.println("GET HTTP/1.1");
- client.println("Host: " + String(domain));
- client.println("Connection: close");
- client.println();
- if (client.available())
- {
- //Read the response and display in the the console
- char c = client.read();
- Serial.print(c);
- }
- if (client.connected())
- {
- lastSuccessfulUploadTime = connectAttemptTime;
- failedCounter = 0;
- }
- else
- {
- //Connection failed. Increase failed counter
- failedCounter++;
- Serial.println("Connection to domain failed ("+String(failedCounter, DEC)+")");
- Serial.println();
- }
- }
- else
- {
- //Connection failed. Increase failed counter
- failedCounter++;
- Serial.println("Connection to domain Failed ("+String(failedCounter, DEC)+")");
- Serial.println();
- }
- }
- void startEthernet()
- {
- //Start or restart the Ethernet connection.
- client.stop();
- Serial.println("Connecting Arduino to network...");
- Serial.println();
- //Wait for the connection to finish stopping
- delay(2000);
- //Connect to the network and obtain an IP address using DHCP
- if (Ethernet.begin(mac) == 0)
- {
- Serial.println("DHCP Failed, reset Arduino to try again");
- Serial.println();
- }
- else
- {
- Serial.println("Arduino connected to network using DHCP");
- Serial.println();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement