Hardware Reference
In-Depth Information
setup() is declared on line 19. setup() needs to do several things: coni gure
the serial port for debug messages (line 21), set the LED pin correctly and turn
the LED on (line 26), test to see if a WiFi shield is connected (line 30) and attempt
to connect to a wireless network (line 37). It loops until the sketch connects to
the designated network. When it does, the LED is turned off, and a message is
sent to the serial port.
loop() is declared on line 53 and does one simple task. It gets a humidity
reading from the DHT11 (on line 56), prints out the data to the serial port (line
59), and then calculates if the sensor reading is less than the minimum humid-
ity level. If it has, then the plant is thirsty, and the user is warned. It sends out
a message to the serial connection on line 58 and then calls a function: sendE-
mail() . Finally, the variable thirsty is set to true . If the minimum humidity
level has not been reached, the plant is probably happy as it is, and the thirsty
variable is set to false , telling the sketch that all is well. Finally, a delay() tells
the Arduino to wait for one-half an hour before taking another reading.
setup() and loop(), required by all Arduino sketches, have been written, but
two more are required; one of them reads in data from the DHT11 and reports
the humidity level, and the second one sends an e-mail. The i rst function is
getDht11Humidity() . This function is responsible for initiating communications
with the DHT11, requesting data, receiving that data, and parsing part of it. It's
a complicated function, but don't worry; it isn't that hard.
First off, there needs to be some variables to manipulate and hold data from
the sensor, an array named data , and two bytes named mask and idx . To request
a sample from the DHT11, the data line must be pulled low for at least 18 mil-
liseconds and then set high. This is done on line 89 by setting the pin as an
OUTPUT . It is pulled LOW ; then a delay() function waits for 20 ms before setting
the pin HIGH again. The sketch waits for 40 microseconds and then switches the
DHT pin to INPUT . The DHT11 can now transmit data.
First, the DHT coni rms that it has received an order by replying with an
ACK . According to the datasheet, when the DHT11 is ordered to send data,
it i rst responds by i rst driving the data pin low for 80 μS, and then high for
80 μS. It then again pulls the data pin low, ready to send data. This is its way of
acknowledging the order, and informing the microcontroller that it will soon
send data. The sketch waits until the line is set HIGH, and then it waits again
until the line is pulled LOW. This is done on lines 98 and 104. Both portions of
the sketch have a time-out; if 255 cycles have passed, the sketch reports a time
out. The 255 cycles correspond to more than 80 μs, so if the time out occurs,
there was indeed a problem; the ACK wasn't sent.
On line 110, a for loop is created. When the DHT11 has i nished acknowledg-
ing reception, it will send 40 bits of data. This loop repeats 40 times once for
each of the 40 bits the DHT11 should send. First, the pin is set LOW. Remember,
the DHT11 sends and receives on a single wire. Previously, the Arduino had
control of the wire, but when the signal was sent, it also signaled the DHT11
Search WWH ::




Custom Search