Offline
Newbie
Karma: 0
Posts: 10
|
 |
« on: January 08, 2013, 06:16:28 am » |
I'm working on a monitoring project that logs some data to the SD card of the official Ethernet Shield as csv file, I want to show the logs contents on a web page that is produced by the arduino , but when I try to open the csv file for reading after the client is connected to the server , I get an error but when I try to open it on startup its working OK. I have read that I should switch between ethernet SS PIN and SD card SS pin but I cannot manage to make it work !! Is it possible to switch ethernet SS pin while the client is connected and then switch it back after reading from SD card without losing the client connection?? here is my code : #include <SPI.h> #include <Ethernet.h> #include <SD.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; IPAddress ip(192,168,2, 230); EthernetServer server(80);
File myFile;
void setup() { Ethernet.begin(mac, ip); server.begin();
Serial.begin(9600); Serial.print("Initializing SD card..."); pinMode(10, OUTPUT); if (!SD.begin(4)) { Serial.println("initialization failed!"); return; } Serial.println("initialization done."); }
void loop() { EthernetClient client = server.available(); if (client) { boolean current_line_is_blank = true; while (client.connected()) { if (client.available()) { char c = client.read(); if (c == '\n' && current_line_is_blank) { client.println("HTTP/1.1 200 OK"); client.println("Content-Type: text/html"); client.println(); client.println("<h2>CSV Data : </h2>");
// csv file data myFile = SD.open("datalog.csv"); if (myFile) { Serial.println("datalog.csv:"); // read from the file until there's nothing else in it: while (myFile.available()) { client.println(myFile.read()); } myFile.close(); } else { Serial.println("error opening datalog.csv"); // ################### I GOT THIS ERROR }
break; } if (c == '\n') { current_line_is_blank = true; } else if (c != '\r') { current_line_is_blank = false; } } } delay(1); client.stop(); }
}
|
|
|
|
« Last Edit: January 08, 2013, 06:19:13 am by belalsaleh »
|
Logged
|
|
|
|
|
Miramar Beach, Florida
Offline
Faraday Member
Karma: 50
Posts: 3448
|
 |
« Reply #1 on: January 08, 2013, 07:35:06 am » |
Is it possible to switch ethernet SS pin while the client is connected and then switch it back after reading from SD card without losing the client connection?? Yes. I use both for FTP. Here is my code: http://playground.arduino.cc/Code/FTPIt uses two sockets and the SD simultaneously to transfer files to or from my FTP server. The SS pins are handled by the low level libraries of each device. The trick is setting them up. Both devices need the SS line HIGH when leaving the setup() function.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 10
|
 |
« Reply #2 on: January 08, 2013, 10:59:47 am » |
Thank you Tim for your answer , I have changed my code to set them both HIGH before the end of setup().. but still having the same error !
|
|
|
|
|
Logged
|
|
|
|
|
Miramar Beach, Florida
Offline
Faraday Member
Karma: 50
Posts: 3448
|
 |
« Reply #3 on: January 08, 2013, 12:14:33 pm » |
If you can't open the file with SD.open(filename), then there are normally two reasons. 1) Opening a SD file takes a lot of SRAM, and you ran out. 2) The file doesn't exist I get an error but when I try to open it on startup its working OK. Post the code that did work when you opened the file in setup().
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 10
|
 |
