I need to implement a xml server, which takes some information from SD card, actually contents of 48 files. I understand that I need to switch between sd and Ethernet and I tried every suggestion I could find in here, but I still can accomplish it. Could you please make me a suggestion?(By the way I’m using an Arduino Ethernet Board)
#include <SPI.h>
#include <Ethernet.h>
#include <SD.h>
File myFile;
boolean state[48];
char ch[3];
EthernetServer server2(80);
void setup(){
for(int i = 0;i<48;i++)
state[i] = false;
Serial.begin(9600);
uint8_t mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x51, 0x06 };
uint8_t ip[] = { 192, 168, 1, 7 };
uint8_t gateway[] = { 192, 168, 1, 255 };
uint8_t subnet[] = { 255, 255, 255, 0 };
pinMode(10,OUTPUT);
digitalWrite(10,HIGH);
if(!SD.begin(4));
return;
Ethernet.begin(mac, ip,gateway,subnet);
digitalWrite(10,HIGH);
delay(2000);
server2.begin();
}
void loop(){
EthernetClient client2 = server2.available();
if(client2){
boolean currentLineIsBlank = true;
while (client2.connected()) {
if (client2.available()) {
char c = client2.read();
if (c == '\n' && currentLineIsBlank) {
client2.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?><Devices>");
for(int i = 0; i<48; i++){
String(i).toCharArray(ch,3);
myFile = SD.open(ch);
if (myFile) {
client2.print("<Device id=\"");
client2.print(ch);
client2.print("\">");
client2.print("<name>");
String s = " ";
//Serial.println(ch);
// read from the file until there's nothing else in it:
while (myFile.available()) {
s += (char)myFile.read();
}
client2.print(s);
client2.print("</name>");
client2.print("<state>");
client2.print(state[i]);
client2.print("</state>");
client2.println("</Device>");
myFile.close();
s = " ";
//delay(10);
} else {
// if the file didn't open, print an error:
Serial.println("error opening file");
}
}
client2.println("</Devices>");
//client2.println("
");
//client2.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:
client2.stop();
//Serial.println("client disonnected");
}
}
SPI can connect multi-divices(chips),but can communicate with one of them by setting the corresponding chip select pin to LOW and the others set to HIGH.So if you want use SD card, set Pin 4 to LOW and the other to HIGH, and suggest u to set it to HIGH at once to release it.
SurferTime, there are just one line of string in the files. Without ethernet connection I can read them very well. Also this format Ethernet.begin(mac, ip,gateway,subnet); is working, I tested and used it other places.
sadoo:
SurferTime, there are just one line of string in the files. Without ethernet connection I can read them very well. Also this format Ethernet.begin(mac, ip,gateway,subnet); is working, I tested and used it other places.
That will not work outside your localnet. If you specify the gateway as 255.255.255.0, then nothing will be routed outside your localnet. Don't say you weren't warned.
This applies to IDE V1.0.1 and higher. Anything below that has its own bug in the ethernet library. What IDE version are you using?
Have you tried using the devices separately? Here is my server code in the playground. It has the SD startup commented out, but you should be able to understand how to use it. http://playground.arduino.cc/Code/WebServerST
Uncomment the SD.begin() part and try that. Does that work?
What do you mean by the "SD read related part"? There is no SD.read() function calls in the code in the playground. It only initializes the SD. Do you mean the SD.begin(4) call?
Sounds like you have an SRAM problem. I did not notice that when I posted that code in the playground, I did not include the F() function to reduce SRAM usage. I changed it to include those now.
That is not my code. I never use the String variable type in any of my code. You are not reading (or not understanding) what I am trying to tell you. I normally do not quote myself but here goes...
The String variable type is notorious for crashing sketches. A character array would be better until they get the bugs out of String type.