Skip to content
  • MySensors
  • OpenHardware.io
  • Categories
  • Recent
  • Tags
  • Popular
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo
  1. Home
  2. Troubleshooting
  3. RFID and NRF24L01 Wireless Network Coding Issues

RFID and NRF24L01 Wireless Network Coding Issues

Scheduled Pinned Locked Moved Troubleshooting
23 Posts 8 Posters 9.4k Views 8 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • codergirl56C Offline
    codergirl56C Offline
    codergirl56
    wrote on last edited by
    #1

    I am currently working on a project where I am interfacing an RFID-RC522 with a NRF24L01 transceiver on a single Arduino. Ultimately what I would like to have is one RFID-RC522 and NRF24L01 on one arduino, where the RFID can read the UID off of PICC cards, and the connected NRF24L01 can send the UID data to a master NRF24L01 on a different Arduino controller. Currently I am having issues with debugging and coding.

    Here is what I have achieved so far:

    For the wiring, I used @BartE Garage Door example for the hardware. Here is what he did: garage door example. The pins are set up as follows:

    The NRF24L01+ is connect as suggested on the MySensors site:

    pin 2 IRQ (I did not have this one connected as I do not believe it is needed in this project. Correct me if this is wrong).
    pin 9 CE
    pin 10 CSN/CS
    pin 11 MOSI
    pin 12 MISO
    pin 13 SCK

    The RC522 module shares pins: (these I connected together on the same row on a prototype board)

    pin 11 MOSI
    pin 12 MISO
    pin 13 SCK

    and uses

    pin 3.3 V (also shared with the NRF24L01) connected together on prototype board
    GND (for ground)
    pin 7 (iso 9) for CE
    pin 8 (iso 10) for CSN/CS.

    That is the hardware portion of it. Here is the code I have cut and figured out thus far using the garage door example and own research from the mysenors website. I started off adding line by line of code until I ran into problems. Main issue right now is the gw.begin(); not sure if using this function correctly. One main thing I noticed was I had an error with radio inti begin when I first began. I had to go into config.h and change the data rate on the RF24 to 1MPBS rather than the default 250KBPS. That is involved in this gw.begin() instance, and I put that in but it did not solve the problem.

    But there may also be other things I am missing as well. Any help would be greatly appreciated.

    The following is the code I have made thus far. Ultimately I would like to scan a UID on the RFID and have it sent to the NRF24L01 module to be read out on the serial monitor AND to send it to a NRF24L01 master module (to be set up on a different arduino.)

    #include <SPI.h> //used for SPI bus
    #include <MFRC522.h> //used for RFID reader
    #include <MySensor.h> //library that uses SPI
    
    
    #define RST_PIN     7       //  MFRC RST is used in pin 7
    #define SS_PIN      8       //  MFRC SS is used in pin 8
    
    
    MFRC522      mfrc522(SS_PIN, RST_PIN);  // Create MFRC522 instance
    #define    RFID    1
    
    //construct instance of NRF24 
    MyTransportNRF24 transport(9, 10);
     
    MySensor gw; //create MySensor instance
    
    MyMessage msg(RFID, V_VAR1); //create MyMessage instance; this holds message data 
                                 //and has helpers for setting payload
    
    void setup() 
    { 
        Serial.begin(115200);     // Initialize serial communications with the PC
    
      // Make sure MFRC will be disabled on the SPI bus
        pinMode(RST_PIN, OUTPUT);  //set the RST pin as output   
        digitalWrite(RST_PIN, LOW); //set RST pin off
        pinMode(SS_PIN, OUTPUT); //set SS pin as output    
        digitalWrite(SS_PIN, LOW); //set SS pin off
    
      //initialize radio and start library       
      gw.begin(incomingMessage, RFID, RF24_1MBPS);
      gw.sendSketchInfo("RFID Radio", "1.0");
    
      //must present sensors before reporting data to controller
      gw.present(RFID, S_CUSTOM); 
      gw.wait(125);
    
      SPI.begin();            // Init SPI bus
      mfrc522.PCD_Init();     // Init MFRC522
      mfrc522.PCD_DumpVersionToSerial();  // Show details of PCD - MFRC522 Card Reader details*/
      
      Serial.println(F("Scan PICC to see UID, type, and data blocks..."));
    }
     
    void loop()
    {
    //main issue area here; wasn't sure what to do
         delay(10000); // Wait 10 seconds
         gw.process();
         
    }
    
    void incomingMessage(const MyMessage &message)
    {
      // Look for new cards
     if ( ! mfrc522.PICC_IsNewCardPresent()) {
        return;
      }
    
      // Select one of the cards
      if ( ! mfrc522.PICC_ReadCardSerial()) {
        return;
      }
    
      // Dump debug info about the card; PICC_HaltA() is automatically called
      mfrc522.PICC_DumpToSerial(&(mfrc522.uid));
      Serial.println(F("Scan PICC to see UID, type, and data blocks..."));
    }Here
    

    Here are the two coding programs for the RFID and NRF24L01 that I am using to base code off of and also what I tested the hardware with initially:

    For the RFID: MCRF522 class from Git Hub; Dump Info Example: (I tested this on the RFID separately before interfacing with the NRF24L01 on one arduino):

    
    #include <SPI.h>
    #include <MFRC522.h>
    
    #define RST_PIN         9          // Configurable, see typical pin layout above
    #define SS_PIN          10         // Configurable, see typical pin layout above
    
    MFRC522 mfrc522(SS_PIN, RST_PIN);  // Create MFRC522 instance
    
    void setup() {
    	Serial.begin(9600);		// Initialize serial communications with the PC
    	while (!Serial);		// Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
    	SPI.begin();			// Init SPI bus
    	mfrc522.PCD_Init();		// Init MFRC522
    	mfrc522.PCD_DumpVersionToSerial();	// Show details of PCD - MFRC522 Card Reader details
    	Serial.println(F("Scan PICC to see UID, type, and data blocks..."));
    }
    
    void loop() {
    	// Look for new cards
    	if ( ! mfrc522.PICC_IsNewCardPresent()) {
    		return;
    	}
    
    	// Select one of the cards
    	if ( ! mfrc522.PICC_ReadCardSerial()) {
    		return;
    	}
    
    	// Dump debug info about the card; PICC_HaltA() is automatically called
    	mfrc522.PICC_DumpToSerial(&(mfrc522.uid));
    }
    

    For the NRF24L01 transceiver, I originally did hardware testing there were two modules--one for receiving and one for transmitting. So two NRF24L01 modules in all. Each was connected to its own Arduino and then connected to the same laptop with two separate USB cables. I ran two separate Arduino environments on two different serial monitors. I used the GettingStarted Example from the RF24 class off of Git Hub. There is one code for one module below. For the other module, I just switched bool radioNumber = 0 to = 1. This website was helpful for this.

    /*
    * Getting Started example sketch for nRF24L01+ radios
    * This is a very basic example of how to send data from one node to another
    * Updated: Dec 2014 by TMRh20
    */
    
    #include <SPI.h>
    #include "RF24.h"
    
    /****************** User Config ***************************/
    /***      Set this radio as radio number 0 or 1         ***/
    bool radioNumber = 0;
    
    /* Hardware configuration: Set up nRF24L01 radio on SPI bus plus pins 7 & 8 */
    RF24 radio(7,8);
    /**********************************************************/
    
    byte addresses[][6] = {"1Node","2Node"};
    
    // Used to control whether this node is sending or receiving
    bool role = 0;
    
    void setup() {
      Serial.begin(115200);
      Serial.println(F("RF24/examples/GettingStarted"));
      Serial.println(F("*** PRESS 'T' to begin transmitting to the other node"));
      
      radio.begin();
    
      // Set the PA Level low to prevent power supply related issues since this is a
     // getting_started sketch, and the likelihood of close proximity of the devices. RF24_PA_MAX is default.
      radio.setPALevel(RF24_PA_LOW);
      
      // Open a writing and reading pipe on each radio, with opposite addresses
      if(radioNumber){
        radio.openWritingPipe(addresses[1]);
        radio.openReadingPipe(1,addresses[0]);
      }else{
        radio.openWritingPipe(addresses[0]);
        radio.openReadingPipe(1,addresses[1]);
      }
      
      // Start the radio listening for data
      radio.startListening();
    }
    
    void loop() {
      
      
    /****************** Ping Out Role ***************************/  
    if (role == 1)  {
        
        radio.stopListening();                                    // First, stop listening so we can talk.
        
        
        Serial.println(F("Now sending"));
    
        unsigned long start_time = micros();                             // Take the time, and send it.  This will block until complete
         if (!radio.write( &start_time, sizeof(unsigned long) )){
           Serial.println(F("failed"));
         }
            
        radio.startListening();                                    // Now, continue listening
        
        unsigned long started_waiting_at = micros();               // Set up a timeout period, get the current microseconds
        boolean timeout = false;                                   // Set up a variable to indicate if a response was received or not
        
        while ( ! radio.available() ){                             // While nothing is received
          if (micros() - started_waiting_at > 200000 ){            // If waited longer than 200ms, indicate timeout and exit while loop
              timeout = true;
              break;
          }      
        }
            
        if ( timeout ){                                             // Describe the results
            Serial.println(F("Failed, response timed out."));
        }else{
            unsigned long got_time;                                 // Grab the response, compare, and send to debugging spew
            radio.read( &got_time, sizeof(unsigned long) );
            unsigned long end_time = micros();
            
            // Spew it
            Serial.print(F("Sent "));
            Serial.print(start_time);
            Serial.print(F(", Got response "));
            Serial.print(got_time);
            Serial.print(F(", Round-trip delay "));
            Serial.print(end_time-start_time);
            Serial.println(F(" microseconds"));
        }
    
        // Try again 1s later
        delay(1000);
      }
    
    
    
    /****************** Pong Back Role ***************************/
    
      if ( role == 0 )
      {
        unsigned long got_time;
        
        if( radio.available()){
                                                                        // Variable for the received timestamp
          while (radio.available()) {                                   // While there is data ready
            radio.read( &got_time, sizeof(unsigned long) );             // Get the payload
          }
         
          radio.stopListening();                                        // First, stop listening so we can talk   
          radio.write( &got_time, sizeof(unsigned long) );              // Send the final one back.      
          radio.startListening();                                       // Now, resume listening so we catch the next packets.     
          Serial.print(F("Sent response "));
          Serial.println(got_time);  
       }
     }
    
    
    
    
    /****************** Change Roles via Serial Commands ***************************/
    
      if ( Serial.available() )
      {
        char c = toupper(Serial.read());
        if ( c == 'T' && role == 0 ){      
          Serial.println(F("*** CHANGING TO TRANSMIT ROLE -- PRESS 'R' TO SWITCH BACK"));
          role = 1;                  // Become the primary transmitter (ping out)
        
       }else
        if ( c == 'R' && role == 1 ){
          Serial.println(F("*** CHANGING TO RECEIVE ROLE -- PRESS 'T' TO SWITCH BACK"));      
           role = 0;                // Become the primary receiver (pong back)
           radio.startListening();
           
        }
      }
    
    
    } // Loop```
    

    Any help figuring this out would be greatly appreciated!

    1 Reply Last reply
    0
    • gohanG Offline
      gohanG Offline
      gohan
      Mod
      wrote on last edited by
      #2

      did you figure out how to make it work?

      1 Reply Last reply
      0
      • martinsM Offline
        martinsM Offline
        martins
        wrote on last edited by
        #3

        Sit with the same scenario. Separately both function. As soon as you share miso, mosi and sck it fails to initialize the radio. Anybody knows whether you can share this ?

        Thanks

        1 Reply Last reply
        0
        • martinsM Offline
          martinsM Offline
          martins
          wrote on last edited by
          #4

          Found the solution in this thread:

          https://forum.mysensors.org/topic/2439/rfid-garage-door-opener/35

          void before() {
          // Make sure MFRC is disabled from the SPI bus
          pinMode(RST_PIN, OUTPUT);
          digitalWrite(RST_PIN, LOW);
          pinMode(SS_PIN, OUTPUT);
          digitalWrite(SS_PIN, LOW);
          }

          gohanG 1 Reply Last reply
          0
          • rejoe2R Offline
            rejoe2R Offline
            rejoe2
            wrote on last edited by
            #5

            At least in some first test I got a second SPI device (MCP4131) on the same wires working using the before() instruction.

            int CS1 = 10; //MCP4131
            void before() {
              pinMode (CS1, OUTPUT);
            digitalWrite(CS1, HIGH);
            }
            

            Another option may be to use SoftSPI for thenRF (see Network-Gateway-sketch).

            Controller: FHEM; MySensors: 2.3.1, RS485,nRF24,RFM69, serial Gateways

            1 Reply Last reply
            0
            • martinsM martins

              Found the solution in this thread:

              https://forum.mysensors.org/topic/2439/rfid-garage-door-opener/35

              void before() {
              // Make sure MFRC is disabled from the SPI bus
              pinMode(RST_PIN, OUTPUT);
              digitalWrite(RST_PIN, LOW);
              pinMode(SS_PIN, OUTPUT);
              digitalWrite(SS_PIN, LOW);
              }

              gohanG Offline
              gohanG Offline
              gohan
              Mod
              wrote on last edited by
              #6

              @martins
              Is it working for you?

              martinsM 1 Reply Last reply
              0
              • gohanG gohan

                @martins
                Is it working for you?

                martinsM Offline
                martinsM Offline
                martins
                wrote on last edited by
                #7

                @gohan Yes

                1 Reply Last reply
                0
                • gohanG Offline
                  gohanG Offline
                  gohan
                  Mod
                  wrote on last edited by
                  #8

                  I can't figure out what is wrong with mine

                  martinsM 1 Reply Last reply
                  0
                  • gohanG gohan

                    I can't figure out what is wrong with mine

                    martinsM Offline
                    martinsM Offline
                    martins
                    wrote on last edited by
                    #9

                    @gohan What is the serial output or error that you get ?

                    1 Reply Last reply
                    0
                    • gohanG Offline
                      gohanG Offline
                      gohan
                      Mod
                      wrote on last edited by
                      #10

                      just look at the garage door opener link you posted, there is log and code. If I unplug the radio and use some demo code to read the rfid tag I can read it, but I can't find where it conflicts with the radio

                      martinsM 1 Reply Last reply
                      0
                      • gohanG gohan

                        just look at the garage door opener link you posted, there is log and code. If I unplug the radio and use some demo code to read the rfid tag I can read it, but I can't find where it conflicts with the radio

                        martinsM Offline
                        martinsM Offline
                        martins
                        wrote on last edited by gohan
                        #11

                        @gohan This is the code that works for me on my pro mini. Try it out.

                        Here is my pins used:

                        MOSI: Pin 11
                        MISO: Pin 12
                        SCK : Pin 13
                        SS : Pin 8 (Configurable)
                        RST : Pin 6 (Configurable)

                        /*
                          Arduino RFID Access Control
                        
                          Security !
                        
                          To keep it simple we are going to use Tag's Unique IDs
                          as only method of Authenticity. It's simple and not hacker proof.
                          If you need security, don't use it unless you modify the code
                        
                          Copyright (C) 2015 Omer Siar Baysal
                        
                          This program is free software; you can redistribute it and/or modify
                          it under the terms of the GNU General Public License as published by
                          the Free Software Foundation; either version 2 of the License, or
                          (at your option) any later version.
                        
                          This program is distributed in the hope that it will be useful,
                          but WITHOUT ANY WARRANTY; without even the implied warranty of
                          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                          GNU General Public License for more details.
                        
                          You should have received a copy of the GNU General Public License along
                          with this program; if not, write to the Free Software Foundation, Inc.,
                          51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
                        
                        */
                        #define MY_DEBUG 
                        
                        #define MY_RADIO_NRF24
                        //#define MY_RF24_PA_LEVEL RF24_PA_LOW
                        
                        //#define MY_RADIO_RFM69
                        //#define MY_RFM69_FREQUENCY RF69_433MHZ
                        //#define MY_IS_RFM69HW
                        
                        #if F_CPU == 8000000L
                        #define MY_BAUD_RATE 38400
                        #endif
                        
                        
                        #define CHILD_ID 8
                        #define MAX_CARDS       18
                        
                        #include <EEPROM.h>     // We are going to read and write PICC's UIDs from/to EEPROM
                        #include <SPI.h>        // RC522 Module uses SPI protocol
                        #include <MFRC522.h>	// Library for Mifare RC522 Devices
                        #include <MySensors.h>
                        
                        /*
                        	For visualizing whats going on hardware
                        	we need some leds and
                        	to control door lock a relay and a wipe button
                        	(or some other hardware)
                        	Used common anode led,digitalWriting HIGH turns OFF led
                        	Mind that if you are going to use common cathode led or
                        	just seperate leds, simply comment out #define COMMON_ANODE,
                        */
                        
                        int value=0;
                        
                        boolean match = false;          // initialize card match to false
                        boolean programMode = false;	// initialize programming mode to false
                        boolean replaceMaster = false;
                        
                        int successRead;		// Variable integer to keep if we have Successful Read from Reader
                        
                        byte storedCard[4];		// Stores an ID read from EEPROM
                        byte readCard[4];		// Stores scanned ID read from RFID Module
                        byte masterCard[4];		// Stores master card's ID read from EEPROM
                        byte countValidCards = 0;
                        /*
                        	We need to define MFRC522's pins and create instance
                        	Pin layout should be as follows (on Arduino Uno):
                        	MOSI: Pin 11 / ICSP-4
                        	MISO: Pin 12 / ICSP-1
                        	SCK : Pin 13 / ICSP-3
                        	SS : Pin 10 (8)(Configurable)
                        	RST : Pin 9 (6)(Configurable)
                        	look MFRC522 Library for
                        	other Arduinos' pin configuration
                        */
                        
                        // bool lockStatus;
                        
                        #define SS_PIN 8
                        #define RST_PIN 6
                        
                        MFRC522 mfrc522(SS_PIN, RST_PIN);  // Create MFRC522 instance.
                        // MFRC522::Uid validCards[MAX_CARDS];
                        
                        //MyMessage lockMsg(CHILD_ID, V_LOCK_STATUS);
                        
                        MyMessage msg(CHILD_ID,V_TRIPPED);
                        
                        
                        // MFRC522 mfrc522(SS_PIN, RST_PIN);	// Create MFRC522 instance.
                        
                        
                        ///////////////////////////////////////// Setup ///////////////////////////////////
                        void setup() {
                          //Protocol Configuration
                          //Serial.begin(9600);	 // Initialize serial communications with PC
                          
                          pinMode(RST_PIN, OUTPUT);
                          digitalWrite(RST_PIN, LOW);
                          pinMode(SS_PIN, OUTPUT);
                          digitalWrite(SS_PIN, LOW);
                          
                          SPI.begin();           // MFRC522 Hardware uses SPI protocol
                          mfrc522.PCD_Init();    // Initialize MFRC522 Hardware
                        
                          //If you set Antenna Gain to Max it will increase reading distance
                          //mfrc522.PCD_SetAntennaGain(mfrc522.RxGain_max);
                        
                          Serial.println(F("Access Control v3.4"));   // For debugging purposes
                          ShowReaderDetails();	// Show details of PCD - MFRC522 Card Reader details
                        
                          if (EEPROM.read(500) != 143) {
                            Serial.println(F("No Master Card Defined"));
                            Serial.println(F("Scan A PICC to Define as Master Card"));
                            do {
                              successRead = getID();            // sets successRead to 1 when we get read from reader otherwise 0
                            }
                            while (!successRead);                  // Program will not go further while you not get a successful read
                        
                        //    printHex(mfrc522.uid.uidByte, mfrc522.uid.size);
                            
                            for ( int j = 0; j < 4; j++ ) {        // Loop 4 times
                              EEPROM.write( 2 + j, readCard[j] );  // Write scanned PICC's UID to EEPROM, start from address 3
                            }
                              
                            EEPROM.write(500, 143);                  // Write to EEPROM we defined Master Card.
                            delay(500);
                            
                            Serial.println(F("Master Card Defined"));
                        //    storeEeprom();
                          }
                          Serial.println(F("-------------------"));
                          Serial.println(F("Master Card's UID"));
                          for ( int i = 0; i < 4; i++ ) {          // Read Master Card's UID from EEPROM
                            masterCard[i] = EEPROM.read(2 + i);    // Write it to masterCard
                            Serial.print(masterCard[i], HEX);
                          }
                        
                          Serial.println("");
                          Serial.println(F("-------------------"));
                          Serial.println(F("Everything Ready"));
                          Serial.println(F("Waiting PICCs to be scanned"));
                          Serial.println(EEPROM.read(500));
                        }
                        
                        void presentation() {
                          // Register binary input sensor to gw (they will be created as child devices)
                          // You can use S_DOOR, S_MOTION or S_LIGHT here depending on your usage. 
                          // If S_LIGHT is used, remember to update variable type you send in. See "msg" above.
                          sendSketchInfo("RFID Gate", "1.0");
                          present(CHILD_ID, S_DOOR);  
                        }
                        
                        ///////////////////////////////////////// Main Loop ///////////////////////////////////
                        void loop () {
                          send(msg.set(value==HIGH ? 1 : 0));
                          do {
                            successRead = getID(); 	// sets successRead to 1 when we get read from reader otherwise 0
                          }
                          while (!successRead); 	//the program will not go further while you not get a successful read
                        
                          if (programMode) {
                            if ( isMaster(readCard) ) { //If master card scanned again exit program mode
                              Serial.println(F("Master Card Scanned"));
                              Serial.println(F("Exiting Program Mode"));
                              Serial.println(F("-----------------------------"));
                              programMode = false;
                              return;
                            }
                            else {
                              if ( findID(readCard) ) { // If scanned card is known delete it
                                Serial.println(F("I know this PICC, removing..."));
                                deleteID(readCard);
                                Serial.println("-----------------------------");
                                Serial.println(F("Scan a PICC to ADD or REMOVE to EEPROM"));
                              }
                              else {                    // If scanned card is not known add it
                                Serial.println(F("I do not know this PICC, adding..."));
                                writeID(readCard);
                                Serial.println(F("-----------------------------"));
                                Serial.println(F("Scan a PICC to ADD or REMOVE to EEPROM"));
                              }
                            }
                          }
                          else {
                            if ( isMaster(readCard)) {  	// If scanned card's ID matches Master Card's ID enter program mode
                              programMode = true;
                              Serial.println(F("Hello Master - Entered Program Mode"));
                              int count = EEPROM.read(0); 	// Read the first Byte of EEPROM that
                              Serial.print(F("I have "));    	// stores the number of ID's in EEPROM
                              Serial.print(count);
                              Serial.print(F(" record(s) on EEPROM"));
                              Serial.println("");
                              Serial.println(F("Scan a PICC to ADD or REMOVE to EEPROM"));
                              Serial.println(F("Scan Master Card again to Exit Program Mode"));
                              Serial.println(F("-----------------------------"));
                            }
                            else {
                              if ( findID(readCard) ) {	// If not, see if the card is in the EEPROM
                                Serial.println(F("Welcome, You shall pass"));
                                granted(300);        	// Open the door lock for 300 ms
                              }
                              else {			// If not, show that the ID was not valid
                                Serial.println(F("You shall not pass"));
                                denied();
                              }
                            }
                          }
                        }
                        
                        /////////////////////////////////////////  Access Granted    ///////////////////////////////////
                        void granted (int setDelay) {
                          // setLockState(!lockStatus, true);
                          value = 1;
                          send(msg.set(value==HIGH ? 1 : 0));
                          smartSleep(5000);
                          value = 0;
                          send(msg.set(value==HIGH ? 1 : 0));
                        }
                        
                        ///////////////////////////////////////// Access Denied  ///////////////////////////////////
                        void denied() {
                        }
                        
                        
                        ///////////////////////////////////////// Get PICC's UID ///////////////////////////////////
                        int getID() {
                          // Getting ready for Reading PICCs
                            if ( ! mfrc522.PICC_IsNewCardPresent())
                            return 0;
                        
                          // Verify if the NUID has been readed
                          if ( ! mfrc522.PICC_ReadCardSerial())
                            return 0;
                        
                          Serial.print(F("PICC type: "));
                          MFRC522::PICC_Type piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);
                          Serial.println(mfrc522.PICC_GetTypeName(piccType));
                        
                          // Check is the PICC of Classic MIFARE type
                          if (piccType != MFRC522::PICC_TYPE_MIFARE_MINI &&  piccType != MFRC522::PICC_TYPE_MIFARE_1K && piccType != MFRC522::PICC_TYPE_MIFARE_4K) {
                            Serial.println(F("Your tag is not of type MIFARE Classic."));
                            return;
                          }
                          Serial.println(F("Scanned PICC's UID:"));
                          for (int i = 0; i < 4; i++) {  //
                            readCard[i] = mfrc522.uid.uidByte[i];
                            Serial.print(readCard[i], HEX);
                          }
                          Serial.println("");
                          mfrc522.PICC_HaltA();
                          return 1;
                        
                        /*  while (! mfrc522.PICC_IsNewCardPresent()) 
                          {;}
                          if ( ! mfrc522.PICC_IsNewCardPresent()) 
                          { //If a new PICC placed to RFID reader continue
                            Serial.println("No NCP");
                            return 0;
                          }
                          if ( ! mfrc522.PICC_ReadCardSerial()) 
                          {   //Since a PICC placed get Serial and continue
                            Serial.println("RCS");
                            return 0;
                          }
                          // There are Mifare PICCs which have 4 byte or 7 byte UID care if you use 7 byte PICC
                          // I think we should assume every PICC as they have 4 byte UID
                          // Until we support 7 byte PICCs
                          Serial.println(F("Scanned PICC's UID:"));
                          for (int i = 0; i < 4; i++) {  //
                            readCard[i] = mfrc522.uid.uidByte[i];
                            Serial.print(readCard[i], HEX);
                          }
                          Serial.println("");
                          mfrc522.PICC_HaltA(); // Stop reading
                          return 1;
                          */
                          
                        }
                        
                        void ShowReaderDetails() {
                          // Get the MFRC522 software version
                          byte v = mfrc522.PCD_ReadRegister(mfrc522.VersionReg);
                          Serial.print(F("MFRC522 Software Version: 0x"));
                          Serial.print(v, HEX);
                          if (v == 0x91)
                            Serial.print(F(" = v1.0"));
                          else if (v == 0x92)
                            Serial.print(F(" = v2.0"));
                          else
                            Serial.print(F(" (unknown),probably a chinese clone?"));
                          Serial.println("");
                          // When 0x00 or 0xFF is returned, communication probably failed
                          if ((v == 0x00) || (v == 0xFF)) {
                            Serial.println(F("WARNING: Communication failure, is the MFRC522 properly connected?"));
                            Serial.println(F("SYSTEM HALTED: Check connections."));
                            while (true); // do not go further
                          }
                        }
                        
                        ///////////////////////////////////////// Cycle Leds (Program Mode) ///////////////////////////////////
                        void cycleLeds() {
                        }
                        
                        //////////////////////////////////////// Normal Mode Led  ///////////////////////////////////
                        void normalModeOn () {
                        }
                        
                        //////////////////////////////////////// Read an ID from EEPROM //////////////////////////////
                        void readID( int number ) {
                          int start = (number * 4 ) + 2; 		// Figure out starting position
                          for ( int i = 0; i < 4; i++ ) { 		// Loop 4 times to get the 4 Bytes
                            storedCard[i] = EEPROM.read(start + i); 	// Assign values read from EEPROM to array
                          }
                        }
                        
                        ///////////////////////////////////////// Add ID to EEPROM   ///////////////////////////////////
                        void writeID( byte a[] ) {
                          if ( !findID( a ) ) { 		// Before we write to the EEPROM, check to see if we have seen this card before!
                            int num = EEPROM.read(0); 		// Get the numer of used spaces, position 0 stores the number of ID cards
                            int start = ( num * 4 ) + 6; 	// Figure out where the next slot starts
                            num++; 								// Increment the counter by one
                            EEPROM.write( 0, num ); 		// Write the new count to the counter
                            for ( int j = 0; j < 4; j++ ) { 	// Loop 4 times
                              EEPROM.write( start + j, a[j] ); 	// Write the array values to EEPROM in the right position
                            }
                            successWrite();
                            Serial.println(F("Succesfully added ID record to EEPROM"));
                          }
                          else {
                            failedWrite();
                            Serial.println(F("Failed! There is something wrong with ID or bad EEPROM"));
                          }
                        }
                        
                        ///////////////////////////////////////// Remove ID from EEPROM   ///////////////////////////////////
                        void deleteID( byte a[] ) {
                          if ( !findID( a ) ) { 		// Before we delete from the EEPROM, check to see if we have this card!
                            failedWrite(); 			// If not
                            Serial.println(F("Failed! There is something wrong with ID or bad EEPROM"));
                          }
                          else {
                            int num = EEPROM.read(0); 	// Get the numer of used spaces, position 0 stores the number of ID cards
                            int slot; 			// Figure out the slot number of the card
                            int start;			// = ( num * 4 ) + 6; // Figure out where the next slot starts
                            int looping; 		// The number of times the loop repeats
                            int j;
                            int count = EEPROM.read(0); // Read the first Byte of EEPROM that stores number of cards
                            slot = findIDSLOT( a ); 	// Figure out the slot number of the card to delete
                            start = (slot * 4) + 2;
                            looping = ((num - slot) * 4);
                            num--; 			// Decrement the counter by one
                            EEPROM.write( 0, num ); 	// Write the new count to the counter
                            for ( j = 0; j < looping; j++ ) { 				// Loop the card shift times
                              EEPROM.write( start + j, EEPROM.read(start + 4 + j)); 	// Shift the array values to 4 places earlier in the EEPROM
                            }
                            for ( int k = 0; k < 4; k++ ) { 				// Shifting loop
                              EEPROM.write( start + j + k, 0);
                            }
                            successDelete();
                            Serial.println(F("Succesfully removed ID record from EEPROM"));
                          }
                        }
                        
                        ///////////////////////////////////////// Check Bytes   ///////////////////////////////////
                        boolean checkTwo ( byte a[], byte b[] ) {
                          if ( a[0] != NULL ) 			// Make sure there is something in the array first
                            match = true; 			// Assume they match at first
                          for ( int k = 0; k < 4; k++ ) { 	// Loop 4 times
                            if ( a[k] != b[k] ) 		// IF a != b then set match = false, one fails, all fail
                              match = false;
                          }
                          if ( match ) { 			// Check to see if if match is still true
                            return true; 			// Return true
                          }
                          else  {
                            return false; 			// Return false
                          }
                        }
                        
                        ///////////////////////////////////////// Find Slot   ///////////////////////////////////
                        int findIDSLOT( byte find[] ) {
                          int count = EEPROM.read(0); 			// Read the first Byte of EEPROM that
                          for ( int i = 1; i <= count; i++ ) { 		// Loop once for each EEPROM entry
                            readID(i); 								// Read an ID from EEPROM, it is stored in storedCard[4]
                            if ( checkTwo( find, storedCard ) ) { 	// Check to see if the storedCard read from EEPROM
                              // is the same as the find[] ID card passed
                              return i; 				// The slot number of the card
                              break; 					// Stop looking we found it
                            }
                          }
                        }
                        
                        ///////////////////////////////////////// Find ID From EEPROM   ///////////////////////////////////
                        boolean findID( byte find[] ) {
                          int count = EEPROM.read(0);			// Read the first Byte of EEPROM that
                          for ( int i = 1; i <= count; i++ ) {  	// Loop once for each EEPROM entry
                            readID(i); 					// Read an ID from EEPROM, it is stored in storedCard[4]
                            if ( checkTwo( find, storedCard ) ) {  	// Check to see if the storedCard read from EEPROM
                              return true;
                              break; 	// Stop looking we found it
                            }
                            else {  	// If not, return false
                            }
                          }
                          return false;
                        }
                        
                        ///////////////////////////////////////// Write Success to EEPROM   ///////////////////////////////////
                        // Flashes the green LED 3 times to indicate a successful write to EEPROM
                        void successWrite() {
                        }
                        
                        ///////////////////////////////////////// Write Failed to EEPROM   ///////////////////////////////////
                        // Flashes the red LED 3 times to indicate a failed write to EEPROM
                        void failedWrite() {
                        }
                        
                        ///////////////////////////////////////// Success Remove UID From EEPROM  ///////////////////////////////////
                        // Flashes the blue LED 3 times to indicate a success delete to EEPROM
                        void successDelete() {
                        }
                        
                        ////////////////////// Check readCard IF is masterCard   ///////////////////////////////////
                        // Check to see if the ID passed is the master programing card
                        boolean isMaster( byte test[] ) {
                          if ( checkTwo( test, masterCard ) )
                            return true;
                          else
                            return false;
                        }
                        
                        /**
                         * Helper routine to dump a byte array as hex values to Serial. 
                         */
                        void printHex(byte *buffer, byte bufferSize) {
                          for (byte i = 0; i < bufferSize; i++) {
                            Serial.print(buffer[i] < 0x10 ? " 0" : " ");
                            Serial.print(buffer[i], HEX);
                          }
                        }
                        
                        
                        //void presentation()  {
                        //  sendSketchInfo("RFID Lock", "1.0");
                        //  present(CHILD_ID, S_LOCK);
                        //}
                        
                        void before() {
                          // Make sure MFRC is disabled from the SPI bus
                          pinMode(RST_PIN, OUTPUT);
                          digitalWrite(RST_PIN, LOW);
                          pinMode(SS_PIN, OUTPUT);
                          digitalWrite(SS_PIN, LOW);
                        }
                        
                        
                        1 Reply Last reply
                        0
                        • gohanG Offline
                          gohanG Offline
                          gohan
                          Mod
                          wrote on last edited by
                          #12

                          Tnx, I'll try in the next days.

                          Ps I edited your post with the correct tag for the code

                          1 Reply Last reply
                          0
                          • Aron SjöbergA Offline
                            Aron SjöbergA Offline
                            Aron Sjöberg
                            wrote on last edited by
                            #13

                            Help!

                            I have a RFID RC522 module I'm trying to use instead of the PN532 NFC RFID Reader/Writer Module used in the RFID Lock Sensor example (https://www.mysensors.org/build/rfid).

                            I thought I could use the sketch above posted by @martins but I get the following error message. I'm a newbie so I don't really know what I'm doing wrong?

                            Arduino: 1.8.1 (Windows 10), Board: "Arduino Pro or Pro Mini, ATmega328 (5V, 16 MHz)"
                            
                            Build options changed, rebuilding all
                            C:\Users\arons\Documents\Arduino\Vera_RFID-RC522\Vera_RFID-RC522.ino: In function 'int getID()':
                            
                            Vera_RFID-RC522:259: error: return-statement with no value, in function returning 'int' [-fpermissive]
                            
                                 return;
                            
                                 ^
                            
                            exit status 1
                            return-statement with no value, in function returning 'int' [-fpermissive]
                            
                            martinsM 1 Reply Last reply
                            0
                            • Aron SjöbergA Aron Sjöberg

                              Help!

                              I have a RFID RC522 module I'm trying to use instead of the PN532 NFC RFID Reader/Writer Module used in the RFID Lock Sensor example (https://www.mysensors.org/build/rfid).

                              I thought I could use the sketch above posted by @martins but I get the following error message. I'm a newbie so I don't really know what I'm doing wrong?

                              Arduino: 1.8.1 (Windows 10), Board: "Arduino Pro or Pro Mini, ATmega328 (5V, 16 MHz)"
                              
                              Build options changed, rebuilding all
                              C:\Users\arons\Documents\Arduino\Vera_RFID-RC522\Vera_RFID-RC522.ino: In function 'int getID()':
                              
                              Vera_RFID-RC522:259: error: return-statement with no value, in function returning 'int' [-fpermissive]
                              
                                   return;
                              
                                   ^
                              
                              exit status 1
                              return-statement with no value, in function returning 'int' [-fpermissive]
                              
                              martinsM Offline
                              martinsM Offline
                              martins
                              wrote on last edited by
                              #14

                              @Aron-Sjöberg said in RFID and NRF24L01 Wireless Network Coding Issues:

                              int getID()

                              Change that line in the getID() function to return 0;

                              PlantexP 1 Reply Last reply
                              0
                              • martinsM martins

                                @Aron-Sjöberg said in RFID and NRF24L01 Wireless Network Coding Issues:

                                int getID()

                                Change that line in the getID() function to return 0;

                                PlantexP Offline
                                PlantexP Offline
                                Plantex
                                wrote on last edited by
                                #15

                                @martins

                                Could You please explain what do You mean by "Change that line in the getID() function to return 0;"

                                I am trying to run the sketch but I have the same problem as @Aron-Sjöberg.

                                Could You let me know which MFC library version I should use ?

                                1 Reply Last reply
                                0
                                • IspandyI Offline
                                  IspandyI Offline
                                  Ispandy
                                  wrote on last edited by
                                  #16

                                  Hi everyone,
                                  I have problem on RFID sketch, when i scan the card its unlock the door but when i switch from openhab its not unlock.

                                  the sketch is

                                  
                                  /*
                                     --------------------------------------------------------------------------------------------------------------------
                                     Example sketch/program showing An Arduino Door Access Control featuring RFID, EEPROM, Relay
                                     --------------------------------------------------------------------------------------------------------------------
                                     This is a MFRC522 library example; for further details and other examples see: https://github.com/miguelbalboa/rfid
                                     This example showing a complete Door Access Control System
                                    Simple Work Flow (not limited to) :
                                                                       +---------+
                                    +----------------------------------->READ TAGS+^------------------------------------------+
                                    |                              +--------------------+                                     |
                                    |                              |                    |                                     |
                                    |                              |                    |                                     |
                                    |                         +----v-----+        +-----v----+                                |
                                    |                         |MASTER TAG|        |OTHER TAGS|                                |
                                    |                         +--+-------+        ++-------------+                            |
                                    |                            |                 |             |                            |
                                    |                            |                 |             |                            |
                                    |                      +-----v---+        +----v----+   +----v------+                     |
                                    |         +------------+READ TAGS+---+    |KNOWN TAG|   |UNKNOWN TAG|                     |
                                    |         |            +-+-------+   |    +-----------+ +------------------+              |
                                    |         |              |           |                |                    |              |
                                    |    +----v-----+   +----v----+   +--v--------+     +-v----------+  +------v----+         |
                                    |    |MASTER TAG|   |KNOWN TAG|   |UNKNOWN TAG|     |GRANT ACCESS|  |DENY ACCESS|         |
                                    |    +----------+   +---+-----+   +-----+-----+     +-----+------+  +-----+-----+         |
                                    |                       |               |                 |               |               |
                                    |       +----+     +----v------+     +--v---+             |               +--------------->
                                    +-------+EXIT|     |DELETE FROM|     |ADD TO|             |                               |
                                            +----+     |  EEPROM   |     |EEPROM|             |                               |
                                                       +-----------+     +------+             +-------------------------------+
                                     Use a Master Card which is act as Programmer then you can able to choose card holders who will granted access or not
                                   * **Easy User Interface**
                                     Just one RFID tag needed whether Delete or Add Tags. You can choose to use Leds for output or Serial LCD module to inform users.
                                   * **Stores Information on EEPROM**
                                     Information stored on non volatile Arduino's EEPROM memory to preserve Users' tag and Master Card. No Information lost
                                     if power lost. EEPROM has unlimited Read cycle but roughly 100,000 limited Write cycle.
                                   * **Security**
                                     To keep it simple we are going to use Tag's Unique IDs. It's simple and not hacker proof.
                                     @license Released into the public domain.
                                     Typical pin layout used:
                                     -----------------------------------------------------------------------------------------
                                                 MFRC522      Arduino       Arduino   Arduino    Arduino          Arduino
                                                 Reader/PCD   Uno/101       Mega      Nano v3    Leonardo/Micro   Pro Micro
                                     Signal      Pin          Pin           Pin       Pin        Pin              Pin
                                     -----------------------------------------------------------------------------------------
                                     RST/Reset   RST          9             5         D9         RESET/ICSP-5     RST
                                     SPI SS      SDA(SS)      10            53        D10        10               10
                                     SPI MOSI    MOSI         11 / ICSP-4   51        D11        ICSP-4           16
                                     SPI MISO    MISO         12 / ICSP-1   50        D12        ICSP-1           14
                                     SPI SCK     SCK          13 / ICSP-3   52        D13        ICSP-3           15
                                  */
                                  #define MY_DEBUG
                                  #define MY_RADIO_RF24
                                  #define MY_NODE_ID 50
                                  
                                  #include <SPI.h>        // RC522 Module uses SPI protocol
                                  #include <MFRC522.h>  // Library for Mifare RC522 Devices
                                  #include <MySensors.h>
                                  
                                  /*
                                    Instead of a Relay you may want to use a servo. Servos can lock and unlock door locks too
                                    Relay will be used by default
                                  */
                                  #define RFID 1
                                  
                                  // #include <Servo.h>
                                  
                                  /*
                                    For visualizing whats going on hardware we need some leds and to control door lock a relay and a wipe button
                                    (or some other hardware) Used common anode led,digitalWriting HIGH turns OFF led Mind that if you are going
                                    to use common cathode led or just seperate leds, simply comment out #define COMMON_ANODE,
                                  */
                                  
                                  //#define COMMON_ANODE
                                  
                                  #ifdef COMMON_ANODE
                                  #define LED_ON LOW
                                  #define LED_OFF HIGH
                                  #else
                                  #define LED_ON HIGH
                                  #define LED_OFF LOW
                                  #endif
                                  
                                  #define redLed 4    // Set Led Pins
                                  #define greenLed 5
                                  #define blueLed 6
                                  
                                  #define relay 2       // Set Relay Pin
                                  #define wipeB 3     // Button pin for WipeMode
                                  
                                  bool programMode = false;  // initialize programming mode to false
                                  
                                  uint8_t successRead;    // Variable integer to keep if we have Successful Read from Reader
                                  
                                  byte storedCard[4];   // Stores an ID read from EEPROM
                                  byte readCard[4];   // Stores scanned ID read from RFID Module
                                  byte masterCard[4];   // Stores master card's ID read from EEPROM
                                  byte dumpedCard[4];
                                  
                                  static const uint8_t MAGIC = 168;
                                  
                                  // Create MFRC522 instance.
                                  #define SS_PIN 8
                                  #define RST_PIN 7
                                  MFRC522 mfrc522(SS_PIN, RST_PIN);
                                  
                                  void before() {
                                    // Make sure MFRC is disabled from the SPI bus
                                    pinMode(RST_PIN, OUTPUT);
                                    digitalWrite(RST_PIN, LOW);
                                    pinMode(SS_PIN, OUTPUT);
                                    digitalWrite(SS_PIN, LOW);
                                  }
                                  
                                  ///////////////////////////////////////// Setup ///////////////////////////////////
                                  void setup() {
                                     //Arduino Pin Configuration
                                    pinMode(redLed, OUTPUT);
                                    pinMode(greenLed, OUTPUT);
                                    pinMode(blueLed, OUTPUT);
                                    pinMode(wipeB, INPUT_PULLUP);   // Enable pin's pull up resistor
                                    pinMode(relay, OUTPUT);
                                    //Be careful how relay circuit behave on while resetting or power-cycling your Arduino
                                    digitalWrite(relay, HIGH);    // Make sure door is locked
                                    digitalWrite(redLed, LED_OFF);  // Make sure led is off
                                    digitalWrite(greenLed, LED_OFF);  // Make sure led is off
                                    digitalWrite(blueLed, LED_OFF); // Make sure led is off
                                  
                                    //Protocol Configuration
                                    Serial.begin(115200);  // Initialize serial communications with PC
                                    SPI.begin();           // MFRC522 Hardware uses SPI protocol
                                    mfrc522.PCD_Init();    // Initialize MFRC522 Hardware
                                  
                                    //If you set Antenna Gain to Max it will increase reading distance
                                    //mfrc522.PCD_SetAntennaGain(mfrc522.RxGain_max);
                                  
                                     Serial.println(F("Access Control v2.0"));   // For debugging purposes
                                    ShowReaderDetails();  // Show details of PCD - MFRC522 Card Reader details
                                    checkWipe();
                                  
                                    // Check if master card defined, if not let user choose a master card
                                    // This also useful to just redefine the Master Card
                                    // You can keep other EEPROM records just write other than MAGIC to EEPROM address 1
                                    // EEPROM address 1 should hold magical number which is 'MAGIC'
                                    if (loadState(1) != MAGIC) {
                                      Serial.println(F("No Master Card Defined"));
                                      Serial.println(F("Scan A PICC to Define as Master Card"));
                                      do {
                                        successRead = getID();            // sets successRead to 1 when we get read from reader otherwise 0
                                        digitalWrite(blueLed, LED_ON);    // Visualize Master Card need to be defined
                                        delay(200);
                                        digitalWrite(blueLed, LED_OFF);
                                        delay(200);
                                      }
                                      while (!successRead);                  // Program will not go further while you not get a successful read
                                      for ( uint8_t j = 0; j < 4; j++ ) {        // Loop 4 times
                                        saveState( 2 + j, readCard[j] );  // Write scanned PICC's UID to EEPROM, start from address 3
                                      }
                                      saveState(1, MAGIC);                  // Write to EEPROM we defined Master Card.
                                      Serial.println(F("Master Card Defined"));
                                    }
                                     Serial.println(F("-------------------"));
                                    Serial.println(F("Master Card's UID"));
                                    for ( uint8_t i = 0; i < 4; i++ ) {          // Read Master Card's UID from EEPROM
                                      masterCard[i] = loadState(2 + i);    // Write it to masterCard
                                      Serial.print(masterCard[i], HEX);
                                    }
                                    Serial.println("");
                                    Serial.println(F("-------------------"));
                                    Serial.println(F("Everything is ready"));
                                    Serial.println(F("Waiting PICCs to be scanned"));
                                    cycleLeds();    // Everything ready lets give user some feedback by cycling leds
                                  } 
                                  
                                  //////////////////////////////////////// MySensors Presentation ///////////////////////////////
                                  
                                  MyMessage lockMsg(RFID, V_LOCK_STATUS);
                                  void presentation() {
                                    // Register binary input sensor to gw (they will be created as child devices)
                                    // You can use S_DOOR, S_MOTION or S_LIGHT here depending on your usage. 
                                    // If S_LIGHT is used, remember to update variable type you send in. See "msg" above.
                                    sendSketchInfo("RFID PintuBesi", "1.0");
                                    present(RFID, S_LOCK);
                                  }
                                  
                                  ///////////////////////////////////////// Main Loop ///////////////////////////////////
                                  void loop () {
                                    do {
                                      successRead = getID();  // sets successRead to 1 when we get read from reader otherwise 0
                                      checkWipe();
                                      if (programMode) {
                                        cycleLeds();              // Program Mode cycles through Red Green Blue waiting to read a new card
                                      }
                                      else {
                                        normalModeOn();     // Normal mode, blue Power LED is on, all others are off
                                      }
                                    }
                                    while (!successRead);   //the program will not go further while you are not getting a successful read
                                    if (programMode) {
                                      if ( isMaster(readCard) ) { //When in program mode check First If master card scanned again to exit program mode
                                        Serial.println(F("Master Card Scanned"));
                                        Serial.println(F("Exiting Program Mode"));
                                        Serial.println(F("-----------------------------"));
                                        programMode = false;
                                        return;
                                      }
                                      else {
                                        if ( findID(readCard) ) { // If scanned card is known delete it
                                          Serial.println(F("I know this PICC, removing..."));
                                          deleteID(readCard);
                                          Serial.println("-----------------------------");
                                          Serial.println(F("Scan a PICC to ADD or REMOVE to EEPROM"));
                                        }
                                        else {                    // If scanned card is not known add it
                                          Serial.println(F("I do not know this PICC, adding..."));
                                          writeID(readCard);
                                          Serial.println(F("-----------------------------"));
                                          Serial.println(F("Scan a PICC to ADD or REMOVE to EEPROM"));
                                        }
                                      }
                                    }
                                    else {
                                      if ( isMaster(readCard)) {    // If scanned card's ID matches Master Card's ID - enter program mode
                                        programMode = true;
                                        Serial.println(F("Hello Master - Entered Program Mode"));
                                        uint8_t count = loadState(0);   // Read the first Byte of EEPROM that
                                        Serial.print(F("I have "));     // stores the number of ID's in EEPROM
                                        Serial.print(count);
                                        Serial.print(F(" record(s) on EEPROM"));
                                        Serial.println("");
                                        Serial.println(F("Scan a PICC to ADD or REMOVE to EEPROM"));
                                        Serial.println(F("Scan Master Card again to Exit Program Mode"));
                                        Serial.println(F("-----------------------------"));
                                      }
                                      else {
                                        if ( findID(readCard) ) { // If not, see if the card is in the EEPROM
                                          Serial.println(F("Welcome, You shall pass"));
                                          granted(300);         // Open the door lock for 300 ms
                                        }
                                        else {      // If not, show that the ID was not valid
                                          Serial.println(F("You shall not pass"));
                                          denied();
                                        }
                                      }
                                    }
                                  }
                                  
                                  /////////////////////////////////////////  Access Granted    ///////////////////////////////////
                                  void granted ( uint16_t setDelay) {
                                    digitalWrite(blueLed, LED_OFF);   // Turn off blue LED
                                    digitalWrite(redLed, LED_OFF);  // Turn off red LED
                                    digitalWrite(greenLed, LED_ON);   // Turn on green LED
                                    digitalWrite(relay, LOW);     // Unlock door!
                                    send(lockMsg.set(false));
                                    delay(setDelay);          // Hold door lock open for given seconds
                                    digitalWrite(relay, HIGH);    // Relock door
                                    send(lockMsg.set(true));
                                    delay(1000);            // Hold green LED on for a second
                                  }
                                  
                                  ///////////////////////////////////////// Access Denied  ///////////////////////////////////
                                  void denied() {
                                    digitalWrite(greenLed, LED_OFF);  // Make sure green LED is off
                                    digitalWrite(blueLed, LED_OFF);   // Make sure blue LED is off
                                    digitalWrite(redLed, LED_ON);   // Turn on red LED
                                    delay(1000);
                                  }
                                  
                                  
                                  ///////////////////////////////////////// Get PICC's UID ///////////////////////////////////
                                  uint8_t getID() {
                                    // Getting ready for Reading PICCs
                                    if ( ! mfrc522.PICC_IsNewCardPresent()) { //If a new PICC placed to RFID reader continue
                                      return 0;
                                    }
                                    if ( ! mfrc522.PICC_ReadCardSerial()) {   //Since a PICC placed get Serial and continue
                                      return 0;
                                    }
                                    // There are Mifare PICCs which have 4 byte or 7 byte UID care if you use 7 byte PICC
                                    // I think we should assume every PICC as they have 4 byte UID
                                    // Until we support 7 byte PICCs
                                    Serial.println(F("Scanned PICC's UID:"));
                                    for ( uint8_t i = 0; i < 4; i++) {  //
                                      readCard[i] = mfrc522.uid.uidByte[i];
                                      Serial.print(readCard[i], HEX);
                                    }
                                    Serial.println("");
                                    mfrc522.PICC_HaltA(); // Stop reading
                                    return 1;
                                  }
                                  
                                  void ShowReaderDetails() {
                                    // Get the MFRC522 software version
                                    byte v = mfrc522.PCD_ReadRegister(mfrc522.VersionReg);
                                    Serial.print(F("MFRC522 Software Version: 0x"));
                                    Serial.print(v, HEX);
                                    if (v == 0x91)
                                      Serial.print(F(" = v1.0"));
                                    else if (v == 0x92)
                                      Serial.print(F(" = v2.0"));
                                    else
                                      Serial.print(F(" (unknown),probably a chinese clone?"));
                                    Serial.println("");
                                    // When 0x00 or 0xFF is returned, communication probably failed
                                    if ((v == 0x00) || (v == 0xFF)) {
                                      Serial.println(F("WARNING: Communication failure, is the MFRC522 properly connected?"));
                                      Serial.println(F("SYSTEM HALTED: Check connections."));
                                      // Visualize system is halted
                                      digitalWrite(greenLed, LED_OFF);  // Make sure green LED is off
                                      digitalWrite(blueLed, LED_OFF);   // Make sure blue LED is off
                                      digitalWrite(redLed, LED_ON);   // Turn on red LED
                                      while (true); // do not go further
                                    }
                                  }
                                  
                                  ///////////////////////////////////////// Cycle Leds (Program Mode) ///////////////////////////////////
                                  void cycleLeds() {
                                    digitalWrite(redLed, LED_OFF);  // Make sure red LED is off
                                    digitalWrite(greenLed, LED_ON);   // Make sure green LED is on
                                    digitalWrite(blueLed, LED_OFF);   // Make sure blue LED is off
                                    delay(200);
                                    digitalWrite(redLed, LED_OFF);  // Make sure red LED is off
                                    digitalWrite(greenLed, LED_OFF);  // Make sure green LED is off
                                    digitalWrite(blueLed, LED_ON);  // Make sure blue LED is on
                                    delay(200);
                                    digitalWrite(redLed, LED_ON);   // Make sure red LED is on
                                    digitalWrite(greenLed, LED_OFF);  // Make sure green LED is off
                                    digitalWrite(blueLed, LED_OFF);   // Make sure blue LED is off
                                    delay(200);
                                  }
                                  
                                  //////////////////////////////////////// Normal Mode Led  ///////////////////////////////////
                                  void normalModeOn () {
                                    digitalWrite(blueLed, LED_ON);  // Blue LED ON and ready to read card
                                    digitalWrite(redLed, LED_OFF);  // Make sure Red LED is off
                                    digitalWrite(greenLed, LED_OFF);  // Make sure Green LED is off
                                    digitalWrite(relay, HIGH);    // Make sure Door is Locked
                                  }
                                  
                                  //////////////////////////////////////// Read an ID from EEPROM //////////////////////////////
                                  void readID( uint8_t number ) {
                                    uint8_t start = (number * 4 ) + 2;    // Figure out starting position
                                    loadCard(start, storedCard);
                                  }
                                  
                                  ///////////////////////////////////////// Add ID to EEPROM   ///////////////////////////////////
                                  void writeID( byte a[] ) {
                                    if ( !findID( a ) ) {     // Before we write to the EEPROM, check to see if we have seen this card before!
                                      uint8_t num = loadState(0);     // Get the numer of used spaces, position 0 stores the number of ID cards
                                      uint8_t start = ( num * 4 ) + 6;  // Figure out where the next slot starts
                                      num++;                // Increment the counter by one
                                      saveState( 0, num );     // Write the new count to the counter
                                      //for ( uint8_t j = 0; j < 4; j++ ) {   // Loop 4 times
                                      //  saveState( start + j, a[j] );  // Write the array values to EEPROM in the right position
                                      //}
                                      saveCard(start, a);
                                      successWrite();
                                      Serial.println(F("Succesfully added ID record to EEPROM"));
                                    }
                                    else {
                                      failedWrite();
                                      Serial.println(F("Failed! There is something wrong with ID or bad EEPROM"));
                                    }
                                  }
                                  ///////////////////////////////////////// Remove ID from EEPROM   ///////////////////////////////////
                                  void deleteID( byte a[] ) {
                                    if ( !findID( a ) ) {     // Before we delete from the EEPROM, check to see if we have this card!
                                      failedWrite();      // If not
                                      Serial.println(F("Failed! There is something wrong with ID or bad EEPROM"));
                                    }
                                    else {
                                      uint8_t num = loadState(0);   // Get the numer of used spaces, position 0 stores the number of ID cards
                                      uint8_t slot;       // Figure out the slot number of the card
                                      uint8_t start;      // = ( num * 4 ) + 6; // Figure out where the next slot starts
                                      uint8_t looping;    // The number of times the loop repeats
                                      uint8_t j;
                                      uint8_t count = loadState(0); // Read the first Byte of EEPROM that stores number of cards
                                      slot = findIDSLOT( a );   // Figure out the slot number of the card to delete
                                      start = (slot * 4) + 2;
                                      looping = ((num - slot) * 4);
                                      num--;      // Decrement the counter by one
                                      saveState( 0, num );   // Write the new count to the counter
                                      for ( j = 0; j < looping; j++ ) {         // Loop the card shift times
                                        saveState( start + j, loadState(start + 4 + j));   // Shift the array values to 4 places earlier in the EEPROM
                                      }
                                      for ( uint8_t k = 0; k < 4; k++ ) {         // Shifting loop
                                        saveState( start + j + k, 0);
                                      }
                                      successDelete();
                                      Serial.println(F("Succesfully removed ID record from EEPROM"));
                                    }
                                  }
                                  
                                  ///////////////////////////////////////// Check Bytes   ///////////////////////////////////
                                  bool checkTwo ( byte a[], byte b[] ) {   
                                    for ( uint8_t k = 0; k < 4; k++ ) {   // Loop 4 times
                                      if ( a[k] != b[k] ) {     // IF a != b then false, because: one fails, all fail
                                         return false;
                                      }
                                    }
                                    return true;  
                                  }
                                  
                                  ///////////////////////////////////////// Find Slot   ///////////////////////////////////
                                  uint8_t findIDSLOT( byte find[] ) {
                                    uint8_t count = loadState(0);       // Read the first Byte of EEPROM that
                                    for ( uint8_t i = 1; i <= count; i++ ) {    // Loop once for each EEPROM entry
                                      readID(i);                // Read an ID from EEPROM, it is stored in storedCard[4]
                                      if ( checkTwo( find, storedCard ) ) {   // Check to see if the storedCard read from EEPROM
                                        // is the same as the find[] ID card passed
                                        return i;         // The slot number of the card
                                      }
                                    }
                                  }
                                  
                                  ///////////////////////////////////////// Find ID From EEPROM   ///////////////////////////////////
                                  bool findID( byte find[] ) {
                                    uint8_t count = loadState(0);     // Read the first Byte of EEPROM that
                                    for ( uint8_t i = 1; i <= count; i++ ) {    // Loop once for each EEPROM entry
                                      readID(i);          // Read an ID from EEPROM, it is stored in storedCard[4]
                                      if ( checkTwo( find, storedCard ) )    // Check to see if the storedCard read from EEPROM
                                        return true;
                                        }
                                    return false;
                                  }
                                  
                                  ///////////////////////////////////////// Write Success to EEPROM   ///////////////////////////////////
                                  // Flashes the green LED 3 times to indicate a successful write to EEPROM
                                  void successWrite() {
                                    digitalWrite(blueLed, LED_OFF);   // Make sure blue LED is off
                                    digitalWrite(redLed, LED_OFF);  // Make sure red LED is off
                                    digitalWrite(greenLed, LED_OFF);  // Make sure green LED is on
                                    delay(200);
                                    digitalWrite(greenLed, LED_ON);   // Make sure green LED is on
                                    delay(200);
                                    digitalWrite(greenLed, LED_OFF);  // Make sure green LED is off
                                    delay(200);
                                    digitalWrite(greenLed, LED_ON);   // Make sure green LED is on
                                    delay(200);
                                    digitalWrite(greenLed, LED_OFF);  // Make sure green LED is off
                                    delay(200);
                                    digitalWrite(greenLed, LED_ON);   // Make sure green LED is on
                                    delay(200);
                                  }
                                  
                                  ///////////////////////////////////////// Write Failed to EEPROM   ///////////////////////////////////
                                  // Flashes the red LED 3 times to indicate a failed write to EEPROM
                                  void failedWrite() {
                                    digitalWrite(blueLed, LED_OFF);   // Make sure blue LED is off
                                    digitalWrite(redLed, LED_OFF);  // Make sure red LED is off
                                    digitalWrite(greenLed, LED_OFF);  // Make sure green LED is off
                                    delay(200);
                                    digitalWrite(redLed, LED_ON);   // Make sure red LED is on
                                    delay(200);
                                    digitalWrite(redLed, LED_OFF);  // Make sure red LED is off
                                    delay(200);
                                    digitalWrite(redLed, LED_ON);   // Make sure red LED is on
                                    delay(200);
                                    digitalWrite(redLed, LED_OFF);  // Make sure red LED is off
                                    delay(200);
                                    digitalWrite(redLed, LED_ON);   // Make sure red LED is on
                                    delay(200);
                                  }
                                  
                                  ///////////////////////////////////////// Success Remove UID From EEPROM  ///////////////////////////////////
                                  // Flashes the blue LED 3 times to indicate a success delete to EEPROM
                                  void successDelete() {
                                    digitalWrite(blueLed, LED_OFF);   // Make sure blue LED is off
                                    digitalWrite(redLed, LED_OFF);  // Make sure red LED is off
                                    digitalWrite(greenLed, LED_OFF);  // Make sure green LED is off
                                    delay(200);
                                    digitalWrite(blueLed, LED_ON);  // Make sure blue LED is on
                                    delay(200);
                                    digitalWrite(blueLed, LED_OFF);   // Make sure blue LED is off
                                    delay(200);
                                    digitalWrite(blueLed, LED_ON);  // Make sure blue LED is on
                                    delay(200);
                                    digitalWrite(blueLed, LED_OFF);   // Make sure blue LED is off
                                    delay(200);
                                    digitalWrite(blueLed, LED_ON);  // Make sure blue LED is on
                                    delay(200);
                                  }
                                  
                                  ////////////////////// Check readCard IF is masterCard   ///////////////////////////////////
                                  // Check to see if the ID passed is the master programing card
                                  bool isMaster( byte test[] ) {
                                    return checkTwo(test, masterCard);
                                  }
                                  
                                  bool monitorWipeButton(uint32_t interval) {
                                    uint32_t now = (uint32_t)millis();
                                    while ((uint32_t)millis() - now < interval)  {
                                      // check on every half a second
                                      if (((uint32_t)millis() % 500) == 0) {
                                        if (digitalRead(wipeB) != LOW)
                                          return false;
                                      }
                                    }
                                    return true;
                                  }
                                  
                                  void saveCard(uint8_t startPos, byte *card) {
                                    for ( uint8_t j = 0; j < 4; j++ ) {        // Loop 4 times
                                      saveState( startPos + j, card[j] );  // Write scanned PICC's UID to EEPROM, start from address 3
                                    }
                                  }
                                  
                                  void loadCard(uint8_t startPos, byte *card) {
                                    for ( uint8_t i = 0; i < 4; i++ )    // Read Card's UID from EEPROM
                                      card[i] = loadState(startPos + i);    // Write it to the card
                                  }
                                  
                                  void checkWipe() {
                                    //Wipe Code - If the Button (wipeB) Pressed while setup run (powered on) it wipes EEPROM
                                    if (digitalRead(wipeB) == LOW) {  // when button pressed pin should get low, button connected to ground
                                      digitalWrite(redLed, LED_ON); // Red Led stays on to inform user we are going to wipe
                                      Serial.println(F("Wipe Button Pressed"));
                                      Serial.println(F("You have 10 seconds to Cancel"));
                                      Serial.println(F("This will be remove all records and cannot be undone"));
                                      bool buttonState = monitorWipeButton(10000); // Give user enough time to cancel operation
                                      if (buttonState == true && digitalRead(wipeB) == LOW) {    // If button still be pressed, wipe EEPROM
                                        Serial.println(F("Starting Wiping EEPROM"));
                                         Serial.println(F("Starting Wiping"));
                                        saveState(0, 0 );     // light wipe. only erase number
                                        saveState(1, 0);      // light wipe. only erase magic number
                                        Serial.println(F("EEPROM Successfully Wiped"));
                                        digitalWrite(redLed, LED_OFF);  // visualize a successful wipe
                                        delay(200);
                                        digitalWrite(redLed, LED_ON);
                                        delay(200);
                                        digitalWrite(redLed, LED_OFF);
                                        delay(200);
                                        digitalWrite(redLed, LED_ON);
                                        delay(200);
                                        digitalWrite(redLed, LED_OFF);
                                      }
                                      else {
                                        Serial.println(F("Wiping Cancelled")); // Show some feedback that the wipe button did not pressed for 15 seconds
                                        digitalWrite(redLed, LED_OFF);
                                      }
                                    }
                                  }
                                  
                                  
                                  void receive(const MyMessage &message)
                                  {
                                    if (message.type == V_LOCK_STATUS) {
                                      // Change relay state
                                        if (!message.getBool()) {
                                        granted(300);
                                      }
                                      }
                                  }
                                  

                                  need advise, thanks
                                  regards

                                  1 Reply Last reply
                                  0
                                  • E Offline
                                    E Offline
                                    evb
                                    wrote on last edited by
                                    #17

                                    @mfalkvidd, should this not be better receive it's own topic?

                                    @Ispandy, does your node receives the message from openhab?
                                    Change the code in the receive method to be able to serial print debug messages. Maybe openhab is not using the correction message type...

                                    IspandyI 1 Reply Last reply
                                    0
                                    • E evb

                                      @mfalkvidd, should this not be better receive it's own topic?

                                      @Ispandy, does your node receives the message from openhab?
                                      Change the code in the receive method to be able to serial print debug messages. Maybe openhab is not using the correction message type...

                                      IspandyI Offline
                                      IspandyI Offline
                                      Ispandy
                                      wrote on last edited by
                                      #18

                                      @evb, thank you for the reply
                                      In that sketch above the node doesn't receive message from openhab, but the other node using same message type "message.type == V_LOCK_STATUS" it does receive message from openhab.

                                      E 1 Reply Last reply
                                      0
                                      • IspandyI Ispandy

                                        @evb, thank you for the reply
                                        In that sketch above the node doesn't receive message from openhab, but the other node using same message type "message.type == V_LOCK_STATUS" it does receive message from openhab.

                                        E Offline
                                        E Offline
                                        evb
                                        wrote on last edited by
                                        #19

                                        @Ispandy are you now speaking about 2 nodes?

                                        In that sketch above the node doesn't receive message from openhab
                                        Did you add serial prints to be sure you don't receive anything. May you receive messages but not of the type V_LOCK_STATUS.

                                        You should explain more in detail how your setup is done, because for me it isn't clear

                                        IspandyI 1 Reply Last reply
                                        0
                                        • E evb

                                          @Ispandy are you now speaking about 2 nodes?

                                          In that sketch above the node doesn't receive message from openhab
                                          Did you add serial prints to be sure you don't receive anything. May you receive messages but not of the type V_LOCK_STATUS.

                                          You should explain more in detail how your setup is done, because for me it isn't clear

                                          IspandyI Offline
                                          IspandyI Offline
                                          Ispandy
                                          wrote on last edited by
                                          #20

                                          @evb yes i have 2 nodes (node 3 and node 50) all with MRFC522 right now, the node 3 is working perfectly but i dont like add and remove card system, I just want to change the part of the RFID sketch on node 3 using the sketch above, so i make one node for test (node 50) and put the code

                                          send(lockMsg.set(false));
                                          

                                          and

                                          send(lockMsg.set(true));
                                          

                                          to this

                                          void granted ( uint16_t setDelay) {
                                            digitalWrite(blueLed, LED_OFF);   // Turn off blue LED
                                            digitalWrite(redLed, LED_OFF);  // Turn off red LED
                                            digitalWrite(greenLed, LED_ON);   // Turn on green LED
                                            digitalWrite(relay, LOW);     // Unlock door!
                                            send(lockMsg.set(false));
                                            delay(setDelay);          // Hold door lock open for given seconds
                                            digitalWrite(relay, HIGH);    // Relock door
                                            send(lockMsg.set(true));
                                            delay(1000);            // Hold green LED on for a second
                                          }
                                          

                                          and i add this to node 50

                                          if (message.type == V_LOCK_STATUS) {
                                              // Change relay state
                                              if (!message.getBool()) {
                                                granted(300);
                                              }
                                          
                                              // Write some debug info
                                              Serial.print(F("Lock status: "));
                                              Serial.println(message.getBool());
                                            }
                                              else
                                              {
                                                // Write some debug info
                                                Serial.print(F("Incoming msg type: "));
                                                Serial.print(message.type);
                                                Serial.print(F(" id: "));
                                                Serial.print(message.sensor);
                                                Serial.print(F(" content: "));
                                                Serial.println(message.getInt());
                                              }
                                          

                                          nothing happen...

                                          sketch Node 3

                                          /
                                          // Enable debug prints
                                          #define MY_DEBUG
                                          #define MY_NODE_ID 3
                                          // Enable and select radio type attached
                                          #define MY_RADIO_RF24
                                          //#define MY_RF24_CHANNEL  10 
                                          #include <MySensors.h>
                                          #include <SPI.h>
                                          #include <MFRC522.h> 
                                          #include <Bounce2.h>
                                          
                                          #define RF_INIT_DELAY   125
                                          #define ONE_SEC         1000
                                          #define MAX_CARDS       18
                                          #define PROG_WAIT       10
                                          #define HEARTBEAT       10
                                          #define BAUD            115200
                                          
                                          /*Pin definitions*/
                                          #define LED_PIN         4
                                          #define GARAGEPIN       2
                                          #define SWITCH_PIN      3
                                          #define RST_PIN   7   //  MFRC 
                                          #define SS_PIN    8   //  MFRC 
                                          
                                          MFRC522      mfrc522(SS_PIN, RST_PIN);  // Create MFRC522 instance
                                          MFRC522::Uid olduid;
                                          MFRC522::Uid masterkey = { 4,{ 0xEF,0xDB,0x8E,0x79 },0 };
                                          
                                          byte       countValidCards = 0;
                                          MFRC522::Uid validCards[MAX_CARDS];
                                          
                                          void       ShowCardData(MFRC522::Uid* uid);
                                          bool       sameUid(MFRC522::Uid* old, MFRC522::Uid* check);
                                          void       copyUid(MFRC522::Uid* src, MFRC522::Uid* dest);
                                          bool       isValidCard(MFRC522::Uid* uid);
                                          int        releasecnt = 0;
                                          
                                          //#define    CHILD_ID_ALARM    2
                                          #define    CHILD_ID_LOCK     1
                                          Bounce     debouncer = Bounce();
                                          
                                          int        oldSwitchValue = -1;
                                          int        switchValue = 0;
                                          long       timer = -1;
                                          bool       programmode = false;
                                          bool       ledon;
                                          int        programTimer = 0;
                                          bool       armed = true;
                                          unsigned long lastTime = 0;
                                          
                                          MyMessage  lockMsg(CHILD_ID_LOCK, V_LOCK_STATUS);
                                          //MyMessage  lockArmMsg(CHILD_ID_ALARM, V_ARMED);
                                          //MyMessage  wrongMsg(CHILD_ID_ALARM, V_TRIPPED);
                                          
                                          void before() {
                                            // Make sure MFRC is disabled from the SPI bus
                                            pinMode(RST_PIN, OUTPUT);
                                            digitalWrite(RST_PIN, LOW);
                                            pinMode(SS_PIN, OUTPUT);
                                            digitalWrite(SS_PIN, LOW);
                                          }
                                          
                                          void presentation() {
                                            sendSketchInfo("RFID Garage", "1.1"); delay(RF_INIT_DELAY);
                                            // Register all sensors to gw (they will be created as child devices)
                                          
                                            present(CHILD_ID_LOCK, S_LOCK);      delay(RF_INIT_DELAY);
                                            //present(CHILD_ID_ALARM, S_MOTION);   delay(RF_INIT_DELAY);
                                          }
                                          
                                          void setup() {
                                            Serial.begin(BAUD);   // Initialize serial communications with the PC
                                            pinMode(GARAGEPIN, OUTPUT);     // Initialise in/output ports
                                          
                                                            // Make sure MFRC will be disabled on the SPI bus
                                            /*pinMode(RST_PIN, OUTPUT);
                                            digitalWrite(RST_PIN, LOW);
                                            pinMode(SS_PIN, OUTPUT);
                                            digitalWrite(SS_PIN, LOW);*/
                                          
                                            pinMode(LED_PIN, OUTPUT);
                                            digitalWrite(LED_PIN, LOW);
                                            // Setup the button
                                            pinMode(SWITCH_PIN, INPUT);
                                            // Activate internal pull-up
                                            digitalWrite(SWITCH_PIN, HIGH);
                                          
                                            // After setting up the button, setup debouncer
                                            debouncer.attach(SWITCH_PIN);
                                            debouncer.interval(5);
                                          
                                            // Init mysensors library
                                            
                                            /*sendSketchInfo("RFID Garage", "1.1"); delay(RF_INIT_DELAY);
                                          
                                            // Register all sensors to gw (they will be created as child devices)
                                            present(CHILD_ID_LOCK, S_LOCK);      delay(RF_INIT_DELAY);
                                            present(CHILD_ID_ALARM, S_MOTION);   delay(RF_INIT_DELAY);*/
                                          
                                            recallEeprom();
                                          
                                            // Init MFRC RFID sensor
                                            SPI.begin();      // Init SPI bus
                                            mfrc522.PCD_Init();   // Init MFRC522
                                            ShowReaderDetails();          // Show details of PCD - MFRC522 Card Reader details
                                          
                                            //send(lockArmMsg.set(armed));
                                            Serial.println(F("Init done..."));
                                          }
                                          
                                          void loop() {
                                            timer++;
                                            delay(HEARTBEAT);
                                            debouncer.update();
                                          
                                            // Get the update value
                                            int switchValue = debouncer.read();
                                            if (switchValue != oldSwitchValue) {
                                              // Send in the new value
                                              Serial.print(F("Switch "));
                                              Serial.println(switchValue);
                                          
                                              if (switchValue && programmode) {
                                                lastTime = millis() / 1000;
                                              }
                                          
                                              if (!switchValue && programmode && lastTime > 0) {
                                                if ((millis() / 1000) - lastTime > 3) {
                                                  Serial.println(F("Reset all cards"));
                                                  countValidCards = 0;
                                                  blinkFast(50);
                                                }
                                                else {
                                                  Serial.println(F("Program off"));
                                                  digitalWrite(LED_PIN, LOW);
                                                  programmode = false;
                                          
                                                  storeEeprom();
                                                }
                                              }
                                          
                                              if (!switchValue) {
                                                programTimer = 0;
                                              }
                                              oldSwitchValue = switchValue;
                                            }
                                          
                                            if (programmode && ((timer % (ONE_SEC / HEARTBEAT)) == 0)) {
                                              ledon = !ledon;
                                              digitalWrite(LED_PIN, ledon);
                                              programTimer++;
                                          
                                              // Stop program mode after 20 sec inactivity
                                              if (programTimer > PROG_WAIT) {
                                                programmode = false;
                                                digitalWrite(LED_PIN, false);
                                                Serial.println(F("Program expired"));
                                              }
                                            }
                                          
                                            if ((timer % (200 / HEARTBEAT)) == 0) {
                                              // Look for new cards
                                              if (!mfrc522.PICC_IsNewCardPresent()) {
                                                if (releasecnt > 0) {
                                                  releasecnt--;
                                                  if (!releasecnt) {
                                                    olduid.size = 0;
                                                    Serial.println(F("release"));
                                                  }
                                                }
                                                return;
                                              }
                                              releasecnt = 5;
                                          
                                              // Select one of the cards
                                              if (!mfrc522.PICC_ReadCardSerial()) {
                                                return;
                                              }
                                          
                                              // Dump debug info about the card; PICC_HaltA() is automatically called
                                              //mfrc522.PICC_DumpToSerial(&(mfrc522.uid));
                                              if (!olduid.size || !sameUid(&(mfrc522.uid), &olduid)) {
                                                ShowCardData(&(mfrc522.uid));
                                                copyUid(&(mfrc522.uid), &olduid);
                                                if (isValidCard(&olduid)) {
                                                  OpenDoor(false);
                                                }
                                                else {
                                          
                                                  if (sameUid(&(mfrc522.uid), &masterkey)) {
                                                    // Only switch in program mode when mastercard is found AND the program button is pressed
                                                    if (switchValue) {
                                                      Serial.println(F("Program mode"));
                                                      programmode = true;
                                                      programTimer = 0;
                                                      lastTime = 0;
                                                    }
                                                  }
                                                  else {
                                                    if (programmode) {
                                                      Serial.println(F("new card"));
                                                      programTimer = 0;
                                          
                                                      if (countValidCards < MAX_CARDS)
                                                      {
                                                        // Add card to list...
                                                        copyUid(&(mfrc522.uid), &validCards[countValidCards]);
                                                        countValidCards++;
                                                        blinkFast(15);
                                                      }
                                                    }
                                                    else {
                                                      Serial.println(F("Invalid card"));
                                                      //if (armed) {
                                                       // send(wrongMsg.set(1));
                                                       // delay(2000);
                                                        //send(wrongMsg.set(0));
                                                      //}
                                                    }
                                                  }
                                                }
                                              }
                                            }
                                          }
                                          
                                          void ShowCardData(MFRC522::Uid* uid) {
                                            Serial.print(F("Card UID:"));
                                            for (byte i = 0; i < uid->size; i++) {
                                              if (uid->uidByte[i] < 0x10) {
                                                Serial.print(F(" 0"));
                                              }
                                              else {
                                                Serial.print(F(" "));
                                              }
                                              Serial.print(uid->uidByte[i], HEX);
                                            }
                                            Serial.println();
                                          }
                                          
                                          void copyUid(MFRC522::Uid* src, MFRC522::Uid* dest)
                                          {
                                            dest->size = src->size;
                                            dest->sak = src->sak;
                                          
                                            for (byte i = 0; i < src->size; i++) {
                                              dest->uidByte[i] = src->uidByte[i];
                                            }
                                          }
                                          
                                          bool sameUid(MFRC522::Uid* old, MFRC522::Uid* check)
                                          {
                                            if (old->size != check->size) {
                                              return false;
                                            }
                                            for (byte i = 0; i < old->size; i++) {
                                              if (old->uidByte[i] != check->uidByte[i]) {
                                                return false;
                                              }
                                            }
                                            return true;
                                          }
                                          
                                          bool isValidCard(MFRC522::Uid* uid)
                                          {
                                            for (byte i = 0; i < countValidCards; i++) {
                                              if (validCards[i].size != uid->size) {
                                                break;
                                              }
                                              for (int j = 0; j < uid->size; j++) {
                                                if (validCards[i].uidByte[j] != uid->uidByte[j]) {
                                                  break;
                                                }
                                                if (j == (uid->size - 1)) {
                                                  return true;
                                                }
                                              }
                                            }
                                            return false;
                                          }
                                          
                                          
                                          void storeEeprom()
                                          {
                                            byte address = 0;
                                            saveState(address++, countValidCards);
                                          
                                            for (byte i = 0; i < countValidCards; i++) {
                                              saveState(address++, validCards[i].size);
                                              for (byte j = 0; j < 10; j++) {
                                                saveState(address++, validCards[i].uidByte[j]);
                                              }
                                            }
                                          }
                                          
                                          void recallEeprom()
                                          {
                                            byte address = 0;
                                          
                                            countValidCards = loadState(address++);
                                            if (countValidCards > MAX_CARDS) {
                                              Serial.println(F("Not a valid EEPROM reading set to default"));
                                              countValidCards = 0;
                                              storeEeprom();
                                              return;
                                            }
                                          
                                            for (byte i = 0; i < countValidCards; i++) {
                                              validCards[i].size = loadState(address++);
                                              for (byte j = 0; j < 10; j++) {
                                                validCards[i].uidByte[j] = loadState(address++);
                                              }
                                            }
                                          
                                          }
                                          
                                          void blinkFast(int times)
                                          {
                                            for (int i = 0; i < times; i++) {
                                              ledon = !ledon;
                                              digitalWrite(LED_PIN, ledon);
                                              delay(100);
                                            }
                                          }
                                          
                                          void OpenDoor(bool fakeOpen)
                                          {
                                            Serial.println(F("Open door!"));
                                            send(lockMsg.set(false));
                                          
                                            if (!fakeOpen) {
                                              digitalWrite(LED_PIN, HIGH);
                                              digitalWrite(GARAGEPIN, HIGH);
                                            }
                                            
                                            delay(1000);
                                          
                                            if (!fakeOpen) {
                                              digitalWrite(GARAGEPIN, LOW);
                                              digitalWrite(LED_PIN, LOW);
                                            }
                                          
                                            send(lockMsg.set(true));
                                          }
                                          
                                          void ShowReaderDetails() {
                                            // Get the MFRC522 software version
                                            byte v = mfrc522.PCD_ReadRegister(mfrc522.VersionReg);
                                            Serial.print(F("MFRC522 Software Version: 0x"));
                                            Serial.print(v, HEX);
                                            if (v == 0x91) {
                                              Serial.print(F(" = v1.0"));
                                            }
                                            else if (v == 0x92) {
                                              Serial.print(F(" = v2.0"));
                                            }
                                            else {
                                              Serial.print(F(" (unknown)"));
                                            }
                                            Serial.println("");
                                          
                                            // When 0x00 or 0xFF is returned, communication probably failed
                                            if ((v == 0x00) || (v == 0xFF)) {
                                              Serial.println(F("WARNING: Communication failure, is the MFRC522 properly connected?"));
                                            }
                                          }
                                          
                                          
                                          void receive(const MyMessage &message)
                                          {
                                            if (message.type == V_LOCK_STATUS) {
                                              // Change relay state
                                              if (!message.getBool()) {
                                                OpenDoor(false);
                                              }
                                          
                                              // Write some debug info
                                              Serial.print(F("Lock status: "));
                                              Serial.println(message.getBool());
                                            }
                                            /*else
                                            {
                                              if (message.type == V_ARMED) {
                                                // Change relay state
                                                armed = message.getBool();
                                          
                                                // Write some debug info
                                                Serial.print(F("Arm status: "));
                                                Serial.println(message.getBool());
                                              }*/
                                              else
                                              {
                                                // Write some debug info
                                                Serial.print(F("Incoming msg type: "));
                                                Serial.print(message.type);
                                                Serial.print(F(" id: "));
                                                Serial.print(message.sensor);
                                                Serial.print(F(" content: "));
                                                Serial.println(message.getInt());
                                              }
                                            }
                                          //}
                                          

                                          I'm not good at programming that's why I'm asking for help:expressionless: :expressionless:

                                          E 1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          27

                                          Online

                                          11.7k

                                          Users

                                          11.2k

                                          Topics

                                          113.1k

                                          Posts


                                          Copyright 2025 TBD   |   Forum Guidelines   |   Privacy Policy   |   Terms of Service
                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • MySensors
                                          • OpenHardware.io
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular