Arduino RFID Twitter Lock

Thumbnail: 

Wouldn't you like to have an RFID lock at your hackerspace, that posts on Twitter when you get there to work on your projects?

We, at the Bucharest hackerspace - inventeaza.ro - have built our own RFID lock, using an Arduino compatible Sparkfun Ethernet Pro (Arduino + Ethernet shield) and an Innovations RFID Reader. This lock, posts on twitter, who opens our hackerspace door.
The list of materials that we used to build this project is this:

While the Arduino board, runs the code, to read the RFID Key Card, a relay connected to Arduino opens an Electromechanical Lock when a valid RFID Card is passed in front of the RFID Reader.
Inside the Arduino program, there is a list of valid RFID cards, and the card owner's name, so that when somebody opens the door, his name will be posted on Twitter, using the hackerspace Twitter account (@inventeazaro in our case).

The way we connected all the bits and pieces to the Arduino board, is presented in the picture bellow.

Note that you will need a Twitter token to use it with the Twitter library from here http://arduino-tweet.appspot.com/ .
The code that we are using for the Arduino is this:

/*
* Bucharest Hackerspace inventeaza.ro
* Twitter RFID Lock
*/

#if defined(ARDUINO) && ARDUINO > 18   // Arduino 0019 or later
#include <SPI.h>
#endif
#include <Ethernet.h>
#include <EthernetDNS.h>
#include <Twitter.h>

#include <NewSoftSerial.h>

// Ethernet Shield Settings
byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

// Substitute ip address settings here
byte ip[] = {
  192, 168, 10, 250 };
byte gw[] = {
  192, 168, 10,  1  };
byte nm[] = {
  255, 255, 255, 0  };

// Your Token to Tweet (get it from http://arduino-tweet.appspot.com/)
Twitter twitter("Put Your Tweet token here");

// Message to post
char msg[256]={0};

NewSoftSerial rfid(2,3);

char tagString[12];
int index;
int total;
boolean readStart;
boolean readStop;

int pinUsa = 7;
int pinErr = 5;

unsigned long timpStart;
int interval = 1*1000;
//Register your RFID tags here
struct rfid_tag {
  char tag[13];
  char nickName[20];
};

#define NUM_TAGS  15
struct rfid_tag tags[NUM_TAGS] = {
    {"720065985701", "Constantin Craciun"  },
    {"1F0030C011A7", "Viorel Spinu"},
    {"170011D29BCF", "Andrei Dore"},
    {"4010BE77AB22", "Liana Marinescu"},
    {"00109B4DAE77", "Ionut Cotoi"},
};

void setup(){
  delay(1000);
  Serial.begin(9600);
  rfid.begin(9600);
  Ethernet.begin(mac, ip, gw, nm);
  //ONLY NEEDED IF CONTROLING THESE PINS - EG. LEDs
  pinMode(pinUsa, OUTPUT);
  pinMode(pinErr, OUTPUT);
  pinMode(13, OUTPUT);
  rfidReset();
}

void loop(){

  if(Serial.available()){
    char readByte = Serial.read();

    switch (readByte){
    case 'a':
      deschideUsa();
      break;

    case 'b':
      digitalWrite(pinErr,HIGH);
      delay(1500);
      digitalWrite(pinErr,LOW);
      break;

    default:
      break;
    }

  }

  if(rfid.available() > 0){
    int readByte = rfid.read();
    total++;
    switch(readByte){
    case 2:
      readStart = true;
      total = 1;
      index = 0;
      break;

    case 3:
      readStop = true;
      break;

    case 10:
    case 13:
      break;

    default:
      tagString[index++] = readByte;
      break;
    }
  }

  if(total > 16){
    delay(100);
    rfid.flush();
    rfidReset();
  }
  else   if(readStart && readStop && index == 12){
    Serial.print("1=");
    for(int i = 0; i < index; i++){
      Serial.print(tagString[i],BYTE);
    }
    Serial.print(";");
    checkTag(tagString);
    rfidReset();
    //clearTag(tagString); //Clear the char of all value
  }

  //inchid usa daca a stat deschisa destul timp
  if(timpStart+interval<=millis())
    digitalWrite(pinUsa,LOW);

}

void rfidReset(){
  total = 0;
  index = 0;
  readStart = false;
  readStop = false;
  for(int i = 0; i < strlen(tagString); i++){
    tagString[i] = 0;
  }
}
void checkTag(char tag[]){
  ///////////////////////////////////
  //Check the read tag against known tags
  ///////////////////////////////////

  if(strlen(tag) == 0) return; //empty, no need to contunue
  for(int i=0; i<NUM_TAGS; i++) {
    if(copmarTag(tag, tags[i].tag)){
      deschideUsa();
      sprintf(msg, "%s is at http://inventeaza.ro %ld", tags[i].nickName, millis());
      twitter.post(msg);
      delay(1000);
      digitalWrite(pinUsa,LOW);
      software_Reset();
    }
  }

}
boolean copmarTag(char one[], char two[]){

  if(strlen(one) == 0) return false; //empty

  for(int i = 0; i < 12; i++){
    if(one[i] != two[i]) return false;
  }

  return true; //no mismatches
}
void deschideUsa(){
  timpStart = millis();
  digitalWrite(pinUsa,HIGH);
}

void software_Reset() // Restarts program from beginning but does not reset the peripherals and registers
{
  asm volatile ("  jmp 0"); 
}

The people that worked on this project are: Emi, Viorel Spinu, Andrei Dore, Ionut Cotoi and Constantin Craciun.

If you want to check out our activity on Twitter and see this project in action, follow us on Twitter https://twitter.com/inventeazaro .

Enjoy!

Cursuri

Promovarea unui ONG este o adevarata provocare, in special atunci cand nu exista suficiente resur

Ti-a trecut pe la ureche termenul de “Arduino”, nu prea stii cu ce se mananca, dar vrei sa inveti

Proiecte

Ionut Cotoi, membru al echipei inventeaza.ro si Dumitru Zagan de la firma Z.EL au realizat o bicicleta cu care poti efectua plimbari virtuale in mijl

Wouldn't you like to have an RFID lock at your hackerspace, that posts on Twitter when you get there to work on your projects?

Stiri

Uniunea Studentilor din Romania va invita intre 11 si 12 mai sa participati la Congresul Studenti

In data de 28 aprilie 2012, Centrul de Creatie Tehnica pentru Tineret a organizat un workshop gra