ethernet shield conflicting with SD card?

hello,

i got an ethernetshield attached to the arduino with also a I2C LCD connected.
at my current project i do the following:
On a microSD card is a text file and within this text file is written a number (in this case number 6).
this SD card is plugged in the SDcardslot on the ethernetshield.
Also the ethernetshield will connect to a PHP (connected with a database) and i call a GET function that will return me information.
This information is printed on the LCD.
It will reconnect every 5 seconds to check if new data is send.

now my problem:
It works fine, but after like 4 reconnects it gets bugged.
i made it so, that i prints the data on the serial monitor and i get the following:

gebruiker:
6
is ingelogd
connecting...
connected
GET http://lifestyle.daandamhuis.nl/backend/fetchAantalOpen.php?id=6 HTTP/1.0
disconnecting.
Geen nieuw Antwoord
connecting...
connected
GET http://lifestyle.daandamhuis.nl/backend/fetchAantalOpen.php?id=6 HTTP/1.0
disconnecting.
Geen nieuw Antwoord
connecting...
connected
GET http://lifestyle.daandamhuis.nl/backend/fetchAantalOpen.php?id=6 HTTP/1.0
disconnecting.
Geen nieuw Antwoord
connecting...
connected
HTTP/1.0
disconnecting.
Nieuwe Antwoorden

As you can see at the 4th reconnect and reconnects after that will just print HTTP/1.0 and im not receiving what i want to receive.
Now i tested it with the SDcard code removed, and then the problem is fixed.
allthough, thats not the solution.

anyone has any idea how to fix this?
thanks!

//ARDUINO 1.0+ ONLY
//ARDUINO 1.0+ ONLY
#include <LiquidCrystal_I2C.h>
#include <Ethernet.h>
#include <SPI.h>
#include <Wire.h>
#include <SD.h>

////////////////////////////////////////////////////////////////////////
//CONFIGURE
////////////////////////////////////////////////////////////////////////
LiquidCrystal_I2C lcd(0x27,16,2);
byte mac[] = { 
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 
  192, 168, 1, 177 }; 
byte dnss[] = { 
  8, 8, 8, 8 }; 

//Connectie naar de Webserver
EthernetClient client;
char server[] = "lifestyle.daandamhuis.nl";


char inString[32]; // string for incoming serial data
int stringPos = 0; // string index counter
boolean startRead = false; // is reading?

//Controle variabelen voor de Notificaties
int oudAantal;
int nieuwAantal = 1;
int state;
int pageValue;
File usersd;
int userid;
int led1 = 7;
int led2 = 6;
int user;

void setup(){
  Ethernet.begin(mac, ip, dnss);
  Serial.begin(9600);
  lcd.init();
  lcd.backlight();
  lcd.setCursor(0,0);
    pinMode(10, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);

  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    return;
  }  
  // re-open the file for reading{
    
  usersd = SD.open("test.txt");
  if (usersd) {
    // read from the file until there's nothing else in it:
    char data;
    while ((data = usersd.read()) > 0) 
    {
       user = data - '0'; // yes, accumulate the value
      Serial.println("gebruiker:");
      lcd.print("gebruiker:");
      Serial.println(user);
      lcd.print(user);
      lcd.setCursor(0,1);
      Serial.println("is ingelogd");
      lcd.print("is ingelogd");
    }
    // close the file:
    usersd.close();
  }
  else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }

}



void loop(){
  pageValue = connectAndRead(); //connect to the server and read the output

  //pageValue = stringToInt(pageValue);
  nieuwAantal = pageValue;

  if (nieuwAantal == oudAantal) {
    state = 1;
  }

  if (nieuwAantal != oudAantal) {
    state = 2;
  }


  switch(state)
  {
  case 1:
    Serial.println("Geen nieuw Antwoord");
    oudAantal = nieuwAantal;
    functie(nieuwAantal);
    break;


  case 2:
    Serial.println("Nieuwe Antwoorden");
    lcd.backlight();
    oudAantal = nieuwAantal;
    functie(nieuwAantal);
    break;
  }


  //Serial.println(pageValue); //print out the findings.

  delay(5000); //wait 5 seconds before connecting again
}



