Hardware Reference
In-Depth Information
The program to the right
tests whether you can
control the meter.
Test It
/*
Voltmeter Tester
Uses analogWrite() to control a voltmeter.
Context: Arduino
*/
const int meterPin = 9;
You will need to adjust the range of
pwmValue depending on your meter's
sensitivity. The meters used to design
this project had different ranges. The
meter in the parts list responds to a
0- to 5-volt range, so the preceding
program moves it from its bottom
to its top. The antique meter, on the
other hand, responds to 0 to 3 volts,
so it was necessary to limit the range
of pwmValue to 0-165. When it was at
165, the meter reached its maximum.
Note your meter's minimum and
maximum values. You'll use them later
to scale the air-quality reading to the
meter's range.
int pwmValue = 0; // the value used to set the meter
void setup() {
Serial.begin(9600);
}
void loop() {
// move the meter from lowest to highest values:
for (pwmValue = 0; pwmValue < 255; pwmValue ++) {
analogWrite(meterPin, pwmValue);
Serial.println(pwmValue);
delay(10);
}
delay(1000);
// reset the meter to zero and pause:
analogWrite(meterPin, 0);
delay(1000);
}
Write a PHP Script to Read
the Web Page
Figure 4-10 shows AIRNow's page for New York
City ( http://airnow.gov/?action=airnow.local_
city&zipcode=10003&submit=Go ). AIRNow's page is
formatted well for extracting the data. The AQI number
is clearly shown in text, and if you remove all the HTML
tags, it appears on a line by itself, always following the line
Current Conditions .
Next, you need to get the data from AIRNow's site in a
form the microcontroller can read. The microcontroller
can read in short strings serially, and converting those
ASCII strings to a binary number is fairly simple. Using a
microcontroller to parse through all the text of a web page
is possible, but a bit complicated. However, it's the kind of
task for which PHP was made. The program that follows
reads the AIRNow page, extracts the current air-quality
index (AQI) reading, and makes a simpler summary page
that's easy to read with the microcontroller. The Ethernet
controller is the microcontroller's gateway to the Internet,
allowing it to open a TCP connection to your web host,
where you will install this PHP script.
NOTE: One of the most difficult things about maintaining applica-
tions like this, which scrape data from an existing website, is the
probability that the designers of the website could change the
format of their page. If that happens, your application could stop
working, and you'll need to rewrite your code. In fact, it happened
between the first and second editions of this topic. This is a case
where it's useful to have the PHP script do the scraping of the
remote site. It's more convenient to rewrite the PHP than it is to
reprogram the microcontroller once it's in place.
X
NOTE: You could also run this script on one of the computers on
your local network. As long as the microcontroller is connected
to the same network, you'll be able to connect to it and request
the PHP page. For information on installing PHP or finding a web-
hosting provider that supports PHP, see www.php.net/manual/en/
tutorial.php#tutorial.requirements .
 
Search WWH ::




Custom Search