j'ai acheté en 2016 chez AprilBrother (pour 11$ + frais de port) une carte compatible arduino nommée Cactus Micro Rev2. Fin 2017, en cherchant une solution pour bloquer/débloquer le PC familial à l'aide d'une carte rfid ou jeton/token, je suis tombé sur un projet très simple à mettre en oeuvre.
Windows PC Lock/Unlock Using RFID © CC BY-NC-SA
- un lecteur RFID-RC522 (coûtant ~ 5€)
- un microcontrôleur compatible Arduino permettant l'émulation clavier (Arduino Leonardo, clones et cartes avec microcontrôleur ATMEGA32U4)

J'ai donc choisi le Cactus Micro Rev2, celui-ci intègrant : - un module ESP8266 WIFI (ESP-03) - un microcontrôleur ATMEGA32U4 3.3V/8MHz - un connecteur micro-USB - 4 x 10-bit ADC pins - 12 x E/S numériques (dont 5 PWM capable) - Connexions série Rx/Tx
#include <Keyboard.h>
#include <SPI.h>
#include <MFRC522.h>
#define SS_PIN 10
#define RST_PIN 5
#define KEY_RETURN 0xB0 //The hex value for the return key is 0xB0.
MFRC522 mfrc522 ( SS_PIN, RST_PIN ) ;
char Enter = KEY_RETURN; //Return key is declared as Enter.
String readid;
String card1="48b45a10"; //Change this value to the UID of your card.
void setup( )
{
Serial.begin(9600);
Keyboard.begin();
SPI.begin();
mfrc522.PCD_Init();
}
void temp(byte *buffer, byte bufferSize)//function to store card uid as a string datatype.
{
readid="";
for(byte i = 0;i<bufferSize; i++)
{
readid=readid+String(buffer[i], HEX);
}
}
void loop( )
{
if(!mfrc522.PICC_IsNewCardPresent())
{
return;
}
if(!mfrc522.PICC_ReadCardSerial())
{
return;
}
mfrc522.PICC_DumpToSerial(&(mfrc522.uid)); // Display card details in serial Monitor.
temp(mfrc522.uid.uidByte, mfrc522.uid.size);
if(readid==card1)
{
Keyboard.press(KEY_LEFT_GUI); //Press the left windows key.
Keyboard.press('l'); //Press the "l" key.
Keyboard.releaseAll(); //Release all keys.
delay (100);
Keyboard.press(Enter); //Press the Enter key.
Keyboard.release(Enter); //Release the Enter key.
delay(100);
Keyboard.print("PASSWORD"); // Change this value to your Windows PIN/Password.
Keyboard.releaseAll();
delay(100);
Keyboard.press(Enter);
Keyboard.releaseAll();
}
else
{
return;
}
}
Le même en + propre :
#include <Keyboard.h>
#include <SPI.h>
#include <MFRC522.h>
#define KEY_RETURN 0xB0
MFRC522 mfrc522(2, 10); //(SSpin,RSTpin)
char Enter = KEY_RETURN;
String readid;
String card1 = "CARDID";
void setup( )
{
Serial.begin(9600);
Keyboard.begin();
SPI.begin();
mfrc522.PCD_Init();
}
void temp(byte *buffer, byte bufferSize)
{
readid = "";
for (byte i = 0; i < bufferSize; i++)
{
readid = readid + String(buffer[i], HEX);
}
}
void loop( )
{
if (!mfrc522.PICC_IsNewCardPresent())
{
return;
}
if (!mfrc522.PICC_ReadCardSerial())
{
return;
}
mfrc522.PICC_DumpToSerial(&(mfrc522.uid));
temp(mfrc522.uid.uidByte, mfrc522.uid.size);
if (readid == card1)
{
Keyboard.press(KEY_LEFT_GUI);
Keyboard.press('l');
Keyboard.releaseAll();
delay (100);
Keyboard.print("PASSWORD");
Keyboard.releaseAll();
delay(100);
Keyboard.press(Enter);
Keyboard.releaseAll();
}
else
{
return;
}
delay(5000);
}

le module RFID-RC522 possède 8 broches d'E/S

Il faut donc faire correspondre les broches du RC522 et celles de la carte Cactus r2
[RC522] [Cacus r2]
SDA |----| D2 - SDA
SCK |----| D15 - SCKL
MOSI |----| D16 - MOSI
MISO |----| D14 - MISO
IRQ non utilisé
GND |----| GND
RST |----| RST
VCC |----| VCC
Ajoutons ensuite un sonar qui détecte l'absence afin de locker la session
#include <NewPing.h>
#define TRIGGER_PIN 9
#define ECHO_PIN 10
#define MAX_DISTANCE 200
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
void setup()
{
Serial.begin(9600); // Open serial monitor at 115200 baud to see ping results.
}
void loop()
{
delay(30);
unsigned int pingTime = sonar.ping();
Serial.print("Ping: ");
Serial.print(pingTime / US_ROUNDTRIP_CM);
Serial.println(" cm");
}
|