Hardware Reference
In-Depth Information
Logging data on an SD card
Let's start with the first topic of this chapter—logging data on a SD card using the integ-
rated SD card slot of the Ethernet shield. What we are going to do is perform some meas-
urements on the Arduino board and log each of these measurements to the SD card in a
simple text file.
However, compared to the project of the Chapter 2 , Sending Data to a Web Server , we
don't actually know the date when the measurement was taken. In the previous chapter, we
simply used the date of the computer, but we don't have this information here. To get the
time on the Arduino board, we are going to use another strategy. We are going to use the
Ethernet shield to connect to an NTP time server, grab the time once when the Arduino
sketch starts, and then use the Time library to track the time from there.
To learn more about the Time library, you can visit the following page on the Arduino
website:
http://playground.arduino.cc/Code/Time
The process of logging data on a SD card starts by including the correct libraries as fol-
lows:
#include "DHT.h"
#include <SD.h>
#include <Time.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
#include <SPI.h>
We also need to define the sensor type and the pin to which the sensor is connected. This is
done with the following lines of code:
#define DHTPIN 7
#define DHTTYPE DHT11
To define the MAC address of the Ethernet shield, use the following line of code:
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0E, 0xFE, 0x40 };
Search WWH ::




Custom Search