Arduino RFID 125kHz

Lecture et validation de tags RFID 125kHz – Stockage EEPROM

Matériel :

1)  Carte arduino

2) Shield Grove

3) Grove RFIR reader

Librairies arduino :

  • EEPROM.h (natif)
  • SoftwareSerial.h (natif)

Branchements :

Le lecteur RFID est branché sur le port D2 du shield

Le code :

Téléverser sur arduino :

#include <EEPROM.h>

#include <SoftwareSerial.h>
 
SoftwareSerial SoftSerial(2, 3);

const byte numChars = 32;
char receivedChars[numChars];
String sentence="";
int readtag=1;
long tag=0;
long newtag=0;
int attente=0;


boolean newData = false;

void setup()
{
    SoftSerial.begin(9600);     // the SoftSerial baud rate
    Serial.begin(9600);         // the Serial port of Arduino baud rate.
}
 
void loop() {
    if (readtag==1){
      tag=EEPROMReadlong(1);    //lecture de la valeur du tag stocké dans l'eeprom s'il y en a une

      if (tag==-1) {
        readtag=2;
      }
      else {                  // affichage du tag stocké en mémoire s'il y en a un
        Serial.print("Tag en memoire : ");
        Serial.println(tag);
        readtag=3;
      }
    }
    if (readtag==2 && attente==0){  //attente de la lecture d'un tag s'il n'y a rien stocké en mémoire
      Serial.print("Attente de lecture Tag a enregistrer"); 
      attente=1;
    }
    recvWithStartEndMarkers();  //Lecture du buffer série
    showNewData();              //Analyse du buffer série
    if (readtag==2 && attente==1 && newtag!=0){ //enregistrement du tag en mémoire
      EEPROMWritelong(1, newtag);
      tag=newtag;
      newtag=0;
      readtag=3;
    }
    if (readtag==3 && newtag!=0) {  //Interprétation du tag lu
      Serial.print("Tag lu : ");
      Serial.print(newtag);
      if (newtag==tag){
        Serial.println(" : OK");
      }
      else {
        Serial.println(" : INCONNU");
      }
      newtag=0;
    }
}

void recvWithStartEndMarkers() {
    static boolean recvInProgress = false;
    static byte ndx = 0;
    char startMarker = '0';
    char endMarker = '';
    char rc;
 
 // if (Serial.available() > 0) {
    while (SoftSerial.available() > 0 && newData == false) {
        rc = SoftSerial.read();

        if (recvInProgress == true) {
            if (rc != endMarker) {
                receivedChars[ndx] = rc;
                sentence = String(sentence+rc);
                ndx++;
                if (ndx >= numChars) {
                    ndx = numChars - 1;
                }
            }
            else {
                receivedChars[ndx] = '\0'; // terminate the string
                recvInProgress = false;
                ndx = 0;
                newData = true;
            }
        }

        else if (rc == startMarker) {
            recvInProgress = true;
        }
    }
}

unsigned long hexToDec(String hexString) {
  
  unsigned long decValue = 0;
  int nextInt;
  
  for (int i = 0; i < hexString.length(); i++) {
    
    nextInt = int(hexString.charAt(i));
    if (nextInt >= 48 && nextInt <= 57) nextInt = map(nextInt, 48, 57, 0, 9);
    if (nextInt >= 65 && nextInt <= 70) nextInt = map(nextInt, 65, 70, 10, 15);
    if (nextInt >= 97 && nextInt <= 102) nextInt = map(nextInt, 97, 102, 10, 15);
    nextInt = constrain(nextInt, 0, 15);
    
    decValue = (decValue * 16) + nextInt;
  }
  
  return decValue;
}

void showNewData() {
    if (newData == true) {
        //Serial.print("This just in ... ");
        if(sentence.length()==9){
          sentence="00"+sentence;
        }
        sentence=sentence.substring(1,9);
        Serial.println(receivedChars);
        //Serial.println(sentence);
        newtag=(hexToDec(sentence));
        //Serial.println(newtag);
        newData = false;
        sentence="";
    }


}

long EEPROMReadlong(long address)
      {
      //Read the 4 bytes from the eeprom memory.
      long four = EEPROM.read(address);
      long three = EEPROM.read(address + 1);
      long two = EEPROM.read(address + 2);
      long one = EEPROM.read(address + 3);

      //Return the recomposed long by using bitshift.
      return ((four << 0) & 0xFF) + ((three << 8) & 0xFFFF) + ((two << 16) & 0xFFFFFF) + ((one << 24) & 0xFFFFFFFF);
      }

//This function will write a 4 byte (32bit) long to the eeprom at
//the specified address to address + 3.
void EEPROMWritelong(int address, long value)
      {
      //Decomposition from a long to 4 bytes by using bitshift.
      //One = Most significant -> Four = Least significant byte
      byte four = (value & 0xFF);
      byte three = ((value >> 8) & 0xFF);
      byte two = ((value >> 16) & 0xFF);
      byte one = ((value >> 24) & 0xFF);

      //Write the 4 bytes into the eeprom memory.
      EEPROM.write(address, four);
      EEPROM.write(address + 1, three);
      EEPROM.write(address + 2, two);
      EEPROM.write(address + 3, one);
      }

Ouvrir le moniteur série et approcher un tag RFID du lecteur

Bon amusement.

Laisser un commentaire

Votre adresse de messagerie ne sera pas publiée. Les champs obligatoires sont indiqués avec *