Salve a tutti ho un piccolo problema con il rilevamento della SD su una shield Ethernet su un Arduino Mega 2560.
Premetto che l'arduino è un compatibile della Sainsmart, e la shield una SODIAL(R) Ethernet Shield W5100 comprati su Amazon a prezzi stracciati.
Funziona tutto egregiamente a parte che non riesco a comunicare con la SD, "init failed". Ho provato di tutto ma nulla. Se la stessa sd la monto su un Arduino ETH viene letta perfettamente.
Posto il codice usato.
Perfavore aiutatemi, grazie
#include <SPI.h>
#include <Ethernet.h>
#include <SD.h>
// size of buffer used to capture HTTP requests
#define REQ_BUF_SZ 20
//#define SS_PIN 4
// indirizzo MAC della scheda Ethernet Shield (di solito si lascia questo)
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
// indirizzo ip da assegnare alla scheda Ethernet Shield (modificarlo a piacimento)
byte gateway[] = {192,168,1,7};
byte subnet[] = {255,255,255,0};
//Indirizzo Ip del WebServer
IPAddress ip(192, 168, 1, 179);
IPAddress myDNS(8,8,8,8);
EthernetServer server(80);
File webFile;
char HTTP_req[REQ_BUF_SZ] = {0}; // buffered HTTP request stored as null terminated string
char req_index = 0; // index into HTTP_req buffer
void setup() {
Serial.begin(9600);
while (!Serial) {
;
}
//Necessario per utilizzare la SPI su Mega
pinMode(53, OUTPUT);
digitalWrite(53, HIGH);
//Disable SD
pinMode(4,OUTPUT);
digitalWrite(4,HIGH);
//Disabilito ETH
pinMode(10,OUTPUT);
digitalWrite(10,HIGH);
//Abilito SD
digitalWrite(4,LOW);
if (SD.begin(4)) {
Serial.println("SD inizializzata");
if (!SD.exists("index.htm")) {
Serial.println("ERRORE - File index.htm non trovato!");
//return; // can't find index file
}
}
else
{
Serial.println("SD non inizializzata");
}
Ethernet.begin(mac, ip, myDNS, gateway, subnet);
Serial.print("In ascolto all'indirizzo: ");
//Stampo l'indirizzo ip assegnato alla scheda ethernet
Serial.println(Ethernet.localIP());
//metto in ascolto il servizio sulla porta 80
server.begin();
}
void loop() {
// listen for incoming clients
EthernetClient client = server.available();
if (client) {
Serial.println("new client");
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
// buffer first part of HTTP request in HTTP_req array (string)
// leave last element in array as 0 to null terminate string (REQ_BUF_SZ - 1)
if (req_index < (REQ_BUF_SZ - 1)) {
HTTP_req[req_index] = c; // save HTTP request character
req_index++;
}
Serial.write(c);
if (c == '\n' && currentLineIsBlank) {
if (StrContains(HTTP_req, "GET / ") || StrContains(HTTP_req, "GET /index.htm")) {
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close");
client.println();
webFile = SD.open("index.htm");
break;
}
if (webFile) {
while(webFile.available()) {
client.write(webFile.read()); // send web page to client
}
webFile.close();
}
req_index = 0;
StrClear(HTTP_req, REQ_BUF_SZ);
break;
}
if (c == '\n') {
currentLineIsBlank = true;
} else if (c != '\r') {
currentLineIsBlank = false;
}
}
}
delay(1);
client.stop();
Serial.println("client disconnected");
}
}
// sets every element of str to 0 (clears array)
void StrClear(char *str, char length)
{
for (int i = 0; i < length; i++) {
str[i] = 0;
}
}
// searches for the string sfind in the string str
// returns 1 if string found
// returns 0 if string not found
char StrContains(char *str, char *sfind)
{
char found = 0;
char index = 0;
char len;
len = strlen(str);
if (strlen(sfind) > len) {
return 0;
}
while (index < len) {
if (str[index] == sfind[found]) {
found++;
if (strlen(sfind) == found) {
return 1;
}
}
else {
found = 0;
}
index++;
}
return 0;
}