Hardware Reference
In-Depth Information
Next, it's time to read
from the SD card. To do
this, you'll need to add the SD library.
On the Arduino shields that have an SD
card, the SD Chip Select pin is pin 4. It
varies for other manufacturers' shields,
though, so make sure to check if you're
using a different company's SD shield.
Read It
#include <EEPROM.h>
#include <SD.h>
const int sdChipSelect = 4; // SD card chipSelect
// NOTE: the constants and global variables shown earlier must go here
void setup() {
// initialize serial communication:
Serial.begin(9600);
// initialize the relay output:
pinMode(relayPin, OUTPUT);
if (!SD.begin(sdChipSelect)) {
// if you can't read the SD card, don't go on:
Serial.println(F("initialization failed!"));
}
else {
Serial.println(F("initialization done."));
sendFile("index.htm");
}
}
First, check whether the card is present
using SD.begin() . If it's not, there's not
much point in continuing. If it is, you
can read from it. Add a new method,
sendFile() , which will take an array of
characters as a filename. New lines are
shown in blue.
This new code won't work until you
have a properly formatted MicroSD
card inserted in the shield with a file on
it called index.htm. Put the bare bones
of an HTML document in the file, like
so:
// NOTE: the loop(), readSensor(), and checkThermostat() methods shown
// earlier go here.
<html>
<head>
<title>Hello!</title>
</head>
<body>
Hello!
</body>
</html>
// send the file that was requested:
void sendFile(char thisFile[]) {
String outputString = ""; // a String to get each line of the file
// open the file for reading:
File myFile = SD.open(thisFile);
if (myFile) {
// read from the file until there's nothing else in it:
while (myFile.available()) {
// add the current char to the output string:
char thisChar = myFile.read();
outputString += thisChar;
When you run the sketch at this point,
it will print out the index.htm file in the
Serial Monitor at the beginning, then go
into the temperature reading and relay
control behavior from the previous
page.
// when you get a newline, send out and clear outputString:
if (thisChar == '\n') {
Serial.print(outputString);
outputString = "";
}
}
// close the file:
myFile.close();
}
else {
// if the file didn't open:
Serial.print("I couldn't open the file.");
}
}
 
Search WWH ::




Custom Search