Ethernet Program Problem

I am building a program to blink the LED when the internet goes down via ENC28J60 module using the EthernetENC library.

The problem I am facing is that my program keeps on printing the same status as it was on the first time the program ran. If there is any change, it does not detect it. If at the start, I have disconnected the ethernet cable, it will keep on detecting the same even when I have inserted it back & vice versa.

Where am I going wrong?

PS : Serial Monitor Data attached below

#include <SPI.h>
#include <EthernetENC.h>

int flag;
int ledpin = 7;
int count = 0;
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
char server[] = "www.google.com";    // name address for Google (using DNS)
char server2[] = "www.yahoo.com";
char server3[] = "www.microsoft.com";




// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(192, 168, 0, 177);
IPAddress myDns(192, 168, 0, 1);




EthernetClient client;



void setup() {
  // You can use Ethernet.init(pin) to configure the CS pin
   // Ethernet.init(10);  // Most Arduino shields
  pinMode(ledpin, OUTPUT);
  digitalWrite(ledpin, HIGH);
  flag = 0;
  Serial.begin(9600);

    
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }


  // start the Ethernet connection:
  Serial.println("Initialize Ethernet with DHCP:");
 
}






int ethernetavailable()
{
  
   if (Ethernet.begin(mac) == 0) 
   {
    Serial.println("Failed to configure Ethernet using DHCP");
    // Check for Ethernet hardware present
    if (Ethernet.hardwareStatus() == EthernetNoHardware) {
      Serial.println("Ethernet shield was not found.  Sorry, can't run without hardware. :(");
      while (true) {
       return 0; // do nothing, no point running without Ethernet hardware
      }
    }
    if (Ethernet.linkStatus() == LinkOFF) {
      Serial.println("Ethernet cable is not connected.");
    }
    // try to congifure using IP address instead of DHCP:
    Ethernet.begin(mac, ip, myDns);
    return 0;
  } 


  else 
  {
    Serial.print("  DHCP assigned IP ");
    Serial.println(Ethernet.localIP());
    delay(1);
    return 1;
  }
  
  
}


int checkgoogle()
{
  if (client.connect(server, 80)) {
    
      String LocalIP = String() + client.remoteIP()[0]+client.remoteIP()[1]+client.remoteIP()[2]+client.remoteIP()[3];
      Serial.println(LocalIP);
      if(LocalIP == "0000")
       {
      
        Serial.println("IP DETECTED 000000 GOOGLE");
        return 0;
        }


        else
        {
          
          Serial.println("GOOGLE OKAY");
          digitalWrite(ledpin, HIGH);
          for(count = 0; count < 300 ; count++)
          {
               delay(1);
          }
           return 1;
        }


  } 
  else
  {
  return 0;
  }
}




int checkyahoo()
{
  if (client.connect(server2, 80)) {
    
      String LocalIP = String() + client.remoteIP()[0]+client.remoteIP()[1]+client.remoteIP()[2]+client.remoteIP()[3];
      if (LocalIP = "0000")
       {
      
        Serial.println("IP DETECTED 000000 YAHOO");
        return 0;
        }


        else
        {
          
          Serial.println("YAHOO OKAY");
          digitalWrite(ledpin, HIGH);
          for(count = 0; count < 300 ; count++)
          {
               delay(1);
          }
           return 1;
        }


  } 
  else
  {
  return 0;
  }
}




int checkmicrosoft()
{
  if (client.connect(server3, 80)) {
    
      String LocalIP = String() + client.remoteIP()[0]+client.remoteIP()[1]+client.remoteIP()[2]+client.remoteIP()[3];
      if (LocalIP = "0000")
       {
      
        Serial.println("IP DETECTED 000000 MICROSOFT");
        return 0;
        }


        else
        {
          Serial.println("MICROSOFT OKAY");
          digitalWrite(ledpin, HIGH);
          for(count = 0; count < 300 ; count++)
          {
               delay(1);
          }
           return 1;
        }


  } 
  else
  {
  return 0;
  }
}






