Go Down

Topic: [SLVD + SNIP]Arduino mega + sensor shield + ENC28J60 + sd card == headache..... (Read 479 times) previous topic - next topic

mikeynl

Hi Guys,

I really got stuck, tried dozens of advises, dozens of snippets. And non is giving me the correct result.

I got one working, the other stopped working. Then I get the other working, then the other stopped working.

What i want is to serve a txt file with the ethernet shield. The text file is updated everytime the button is pressed. This part is working, along with lcd, rtc etc etc. But my problem start when I upgrade my part witt the ethernet shield. It doesnt matter if i disconnect everything except sd and ethernet. Maybe the mix together is a no go.

My last try is this:

Code: [Select]
void setup()
{
   
  Serial.begin(9600);

  pinMode(10, OUTPUT);
  digitalWrite(10,LOW);   

  Ethernet.begin(mac, ip);
  server.begin();
 
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
 
  digitalWrite(10,HIGH);

  if (!sd.begin(4))
     Serial.println("SD initialization failed.");
   
  digitalWrite(10,LOW);
     
  Serial.print("server is (still?) at ");
  Serial.println(Ethernet.localIP());   
     
}


Anyone an idea ? or can point me in the right shields that will work together ?

Thank you !

-Mike

mikeynl

OK, i got it working.

The ethernet chip using normal SPI and the SDcard via softpsi.

Dont know if it is the best solution, but it is working.

Working code snippet below

Code: [Select]
#include <SPI.h>
#include <SdFat.h>

#include <UIPEthernet.h>
#define REQ_BUF_SZ   60
EthernetServer server(80);

EthernetClient client;

// MAC address from Ethernet shield sticker under board
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 0, 20);    // IP address, may need to change depending on network
//EthernetServer server(80);        // create a server at port 80
File webFile;                     // the web page file on the SD card
char HTTP_req[REQ_BUF_SZ] = {0};  // buffered HTTP request stored as null terminated string
char req_index = 0;               // index into HTTP_req buffer
unsigned char LED_state[3] = {0}; // stores the states of the LEDs, 1 bit per LED

File statFile;

const uint8_t SOFT_MISO_PIN = 34;
const uint8_t SOFT_MOSI_PIN = 35;
const uint8_t SOFT_SCK_PIN  = 36;
// Chip select may be constant or RAM variable.
const uint8_t SD_CHIP_SELECT_PIN = 11;

// SdFat software SPI template
SdFatSoftSpi<SOFT_MISO_PIN, SOFT_MOSI_PIN, SOFT_SCK_PIN> sd;

// Test file.
SdFile file;

void setup()
{
  Serial.begin(9600);
  while (!Serial) {}
  pinMode(11,OUTPUT);
  digitalWrite(11,LOW);

  Serial.println("Type any character to start");
  while (Serial.read() <= 0) {}

  if (!sd.begin(SD_CHIP_SELECT_PIN))
  {
    sd.initErrorHalt();
  }

 
  Ethernet.begin(mac, ip);  // initialize Ethernet device
  server.begin();           // start to listen for clients
 
  statFile = sd.open("stats.txt");
  if (statFile)
  {
    while (statFile.available()) {
    Serial.write(statFile.read());
  }
  // close the file:
  statFile.close();
  }
  else
  {
    // if the file didn't open, print an error:
    Serial.println("SDCARDDEBUG: error opening stats.txt\n");
  }
}
//-----------------------------------------------------------------------------
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");  // the connection will be closed after completion of the response
          client.println("Refresh: 5");  // refresh the page automatically every 5 sec
          client.println();
          client.println("<!DOCTYPE HTML>");
          client.println("<html>");
     

          statFile = sd.open("stats.txt");        // open web page file
          if (statFile) {
              while(statFile.available()) {
                  client.write(statFile.read()); // send web page to client
              }
              statFile.close();
          }
 
          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");
  }
}

Go Up