for the user interface, such as the screen width, the screen height, and the
font size.
After that, we define a few member variables.
to the
Serial
object we use to communicate with the Arduino.
tains the last temperature value we received from the Arduino, and
is true if the value we read was in degrees Celsius. We need the
to define the font we use to output the temperature on the screen.
In the
setup
method, we set the window size and create the font we re going to
use. Then we print out a list of all serial devices available. We initialize our
_arduinoPort
variable with the first one we find, hoping that it s the Arduino.
You could also loop through the list automatically and search for something
that looks like an Arduino port name, but that d be fragile, too.
We call the
clear
method to empty the serial port s buffer. With
make sure that we get notified about serial events only when we ve received
a linefeed character. The call to
serial buffer that doesn t contain an incomplete line of data.
centered horizontally and vertically. Eventually, we print the temperature
using the
method. To make the result look nicer, we use the official Uni-
text
code characters for degrees Celsius (
Now let s implement the business logic of our "Take me to the beach" alarm:
Ethernet/TweetTemperature/TweetTemperature.pde
void
serialEvent(Serial port) {
final String
arduinoData = port.readStringUntil(LINE_FEED);
if
(arduinoData != null) {
final String[]
data = split(trim(arduinoData),
if
(data.length == 2 &&
(data[1].equals("C") || data[1].equals("F")))
{
_isCelsius = data[1].equals("C");
_temperature = float(data[0]);
if
(Float.isNaN(_temperature))
return;
println(_temperature);
int
sleepTime = 5 * 60 * 1000;
if
(_temperature > MAX_WORKING_TEMP) {
tweetAlarm();
Chapter 10. Networking with Arduino
_arduinoPort
readStringUntil
ensures that we start with a fresh
The
draw
method prints the last temperature
we received on the screen. It sets the back-
ground color to white using
text color to black using
font and ensures the text we are printing is
\u2103
) and Fahrenheit (
'
www.it-ebooks.info
170
contains a reference
_temperature
con-
_isCelsius
_font
variable
bufferUntil
, we
background
and the
fill
. Then it sets the
\u2109
).
');
report erratum
discuss
Need help?
Do you have a question about the Arduino Uno and is the answer not in the manual?