ogni volta che uso sd o ethernet devo abilitare/disabilitare il relativo pin??? la libreria non lo fa di suo???
al massimo mi è capitato di trovare esempi in cui dice di settare il 10 come output alto anche dopo l'inizializzazione "Ethernet.begin" per disabilitare l'SPI.
#include <SdFat.h>
#include <SdFatUtil.h>
#include <Ethernet.h>
#include <SPI.h>
const int chipSelect = 4;// pin di inzializzazione sd NON MODIFICARE MAI
const int InputPin = 2; // ingresso da rilevare
const int RefreshTime = 300; //periodo di scansione ingresso [ms]
int StatoAttuale = 0;
int StatoPrecedente = 0;
//SD
Sd2Card card;
SdFat sd;
SdFile myFile;
SdVolume volume;
SdFile root;
//ETHERNET
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 0, 80 };
EthernetServer server(80);
// store error strings in flash to save RAM
#define error(s) error_P(PSTR(s))
void error_P(const char* str) {
PgmPrint("error: ");
SerialPrintln_P(str);
if (card.errorCode()) {
PgmPrint("SD error: ");
Serial.print(card.errorCode(), HEX);
Serial.print(',');
Serial.println(card.errorData(), HEX);
}
while(1);
}
void ListFiles(EthernetClient client, uint8_t flags) {
// This code is just copied from SdFile.cpp in the SDFat library
// and tweaked to print to the client output in html!
dir_t p;
root.rewind();
client.println("<ul>");
while (root.readDir(&p) > 0) {
// done if past last used entry
if (p.name[0] == DIR_NAME_FREE) break;
// skip deleted entry and entries for . and ..
if (p.name[0] == DIR_NAME_DELETED || p.name[0] == '.') continue;
// only list subdirectories and files
if (!DIR_IS_FILE_OR_SUBDIR(&p)) continue;
// print any indent spaces
client.print("<li><a href=\"");
for (uint8_t i = 0; i < 11; i++) {
if (p.name[i] == ' ') continue;
if (i == 8) {
client.print('.');
}
client.print((char)p.name[i]);
}
client.print("\">");
// print file name with possible blank fill
for (uint8_t i = 0; i < 11; i++) {
if (p.name[i] == ' ') continue;
if (i == 8) {
client.print('.');
}
client.print((char)p.name[i]);
}
client.print("</a>");
if (DIR_IS_SUBDIR(&p)) {
client.print('/');
}
// print modify date/time if requested
if (flags & LS_DATE) {
root.printFatDate(p.lastWriteDate);
client.print(' ');
root.printFatTime(p.lastWriteTime);
}
// print size if requested
if (!DIR_IS_SUBDIR(&p) && (flags & LS_SIZE)) {
client.print(' ');
client.print(p.fileSize);
}
client.println("</li>");
}
client.println("</ul>");
}
void write_log(char* NomeFile, String Messaggio, String Data){
//apertura del file
if (!myFile.open(NomeFile, O_RDWR | O_CREAT | O_AT_END)) {
sd.errorHalt("apertura file di log fallita");
}
Serial.print("Scrivendo sul file di log..");
//scrittura sul file
myFile.println(Messaggio+";"+Data);
Serial.println(Messaggio+";"+Data);
//chiusura del file
myFile.close();
}
boolean CheckCambiamento(int NumeroPin){
int i=0;
StatoAttuale = digitalRead(NumeroPin);
if (StatoAttuale != StatoPrecedente) {
if (StatoAttuale == HIGH) {
Serial.println("Rilevato cambiamento di stato:->ALTO");
}
else {
Serial.println("Rilevato cambiamento di stato:->BASSO");
}
StatoPrecedente=StatoAttuale;
return true;
}
StatoPrecedente=StatoAttuale;
return false;
}
// How big our line buffer should be. 100 is plenty!
#define BUFSIZ 100
void webserver()
{
char clientline[BUFSIZ];
int index = 0;
EthernetClient client = server.available();
if (client) {
// an http request ends with a blank line
boolean current_line_is_blank = true;
// reset the input buffer
index = 0;
while (client.connected()) {
if (client.available()) {
char c = client.read();
// If it isn't a new line, add the character to the buffer
if (c != '\n' && c != '\r') {
clientline[index] = c;
index++;
// are we too big for the buffer? start tossing out data
if (index >= BUFSIZ)
index = BUFSIZ -1;
// continue to read more data!
continue;
}
// got a \n or \r new line, which means the string is done
clientline[index] = 0;
// Print it out for debugging
Serial.println("Clientline:");
Serial.println(clientline);
Serial.println("--------------------------");
// Look for substring such as a request to get the root file
if (strstr(clientline, "GET / ") != 0) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
// print all the files, use a helper to keep it clean
client.println("<h2>Files:</h2>");
ListFiles(client, LS_SIZE);
} else if (strstr(clientline, "GET /") != 0) {
// this time no space after the /, so a sub-file!
char *filename;
filename = clientline + 5; // look after the "GET /" (5 chars)
// a little trick, look for the " HTTP/1.1" string and
// turn the first character of the substring into a 0 to clear it out.
(strstr(clientline, " HTTP"))[0] = 0;
// print the file we want
Serial.println("Nome del file che cerco di aprire:");
Serial.println(filename);
if (!myFile.open(&root, filename, O_READ)) {
Serial.println("ho fallito");
client.println("HTTP/1.1 404 Not Found");
client.println("Content-Type: text/html");
client.println();
client.println("<h2>File non trovato 3</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 = myFile.read()) > 0) {
// uncomment the serial to debug (slow!)
//Serial.print((char)c);
client.print((char)c);
}
myFile.close();
} else {
// everything else is a 404
client.println("HTTP/1.1 404 Not Found");
client.println("Content-Type: text/html");
client.println();
client.println("<h2>File non trovato 4!</h2>");
}
break;
}
}
// give the web browser time to receive the data
delay(1);
client.stop();
}
}
void setup() {
delay(300);
pinMode(InputPin, INPUT);
Serial.begin(9600);
Serial.println("Procedure di inizializzazione...");
PgmPrint("RAM libera: ");
Serial.println(FreeRam());
pinMode(10, OUTPUT); // set the SS pin as an output (necessary!)
digitalWrite(10, HIGH); // but turn off the W5100 chip!
if (!card.init(SPI_HALF_SPEED, chipSelect)) error("inizializzazione della sd fallita!");
if (!volume.init(&card)) error("inizializzazione del volume fallita!");
if (!root.openRoot(&volume)) error("apertura root sd fallita");
// list file in root with date and size
root.ls(LS_DATE | LS_SIZE);
if (!sd.begin(chipSelect, SPI_HALF_SPEED)) sd.initErrorHalt();
//apro un file di prova solo per testare che funziona l'sd
if (!myFile.open("Test4.txt", O_RDWR | O_CREAT | O_AT_END)) {
//Serial.println(i);
sd.errorHalt("Apertura del file in scrittura fallito");//concatenare nome file
}
myFile.close();
// Debugging complete, we start the server!
Ethernet.begin(mac, ip);
server.begin();
digitalWrite(10, HIGH);
}
void loop() {
delay(RefreshTime);
Serial.println("Controllando lo stato dell'ingresso...");
if (CheckCambiamento(InputPin)) {
if(StatoAttuale==HIGH){
write_log("23062013.txt","RILEVATA VARIAZIONE->ACCESO"," data di prova");
Serial.println("ALTO_Scrittura su file eseguita");
}
else {
write_log("23062013.txt","RILEVATA VARIAZIONE->SPENTO"," data di prova");
Serial.println("BASSO_Scrittura su file eseguita");
}
}
else {
Serial.println("Nessuna variazione rilevata");
webserver();
}
//Serial.println("Server Eseguito");
}