0

I'm making an attendance System using an RFID scanner, scanning RFID Card & saving Data in Excel.

When I used this code with Arduino Uno (by changing #define SS_PIN 12 and#define RST_PIN 9) it worked perfectly fine.

However, when I used the same code (by changing #define SS_PIN 15 and #define RST_PIN 5) the RFID is unable to scan any of the RFID Cards.

PIN CONNECTION Of ESP8266 & MFRC522 SDA to D8 SCK to D5 MOSI to D7 MISO to D6 GND to GND RST to D1 3.3V to 3V3

#include <ESP8266WiFi.h>     //Include Esp library
#include <WiFiClient.h> 
#include <ESP8266WebServer.h>
#include <ESP8266HTTPClient.h>
#include <SPI.h>
#include <MFRC522.h>        //include RFID library

#define SS_PIN 15 //RX slave select
#define RST_PIN 5

MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.

byte card_ID[4]; //card UID size 4byte
byte Name1[4]={0x36,0x69,0x25,0xD9};//first UID card 36 69 25 D9
byte Name2[4]={0xA7,0xB5,0x33,0x04};//second UID card A7 B5 33 04

int NumbCard[2];//the number of cards. in my case i have just two cards.
int j=0;  
int statu[2];//the number of cards. in my case i have just two cards.
int s=0;        

int const RedLed=6;
int const GreenLed=5;
int const Buzzer=8;

String Log;
String Name;//user name
long Number;//user number
int n ;//The number of card you want to detect (optional)  

void setup() {

  Serial.begin(9600); // Initialize serial communications with the PC
  SPI.begin();  // Init SPI bus
  mfrc522.PCD_Init(); // Init MFRC522 card

  Serial.println("CLEARSHEET");                 // clears starting at row 1
  Serial.println("LABEL,Date,Name,Number,Time IN,Time OUT");// make four columns (Date,Time,[Name:"user name"]line 48 & 52,[Number:"user number"]line 49 & 53)

  pinMode(RedLed,OUTPUT);
  pinMode(GreenLed,OUTPUT);
  pinMode(Buzzer,OUTPUT);

  delay(200);
   }

void loop() {
  //look for new card
   if ( ! mfrc522.PICC_IsNewCardPresent()) {
  return;//got to start of loop if there is no card present
 }
 // Select one of the cards
 if ( ! mfrc522.PICC_ReadCardSerial()) {
  return;//if read card serial(0) returns 1, the uid struct contians the ID of the read card.
 }

 for (byte i = 0; i < mfrc522.uid.size; i++) {
     card_ID[i]=mfrc522.uid.uidByte[i];

       if(card_ID[i]==Name1[i]){
       Name="First Employee";//user name
       Number=123456;//user number
       j=0;
       s=0;
      }
      else if(card_ID[i]==Name2[i]){
       Name="Second Employee";//user name
       Number=789101;//user number
       j=1;
       s=1;
      }
      else{
          digitalWrite(GreenLed,LOW);
          digitalWrite(RedLed,HIGH);
          goto cont;//go directly to line 71
     }
}
      if(NumbCard[j] == 1 && statu[s] == 0){
      statu[s]=1;
      Serial.print("DATA,DATE," + Name);//send the Name to excel
      Serial.print(",");
      Serial.print(Number); //send the Number to excel
      Serial.print(",");
      Serial.print("");
      Serial.print(",");
      Serial.println("TIME");

      }
      else if(NumbCard[j] == 0){
      NumbCard[j] = 1;
      n++;
      Serial.print("DATA,DATE," + Name);//send the Name to excel
      Serial.print(",");
      Serial.print(Number); //send the Number to excel
      Serial.print(",");
      Serial.print("TIME");
      Serial.print(",");
      Serial.println("");
      digitalWrite(GreenLed,HIGH);
      digitalWrite(RedLed,LOW);
      digitalWrite(Buzzer,HIGH);
      delay(30);
      digitalWrite(Buzzer,LOW);
      }
      else if(statu[s] == 1){
     //Turn Red LED when the employee Already Left
     digitalWrite(RedLed,HIGH);
      }
      delay(1000);
cont:
delay(2000);
digitalWrite(GreenLed,LOW);
digitalWrite(RedLed,LOW);

//if you want to close the Excel when all card had detected and save Excel file in Names Folder. in my case i have just 2 card (optional)
/*if(n==2){
    Serial.println("SAVEWORKBOOKAS,Names/WorkNames");
    Serial.println("FORCEEXCELQUIT");
    }*/
}
  • What voltage do the SPI lines expect/require? The Arduino Uno signals with 5V, while the ESP8266 signals with 3.3V. Also, you may check if the MFRC522.h library supports the ESP8266 (or if it's using low level atmega code) – Paul Feb 28 at 11:30
  • pinMode(RedLed,OUTPUT); ... RedLed is GPIO 6 ... GPIO 6 is SDCLK - the clock for the flash ram ... using GPIO 6 like that will cause your code to fail - similarly, using GPIO 8 ... SD_DATA1 ... will also cause failure ...you're also using GPIO 5 as reset AND buzzer – Jaromanda X Feb 28 at 11:53
  • sorry, not flash ram - flash memory :p i.e. GPIO 6 and 8 (and 7, 9, 10, 11) are instrumental in being able to read the flash memory that contains your code .... and are not even broken out on the bulk of ESP8266 modules on the market - the nodemcu being the exception of course, because, well, it's nodemcu and they do things differently - but GPIO6-11 should NEVER be used by your code ... Though 9 and 10 can be, if you know what you are doing – Jaromanda X Feb 28 at 12:06
  • 1
    you mix Dx labels and io pin numbering, so it is hard to know what is where wired. esp8266 doesn't use Dx constants only some boards use it to match the labels – Juraj Feb 28 at 12:26
  • @Juraj, actually, its code only uses gpio numbers – Jaromanda X Feb 28 at 20:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.