« Reply #4 on: January 08, 2013, 12:40:27 pm » |
I'm sure the file exists because the same exact code is working OK on startup(): and the file is only about 21 words .. void setup() { Ethernet.begin(mac, ip); server.begin();
Serial.begin(9600); Serial.print("Initializing SD card..."); pinMode(10, OUTPUT); if (!SD.begin(4)) { Serial.println("initialization failed!"); return; } Serial.println("initialization done.");
myFile = SD.open("datalog.csv"); if (myFile) { Serial.println("datalog.csv:"); // read from the file until there's nothing else in it: while (myFile.available()) { Serial.println(myFile.read());// ################ I GOT THE FILE CONTENTS ON THE SERIAL MONITOR SCREEN } myFile.close(); } else { Serial.println("error opening datalog.csv"); } }
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
God Member
Karma: 37
Posts: 974
Get Bitlash: http://bitlash.net
|
 |
« Reply #5 on: January 08, 2013, 12:46:40 pm » |
still having the same error !
Could you describe the error, please? -br
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 10
|
 |
« Reply #6 on: January 08, 2013, 01:19:34 pm » |
Could you describe the error, please?
-br
I cannot open a file for reading from SD card when a client is connected to a web server on my arduino UNO r3 with Ethernet Shield , the full code on the first post.. Thank you..
|
|
|
|
|
Logged
|
|
|
|
|
Miramar Beach, Florida
Offline
Faraday Member
Karma: 50
Posts: 3448
|
 |
« Reply #7 on: January 08, 2013, 02:44:48 pm » |
I used this as a test, and it worked good. #include <SPI.h> #include <Ethernet.h> #include <SD.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// change to your network settings IPAddress ip( 192,168,2,2 ); IPAddress gateway( 192,168,2,1 ); IPAddress subnet( 255,255,255,0 );
EthernetServer server(80);
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() { EthernetClient client = server.available(); if(client) { boolean currentLineIsBlank = true; boolean currentLineIsGet = true; int tCount = 0; char tBuf[64];
Serial.print("Client request: "); while (client.connected()) { while(client.available()) { char c = client.read();
if(currentLineIsGet && tCount < 63) { tBuf[tCount] = c; tCount++; tBuf[tCount] = 0; }
if (c == '\n' && currentLineIsBlank) { Serial.println(tBuf); Serial.print("POST data: "); while(client.available()) Serial.write(client.read()); Serial.println();
// send a standard http response Serial.println("Sending response"); client.write("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n");
File fh = SD.open("index.htm");
if(fh) { byte clientBuf[64]; int clientCount = 0; while(fh.available()) { clientBuf[clientCount] = fh.read(); clientCount++; if(clientCount > 63) { Serial.println("Packet"); client.write(clientBuf,64); clientCount = 0; } } if(clientCount > 0) client.write(clientBuf,clientCount);
fh.close(); } else Serial.println("file open failed");
client.stop(); } else if (c == '\n') { currentLineIsBlank = true; currentLineIsGet = false; } else if (c != '\r') { currentLineIsBlank = false; } } } Serial.println("done"); } } I uploaded a file to the SD card with the html in it as index.htm. Here is the contents. <html><body> TEST </body></html>
Does this work for you?
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 10
|
 |
« Reply #8 on: January 08, 2013, 03:18:00 pm » |
Does this work for you?
no !!! The web page is blank, and I got "file open failed" error on the serial monitor:"" Starting SD..ok Ready Client request: GET / HTTP/1.1
POST data: Sending response file open failed done
this is driving me crazy !!! I have tried tens of codes with no lock ...
|
|
|
|
|
Logged
|
|
|
|
|
Miramar Beach, Florida
Offline
Faraday Member
Karma: 50
Posts: 3448
|
 |
« Reply #9 on: January 08, 2013, 03:29:45 pm » |
What Arduino are you using? I am testing this on a Mega 2560, and it is showing that code has barely enough SRAM to run on an Uno. Maybe you are running out of SRAM.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 10
|
 |
« Reply #10 on: January 08, 2013, 03:37:52 pm » |
I'm using Arduino Uno Rev3..
can I test the SRAM some how ?
or is there is away to reset the arduino to its "factory settings"?
|
|
|
|
|
Logged
|
|
|
|
|
Miramar Beach, Florida
Offline
Faraday Member
Karma: 50
Posts: 3448
|
 |
« Reply #11 on: January 08, 2013, 03:44:06 pm » |
I include this function code to any sketch I want to check the SRAM. int freeRam() { extern int __heap_start,*__brkval; int v; return (int)&v - (__brkval == 0 ? (int)&__heap_start : (int) __brkval); } Then call it something like this. Serial.print("Free SRAM = "); Serial.println(freeRam());
It does not show 0 when you run out of memory. It will show an unrealistic amount of SRAM remaining. If I run my Mega out of memory, it will return more free SRAM than my Mega has.
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 311
Posts: 35470
Seattle, WA USA
|
 |
« Reply #12 on: January 08, 2013, 04:32:06 pm » |
or is there is away to reset the arduino to its "factory settings"? It is returned to it's "factory settings" every time you upload a new sketch (except for EEPROM, but that has nothing to do with your issue).
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 10
|
 |
« Reply #13 on: January 08, 2013, 04:37:16 pm » |
Thanks you all..
The SRAM is 657 right before the open file failed.. and 813 right before the end of setup()....
|
|
|
|
|
Logged
|
|
|
|
|
Miramar Beach, Florida
Offline
Faraday Member
Karma: 50
Posts: 3448
|
 |
« Reply #14 on: January 08, 2013, 04:41:52 pm » |
That should be pretty close to correct. I figured a bit over 300 remaining as an estimate. If you are not out of memory, I don't know what to tell you. It should work.
|
|
|
|
|
Logged
|
|
|
|
|
|