Hardware Reference
In-Depth Information
Finally, the postPicture() method
takes the last image saved. The
method uploads it by calling the
save2web.php script using an HTTP POST
request.
8
void postPicture() {
// load the saved image into an array of bytes:
byte[] thisFile =loadBytes(fileName);
// open a new connection to the server:
thisClient = new Client(this, "www.example.com", 80);
// make an HTTP POST request:
thisClient.write("POST " + pictureScriptUrl + " HTTP/1.1\n");
thisClient.write("Host: www.example.com\n");
// tell the server you're sending the POST in multiple parts,
// and send a unique string that will delineate the parts:
thisClient.write("Content-Type: multipart/form-data; boundary=");
thisClient.write(boundary + "\n");
// form the beginning of the request:
String requestHead ="\n--" + boundary + "\n";
requestHead +="Content-Disposition: form-data; name=\"file\"; ";
requestHead += "filename=\"" + fileName + "\"\n";
requestHead +="Content-Type: image/jpeg\n\n";
// form the end of the request:
String tail ="\n\n--" + boundary + "--\n\n";
// calculate and send the length of the total request,
// including the head of the request, the file, and the tail:
int contentLength = requestHead.length() + thisFile.length
+ tail.length();
thisClient.write("Content-Length: " + contentLength + "\n\n");
// send the header of the request, the file, and the tail:
thisClient.write(requestHead);
thisClient.write(thisFile);
thisClient.write(tail);
}
cat from the mat. You should get another message in the
console from the call to the mailer script. See whether the
image changes once the cat is off the mat. It shouldn't; the
image that remains should be the last one of the cat—that
is, until the cat jumps on the mat again. The timestamp in
that image will give you an idea when the cat was last on
the mat.
One Final Test
To test this code, open the browser to the Cat Cam page,
and then run the sketch. On startup, it should take a
picture and upload it. A few seconds later, the Cat Cam
page will refresh itself, and you'll see that image. Then get
the cat to jump on the mat. The sketch will take another
image and upload it, and will then send you an email.
Check your mail to see whether there's a new message
from the cat.
When all that works, take a breath and admire your work.
Figure 3-15 shows the finished cat bed. Congratulations!
You've just made your first Internet-connected project. You
and your cat can now be in constant contact.
X
As long as the cat stays on the mat, the sketch will upload
a new image every five seconds. Watch for a minute or so
to see the image change a few times, and then remove the
 
Search WWH ::




Custom Search