Hardware Reference
In-Depth Information
Read the PHP Script Using
the Microcontroller
you're writing that program in Processing, in Arduino, or in
another language on another microcontroller, the steps are
still the same:
Next, it's time to connect to the PHP script through the
Net using the Ethernet module. This time, you'll use the
shield as a client, not a server. Before you start program-
ming, plan the sequence of messages. Using the Ethernet
module as a network client is very similar to using Pro-
cessing as a network client. In both cases, you have to
know the correct sequence of messages to send and how
the responses will be formatted. You also have to write a
program to manage the exchange of messages. Whether
1. Open a connection to the web server.
2. Send an HTTP GET request.
3. Wait for a response.
4. Process the response.
5. Wait an appropriate interval and do it all again.
Attempt to connect
to server
Figure 4-11
A flowchart of the Arduino program
for making and processing an HTTP
GET request.
Connected to
server?
no
yes
Make HTTP
request
enough time passed
since last connect
attempt?
"Air Quality:" in
response?
no
yes
yes
Parse AQI value
from response
Map result to
meter's range
Set meter
Figure 4-11 is a flowchart of what happens in the microcon-
troller program. The major decisions (if statements in your
code) are marked by diamonds; the methods are marked
by rectangles. Laying out the whole program in a flowchart
like this will help you keep track of what's going on at any
given point. It also helps you to see what methods depend
on a particular condition being true or not.
This program will check the PHP script every two minutes.
If there's a new value for the air quality, it'll read it and set
the meter. If it can't get a connection, it will try again two
minutes later. Because it's a client and not a server, there's
no web interface to the project, only the meter.
TextFinder Library
For this sketch, you're going to need Michael Margolis'
TextFinder library for Arduino. Download it from www.
arduino.cc/playground/Code/TextFinder , unzip it, and save
the TextFinder folder to the libraries folder of your Arduino
sketches directory (the default location is Documents/
Arduino/libraries/ on OS X, My Documents\Arduino\libraries\
on Windows 7, and ~/Documents/Arduino/libraries/ on
Ubuntu Linux. If the libraries directory doesn't exist, create
The circuit for this project also uses LEDs to keep track
of the state of the program. LEDs attached to I/O pins
will indicate the state. There's an LED to indicate that it's
connected, another to indicate that it's disconnected, a
third to indicate if it got a valid reading, and a fourth to
indicate that the microcontroller is resetting.
 
Search WWH ::




Custom Search