Arduino Due mit Ethernet & SD - Shield

Um ein kombiniertes Ethernet & SD-Karten Shield mit dem Arduino zu betreiben müssen beide Controller Ethernet sowie auch SD-Karte über die SPI angesprochen werden. Damit dies ohne Probleme funktioniert müssen die Shields aktiviert bzw. deaktiviert werden.

Ansprechen der SD-Karte und des Ethernetmoduls

Ein reines Ethernet-Shield kann mit den Arduino-Beispielen problemlos in Betrieb genommen werden. Auch beim reinen SD-Karten Shield sind keine Probleme zu erwarten. Wird ein komnbiniertes Shield verwendet, das sowohl einen Ethernet- wie auch einen SD-Karten Baustein mitbringt, müssen die Bausteine im Quellcode dynamisch aktiviert und deaktiviert werden.

Folgender Sketch kann hierfür verwendet werden :


#include <SPI.h>
#include <Ethernet.h>
#include <SD.h>
// set up variables using the SD utility library functions:
Sd2Card card;
SdVolume volume;
SdFile root;
SdFile file;
// Matching SD in Arduino Ethernet shield: pin 4
const int chipSelect = 4;  
// Text to be read from SD card
String text1;// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
static byte arduinoMAC[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
static IPAddress ArduinoIPAddress(192,168,1,230);
EthernetServer telnetServer = EthernetServer(80);// Matching pin to enable SD card and disable Ethernet
void ethEnable()
{
 pinMode(4, OUTPUT);
 digitalWrite(4, HIGH);
 pinMode(10, OUTPUT);
 digitalWrite(10, LOW);
}
// Matching pin to enable Ethernet and disable SD card
void sdEnable()
{
 pinMode(4, OUTPUT);
 digitalWrite(4, LOW);
 pinMode(10, OUTPUT);
 digitalWrite(10, HIGH);
}
// Initialization and reading of SD card
void sdCARD_INIT()
{
   Serial.print("\nInitializing SD card...");
   if (!card.init(SPI_HALF_SPEED, chipSelect)) {
   Serial.println("initialization failed. Things to check:");
   Serial.println("* is a card is inserted?");
   Serial.println("* Is your wiring correct?");
   Serial.println("* did you change the chipSelect pin to match your shield or module?");
   return;
 } else {
  Serial.println("Wiring is correct and a card is present.");
 } // print the type of card
 Serial.print("\nCard type: ");
 switch(card.type()) {
   case SD_CARD_TYPE_SD1:
     Serial.println("SD1");
     break;
   case SD_CARD_TYPE_SD2:
     Serial.println("SD2");
     break;
   case SD_CARD_TYPE_SDHC:
     Serial.println("SDHC");
     break;
   default:
     Serial.println("Unknown");
 } // Opening the 'volume'/'partition' - it should be FAT16 or FAT32
 if (!volume.init(card)) {
   Serial.println("Could not find FAT16/FAT32 partition.\nMake sure you've formatted the card");
   return;
 } // print the type and size of the first FAT-type volume
 uint32_t volumesize;
 Serial.print("\nVolume type is FAT");
 Serial.println(volume.fatType(), DEC);
 Serial.println(); 
 volumesize = volume.blocksPerCluster();    // clusters are collections of blocks
 volumesize *= volume.clusterCount();       // we'll have a lot of clusters
 volumesize *= 512;                            // SD card blocks are always 512 bytes
 Serial.print("Volume size (bytes): ");
 Serial.println(volumesize);
 Serial.print("Volume size (Kbytes): ");
 volumesize /= 1024;
 Serial.println(volumesize);
 Serial.print("Volume size (Mbytes): ");
 volumesize /= 1024;
 Serial.println(volumesize); 
 Serial.println("\nFiles found on the card (name, date and size in bytes): ");
 root.openRoot(volume); 
 // list all files in the card with date and size
 root.ls(LS_R | LS_DATE | LS_SIZE); // reading a file
 if (file.open(root, "PRINT00.TXT", O_READ)) {
   Serial.println("Opened PRINT00.TXT");
 }
 else{
   Serial.println("error reading PRINT00.TXT");
   //error("file.open");
 }
Serial.println(); 
 // printing text inside the file
 int16_t c;
 while ((c = file.read()) > 0)
 {text1 = text1 + char(c);}
 Serial.println();
 Serial.println(text1);
 }void Ethernet_IP_CONFIG()
{
//Changing the IP address for test     
ArduinoIPAddress[0] = 192;     
ArduinoIPAddress[1] = 168;    
ArduinoIPAddress[2] = 0;     
ArduinoIPAddress[3] = 166;     
delay(150);  
Serial.println(ArduinoIPAddress);
     Serial.println("\nNew IP address...Done");
}
void setup()
{
 // Open serial communications:
 Serial.begin(9600);
 // Enable SD card to read text in a file
 sdEnable();
 sdCARD_INIT();
 Ethernet_IP_CONFIG();
// start the Ethernet connection and the server:
 Serial.println("starting the Ethernet connection and the server");
 ethEnable();
 Ethernet.begin(arduinoMAC, ArduinoIPAddress);
 telnetServer.begin();
 Serial.print("server is at ");
 Serial.println(Ethernet.localIP());
}
void loop() {
 // listen for incoming clients
 EthernetClient client = telnetServer.available();
 if (client) {
     Serial.println("new client");
     client.println("HTTP/1.1 200 OK");
     client.println("Content-Type: text/html");
     client.println("Connection: close");
     client.println();
     client.println("<!DOCTYPE HTML>");
     client.println("<html>");
     // add a meta refresh tag, so the browser pulls again every 5 seconds:
     client.println("<meta http-equiv=\"refresh\" content=\"5\">");
     // output the value of each analog input pin
     for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
       int sensorReading = analogRead(analogChannel);
       // The following line was originally the printing of the text "analog channel"
       // Now will be the text inside the file in SD card "canal analogico"
       client.print(text1);
       client.print(analogChannel);
       client.print(" is ");
       client.print(sensorReading);
       client.println("<br />");      
     }
     client.println("</html>");   }
   // give the web browser time to receive the data
   delay(10);
   // close the connection:
   client.stop();
   Serial.println("no client connected");
 }

Der Sketch gibt eine Kurzinfo zur SD-Karte aus und eröffnet anschließend einen Webserver auf der IP-Adresse 192.168.0.166 auf dem Werte der analogen Eingänge ausgegeben werden. Der Sketch kombiniert zwei Beispiele aus der Arduino IDE.

Die Bildergalerie zeigt einen Arduino Due mit aufgesetztem kombiniertem Ethernet- und SD-Shield.

Wir benutzen Cookies

Wir nutzen Cookies auf unserer Website. Einige von ihnen sind essenziell für den Betrieb der Seite, während andere uns helfen, diese Website und die Nutzererfahrung zu verbessern (Tracking Cookies). Sie können selbst entscheiden, ob Sie die Cookies zulassen möchten. Bitte beachten Sie, dass bei einer Ablehnung womöglich nicht mehr alle Funktionalitäten der Seite zur Verfügung stehen.