int connectAndRead(){
  //connect to the server

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

  //port 80 is typical of a www page
  if (client.connect(server, 80)) {
    Serial.println("connected");
    String ConnectionString = "GET http://lifestyle.daandamhuis.nl/backend/fetchAantalOpen.php?id=";
    ConnectionString = ConnectionString + user;
    ConnectionString = ConnectionString + " HTTP/1.0";
    Serial.println(ConnectionString);
    client.println(ConnectionString);
    client.println("Host: lifestyle.daandamhuis.nl");
    client.println();

    //Connected - Read the page
    return readPage(); //go and read the output

  }
  else{
    return 0; //Geen connectie met de server
  }

}

int readPage(){
  //read the page, and capture & return everything between '<' and '>'

  stringPos = 0;
  memset( &inString, 0, 32 ); //clear inString memory

  while(true){

    if (client.available()) {
      char c = client.read();

      if (c == '<' ) { //'<' is our begining character
        startRead = true; //Ready to start reading the part 
      }
      else if(startRead){

        if(c != '>'){ //'>' is our ending character
          inString[stringPos] = c;
          stringPos ++;
        }
        else{
          //got what we need here! We can disconnect now
          startRead = false;
          client.stop();
          client.flush();
          Serial.println("disconnecting.");
          return stringToInt(inString);

        }

      }
    }

  }
}

int stringToInt(String number)
{
  int length = number.length() + 1;
  char arrayOfCharacters[length];

  number.toCharArray(arrayOfCharacters, length);
  arrayOfCharacters[length - 1] = '\0';

  return atoi(arrayOfCharacters);
}

void functie(int getal)
{
  if(getal == 1)
  {
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Er is ");
    lcd.print(pageValue);
    lcd.print(" vraag");
    lcd.setCursor(0,1);
    lcd.print("beantwoord");
    digitalWrite(7, HIGH);
    digitalWrite(8, HIGH);
    delay(3000);
    lcd.noBacklight();
    digitalWrite(7, LOW);
    digitalWrite(8, LOW);
  }
  else if(getal == 0)
  {
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Er zijn ");
    lcd.print("geen");
    lcd.print(" vra");
    lcd.setCursor(0,1);
    lcd.print("gen beantwoord");
    digitalWrite(7, HIGH);
    digitalWrite(8, HIGH);
    delay(3000);
    lcd.noBacklight();
    digitalWrite(7, LOW);
    digitalWrite(8, LOW);
  }
  else
  { 
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Er zijn ");
    lcd.print(pageValue);
    lcd.print(" vragen");
    lcd.setCursor(0,1);
    lcd.print("beantwoord");
    digitalWrite(7, HIGH);
    digitalWrite(8, HIGH);
    delay(3000);
    lcd.noBacklight();
    digitalWrite(7, LOW);
    digitalWrite(8, LOW);
  }
}

It works fine, but after like 4 reconnects it gets bugged.

Coincidentally, there are 4 sockets available in the w5100. Try this code. Leave the SD card in the slot.
http://playground.arduino.cc/Code/WebClient
Does it also fail after 4 requests?

If you are using a server that uses virtual domain hosting, you might need to send the Host in the request header.

hello and i'm back from the holidays!

ive been trying to work on the code surferTim posted, but because i don't fully understand the code and i'm not really good in putting my code (and someone else his code) together i need help.
i get the following error:
sketch_jan06b.cpp: In function 'void loop()':
sketch_jan06b:7: error: too few arguments to function 'int postPage(char*, char*, char*)'
sketch_jan06b:73: error: at this point in file

code:

/*
   Web client sketch for IDE v1.0.1 and w5100/w5200
   Uses POST method.
   Posted November 2012 by SurferTim
*/
#include <LiquidCrystal_I2C.h>
#include <SPI.h>
#include <Ethernet.h>
#include <Wire.h>
#include <SD.h>