void loop()
{
  flag = ethernetavailable();
  Serial.println(flag);
  while(flag)
    {   
        checkgoogle();
        if(!checkgoogle())
          
          {
            if(!checkyahoo())
              {
                if(!checkmicrosoft())
                {
                            digitalWrite(ledpin, LOW);
                            delay(1);
                            digitalWrite(ledpin, LOW);
                            for(count = 0; count < 300 ; count++)
                            {
                                 delay(1);
                            }
                }
              }
          }
     }
}

This happens when ethernet cable is plugged in at the start and I remove it after 5 seconds :

18:05:42.412 → DHCP (de:ad:13:69:f7:82Initialize Ethernet with DHCP:
18:05:46.998 → DHCP assigned IP 192.168.1.31
18:05:47.410 → DHCP assigned IP 192.168.1.31
18:05:47.410 → 1
18:05:47.682 → 17221727164
18:05:47.682 → GOOGLE OKAY
18:05:48.191 → 17221727164
18:05:48.191 → GOOGLE OKAY
18:05:48.704 → 17221727164
18:05:48.704 → GOOGLE OKAY
18:05:49.215 → 17221727164
18:05:49.215 → GOOGLE OKAY
18:05:49.728 → 17221727164
18:05:49.728 → GOOGLE OKAY
18:05:50.207 → 17221727164
18:05:50.207 → GOOGLE OKAY
18:05:50.720 → 17221727164
18:05:50.720 → GOOGLE OKAY
18:05:51.232 → 17221727164
18:05:51.232 → GOOGLE OKAY
18:05:51.746 → 17221727164
18:05:51.746 → GOOGLE OKAY
18:05:52.258 → 17221727164
18:05:52.258 → GOOGLE OKAY
18:05:52.771 → 17221727164
18:05:52.771 → GOOGLE OKAY
18:05:53.290 → 17221727164
18:05:53.290 → GOOGLE OKAY
18:05:53.809 → 17221727164
18:05:53.809 → GOOGLE OKAY
18:05:54.296 → 17221727164
18:05:54.296 → GOOGLE OKAY
18:05:54.816 → 17221727164
18:05:54.816 → GOOGLE OKAY
18:05:55.337 → 17221727164
18:05:55.337 → GOOGLE OKAY
18:05:55.820 → 17221727164
18:05:55.820 → GOOGLE OKAY
18:05:56.338 → 17221727164
18:05:56.338 → GOOGLE OKAY
18:05:56.859 → 17221727164
18:05:56.859 → GOOGLE OKAY
18:05:57.377 → 17221727164
18:05:57.377 → GOOGLE OKAY
18:05:57.894 → 17221727164
18:05:57.894 → GOOGLE OKAY
18:05:58.374 → 17221727164
18:05:58.374 → GOOGLE OKAY
18:05:58.886 → 17221727164
18:05:58.886 → GOOGLE OKAY
18:06:14.337 → 17221727164
18:06:14.337 → GOOGLE OKAY
18:06:29.759 → 17221727164
18:06:29.759 → GOOGLE OKAY
18:06:45.205 → 17221727164
18:06:45.205 → GOOGLE OKAY
18:07:00.633 → 17221727164
18:07:00.633 → GOOGLE OKAY
18:07:16.070 → 17221727164
18:07:16.070 → GOOGLE OKAY
18:07:31.495 → 17221727164
18:07:31.495 → GOOGLE OKAY
18:07:46.937 → 17221727164
18:07:46.937 → GOOGLE OKAY

My guess would be that becasue you have already connected the remote ip, port etc. is known so when you try and connect again it just returns the information that it already has and makes no attempt to actually connect again.

Consider using client.stop() to stop teh existing connection and establish a new one.

You have 3 almost identical routines, you would be better off just passing in "google", "yahoo" or "microsoft" to a single routine as that would simplify your code and you could avoid having to make the same changes 3 times.

what else would you expect? you don't print something if connect returns false. so the prints stop

btw: fix == in conditions

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.