Hi,
I'm using an ethernet shield 05 on an arduino mega, and I have the nest issue, could someone help me with, please?
- I make a program with a web server which makes some measurements and write one by one in a file, so a client could read these values from a browser. But when I'm trying to write to a file continously, I mean: I opened a file, write on it, close it, and after a while, I repeat the operation. I want it adds lines, one after one. But even using the example, I could make it right. It always overwrite the file and only shows the last measurements done. How can I solve this?
#include <SD.h>
#include <SPI.h>
#include <Ethernet.h>
#define BUFSIZ 100
byte mac[] = { 0xAB, 0xCD, 0xEF, 0xFE, 0xDC, 0xBA };
byte ip[] = { 192,168,1, 2 };
byte gateway[] = { 192,168,1, 1 };
byte subnet[] = { 255, 255, 252, 0 };
Server server(80);
Sd2Card card;
SdVolume volume;
SdFile root, file;
void setup() {
Serial.begin(9600);
pinMode(53, OUTPUT);
digitalWrite(53, HIGH);
card.init(SPI_HALF_SPEED, 4);
volume.init(&card);
root.openRoot(&volume);
Ethernet.begin(mac, ip);
server.begin();
}
void loop() {
ClientController();
delay(10);
PrintFile();
delay(2000);
}
void PrintFile() {
if (file.open(&root, "Probes.txt", FILE_WRITE)) {
file.println("Trying to write several sentences...");
file.close();
}
}
void ClientController() {
char clientline[BUFSIZ];
int index = 0;
Client client = server.available();
if (client) {
boolean current_line_is_blank = true;
index = 0;
while (client.connected()) {
if (client.available()) {
char c = client.read();
if (c != '\n' && c != '\r') {
clientline[index] = c;
index++;
if (index >= BUFSIZ)
index = BUFSIZ -1;
continue;
}
clientline[index] = 0;
Serial.println(clientline);
if (strstr(clientline, "GET / ") != 0) {
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
client.println("<h2>Files:</h2>");
ListFiles(client, LS_SIZE);
}
else if (strstr(clientline, "GET /") != 0) {
char *filename;
filename = clientline + 5; // look after the "GET /" (5 chars)
(strstr(clientline, " HTTP"))[0] = 0;
Serial.println(filename);
if (! (file.open(&root, filename, O_READ))) {
client.println("HTTP/1.1 404 Not Found");
client.println("Content-Type: text/html");
client.println();
client.println("<h2>File Not Found!</h2>");
break;
}
Serial.println("Opened!");
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/plain");
client.println();
int16_t c;
while ((c = file.read()) > 0) {
client.print((char)c);
}
file.close();
}
else {
client.println("HTTP/1.1 404 Not Found");
client.println("Content-Type: text/html");
client.println();
client.println("<h2>File Not Found!</h2>");
}
break;
}
}
delay(1);
client.stop();
}
}
void ListFiles(Client client, uint8_t flags) {
dir_t p;
root.rewind();
client.println("<ul>");
while (root.readDir(p) > 0) {
if (p.name[0] == DIR_NAME_FREE) break;
if (p.name[0] == DIR_NAME_DELETED || p.name[0] == '.') continue;
if (!DIR_IS_FILE_OR_SUBDIR(&p)) continue;
client.print("<li><a href=\"");
for (uint8_t i = 0; i < 11; i++) {
if (p.name == ' ') continue;
if ( i == 8 ) {
client.print('.');
}
client.print(p.name);
}
client.print("\">");
for (uint8_t i = 0; i < 11; i++) {
if (p.name == ' ') continue;
if ( i == 8 ) {
client.print('.');
}
client.print(p.name);
}
client.print("</a>");
if (DIR_IS_SUBDIR(&p)) {
client.print('/');
}
if (flags & LS_DATE) {
root.printFatDate(p.lastWriteDate);
client.print(' ');
root.printFatTime(p.lastWriteTime);
}
if (!DIR_IS_SUBDIR(&p) && (flags & LS_SIZE)) {
client.print(' ');
client.print(p.fileSize);
}
client.println("</li>");
}
client.println("</ul>");
}