LiquidCrystal_I2C lcd(0x27,16,2);
byte mac[] = {  
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

//Change to your server domain
char serverName[] = "lifestyle.daandamhuis.nl";

// change to the page on that server
char pageName[] = "/fetchAantalOpen.php";

EthernetClient client;
int totalCount = 0; 
int loopCount = 0;
int stringPos = 0; 
int oudAantal;
int nieuwAantal = 1;
int state;
int pageValue;
File usersd;
int userid;
int led1 = 7;
int led2 = 6;
int user;
// insure params is big enough to hold your variables
char params[32];
char inString[32]; // string for incoming serial data
boolean startRead = false; // is reading?

void setup() {
  Serial.begin(9600);

  // disable SD SPI
  pinMode(4,OUTPUT);
  digitalWrite(4,HIGH);

  Serial.print("Starting ethernet...");
  if(!Ethernet.begin(mac)) Serial.println("failed");
  else Serial.println(Ethernet.localIP());

  delay(2000);
  Serial.println("Ready");
}

void loop()
{
  if(loopCount < 30)
  {
    delay(1000);
  }
  else
  {
    loopCount = 0;
    // params must be url encoded.
    sprintf(params,"temp1=%i",totalCount);     
    if(!postPage(serverName,pageName,params)) Serial.print("Fail ");
    else Serial.print("Pass ");
    totalCount++;
    Serial.println(totalCount,DEC);
  }    

  loopCount++;
  
   pageValue = postPage(); //connect to the server

 //pageValue = stringToInt(pageValue);
 nieuwAantal = pageValue;

 if (nieuwAantal == oudAantal) {
   state = 1;
 }

 if (nieuwAantal != oudAantal) {
   state = 2;
 }


 switch(state)
 {
 case 1:
   Serial.println("Geen nieuw Antwoord");
   oudAantal = nieuwAantal;
   functie(nieuwAantal);
   break;


 case 2:
   Serial.println("Nieuwe Antwoorden");
   lcd.backlight();
   oudAantal = nieuwAantal;
   functie(nieuwAantal);
   break;
 }


 //Serial.println(pageValue); //print out the findings

 delay(5000); //wait 5 seconds before connecting again
}


int postPage(char* domainBuffer,char* page,char* thisData)
{
  int inChar;
  char outBuf[64];

  Serial.print("connecting...");

  if(client.connect(domainBuffer,80))
  {
    Serial.println("connected");

    // send the header
    sprintf(outBuf,"POST %s HTTP/1.1",page);
    client.println(outBuf);
    sprintf(outBuf,"Host: %s",domainBuffer);
    client.println(outBuf);
    client.println("Connection: close\r\nContent-Type: application/x-www-form-urlencoded");
    sprintf(outBuf,"Content-Length: %u\r\n",strlen(thisData));
    client.println(outBuf);

    // send the body (variables)
    client.print(thisData);
    
    Serial.println("connected");
    String ConnectionString = "GET http://lifestyle.daandamhuis.nl/backend/fetchAantalOpen.php?id=1";
    Serial.println(ConnectionString);
    client.println(ConnectionString);
    client.println("Host: lifestyle.daandamhuis.nl");
    client.println();
    return readPage();
  } 
  else
  {
    Serial.println("failed");
    return 0;
  }
   Serial.println();
  Serial.println("disconnecting.");
  client.stop();
  return 1;
}

int readPage(){
  //read the page, and capture & return everything between '<' and '>'
  int connectLoop = 0;
  stringPos = 0;
  memset( &inString, 0, 32 ); //clear inString memory

  while(true){

    if (client.available()) {
      char c = client.read();

      if (c == '<' ) { //'<' is our begining character
        startRead = true; //Ready to start reading the part 
        connectLoop = 0;
      }
      else if(startRead){

        if(c != '>'){ //'>' is our ending character
          inString[stringPos] = c;
          stringPos ++;
        }
        
        else{
          //got what we need here! We can disconnect now
          startRead = false;
          client.stop();
          client.flush();
          Serial.println("disconnecting.");
          return stringToInt(inString);

        }
        

      }
      delay(1);
    connectLoop++;
    if(connectLoop > 10000)
    {
      Serial.println();
      Serial.println("Timeout");
      client.stop();
    }
    }

  }
}
int stringToInt(String number)
{
  int length = number.length() + 1;
  char arrayOfCharacters[length];

  number.toCharArray(arrayOfCharacters, length);
  arrayOfCharacters[length - 1] = '\0';

  return atoi(arrayOfCharacters);
}
void functie(int getal)
{
  if(getal == 1)
  {
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Er is ");
    lcd.print(pageValue);
    lcd.print(" vraag");
    lcd.setCursor(0,1);
    lcd.print("beantwoord");
    digitalWrite(7, HIGH);
    digitalWrite(8, HIGH);
    delay(3000);
    lcd.noBacklight();
    digitalWrite(7, LOW);
    digitalWrite(8, LOW);
  }
  else if(getal == 0)
  {
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Er zijn ");
    lcd.print("geen");
    lcd.print(" vra");
    lcd.setCursor(0,1);
    lcd.print("gen beantwoord");
    digitalWrite(7, HIGH);
    digitalWrite(8, HIGH);
    delay(3000);
    lcd.noBacklight();
    digitalWrite(7, LOW);
    digitalWrite(8, LOW);
  }
  else
  { 
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Er zijn ");
    lcd.print(pageValue);
    lcd.print(" vragen");
    lcd.setCursor(0,1);
    lcd.print("beantwoord");
    digitalWrite(7, HIGH);
    digitalWrite(8, HIGH);
    delay(3000);
    lcd.noBacklight();
    digitalWrite(7, LOW);
    digitalWrite(8, LOW);
  }
}

Simple test code for downloading files from the SD card to a browser.

//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);
  digitalWrite(10,HIGH);

  //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) {
            //Serial.println("test.txt:");
            // read from the file until there's nothing else in it:
            while (myFile.available()) {
              client.write(myFile.read());
            }
            // close the file:
            myFile.close();

          }
            delay(1);
            //stopping client
            client.stop();
            readString="";
          //}
        }
      }
    }
  } 
}
    if(!postPage(serverName,pageName,params)) Serial.print("Fail ");
   pageValue = postPage(); //connect to the server

