arduino detect when internet is disconnected

Hello, i have an internet router that always disconnects. to reconnect, i have to plug out the power to the router and reconnect it back. my problem is that i have alot of home automation that requires a steady internet. so im planning to do something like connect an Ethernet to my arduino Ethernet shield. when the ethernet shield is Disconnected, it will activate a relay to switch the router off and on again. my question is, is there a way my ethernet shield can detect when network is disconnected? or any other way i can overcome the problem? any suggestion will be great. Thanks!

Use the arduino as an ethernet client - see IDE example. Periodically, try to connect to a web site. If you fail, bounce the router using a relay or powerswitch tail. For a more robust solution, try several different sites before you conclude that you have no connectivity.

hello will this do?

/*
  Repeating Web client
 
 This sketch connects to a a web server and makes a request
 using a Wiznet Ethernet shield. You can use the Arduino Ethernet shield, or
 the Adafruit Ethernet shield, either one will work, as long as it's got
 a Wiznet Ethernet module on board.
 
 This example uses DNS, by assigning the Ethernet client with a MAC address,
 IP address, and DNS address.
 int pin = 13;
 Circuit:
 * Ethernet shield attached to pins 10, 11, 12, 13
 
 created 19 Apr 2012
 by Tom Igoe
 
 http://arduino.cc/en/Tutorial/WebClientRepeating
 This code is in the public domain.
 
 */

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

// assign a MAC address for the ethernet controller.
// fill in your address here:
byte mac[] = { 
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
// fill in an available IP address on your network here,
// for manual configuration:
IPAddress ip(10,0,0,20);

// fill in your Domain Name Server address here:
IPAddress myDns(1,1,1,1);

// initialize the library instance:
EthernetClient client;

char server[] = "www.arduino.cc";

unsigned long lastConnectionTime = 0;          // last time you connected to the server, in milliseconds
boolean lastConnected = false;                 // state of the connection last time through the main loop
const unsigned long postingInterval = 60*1000;  // delay between updates, in milliseconds

void setup() {
  // start serial port:
  Serial.begin(9600);
  // give the ethernet module time to boot up:
  delay(1000);
  // start the Ethernet connection using a fixed IP address and DNS server:
  Ethernet.begin(mac, ip, myDns);
  // print the Ethernet board/shield's IP address:
  Serial.print("My IP address: ");
  Serial.println(Ethernet.localIP());
}

void loop() {
  // if there's incoming data from the net connection.
  // send it out the serial port.  This is for debugging
  // purposes only:
  if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }

  // if there's no net connection, but there was one last time
  // through the loop, then stop the client:
  if (!client.connected() && lastConnected) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
  }

  // if you're not connected, and ten seconds have passed since
  // your last connection, then connect again and send data:
  if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) {
    httpRequest();
  }
  // store the state of the connection for next time through
  // the loop:
  lastConnected = client.connected();
}

// this method makes a HTTP connection to the server:
void httpRequest() {
  // if there's a successful connection:
  if (client.connect(server, 80)) {
    Serial.println("connecting...");
    // send the HTTP PUT request:
    client.println("GET /latest.txt HTTP/1.1");
    client.println("Host: www.arduino.cc");
    client.println("User-Agent: arduino-ethernet");
    client.println("Connection: close");
    client.println();

    // note the time that the connection was made:
    lastConnectionTime = millis();
  } 
  else {
    // if you couldn't make a connection:
    Serial.println("connection failed");
    Serial.println("disconnecting.");
    client.stop();
   digitalwrite ( pin, HIGH);
  }
}

How often do you need to check the connection? I would try to use something that might be handy for another purpose than just hitting away at a http server.

Have you considered using NTP as the internet check?

or any other way i can overcome the problem?

Perhaps a new router? I've got an inexpensive Netgear 614 that works great. There have been previous similar discussions concerning internet connection loss. If you search the forum for ping and similar terms you might find them.

zoomkat:

or any other way i can overcome the problem?

