Hardware Reference
In-Depth Information
We also should not forget to create an instance of the DHT sensor. This is done using the
following line:
DHT dht(DHTPIN, DHTTYPE);
Now, in the setup() function of the sketch, we will try to use DHCP to get an IP ad-
dress for the Ethernet shield using the following code:
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
Ethernet.begin(mac, ip);
}
Still in the setup() function, we also have to start our Ethernet server, and print out the
IP address of the server using the following code:
server.begin();
Serial.print("Server is at ");
Serial.println(Ethernet.localIP());
Finally, we will start the DHT sensor as follows:
dht.begin();
Now, in the loop() part of the sketch, we will perform the temperature and humidity
measurements, as shown in the following code:
float h = dht.readHumidity();
float t = dht.readTemperature();
Convert the measurements to strings using the following code:
String temp = String((int) t);
String hum = String((int) h);
Then, we need to handle the connections that come to the Arduino board, for example,
from a web browser. To do so, we are actually going to create an instance of the Ether-
netClient class every time the server becomes available. As this is a class we already
saw in previous chapters of the topic, this is something we already know how to work
with.
Search WWH ::




Custom Search