So, how many arguments does postPage() take?

A clue is here:

int postPage(char* domainBuffer,char* page,char* thisData)

Uno, dos, tres...

Simple test code for downloading files from the SD card to a browser.

What the f**k does that have to do with the previous post?

What the f**k does that have to do with the previous post?

I'm just posting working code for the OP that doesn't crash after four downloads. So "catfish", where is your working code. :wink:

ok i kinda went my own way of finding out how it works.
and, i found the solution.
for those interested, here is my code (the comments are in dutch though):

//ARDUINO 1.0+ ONLY
//ARDUINO 1.0+ ONLY
#include <LiquidCrystal_I2C.h>
#include <Ethernet.h>
#include <SPI.h>
#include <Wire.h>
#include <SD.h>

////////////////////////////////////////////////////////////////////////
//CONFIGURE
////////////////////////////////////////////////////////////////////////
LiquidCrystal_I2C lcd(0x27,16,2);
byte mac[] = { 
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 
  192, 168, 1, 177 }; 
byte dnss[] = { 
  8, 8, 8, 8 }; 
String ConnectionString;
//Connectie naar de Webserver
EthernetClient client;
char server[] = "lifestyle.daandamhuis.nl";


char inString[32]; // string for incoming serial data
int stringPos = 0; // string index counter
boolean startRead = false; // is reading?

//Controle variabelen voor de Notificaties
int oudAantal;
int nieuwAantal = 1;
int state;
int pageValue;
File usersd;
int userid;
int led1 = 7;
int led2 = 6;
int user;

void setup(){
 pinMode(4,OUTPUT); //zet SD op output
 digitalWrite(4,HIGH); //pin 4 (pin voor de SD) word HIGH gezet. HIGH = SD card will ignore SPI traffic
  Ethernet.begin(mac, ip, dnss); //stop de gegevens erin die nodig zijn voor connecten.
  Serial.begin(9600);
  lcd.init(); //initialiseer de lcd.
  lcd.backlight(); // ze de backlight aan.
  lcd.setCursor(0,0); //zet de positie van de lcd op het begin
    pinMode(10, OUTPUT); //zet ethernet op output
  pinMode(6, OUTPUT); //voor led1
  pinMode(7, OUTPUT); //voor led2

//als het iets anders uitleest dan pin 4, dan is de initialisatie fout gegaan.
  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    return;
  }  
  
  // open de file
  usersd = SD.open("test.txt");
  //als file geopend is
  if (usersd) {
    // lees van de file totdat er niks meer is.
    char data;
    while ((data = usersd.read()) > 0) 
    {
      //als er iets word gelezen, zet de ontvangen gegevens in een variabele 
       user = data - '0'; // zet de gelezen character(s) om naar decimaal getal
      Serial.println("gebruiker:");
      lcd.print("gebruiker:");
      Serial.println(user);
      lcd.print(user);
      lcd.setCursor(0,1);
      Serial.println("is ingelogd");
      lcd.print("is ingelogd");
    }
    // close de file:
    usersd.close();
  }
 else 
  {
    // als de file niet is geopend, dan geef een error weer:
    Serial.println("error opening test.txt");
    
  }
}



