Tutorials‎ > ‎

ArduinoInternetPower

This Arduino sketch code continuously pings an IP address (assuming you have a shield with network access).

When the IP address goes offline, it toggles pins on the Arduino output, which you can use to control other things
that you are interested in.

(I use just to turn stuff on or off with a diffferent tutorial):

Here's the code:

Sketch Code

#include <ICMPPing.h>
#include <SPI.h>
#include <Ethernet.h>


// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = {  
  0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 };

// Initialize the Ethernet client library
// with the IP address and port of the server 
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;
// Initialize the Ethernet server library
// with the IP address and port you want to use 
// (port 80 is default for HTTP):
EthernetServer server(80);
byte pingAddr[] = {192,168,3,97}; // ip address to ping
char buffer [256];
SOCKET pingSocket = 3;
long previousMillis = 0;
ICMPPing ping(pingSocket);

int led = 6;

void setup() {
  pinMode(led, OUTPUT);
  digitalWrite(led, LOW);
  
  // start the serial library:
  Serial.begin(9600);
  // start the Ethernet connection:
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    for(;;)
      ;
  }
  // print your local IP address:
  Serial.print("My IP address: ");
  for (byte thisByte = 0; thisByte < 4; thisByte++) {
    // print the value of each byte of the IP address:
    Serial.print(Ethernet.localIP()[thisByte], DEC);
    Serial.print("."); 
  }
  Serial.println();
  server.begin();
}

bool success = true;

void loop() {
  unsigned long currentMillis = millis();
// listen for incoming clients
  EthernetClient client = server.available();
  if (client) {
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
        if (c == '\n' && currentLineIsBlank) {
          // send a standard http response header
          
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();
          client.println("Hello Michael: ");
          client.println(currentMillis);
          client.println(", last buffer: ");
          client.println(success ? "success" : "failed");
          if(success) {
            client.println(", result: ");
            client.println(buffer);
          }
          break;
        }
        if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
        } 
        else if (c != '\r') {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
        }
      }
    }
    // give the web browser time to receive the data
    delay(1);
    // close the connection:
    client.stop();
  }
  
  if((currentMillis - previousMillis) > 30000) {                          
        bool current = false; 
          previousMillis = currentMillis;   
           current = ping(4, pingAddr, buffer);
          Serial.println(buffer);
         if(current) {
           if(!success) {
             Serial.println("switching to low");
             digitalWrite(led, LOW);
           }
         } else {
           if(success) {
             Serial.println("switching to high");
             digitalWrite(led, HIGH);
           }
         }
         success = current;
  }
}


Comments