ENC28J60 ethernet and SD card not playing nice together

Hello,

I’m trying to read a txt file on the web and duplicate it on my SD card. I’ve scoured the internet and have managed to put together an enc28j60 and a MicroSD Card adapter by adding a 220 ohm resistor from the SDcard’s MISO to pin 12 (Adding that info for others searching for that issue, took me forever to find)… I’ve gotten everything to work for the most part… my issue is, I’m trying to write to the SD card in a while loop, it doesn’t work for the first two loops, but works fine thereafter. I feel like I’ve tried a ton of different ideas. Even changing when/where I begin my ethernet, as you’ll see in the code below. Please forgive my ugly coding skills, just a fun hobby for this guy :slight_smile:
Maybe I’m going about this completely wrong. any help would be greatly appreciated. (and any code critiques if you want to)
Thanks!!!

#include <SPI.h>
#include <UIPEthernet.h>
#include <SD.h>
String strdStrg = "";
String txtN ="test";
File myFile;


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

void setup(){
  
  Serial.begin(9600);
  while (!Serial);
  
  Serial.println("Initializing SD card...");
  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    return;
  }
  else{
    Serial.println("initialization done!!!");
  }
  //remove txt file
  SD.remove((txtN + ".txt"));
  if(!SD.exists((txtN + ".txt"))){
    Serial.println("txt file Removed");
  }
  
  sendGET();
  delay(100);
  writeWebInfo();
  delay(500);
  justRead();
}

void loop(){
  // put your main code here, to run repeatedly:
}



void sendGET() {

  //set up ethernet enc28j60
  byte mac[] = {  0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 }; //physical mac address
  char serverName[] = "turanian-hashmarks.000webhostapp.com"; // zoomkat's test web page server
  EthernetClient client;

  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    while(true);
  }
  
  if (client.connect(serverName, 80)) {  //starts client connection, checks for connection
    Serial.println("connected.....");
    client.println("GET /answers.txt HTTP/1.1"); //download text
    client.println("Host: turanian-hashmarks.000webhostapp.com");
    client.println("Connection: close");  //close 1.1 persistent connection 
    client.println(); //end of get request
  }
  else {
    Serial.println("connection failed"); //error message if no client connect
  }

  //store txt file into string
  while(client.connected() && !client.available()) delay(1); //waits for data
  while(client.connected() || client.available()) { //connected or data available
    char c = client.read(); //gets byte from ethernet buffer
    //Serial.print(c);
    if( c!= '\r'){
      strdStrg += c;
    }
    else{
      strdStrg = "";
    }
  }
  client.stop();
  Serial.println();
  Serial.println("disconnected.");
  Serial.println();
  //Serial.println(strdStrg);
}

void writeWebInfo(){
  //write stored string to txt file on SD card
  int i = 0;
  int strdStrg_len = strdStrg.length();
  char thisGuy[strdStrg_len];
  strdStrg.toCharArray(thisGuy, strdStrg_len);
  char * tokstrdStrg = strtok(thisGuy, "\n");
  while(tokstrdStrg != NULL){
    Serial.print("line ");
    Serial.print(i);
    Serial.print(" = ");
    Serial.println(tokstrdStrg);
    myFile = SD.open((txtN + ".txt"), FILE_WRITE);
    if (myFile) {
      Serial.println("writing....");
      myFile.println(tokstrdStrg);
      myFile.close();
    }
    else{
      Serial.println(("error opening " + txtN + ".txt"));
    }
    i++;
    tokstrdStrg = strtok (NULL, "\n");
  }
}


void justRead(){
  //read content of txt file
  Serial.println();
  Serial.println();
  myFile = SD.open((txtN + ".txt"));
  if (myFile) {
    Serial.println("opened");
    while (myFile.available()) {
      Serial.write(myFile.read());
    }
    myFile.close();
     Serial.println("closed");
  } else {
    Serial.println(("error opening " + txtN + ".txt"));
  }
}

what modules do you use exactly? and with what Arduino? many SPI modules have problems with logic level conversion blocking the SPI bus

Right... I'm using a teensy 3.2, this SD module, and this enc28j60

thanks

wesman007:
Right… I’m using a teensy 3.2, this SD module, and this enc28j60

thanks

the SD module is the problem. MISO goes over level conversion chip. can you read the type of the chip? the one with 14 legs. is it 74HC…?

Here is what's on the chip...

LVC125A AK830 UnN04 28C

wesman007:
Here is what’s on the chip…

LVC125A
AK830
UnN04
28C

so it is a 3-state buffer so if they wired it right, than it should work. but I wonder how they wire it to convert 5 V to 3.3 V on and from 3.3 to 5 V.
your Teensy 3.2 is a 3.3 V device so don’t use a 5 V conversion adapter for SD card.

The vcc is connected to the 3.3v on the teensy

wesman007: The vcc is connected to the 3.3v on the teensy

that is not right, because VCC goes to the power conversion chip on the SD module, which expects 5 V input. you use a 5 V adapter module for the 3.3 V SD card.