Hey Everyone, this is my first post here, but I have been reading for a while. Got my first Arduino a couple months ago and my experimenting has been very low key. Now I am working on a much more ambitious project that I need help with.
I am trying to build an (almost) identical system to the one shown here: TechShop RFID | This is a prototype RFID internet controlled… | Flickr Built by Garrett Mace of Macetech http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?action=viewprofile;username=macegr, this system reads an RFID tag, sends the tag to a server for verification, and activates a trigger based on the response.
My system is the following: an Arduino Duemilanove, ID-12/20 reader, official Ethernet shield, and an installation of Xampp running Apache, MySQL, and PHP. This system will arm/disarm the alarm system at remote radio tower sites. I want to scan an RFID tag, push it to the server for authentication, read the response from the server and act accordingly.
So far, I have the supported web stuff sorted out - I can manually send a card ID and site variables in the URL to the server and get back a 1 or 0 depending on if it's valid or not. I have used various examples and notes from all over this forum and other sources (examples) to send the URL from the Arduino (WebClient.pde) and get back the proper response. I have also used example code for the RFID portion to confirm reading and printing to serial console.
My issue comes when putting it all together. I've been bashing my head around trying to understand and put things together one step at a time, but my brain is starting to hurt.
I have tried to adapt the RFID code from here http://www.instructables.com/id/Arduino-RFID-Door-Lock/ (very cool project btw). The code below is where I am at, obviously messy and I need to fix up the commenting left behind but it helps me figure out where I am. Or I'm just WAY lost... :o
#include <Ethernet.h>
#define powerPin 10
#define doorPin 11
byte mac[] = { 0x00, 0x00, 0x5E, 0x00, 0x01, 0xAA };
byte ip[] = { 192, 168, 2, 9 };
byte gateway[] = { 192, 168, 2, 254 };
byte subnet[] = { 255, 255, 255, 0 };
byte server[] = { 192, 168, 2, 148 };
Client client(server, 80);
int DO_RESET_ETH_SHIELD = 8; // A delayed reset for a hardware issue
boolean match = false;
byte readCard[6]; // Stores an ID read from the RFID reader
byte checksum = 0; // Stores the checksum to verify the ID
void setup()
{
pinMode(powerPin, OUTPUT); // Connected to Blue on tri-color LED to indicate reader is ready
pinMode(doorPin, OUTPUT); // Connected to relay to activate the door lock
Serial.begin(9600); // Connect to the serial port
init_ethernet();
Ethernet.begin(mac, ip);
}
void loop ()
{
byte val = 0; // Temp variable to hold the current byte
normalModeOn();
if(Serial.available() > 0) // If the serial port is available and sending data...
{
if((val = Serial.read()) == 2) // First Byte should be 2, STX byte
{
getID(); // Get the ID, sets readCard = to the read ID
if (client.connect())
{
Serial.println("Connected!");
client.print("GET /Test/test1.php?site=berland&active=1&card=");
client.print(readCard[5]);
client.println();
}
else
{
Serial.println("Connection failed");
}
Serial.println();
}
}
}
// If the serial port is ready and we received the STX BYTE (2) then this function is called
// to get the 4 BYTE ID + 1 BYTE checksum. The ID+checksum is stored in readCard[6]
// Bytes 0-4 are the 5 ID bytes, byte 5 is the checksum
void getID()
{
byte bytesread = 0;
byte i = 0;
byte val = 0;
byte tempbyte = 0;
// 5 HEX Byte code is actually 10 ASCII Bytes.
while ( bytesread < 12 ) // Read 10 digit code + 2 digit checksum
{
if( Serial.available() > 0) // Check to make sure data is coming on the serial line
{
val = Serial.read(); // Store the current ASCII byte in val
if((val == 0x0D)||(val == 0x0A)||(val == 0x03)||(val == 0x02))
{ // If header or stop bytes before the 10 digit reading
break; // Stop reading
}
if ( (val >= '0' ) && ( val <= '9' ) ) // Do Ascii/Hex conversion
{
val = val - '0';
}
else if ( ( val >= 'A' ) && ( val <= 'F' ) )
{
val = 10 + val - 'A';
}
if ( bytesread & 1 == 1 ) // Every two ASCII charactors = 1 BYTE in HEX format
{
// Make some space for this hex-digit by
// shifting the previous hex-digit with 4 bits to the left:
readCard[bytesread >> 1] = (val | (tempbyte << 4));
if ( bytesread >> 1 != 5 ) // If we're at the checksum byte,
{
checksum ^= readCard[bytesread >> 1]; // Calculate the checksum using XOR
};
}
else // If it is the first HEX charactor
{
tempbyte = val; // Store the HEX in a temp variable
};
bytesread++; // Increment the counter to keep track
}
}
bytesread = 0;
}
// Opens door and turns on the green LED for setDelay seconds
void openDoor( int setDelay )
{
setDelay *= 1000; // Sets delay in seconds
digitalWrite(doorPin, HIGH); // Unlock door!
delay(setDelay); // Hold door lock open for 5 seconds
digitalWrite(doorPin, LOW); // Relock door
}
// Controls LED's for Normal mode, Blue on, all others off
void normalModeOn()
{
digitalWrite(powerPin, HIGH); // Power pin ON and ready to read card
digitalWrite(doorPin, LOW); // Make sure Door is Locked
}
void init_ethernet()
{
pinMode(DO_RESET_ETH_SHIELD, OUTPUT); // sets the digital pin as output
digitalWrite(DO_RESET_ETH_SHIELD, LOW);
delay(1000); //for ethernet chip to reset
digitalWrite(DO_RESET_ETH_SHIELD, HIGH);
delay(1000); //for ethernet chip to reset
pinMode(DO_RESET_ETH_SHIELD, INPUT); // sets the digital pin input
delay(1000); //for ethernet chip to reset
Ethernet.begin(mac,ip,gateway,subnet);
delay(1000); //for ethernet chip to reset
Ethernet.begin(mac,ip,gateway,subnet);
delay(1000); //for ethernet chip to reset
}
At this point I get the output Connected! at the serial interface when I read a card, but can't tell what it gets back. Also, after 4 reads it always fails to connect (or says it does, but I never see any more TX data). Also, my pin 11 is high all the time. :-?
Again, I am new to this, so any help is appreciated. Thanks in advance to anyone reading this.
Aaron