user3674172
user3674172

Reputation: 9

Arduino ethernet shield not working

I have an arduino ethernet shield and I am trying to run the "Webserver" example on it. I have the shield sitting on top of the arduino, with the arduino connected to my PC via USB and the shield connected to my PC as well with an RJ45 ethernet cable. I am using my university's wifi network to connect to the internet and do not have access to any routers. So here is my problem: when I type ipconfig on the command line I see that my computer's IP adress is 143.215.98.213. So in the "Webserver" example code given in the arduino IDE, the only change I made is to set the IP adress as: IPAddress ip(143,215,98,2); (I pinged the adress 143.215.98.2, it was not used so it's fine I guess). The Webserver code is supposed to read the analog inputs from the arduino and print it on an html page. When I upload the code to the arduino and type in the adress 143.215.98.2 to my browser, the browser cannot connect to any page. The TX and RX leds do not light up. Also, I tried pinging t143.215.98.2 while the code is running and I get no response (leds on the arduino do not blink either). Here is the webserver example code I am using:

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

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(143,215,98,2); 
//IPAddress ip(128,61,79,1); 
//IPAddress ip(192,168,1,1);

// Initialize the Ethernet server library
// with the IP address and port you want to use 
// (port 80 is default for HTTP):
EthernetServer server(80);

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
  }


  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);
  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
}


void loop() {
  // listen for incoming clients
  EthernetClient client = server.available();
  if (client) {
    Serial.println("new client");
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        Serial.write(c);
        // 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("Connection: close");  
      client.println("Refresh: 5");  // refresh the page automatically every 5 sec
          client.println();
          client.println("<!DOCTYPE HTML>");
          client.println("<html>");
          // output the value of each analog input pin
          for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
            int sensorReading = analogRead(analogChannel);
            client.print("analog input ");
            client.print(analogChannel);
            client.print(" is ");
            client.print(sensorReading);
            client.println("<br />");       
          }
          client.println("</html>");
          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();
    Serial.println("client disonnected");
  }
}

So what could be the problem ?

Upvotes: 0

Views: 8104

Answers (3)

Miaooaim
Miaooaim

Reputation: 1

A lot of delay, but I write here to help someone with similar problem.

I'm going to be crazy with the Chinese Arduino Ethernet Shield: With some switch it works, with some other it doesn't. Then after a lot of search in the net I've found this: https://reedpaper.wordpress.com/2018/09/17/arduino-ethernet-w5100-how-to-fix-the-wrong-board/ And the problem is gone!

In some Chinese boards there is an error in the 49.9ohm resistors near the ethernet connector. Instead of mounting a 510 array resistor they use a 511 array so instead have 51ohm there are a 510ohm resistors and the connection result is unpredictable.

Upvotes: 0

AMPS
AMPS

Reputation: 309

Check your Ip address properly. Whether your using static or dynamic type.

Goto INTERNET protocol. select Following IP address amd configure as and include in Program it should work

IPAddress ip(192,168,1, 177);
IPAddress gateway(192,168,1, 1);
IPAddress subnet(255, 255, 0, 0);

Upvotes: 0

user1146839
user1146839

Reputation: 1

I think the problem is the IP address. Is it possible to obtain an automated ip from the DHCP? If so, the you should remove the ip address and just start the server with Ethernet.begin(mac).

If it isn't possible to obtain an automated ip.. you can ask the network administrator for free ip sections to us on the network. (If he gives that kind of info away)

Anyway, good luck!

Upvotes: 0

Related Questions