0

I'm new to using Arduino and I have managed a few changes to this sketch. But I want to check for a second tag if the first one is not correct. I just can't seem to get it to work. I think it would be good if the tag is not a match to ask it to check the next tag and carry on, but I'm not sure how to make this happen.

//RFID Password Typer
//If a matching tag is found, the keyboard types
//CTRL-ALT-DEL, then types the password and presses enter
//Which should log you on to a Windows computer
//Or Lock the computer if you're already logged in

#include <SPI.h>
#include <MFRC522.h>
#include "Keyboard.h"
#include <Mouse.h>

#define RST_PIN 9         
#define SS_PIN 10        

byte tag[10]={0x64,0xAE,0xDD,0xFC,};
char pswd[]="********\n";      //password, end with just \n
MFRC522 mfrc522(SS_PIN, RST_PIN);  // Create MFRC522 instance

void setup() {
  SPI.begin();              //start SPI
  mfrc522.PCD_Init();       //start RC522 module
  Keyboard.begin();         //start USB keyboard
  pinMode(13, OUTPUT);
  digitalWrite(13, LOW);     //LED off
}

void loop() {
  if (!mfrc522.PICC_IsNewCardPresent()){ return; }
  if (!mfrc522.PICC_ReadCardSerial()){ return; }
  for(byte i = 0; i < mfrc522.uid.size; i++) {
    if(mfrc522.uid.uidByte[i]!=tag[i]){ return; }
  }
  mfrc522.PICC_HaltA(); //stop tag so we don't get repeats
  digitalWrite(13, HIGH); //LED on so we know we've got tag match
  Mouse.click();
  delay(1000);
  Keyboard.press(KEY_LEFT_CTRL);
  delay(1000);
  Keyboard.releaseAll();
  Keyboard.press(KEY_LEFT_CTRL);
  Keyboard.press(KEY_LEFT_ALT);
  Keyboard.press(KEY_DELETE);
  delay(100);
  Keyboard.releaseAll();
  delay(300);
  Keyboard.print(pswd);
  delay(100);
  digitalWrite(13, LOW); //LED off
}
  • 1
    Please edit your post and format the code using the code formatting markdown (four leading spaces). For help see Markdown help. You should be able to do this by selecting the code and pressing Ctrl+K to have your browser do this for you. – Nick Gammon Mar 24 '18 at 22:43
  • What have tried? You are looping through the UID of the card and exit if one of the bytes are not values. (This is by the way a timing side-channel.). You can just add a second for loop to see if it's equal to another constant tag value. For that you should put the "compare two byte arrays of a given size" logic in a function for easy re-use. – Maximilian Gerhardt Mar 24 '18 at 22:48
1

here's how I'd do it ... allowing for 4, 7 and 10 byte uid's (which are all possible apparently)

struct tagInfo {
  byte len;
  byte uid[10];
};
struct tagInfo tag[] = { // add your tags here
  // the len is 1, 2 or 3, for lengths 4, 7, and 10
  // you could just use 4, 7, 10 here and not bother
  // with the len * 3 + 1 calculation later
  // the choice is yours, really
  {1, {0x01, 0x02, 0x03, 0x04}},
  {1, {0x02, 0x03, 0x04, 0x05}},
  {2, {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}},
  {1, {0x02, 0x02, 0x02, 0x02}}
};
const int numtags = sizeof(tag)/sizeof(struct tagInfo);

Then, your loop

void loop() {
  if (!mfrc522.PICC_IsNewCardPresent()) { return; }
  if (!mfrc522.PICC_ReadCardSerial()) { return; }
  int i; // declare before the loop because we want to check the value after the loop ends
  for (i = 0; i < numtags; i++) {
    byte len = tag[i].len * 3 + 1;
    if(len == mfrc522.uid.size) { // 4, 7, 10?
      if(!memcmp(mfrc522.uid.uidByte, tag[i].uid, len)) {
        break; // found an exact match so stop the loop
      }
    }
  }
  if (i == NUMTAGS) { return; } // no matches
  // all good here - run the rest of your code
}
0

Got it. Not sure if it's the best way but it works. Thanks for your help.

//RFID Password Typer
//If a matching tag is found, the keyboard types
//CTRL-ALT-DEL, then types the password and presses enter
//Which should log you on to a Windows computer
//Or Lock the computer if you're already logged in

#include <SPI.h>
#include <MFRC522.h>
#include "Keyboard.h"

#define RST_PIN 9         
#define SS_PIN 10        

byte tag[10]={****,****,****,****,};
byte tag1[10]={****,****,****,***^,};
byte tag2[10]={****,****,****,**^^,};
byte tag3[10]={****,****,****,*^^^,};
char pswd[]="*********\n";      //password, end with just \n
MFRC522 mfrc522(SS_PIN, RST_PIN);  // Create MFRC522 instance

void setup() {
  SPI.begin();              //start SPI
  mfrc522.PCD_Init();       //start RC522 module
  Keyboard.begin();         //start USB keyboard
  pinMode(13,OUTPUT);
  digitalWrite(13,LOW);     //LED off
}

void loop() {
  if (!mfrc522.PICC_IsNewCardPresent()) { return; }
  if (!mfrc522.PICC_ReadCardSerial()) { return; }
  for(byte i = 0; i < mfrc522.uid.size; i++) {
    if(mfrc522.uid.uidByte[i]!=tag[i]) {
    } else if(mfrc522.uid.uidByte[i]!=tag1[i]) {
    } else if(mfrc522.uid.uidByte[i]!=tag2[i]) {
    } else if(mfrc522.uid.uidByte[i]!=tag3[i]) { return; }
  }
  mfrc522.PICC_HaltA(); //stop tag so we don't get repeats
  digitalWrite(13, HIGH); //LED on so we know we've got tag match
  Keyboard.press(KEY_LEFT_CTRL);
  Keyboard.press(KEY_LEFT_ALT);
  Keyboard.press(KEY_DELETE);
  delay(100);
  Keyboard.releaseAll();
  delay(300);
  Keyboard.print(pswd);
  delay(100);
  digitalWrite(13,LOW); //LED off
}
  • no, not the best way at all - look at the logic in testing for a match ... work through it on paper, you'll see it is wrong – Jaromanda X Mar 25 '18 at 7:22
  • This is not the logic you want. You are looping through the UID and looking if any of the constant tags have that value in the current place. If you have tag1 = { 0x1, 0x2, 0x3} tag2 = {0x4, 0x5, 0x6} the tag {0x4, 0x2, 0x6} will be accepted although it does not match any tag completely. The other answer is what you'll want. – Maximilian Gerhardt Mar 25 '18 at 10:47

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.