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 ![]()
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"));
}
}