stoica_c_florin
Published

RFID-Controlled Light Switch

This light switch can be turned on and off using designated RFID tags.

IntermediateProtip8,218
RFID-Controlled Light Switch

Things used in this project

Story

Read more

Custom parts and enclosures

Schematic

Target 3001 format

Schematics

Schematic

Code

RFID_light_switch.ino

Arduino
// define constants for pins

#include <SoftwareSerial.h>

int green_LED = 3;
int RELAY = 5;
int red_LED = 4;
int white_LED = 12;

SoftwareSerial ssrfid = SoftwareSerial(6,8);

// variables to keep state
int readVal = 0;              // individual character read from serial
unsigned int readData[10];    // data read from serial
int counter = -1;             // counter to keep position in the buffer
char tagId[11];               // final tag ID converted to a string
int ON_OFF = 2;               // 
char* authorizedTags[10];     // array to hold the list of authorized tags

void initAuthorizedTags()     // fills the list of authorzied tags
{
  // add your own tag IDs here
  authorizedTags[0] = "1B00B2D95B"; //Tag 1
  authorizedTags[1] = "0000000000"; //Tag 2  
  authorizedTags[2] = "0000000000"; //Tag 3
  authorizedTags[3] = "0000000000"; //Tag 4
  authorizedTags[4] = "0000000000"; //Tag 5
  authorizedTags[5] = "0000000000"; //Tag 6
  authorizedTags[6] = "0000000000"; //Tag 7
  authorizedTags[7] = "0000000000"; //Tag 8
  authorizedTags[8] = "0000000000"; //Tag 9
  authorizedTags[9] = "0000000000"; //Tag 10
}

void setup()
{                
  Serial.begin(9600);
  ssrfid.begin(9600);
  ssrfid.listen(); 
  
  pinMode(green_LED, OUTPUT);
  pinMode(RELAY, OUTPUT);
  pinMode(red_LED, OUTPUT);
  pinMode(white_LED, OUTPUT);
  
  digitalWrite(green_LED, LOW);
  digitalWrite(RELAY, LOW);
  digitalWrite(red_LED, LOW);
  digitalWrite(white_LED, HIGH);
  
  initAuthorizedTags();
}

int checkTag()             // check if the tag ID we just read is any of the authorized tags
{
  int i;
  for (i = 0; i < 10; ++i)
  {
    if (strcmp(authorizedTags[i], tagId) == 0)
    {
      return 1;
    }
  }
  return 0;
}

void parseTag()            // convert the int values read from serial to ASCII chars
{
  int i;
  for (i = 0; i < 10; ++i)
  {
    tagId[i] = readData[i];
  }
  tagId[10] = 0;
}

void processTag()         // once a whole tag is read, process it
{
  parseTag();             // convert id to a string
  printTag();             // print it
  if (checkTag() == 1)    // check if the tag is authorized
  {
    tagSuccess();         // if so, relay and green LED will be turned from OFF to ON or from ON to OFF, depend the value of ON_OFF variable (odd or even)
  } 
  else
  {
    tagFailed();          // otherwise, inform user of failure
  }
}

void printTag()
{
  Serial.print("Tag value: ");
  Serial.println(tagId);
}

void tagSuccess()                     // perform an action when an authorized tag was read
{
  Serial.println("Tag authorized.");
  if ((ON_OFF % 2) == 0)              // if ON_OFF is even number means the relay (light) and the green LED is "off" and will go "on"
  {
  digitalWrite(green_LED, HIGH);
  digitalWrite(RELAY, HIGH);
  digitalWrite(white_LED, LOW);
  digitalWrite(red_LED, LOW);
  Serial.println("Light ON");
  delay(2000);
  }
    else                              // if ON_OFF is odd number means the relay (light) and the green LED is "on" and will go "off"
  {
  digitalWrite(green_LED, LOW);
  digitalWrite(RELAY, LOW);
  digitalWrite(white_LED, HIGH);
  digitalWrite(red_LED, LOW);
  Serial.println("Light OFF");
  delay(2000);
  }
  ON_OFF ++;                          // increment ON_OFF for next cicle
}

void tagFailed()                          // inform the user that the tag is not authorized
{
  Serial.println("Unauthorized access!");
  digitalWrite(green_LED, LOW);           // put the green LED low
  digitalWrite(red_LED, HIGH);            // put the red LED high, signal for unauthorized tag
  delay(2000);
  if ((ON_OFF % 2) != 0)
  {
    digitalWrite(green_LED, HIGH);        // if tag fail and the light is on, green LED will be put back high
  }
}


void clearSerial()                  // this function clears the rest of data on the serial, to prevent multiple scans
{
  while (ssrfid.read() >= 0) 
  {
    ;                               // do nothing
  }
}

void loop() 
{
  // turn red LED off
  digitalWrite(red_LED, LOW);

  if (ssrfid.available() > 0) 
  {
    readVal = ssrfid.read();        // read the incoming byte:
    if (readVal == 2)               // a "2" signals the beginning of a tag
    {
      counter = 0;                  // start reading
    } 
    else if (readVal == 3)          // a "3" signals the end of a tag
    {
     processTag();                  // process the tag we just read
     clearSerial();                 // clear serial to prevent multiple reads
     counter = -1;                  // reset reading state
    }
    else if (counter >= 0)          // if we are in the middle of reading a tag
    {
     readData[counter] = readVal;   // save valuee
     ++counter;                     // increment counter
    } 
  }
}

Credits

stoica_c_florin

stoica_c_florin

1 project • 4 followers

Comments