Perhaps a new router? I've got an inexpensive Netgear 614 that works great. There have been previous similar discussions concerning internet connection loss. If you search the forum for ping and similar terms you might find them.

Voted.

For the price of Arduino and ethernet shield, you might get a good router. by loading linux based router OS (Open-wrt, dd-wrt...), and setup watchdog processing, you could reboot router itself or an other up-level router even restart service or reboot the server which host this forum ( If you need have access right). 8)

zoomkat:

or any other way i can overcome the problem?

Perhaps a new router? I've got an inexpensive Netgear 614 that works great. There have been previous similar discussions concerning internet connection loss. If you search the forum for ping and similar terms you might find them.

I agree. Something like the Ubiquiti Edgerouter Lite is a nice solid router (though probably overkill for a home environment). I have also heard good things about certain Asus routers.

If you keep pinging random servers, they may end up blocking you. Using something like NTP might work, or going to a basic website like google.com and checking the first line of the page for "<!doctype html>"

i think its just the ISP, filing a complain and see what they can do

Hi,

Just I was wondering if there a norma ADSL conexion is enough for such thinks? Or a special one is needed? I have tarifa Jazztel and I don´t know if it´s ok.

Do you found a solution? Cause Im searching something like that (I want that when I have no connection, automatically reset my modem)

i know that this is an old topic, but I wanted to make a comment about those suggesting a new router as the answer.
If there is an actual provable problem with the router, then sure, replace it.

BUT, consider this.
I have a Netgear R7000 gaming router. There are times when it randomly just stops all network traffic.
All the lights remain normal. Internal access remains. The ISP's modem's lights are also fine, showing that it is in fact connected. In my case, that modem is in bridge mode, not router mode so that my router can do all the port forwarding, wireless access and my own protection setups.

So at one point, I thought, I'll have to buy a new router. Others were also reporting this problem. It got much worse too with the latest update! I downgraded it at that point.

But I still had very random traffic stops. Sadly, the router, if just left there, would not restart itself nor restore traffic flow. For this, I would have to power cycle it. This brought it back each time.

AND HERE'S WHY, as I discovered.

In these routers, if they are sensing any kind of hacking, like DDOS, brute force attacks and the like, at a certain point, it automatically just stops traffic flow in order to protect the internal network!

So to prove this to my self, I started monitoring its logs, live.
Well wouldn't ya know it! Sure'nuff, I was being attacked, mostly with brute force attacks.
MOST of the hits were from Russia, while some others were from other countries. But Russia was the worst.

So what I did here was to set up FAKE static routes.
If for example, I had attacks from a specific range of IPs, say, 185.200.x.x, I would set up a static route of
185.200.0.0 with mask of 255.255.0.0 and metric of 2. I routed this to an internal IP that doesn't exist. I just reserved that IP so it would not be in the pool.

Yes, I had to set up quite a few, but after about 25 or so, i had less and less successful attacks.
Sure, there are still more, but I'll add bad IPs as I go along.
I also use a reporting site to report the bad IPs and in fact, that site does show how many reports on a specific IP there is and what the attacks are.

So what I'm saying here, is that POSSIBLY, there's nothing wrong with these routers and they may not need to be replace. They just might be being attacked and are shutting traffic to protect.

So I suggest, monitoring the logs and do the research.

Hope that helps.

BlondieSL:
I would set up a static route of
185.200.0.0 with mask of 255.255.0.0 and metric of 2. I routed this to an internal IP that doesn't exist. I just reserved that IP so it would not be in the pool.

We used to call that ^ the sewer in polite design meeting conversations. In the back room it was always [Yorkshire-Accent]Aye, we'll just send all that f*****g sh1te down the bog.[/Yorkshire-Accent]

Realising that this is a multicultural and global forum I should probably explain that Yorkshire is a County in the UK where people are fiercely proud of their heritage and not afraid to tell it how they see it.

'Bog' == 'Toilet'

(In true Yorkshire spirit I should also just point out that England have the crims at 125/6 in Lords right now, gosh they might win this one :o )