Hardware Reference
In-Depth Information
To use the SD card, you will also need to define the chipSelect pin, which is the pin
number 4 on the Ethernet shield. This is done using the following line:
const int chipSelect = 4;
Now, we are going to deal with the NTP server. There are actually several NTP servers
you can use (you can see the IP addresses of many of these servers inside the TimeNTP
example that come with the Time library). You can also set your time zone here. To get
more information about which NTP server you can use and which time zone to set, you
can have a look at the example TimeNTP sketch. This sketch is given as an example in-
side the Time library. For this project, I used the first server available with time zone
number 1, as shown in the following code:
IPAddress timeServer(132, 163, 4, 101);
const int timeZone = 1;
To connect to this server, we also need to define an Ethernet client. This time, we are go-
ing to use a protocol different from the earlier one, which is called UDP. UDP is different
from TCP—it's much simpler and lighter. However, it doesn't guarantee that the data was
correctly delivered, which TCP does. You don't actually need to care about the details of
this protocol, as the Time library handles everything. You can just leave the default UDP
port, as shown in the following code:
EthernetUDP Udp;
unsigned int localPort = 8888;
You also need to create an instance of the DHT sensor as follows:
DHT dht(DHTPIN, DHTTYPE);
Now, in the setup() function of the sketch, you need to define the following code re-
sponsible for getting an IP address for the Ethernet shield:
Serial.begin(9600);
if (Ethernet.begin(mac) == 0) {
// no point in carrying on, so do nothing forevermore:
while (1) {
Serial.println("Failed to configure Ethernet using
DHCP");
delay(10000);
Search WWH ::




Custom Search