4 Replies Latest reply on Jan 29, 2015 12:54 PM by DiegoV_Intel

    Ethernet access Fail ?

    Galileo_Dokaebi

      [SOURCE]

       

       

      /*

        Web client

       

      This sketch connects to a website (http://www.google.com)

      using an Arduino Wiznet Ethernet shield.

       

      Circuit:

      * Ethernet shield attached to pins 10, 11, 12, 13

       

      created 18 Dec 2009

      by David A. Mellis

      modified 9 Apr 2012

      by Tom Igoe, based on work by Adrian McEwen

       

      */

       

       

      #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[] = { 0x98, 0x4F, 0xEE, 0x03, 0x02, 0x52 };

      // if you don't want to use DNS (and reduce your sketch size)

      // use the numeric IP instead of the name for the server:

      IPAddress server(74,125,232,128);  // numeric IP for Google (no DNS)

      //char server[] = "www.google.com";    // name address for Google (using DNS)

      // Set the static IP address to use if the DHCP fails to assign

      IPAddress ip(192,168,0,190);

       

       

      // 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;

       

       

      void setup() {

      // Open serial communications and wait for port to open:

        Serial.begin(9600);

         while (!Serial) {

          ; // wait for serial port to connect. Needed for Leonardo only

        }

        delay(1000);

        // start the Ethernet connection:

         

        if (Ethernet.begin(mac) == 0) {

          Serial.print("Ethernet.begin(mac)=");

          Serial.println(Ethernet.begin(mac));

          Serial.println("Failed to configure Ethernet using DHCP");

          // no point in carrying on, so do nothing forevermore:

          // try to congifure using IP address instead of DHCP:

          Ethernet.begin(mac, ip);

        }

        else {

          Serial.print("Ethernet.begin(mac)=");

          Serial.println(Ethernet.begin(mac));

          Serial.print("Ethernet.localIP()=");

          Serial.println(Ethernet.localIP());

          system("ifconfig &> /dev/ttyGS0");

          Ethernet.begin(mac, ip);

          Serial.print("Ethernet.localIP()=");

          Serial.println(Ethernet.localIP());

          system("ifconfig &> /dev/ttyGS0");

        }

       

          // give the Ethernet shield a second to initialize:

        delay(1000);

       

        Serial.println("connecting...");

        int ret;

        // if you get a connection, report back via serial:

        if (ret = client.connect(server, 80)) {

          Serial.println("connected");

          // Make a HTTP request:

          client.println("GET /search?q=arduino HTTP/1.1");

          client.println("Host: www.google.com");

          client.println("Connection: close");

          client.println();

        }

        else {

          // kf you didn't get a connection to the server:

          Serial.println("connection failed");

          Serial.println(ret);

        }

      }

       

       

      void loop()

      {

        // if there are incoming bytes available

        // from the server, read them and print them:

        if (client.available()) {

          char c = client.read();

          Serial.print(c);

        }

       

       

        // if the server's disconnected, stop the client:

        if (!client.connected()) {

          Serial.println();

          Serial.println("disconnecting.");

          client.stop();

       

       

          // do nothing forevermore:

          while(true);

        }

      }

       

       

       

       

       

       

       

       

       

       

      [Result]

       

       

      Ethernet.begin(mac)=1

      Ethernet.localIP()=255.255.255.255

      lo        Link encap:Local Loopback 

                inet addr:127.0.0.1  Mask:255.0.0.0

                UP LOOPBACK RUNNING  MTU:65536  Metric:1

                RX packets:0 errors:0 dropped:0 overruns:0 frame:0

                TX packets:0 errors:0 dropped:0 overruns:0 carrier:0

                collisions:0 txqueuelen:0

                RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

       

       

      Ethernet.localIP()=192.168.  0.190

      eth0      Link encap:Ethernet  HWaddr 98:4F:EE:03:02:52 

                inet addr:192.168.0.190  Bcast:192.168.0.255  Mask:255.255.255.0

                UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

                RX packets:0 errors:0 dropped:0 overruns:0 frame:0

                TX packets:0 errors:0 dropped:0 overruns:0 carrier:0

                collisions:0 txqueuelen:1000

                RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

                Interrupt:41 Base address:0x8000

       

       

      lo        Link encap:Local Loopback 

                inet addr:127.0.0.1  Mask:255.0.0.0

                UP LOOPBACK RUNNING  MTU:65536  Metric:1

                RX packets:0 errors:0 dropped:0 overruns:0 frame:0

                TX packets:0 errors:0 dropped:0 overruns:0 carrier:0

                collisions:0 txqueuelen:0

                RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

       

       

      connecting...

      connection failed

      0

       

       

      disconnecting.

       

       

       

       

       

       

      What's the problem?  Please, help me.

        • 1. Re: Ethernet access Fail ?
          DiegoV_Intel

          Hi Galileo_Dokaebi,

           

          I have tested the code you attached and I got the same result as you. I noticed the issue happens when you assign the static IP. I commented some lines in the code and it worked. My suggestion is to assign the IP address using DHCP.

           

          I post the edited code below, take a look to it and test it with your board.

           

          #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[] = { 0x98, 0x4F, 0xEE, 0x03, 0x02, 0x52 };
          // if you don't want to use DNS (and reduce your sketch size)
          // use the numeric IP instead of the name for the server:
          IPAddress server(74,125,232,128);  // numeric IP for Google (no DNS)
          //char server[] = "www.google.com";    // name address for Google (using DNS)
          // Set the static IP address to use if the DHCP fails to assign
          IPAddress ip(192,168,0,190);
          
          
          // 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;
          
          
          void setup() {
           // Open serial communications and wait for port to open:
            Serial.begin(9600);
             while (!Serial) {
              ; // wait for serial port to connect. Needed for Leonardo only
            }
            delay(1000);
            // start the Ethernet connection:
              
            if (Ethernet.begin(mac) == 0) {
              Serial.print("Ethernet.begin(mac)=");
              Serial.println(Ethernet.begin(mac));
              Serial.println("Failed to configure Ethernet using DHCP");
              // no point in carrying on, so do nothing forevermore:
              // try to congifure using IP address instead of DHCP:
              Ethernet.begin(mac, ip);
            }
            else {
              Serial.print("Ethernet.begin(mac)=");
              Serial.println(Ethernet.begin(mac));
              Serial.print("Ethernet.localIP()=");
              Serial.println(Ethernet.localIP());
              system("ifconfig &> /dev/ttyGS0");
              //Ethernet.begin(mac, ip);
              //Serial.print("Ethernet.localIP()=");
              //Serial.println(Ethernet.localIP());
              //system("ifconfig &> /dev/ttyGS0");
            }
            
            //system("ifconfig &> /dev/ttyGS0");
            // give the Ethernet shield a second to initialize:
            delay(1000);
            
            Serial.println("connecting...");
            int ret;
            ret = client.connect(server, 80);
            // if you get a connection, report back via serial:
            if (ret) {
              Serial.println("connected");
              // Make a HTTP request:
              client.println("GET /search?q=arduino HTTP/1.1");
              client.println("Host: www.google.com");
              client.println("Connection: close");
              client.println();
            } 
            else {
              // kf you didn't get a connection to the server:
              Serial.println("connection failed");
              Serial.println(ret);
            }
          }
          
          
          void loop()
          {
            // if there are incoming bytes available 
            // from the server, read them and print them:
            if (client.available()) {
              char c = client.read();
              Serial.print(c);
            }
          
          
            // if the server's disconnected, stop the client:
            if (!client.connected()) {
              Serial.println();
              Serial.println("disconnecting.");
              client.stop();
          
          
              // do nothing forevermore:
              while(true);
            }
          }
          

           

           

           

          Regards,

          Diego.

          • 2. Re: Ethernet access Fail ?
            Galileo_Dokaebi

            I have tested new code you attached and I got the same result.

            And, I have tested with new IPtime product at the other place. But, I got the same result.

             

            I can do no more to it.

            I want to know if my board is bad or not.

            Also, I want to know if this firmware has some problem.



            • 3. Re: Ethernet access Fail ?
              Galileo_Dokaebi

              I solved this problem.

              ----------------------------------

               

              void setup() {

               

                system("ifup eth0");  

                delay(5000);

               

              ~~

              ~~

              }

               

              --------------------------------------

               

              DHCP OK and Google OK.

              But, I don't know the reason.

              • 4. Re: Ethernet access Fail ?
                DiegoV_Intel

                Hi Galileo_Dokaebi,

                 

                The Ethernet interface was down, I mean, the network interface wasn't initialized properly. Sometimes I get this behavior when I plug the Ethernet cable after the board boots, or after I change the network settings, like the IP address.

                 

                Probably, due to you are changing the IP address in the code, the interface should be initialized again after the change is made using the ifup eth0 command.

                 

                That is the reason I can think.

                 

                Regards,

                Diego.