Pages: [1]   Go Down
Author Topic: Problems using ethernet and SD card  (Read 1384 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 33
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi I'm getting in twist switching between talking to the SD Card and the W5100 chip. This simple sketch is supposed to get some data from the Analog inputs and write it to a file on the card AND output it to a web client. I thought that the libary functions were supposed to handle the CS/SS lines but I cant get the thing to work. I've even thrown in some 'digitalWrite(ChipSelectPin,HIGH); or LOW lines to force the issue. But I just get Garbage scrolling across the serial screen and nothing over the intranet. Individually the two halves work. If I take out the Client.print bit then I can write to the card and visa versa send info to the client if datafile.print part is zapped.

This is really annoying as I want to be able to hold most of the web page info on the SD card as well as a datafile using unfo from the analoginputs. The final pages will be a mix of static HTML selective reads of the data and some NTP info gleaned by UDP. I've looked at the CAT on the MAT bit of Tom Igoe's 'Making Things Talk' several times but I'm dammed if I can see why my sketch doesn't behave.

* Datalogger2.ino (3.59 KB - downloaded 13 times.)
Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 147
Posts: 6038
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You should use the chipSelect variable (pin 4) since you went to all that trouble. Pin 10 is the ethernet SS pin.
Code:
  digitalWrite(ethrSelect, HIGH); // Master mode
  Serial.print("Initializing SD card...");

 if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    return;
  }
  Serial.println("card initialized.");

This should be under another part of the forum, like networking or storage.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 33
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Switching the SD line 'if (!SD.begin(...'  to pin 10 was a deliberate act not a typo and is an indication of how frustrated Ive got with the thing!

The sketch started off with the right line 'if (!SD.begin(chipSelect)) {... but that did not work!!!
even forcing the chip selects on and off, which ought to be handled by '<Ethernet.h>  and ,SD.h>.
Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 147
Posts: 6038
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
The sketch started off with the right line 'if (!SD.begin(chipSelect)) {... but that did not work!!!
even forcing the chip selects on and off, which ought to be handled by <Ethernet.h>  and <SD.h>.
Once the two interfaces are set up, the libraries do handle the SS pins. You do not need to manipulate the SS pins after both begin() functions are complete. It is up to you to avoid data collisions on the SPI bus until then.

edit: Does this work for you?
Code:
#include <SD.h>
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

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

  // disable w5100 while setting up SD
  pinMode(10,OUTPUT);
  digitalWrite(10,HIGH);

  Serial.print("Initializing SD card...");
  if (!SD.begin(4)) Serial.println("failed");
  else Serial.println("OK");
  // the SD.begin(4) function returns with its SPI disabled, so nothing needs to be done

  // set up w5100 with dhcp
  Serial.print("Initializing ethernet...");
  if(!Ethernet.begin(mac)) Serial.println("failed");
  else Serial.println("ok");

  // the Ethernet.begin(mac) function returns with its SPI enabled, so disable it
  digitalWrite(10,HIGH);
}

void loop() {
}
« Last Edit: June 24, 2012, 07:53:41 am by SurferTim » Logged

Pages: [1]   Go Up
Jump to: