Hardware Reference
In-Depth Information
When you run the firmware reader sketch, you should get
a response like this in the Serial Monitor:
This sketch looks for RFID tags and reads block 4 when
it finds a tag. If it finds a Twitter handle there, it makes an
HTTP call to Twitter's XML API to get the latest tweet from
that Twitter user. Then it displays the result on an LCD
screen.
asking for firmware
getting reply: I2C 2.8ÿ
That last byte is the checksum of the string of bytes that
the reader sent in response. If you get a good response,
you're ready to move on. If not, revisit the sidebar on page
344.
Saving Program Memory
The sketch for this project is complex, and it takes a large
chunk of the Arduino's program memory. You'll notice that
the Serial print statements have a new syntax, like this:
It would be convenient if, instead of having to remember
the numeric values for each command, you could just call
the commands by name, like you can with the Processing
library above. The Arduino SonMicro library lets you do just
that. In fact, most of its methods share the same names
as the Processing library's methods. Download the latest
version from https://github.com/tigoe/SonMicroReader-
for-Arduino . Make a new directory called SonMicroReader
in the libraries directory of your Arduino sketch directory,
and copy the contents of the download package to it. Then
restart the Arduino application. The new library should
appear in the Examples submenu of the File menu as
usual. Once that's done, move on to the sketch below.
Serial.println(F("Hello"));
The F() notation tells the print and println statements to
store the text that follows in flash memory, not in program
memory. Because the print statements are for debugging
purposes only and are not going to change, this saves
memory for your program.
X
Get the Tweet
You'll be
using a lot of libraries in this sketch.
There are a few global variables associ-
ated with each one, starting with the
current tag, the last tag read, and the
address block from which to read on
the tag.
/*
Twitter RFID Web Client
Context: Arduino
*/
#include <SPI.h>
#include <Ethernet.h>
#include <TextFinder.h>
#include <Wire.h>
#include <LiquidCrystal.h>
#include <SonMicroReader.h>
8 Change these to match your own device
and network.
The main loop() is a simple state
machine . It does different things
depending on what state it's in, so
there's a variable to keep track of the
state. There are four basic states:
SonMicroReader Rfid; // instance of the reader library
unsigned long tag = 0; // address of the current tag
unsigned long lastTag = 0; // address of the previous tag
int addressBlock = 4; // memory block on the tag to read
• Looking for a tag
• Reading the tag you just got
• Making an HTTP request
• Waiting for the server's response
int state = 0; // the state that the sketch is in
Regardless of what state the sketch is
in, it should also update the LCD every
time through the loop() .
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x01 };
IPAddress ip( 192,168,1,20 ); // will only be used if DHCP fails
IPAddress server(199,59,149,200); // Twitter's API address
Client client; // the client connection
There are also the usual IP and
Ethernet configuration variables.
»
 
Search WWH ::




Custom Search