void loop(){
  pageValue = connectAndRead(); //connect naar de server en lees de output. 

  nieuwAantal = pageValue; //de output word opgeslagen

//als het nieuwaantal gelijk is aan het oude, dan maak de state 1.
  if (nieuwAantal == oudAantal) {
    state = 1;
  }

//is het ongelijk dan state 2.
  if (nieuwAantal != oudAantal) {
    state = 2;
  }


  switch(state)
  {
    //als state is 1, ga in case 1.
  case 1:
    Serial.println("Geen nieuw Antwoord");
    oudAantal = nieuwAantal;
    functie(nieuwAantal); //een functie onderaan dit document
    break;

//als state is 2, ga in case 2.
  case 2:
    Serial.println("Nieuwe Antwoorden");
    lcd.backlight();
    oudAantal = nieuwAantal;
    functie(nieuwAantal);//een functie onderaan dit document
    break;
  }

  delay(5000); //wacht 5 seconden voordat hij weer gaat connecten
}



int connectAndRead()
{
  //connect to the server
  Serial.println("connecting...");
  digitalWrite(10,LOW); //zeer belangrijk om te doen als je met SD combineerd. dit betekend dat je het lezen vanaf de ethernet aan word gezet (pin 10 is voor de ethernet, LOW betekend dat het aangaat (will now listen to SPI traffic))
  
  if (client.connect(server, 80)) {
    Serial.println("connected");
    ConnectionString = "GET http://lifestyle.daandamhuis.nl/backend/fetchAantalOpen.php?id="; //als je via DNS werkt , zet dan HTTP:// ERVOOR!
    ConnectionString = ConnectionString + user;
    ConnectionString = ConnectionString + " HTTP/1.0";
    Serial.println(ConnectionString);
    client.println(ConnectionString); //stuur de GET op naar de client
    client.println("Host: lifestyle.daandamhuis.nl"); //geef de host mee.
    client.println();

    //Connected - lees de pagina
    return readPage(); //go and read the output

  }
  else{
    Serial.println("not connected");
    return 0; //Geen connectie met de server
  }
 
}

//leest de pagina en ontvangt alles wat tussen de '<' en '>' staat
int readPage(){

  stringPos = 0;
  memset( &inString, 0, 32 ); //clear inString memory

  while(true){

    //als er een client is
    if (client.available()) {
      // lees alles uit
      char c = client.read();

      if (c == '<' ) { //'<' is de beginnende character
        startRead = true; //begin met lezen
      }
      else if(startRead){

        if(c != '>'){ //'>' is de laatste charachter
          inString[stringPos] = c;
          stringPos ++;
        }
        else{
          //we hebben alles, disconnecten return resultaat
          startRead = false;
          client.stop();
          client.flush();//Waits for the transmission of outgoing serial data to complete
          Serial.println("disconnecting.");
          return stringToInt(inString);

        }

      }
    }

  }
}

//functie die een string naar een int verwerkt
int stringToInt(String number)
{
  int length = number.length() + 1;
  char arrayOfCharacters[length];

  number.toCharArray(arrayOfCharacters, length);
  arrayOfCharacters[length - 1] = '\0';

  return atoi(arrayOfCharacters);
}

//Print stukken op lcd uit en zet leds aan/uit 
void functie(int getal)
{
  if(getal == 1)
  {
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Er is ");
    lcd.print(pageValue);
    lcd.print(" vraag");
    lcd.setCursor(0,1);
    lcd.print("beantwoord");
    digitalWrite(7, HIGH);//ff nachecken
    digitalWrite(8, HIGH);//ff nachecken
    delay(3000);
    lcd.noBacklight(); //zorgt ervoor dat de tekst niet op de lcd doorbrand.
    digitalWrite(7, LOW);
    digitalWrite(8, LOW);
  }
  else if(getal == 0)
  {
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Er zijn ");
    lcd.print("geen");
    lcd.print(" vra");
    lcd.setCursor(0,1);
    lcd.print("gen beantwoord");
    digitalWrite(7, HIGH);
    digitalWrite(8, HIGH);
    delay(3000);
    lcd.noBacklight();
    digitalWrite(7, LOW);
    digitalWrite(8, LOW);
  }
  else
  { 
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Er zijn ");
    lcd.print(pageValue);
    lcd.print(" vragen");
    lcd.setCursor(0,1);
    lcd.print("beantwoord");
    digitalWrite(7, HIGH);
    digitalWrite(8, HIGH);
    delay(3000);
    lcd.noBacklight();
    digitalWrite(7, LOW);
    digitalWrite(8, LOW);
  }
}