Hardware Reference
In-Depth Information
The sendData() method sends
the current readings to the server
using Processing's loadStrings()
method. loadStrings() does a basic
HTTP GET request, and returns
whatever the server sends it.
8
void sendData(String thisData) {
// if there's data to send
if (thisData != null) {
// URL-encode the data and URL:
String sendString = formatData(url + thisData);
//send the data via HTTP GET:
String[] result = loadStrings(sendString);
// clear currentReadings to get more:
String currentReadings = "";
}
}
8
URL strings need to be formatted
cleanly (no spaces, no newlines or
carriage returns, and so forth), so
sendData() calls formatData(), which
converts spaces, newlines, and returns
into their HTTP-safe equivalents.
String formatData(String thisString) {
// convert newlines, carriage returns, and
// spaces to HTML-safe equivalent:
String result = thisString.replaceAll(" ", "%20");
result = result.replaceAll("\n", "%0A");
result = result.replaceAll("\r", "%0D");
return result;
}
The final method in the main
sketch is getTime() , which just
returns a formatted date/time string.
8
// get the date and time as a String:
String getTime() {
Date currentDate = new Date();
return currentDate.toString();
}
The Button class for this sketch
differs from the one for the RFID
Writer sketch in Chapter 9, in that it
has a variable and methods to track
its pressed state the last time you
checked it.
8
// The Button class defines the behavior and look
// of the onscreen buttons. Their behavior is slightly
// different on a touchscreen than on a mouse-based
// screen, because there is no mouseClick handler.
class Button {
int x, y, w, h; // positions of the buttons
color basecolor, highlightcolor; // color and highlight color
color currentcolor; // current color of the button
String name; // name on the button
boolean pressedLastTime; // if it was pressed last time
// Constructor: sets all the initial values for
// each instance of the Button class
Button(int thisX, int thisY, int thisW, int thisH,
color thisColor, color thisHighlight, String thisName) {
x = thisX;
y = thisY;
h = thisH;
ยป
 
Search WWH ::




Custom Search