Can't connect to IP (with Ethernet Shield & SD)?

I have a webserver on my Arduino Uno with the Seeed Studio Ethernet Shield. I have an SD card to host the ‘index.htm’ file. My serial says it finds the file just fine. But when I go to my browser to put it in my Arduino’s IP (192.168.1.222) it never loads.

*I had this working the other day and started adding comments and all of a sudden it doesn’t work now. I feel like I possibly accidentally deleted a line of code or a missing bracket or something…

#include <SPI.h>
#include <EthernetV2_0.h>
#include <SD.h>
#include <Servo.h>

#define W5200_CS  10
#define SDCARD_CS 4

Servo myservo;  // create servo object to control servo

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 192, 168, 1, 222 }; // ip in lan
byte gateway[] = { 192, 168, 1, 1 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
EthernetServer server(80); //server port

File webFile;


String readString;

//////////////////////

void setup() {

  //start Ethernet
  pinMode(10, OUTPUT);
  digitalWrite(10, HIGH);

  //pinMode(SDCARD_CS,OUTPUT);

  myservo.write(0); //set initial servo position if desired
  myservo.attach(9);  //the pin for the servo control
  //digitalWrite(SDCARD_CS,HIGH); //this is for if there is no SD card in slot 
  //(there is an SD card in for now)
  //enable serial data print
  Serial.begin(9600);
  Serial.println("server servo/pin 9 test 1.0"); // so I can keep track of what is loaded

  Serial.println("Initializing SD card...");
  if (!SD.begin(4)) {
    Serial.println("ERROR - SD card initialization failed!");
    return;    // init failed
  }
  Serial.println("SUCCESS - SD card initialized.");
  // check for index.htm file
  if (!SD.exists("index.htm")) {
    Serial.println("ERROR - Can't find index.htm file!");
    return;  // can't find index file
  }
  Serial.println("SUCCESS - Found index.htm file.");

  Ethernet.begin(mac, ip, gateway, subnet);
  server.begin();

}

void loop() {
  // Create a client connection
  EthernetClient client = server.available();
  if (client) {
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();

        Serial.print(c);

        //read char by char HTTP request
        if (readString.length() < 100) {

          //store characters to string
          readString += c;
          //Serial.print(c);
        }
      

        //if HTTP request has ended
        if (c == '\n' && currentLineIsBlank) {

          Serial.println(readString); //print to serial monitor for debuging

  
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println("Connection: close");
          client.println();
          // send web page
          webFile = SD.open("index.htm");        // open fetch web page file
          if (webFile) {
            while (webFile.available()) {
              client.write(webFile.read()); // send web page to client
            }
            webFile.close();
          }
          break;
        }

        if (c == '\n') {
          // last character on line of received text
          // starting new line with next character read
          currentLineIsBlank = true;
        }
        else if (c != '\r') {
          // a text character was received from client
          currentLineIsBlank = false;
        }
      }
    }

    //          client.println("<HTML>");
    //          client.println("<HEAD>");
    //          client.println("<TITLE>Arduino GET test page</TITLE>");
    //          client.println("</HEAD>");
    //          client.println("<BODY>");
    //
    //          client.println("<H1>Fetch! Automatic Dog Feeder</H1>");
    //
    //          client.println("<a href=\"/?feed\"\"><button>FEED ME!</button></a>");
    //          //client.println("<a href=\"/?off\"\"><button>OFF</button></a>");
    //
    //          client.println("</BODY>");
    //          client.println("</HTML>");

    delay(1);
    //stopping client
    client.stop();

    ///////////////////// control arduino pin
    if (readString.indexOf("feed") > 0) //checks for button click
    {
      myservo.write(180);
      delay(1000);
      myservo.write(0);
      Serial.println("Led On");
    }
    //clearing string for next read
    readString = "";

  }
}

THINGS I’VE TRIED:

  • SEVERAL different IP addresses
  • I’ve tried going to the IP on a different computer on the same network - still doesn’t work
  • making sure the file on SD card is spelled correctly - I have a ‘index.htm’

Your Ethernet.begin call is missing the DNS server parameter.

You shouldn't send that file one character per packet. That causes problems on some networks .

Server test code that serves up a large file fairly quickly.

//zoomkat 12/26/12
//SD server test code
//open serial monitor to see what the arduino receives
//address will look like http://192.168.1.102:84 when submited
//for use with W5100 based ethernet shields

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

byte mac[] = { 
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 
  192, 168, 1, 102 }; // ip in lan
byte gateway[] = { 
  192, 168, 1, 1 }; // internet access via router
byte subnet[] = { 
  255, 255, 255, 0 }; //subnet mask
EthernetServer server(84); //server port
String readString; 

//////////////////////

void setup(){

  Serial.begin(9600);

  // disable w5100 while setting up SD
  pinMode(10,OUTPUT);
  digitalWrite(10,HIGH);
  Serial.print("Starting SD..");
  if(!SD.begin(4)) Serial.println("failed");
  else Serial.println("ok");

  Ethernet.begin(mac, ip, gateway, gateway, subnet);
  //delay(2000);
  server.begin();
  Serial.println("Ready");
}

void loop(){
  // Create a client connection
  EthernetClient client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();

        //read char by char HTTP request
        if (readString.length() < 100) {
          //store characters to string 
          readString += c; 
          //Serial.print(c);
        } 
        //if HTTP request has ended
        if (c == '\n') {

          ///////////////
          Serial.println(readString); //print to serial monitor for debuging 

            client.println("HTTP/1.1 200 OK"); //send new page
          //client.println("Content-Type: text/html");
          client.println("Content-Type: image/jpeg");
          //client.println("Content-Type: image/gif");
          //client.println("Content-Type: application/x-javascript");
          //client.println("Content-Type: text");

          client.println();

          //File myFile = SD.open("boom.htm");
          File myFile = SD.open("HYPNO.JPG");
          //File myFile = SD.open("BLUEH_SL.GIF");
          //File myFile = SD.open("SERVOSLD.HTM");

          if (myFile) {

            byte clientBuf[64];
            int clientCount = 0;

            while(myFile.available())
            {
              clientBuf[clientCount] = myFile.read();
              clientCount++;

              if(clientCount > 63)
              {
                // Serial.println("Packet");
                client.write(clientBuf,64);
                clientCount = 0;
              }
            }
            //final <64 byte cleanup packet
            if(clientCount > 0) client.write(clientBuf,clientCount);            
            // close the file:
            myFile.close();
          }
          delay(1);
          //stopping client
          client.stop();
          readString="";
        }
      }
    }
  } 
}

Thank you, zoomkat! That worked wonderfully. I don't know why I couldn't get my code above to work, hmm.

fancyninjas: Thank you, zoomkat! That worked wonderfully. I don't know why I couldn't get my code above to work, hmm.

Look at zoomkat's code.

SurferTim: Your Ethernet.begin call is missing the DNS server parameter.

You shouldn't send that file one character per packet. That causes problems on some networks.