Hardware Reference
In-Depth Information
that it will be responsible for setting the state of the digital pin. To allow it to
do this, the pin must be set LOW, and now becomes an input.
The state of the input is read on line 114; as long as the pin is low, this por-
tion of the code repeats (unless a time-out occurs). When the line is set HIGH
by the DHT11, this is where the work starts. First, the current system clock
time is stored in a variable. This is the amount of microseconds the system has
been powered on. A while() loop is created on line 125 and repeats as long as
the pin is at a logical one, or HIGH. When the DHT sets the pin LOW, another
time reading is made, and the difference between the two is calculated. If the
data line was high for 24 μs, it was a logical zero. If the line was high for 70 μs,
it was a logical one. The Arduino can't tell exactly when the pulse started and
when it stopped, but it can guess closely. The easiest thing to do is to split the
values: say, 40 μs. If the pulse were calculated as lasting more than 40 μs, the
DHT11 sent a logical one; otherwise, it sent a logical zero. This is done on line
131. Afterward, the value is masked into the data buffer. Each bit is masked on
each byte, incrementing the bit until the byte is complete and then moving on
to the next byte.
So what is this NAN that is returned if something goes wrong? NAN is
short for Not A Number, and is a good way of returning an error message for
functions that expect numerical returns. If the function returns something that
is not a number, that means there was an error reading one of the return bits.
The DHT11 sends the relative humidity value as a byte, an int is created from
the i rst byte sent to be returned to the main program This int will contain the
relative humidity, directly in percent.
Now, all that is left to do is to create a function to write e-mails. The function
is declared on line 144. On line 149, the WiFi client attempts to connect to an
e-mail server, on port 25. It uses an if statement, but checks for the result of
a function, and not a variable. The exclamation mark in front of the function
means NOT; it will execute the contents of the if statement if the result of the
function is NOT TRUE. If the connection is refused, the function returns false .
Despite what might be thought of the complexity of e-mails, the SMTP pro-
tocol is extremely simple. The user must i rst authenticate, tell the server who
he is, who he wants to contact, and then send the data. That's it! Almost… Some
servers will require authentication, this will be explained below.
This function has all the lines necessary for communication with an SMTP
server. You must specify your own “from e-mail”, the “to e-mail”, and a few
other parameters. Remember the firstEmail variable? This is where it is used.
If firstEmail is true , the sketch is sending its i rst email, so a nice e-mail
should be sent. This is done on line 173. If the firstEmail variable is false , this
isn't the i rst time an e-mail has been sent to the user, and he probably needs a
gentle reminder. On line 177, a random number is generated, and then one of
Search WWH ::




Custom Search