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 sketch

Rfid sketch

Scheduled Pinned Locked Moved Troubleshooting
9 Posts 3 Posters 1.5k Views 4 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.
  • T Offline
    T Offline
    The-Holgi
    wrote on last edited by
    #1

    Hello,
    can anyone help me to change the

    The relay should only put on for 3 seconds.

    /**
     * The MySensors Arduino library handles the wireless radio link and protocol
     * between your home built sensors/actuators and HA controller of choice.
     * The sensors forms a self healing radio network with optional repeaters. Each
     * repeater and gateway builds a routing tables in EEPROM which keeps track of the
     * network topology allowing messages to be routed to nodes.
     *
     * Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
     * Copyright (C) 2013-2015 Sensnology AB
     * Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
     *
     * Documentation: http://www.mysensors.org
     * Support Forum: http://forum.mysensors.org
     *
     * This program is free software; you can redistribute it and/or
     * modify it under the terms of the GNU General Public License
     * version 2 as published by the Free Software Foundation.
     *
     *******************************
     *
     * REVISION HISTORY
     * Version 1.0 - Henrik Ekblad
     * 
     * DESCRIPTION
     * RFID Lock sensor/actuator
     * 
     * Use RFID tag to lock/unlock a door or trigger a scene on your controller.
     * This example sketch allows you to add an optional relay or solenoid 
     * which can be activated/opened by RFID or controller.  
     *
     * Use the I2C wiring option for your RFID module and connect to the following Arduino pins.
     * 
     * RFID       Arduino
     * -----      -------
     * GND   ->   GND
     * VCC   ->   +5V
     * SCL   ->   A5
     * SDA   ->   A4
     * 
     * Use normal wiring for NRF24L01 radio
     * 
     * Attach a optional relay or solonoid lock to pin 4
     * http://www.mysensors.org/build/rfid 
     */
    
    // Enable debug prints to serial monitor
    #define MY_DEBUG 
    
    // Enable and select radio type attached
    #define MY_RADIO_NRF24
    //#define MY_RADIO_RFM69
    
    #include <SPI.h>
    #include <MySensors.h>  
    #include <Wire.h>
    #include <PN532_I2C.h>
    #include <PN532.h>
    
    
    // Add your valid rfid keys here. To find you your key just run sketch; hold your new RFID tag in fron ot the reader; 
    // and copy the key from serial output of this sketch.
    const uint8_t maxKeyLength = 7;
    uint8_t validKeys[][maxKeyLength] = {
                        { 0xB3, 0xC6, 0xD9, 0x80, 0x00, 0x00, 0x00 },
                        { 0, 0, 0, 0, 0, 0, 0 },    // ADD YOUR KEYS HERE!
                        { 0, 0, 0, 0, 0, 0, 0 }};
    int keyCount = sizeof validKeys / maxKeyLength; 
    
    
    #define CHILD_ID 99   // Id of the sensor child
    
    // Pin definition
    const int lockPin = 4;         // (Digital 4) The pin that activates the relay/solenoid lock.
    bool lockStatus;
    MyMessage lockMsg(CHILD_ID, V_LOCK_STATUS);
    PN532_I2C pn532i2c(Wire);
    PN532 nfc(pn532i2c);
    
    void setup() {
    
      pinMode(lockPin, OUTPUT);
    
      nfc.begin();
      uint32_t versiondata = nfc.getFirmwareVersion();
      if (! versiondata) {
        Serial.print("Couldn't find PN53x board");
        while (1); // halt
      }
      Serial.print("Found NFC chip PN5"); Serial.println((versiondata>>24) & 0xFF, HEX); 
      Serial.print("Firmware ver. "); Serial.print((versiondata>>16) & 0xFF, DEC); 
      Serial.print('.'); Serial.println((versiondata>>8) & 0xFF, DEC);
      // Set the max number of retry attempts to read from a card
      // This prevents us from waiting forever for a card, which is
      // the default behaviour of the PN532.
      nfc.setPassiveActivationRetries(0x3);
    
      // configure board to read RFID tags
      nfc.SAMConfig();
    
      lockStatus = loadState(0);    // Read last lock status from eeprom
      setLockState(lockStatus, true); // Now set the last known state and send it to controller 
    }
    
    void presentation()  {
      sendSketchInfo("RFID Lock", "1.0");
      present(CHILD_ID, S_LOCK);
    }
    
    void loop() {
      bool success;
      uint8_t key[] = { 0, 0, 0, 0, 0, 0, 0 };  // Buffer to store the returned UID
      uint8_t currentKeyLength;                        // Length of the UID (4 or 7 bytes depending on ISO14443A card type)
    
    
      // Wait for an ISO14443A type cards (Mifare, etc.).  When one is found
      // 'uid' will be populated with the UID, and uidLength will indicate
      // if the uid is 4 bytes (Mifare Classic) or 7 bytes (Mifare Ultralight)
      success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, &key[0], &currentKeyLength);
    
      if (success) {
        Serial.print("Found tag id: ");
        for (uint8_t i=0; i < currentKeyLength; i++) 
        {
          if (i>0) Serial.print(",");
          Serial.print("0x");Serial.print(key[i], HEX); 
        }
        for (uint8_t i=currentKeyLength; i < maxKeyLength; i++) 
        {
          Serial.print(",0x00"); 
        }
    
    
        Serial.println("");
    
        bool valid = false;
        // Compare this key to the valid once registered here in sketch 
        for (int i=0;i<keyCount && !valid;i++) {
          for (int j=0;j<currentKeyLength && !valid;j++) {
            if (key[j] != validKeys[i][j]) {
              break;
            }
            if (j==currentKeyLength-1) {
              valid = true;
            }
          }
        }
        if (valid) {
          // Switch lock status
          setLockState(!lockStatus, true);       
        }
    
        // Wait for card/tag to leave reader    
        while(nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, &key[0], &currentKeyLength));
      } 
    } 
    
    
    
    // Unlocks the door.
    void setLockState(bool state, bool doSend){
      if (state) 
         Serial.println("open lock");
      else
         Serial.println("close lock");
      if (doSend)
        send(lockMsg.set(state));
      digitalWrite(lockPin, state);
      saveState(0,state);
      lockStatus = state;
    }
    
    void receive(const MyMessage &message) {
      // We only expect one type of message from controller. But we better check anyway.
      if (message.type==V_LOCK_STATUS) {
         // Change relay state
         setLockState(message.getBool(), false); 
    
         // Write some debug info
         Serial.print("Incoming lock status:");
         Serial.println(message.getBool());
       } 
    }l```
    mfalkviddM 1 Reply Last reply
    0
    • T The-Holgi

      Hello,
      can anyone help me to change the

      The relay should only put on for 3 seconds.

      /**
       * The MySensors Arduino library handles the wireless radio link and protocol
       * between your home built sensors/actuators and HA controller of choice.
       * The sensors forms a self healing radio network with optional repeaters. Each
       * repeater and gateway builds a routing tables in EEPROM which keeps track of the
       * network topology allowing messages to be routed to nodes.
       *
       * Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
       * Copyright (C) 2013-2015 Sensnology AB
       * Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
       *
       * Documentation: http://www.mysensors.org
       * Support Forum: http://forum.mysensors.org
       *
       * This program is free software; you can redistribute it and/or
       * modify it under the terms of the GNU General Public License
       * version 2 as published by the Free Software Foundation.
       *
       *******************************
       *
       * REVISION HISTORY
       * Version 1.0 - Henrik Ekblad
       * 
       * DESCRIPTION
       * RFID Lock sensor/actuator
       * 
       * Use RFID tag to lock/unlock a door or trigger a scene on your controller.
       * This example sketch allows you to add an optional relay or solenoid 
       * which can be activated/opened by RFID or controller.  
       *
       * Use the I2C wiring option for your RFID module and connect to the following Arduino pins.
       * 
       * RFID       Arduino
       * -----      -------
       * GND   ->   GND
       * VCC   ->   +5V
       * SCL   ->   A5
       * SDA   ->   A4
       * 
       * Use normal wiring for NRF24L01 radio
       * 
       * Attach a optional relay or solonoid lock to pin 4
       * http://www.mysensors.org/build/rfid 
       */
      
      // Enable debug prints to serial monitor
      #define MY_DEBUG 
      
      // Enable and select radio type attached
      #define MY_RADIO_NRF24
      //#define MY_RADIO_RFM69
      
      #include <SPI.h>
      #include <MySensors.h>  
      #include <Wire.h>
      #include <PN532_I2C.h>
      #include <PN532.h>
      
      
      // Add your valid rfid keys here. To find you your key just run sketch; hold your new RFID tag in fron ot the reader; 
      // and copy the key from serial output of this sketch.
      const uint8_t maxKeyLength = 7;
      uint8_t validKeys[][maxKeyLength] = {
                          { 0xB3, 0xC6, 0xD9, 0x80, 0x00, 0x00, 0x00 },
                          { 0, 0, 0, 0, 0, 0, 0 },    // ADD YOUR KEYS HERE!
                          { 0, 0, 0, 0, 0, 0, 0 }};
      int keyCount = sizeof validKeys / maxKeyLength; 
      
      
      #define CHILD_ID 99   // Id of the sensor child
      
      // Pin definition
      const int lockPin = 4;         // (Digital 4) The pin that activates the relay/solenoid lock.
      bool lockStatus;
      MyMessage lockMsg(CHILD_ID, V_LOCK_STATUS);
      PN532_I2C pn532i2c(Wire);
      PN532 nfc(pn532i2c);
      
      void setup() {
      
        pinMode(lockPin, OUTPUT);
      
        nfc.begin();
        uint32_t versiondata = nfc.getFirmwareVersion();
        if (! versiondata) {
          Serial.print("Couldn't find PN53x board");
          while (1); // halt
        }
        Serial.print("Found NFC chip PN5"); Serial.println((versiondata>>24) & 0xFF, HEX); 
        Serial.print("Firmware ver. "); Serial.print((versiondata>>16) & 0xFF, DEC); 
        Serial.print('.'); Serial.println((versiondata>>8) & 0xFF, DEC);
        // Set the max number of retry attempts to read from a card
        // This prevents us from waiting forever for a card, which is
        // the default behaviour of the PN532.
        nfc.setPassiveActivationRetries(0x3);
      
        // configure board to read RFID tags
        nfc.SAMConfig();
      
        lockStatus = loadState(0);    // Read last lock status from eeprom
        setLockState(lockStatus, true); // Now set the last known state and send it to controller 
      }
      
      void presentation()  {
        sendSketchInfo("RFID Lock", "1.0");
        present(CHILD_ID, S_LOCK);
      }
      
      void loop() {
        bool success;
        uint8_t key[] = { 0, 0, 0, 0, 0, 0, 0 };  // Buffer to store the returned UID
        uint8_t currentKeyLength;                        // Length of the UID (4 or 7 bytes depending on ISO14443A card type)
      
      
        // Wait for an ISO14443A type cards (Mifare, etc.).  When one is found
        // 'uid' will be populated with the UID, and uidLength will indicate
        // if the uid is 4 bytes (Mifare Classic) or 7 bytes (Mifare Ultralight)
        success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, &key[0], &currentKeyLength);
      
        if (success) {
          Serial.print("Found tag id: ");
          for (uint8_t i=0; i < currentKeyLength; i++) 
          {
            if (i>0) Serial.print(",");
            Serial.print("0x");Serial.print(key[i], HEX); 
          }
          for (uint8_t i=currentKeyLength; i < maxKeyLength; i++) 
          {
            Serial.print(",0x00"); 
          }
      
      
          Serial.println("");
      
          bool valid = false;
          // Compare this key to the valid once registered here in sketch 
          for (int i=0;i<keyCount && !valid;i++) {
            for (int j=0;j<currentKeyLength && !valid;j++) {
              if (key[j] != validKeys[i][j]) {
                break;
              }
              if (j==currentKeyLength-1) {
                valid = true;
              }
            }
          }
          if (valid) {
            // Switch lock status
            setLockState(!lockStatus, true);       
          }
      
          // Wait for card/tag to leave reader    
          while(nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, &key[0], &currentKeyLength));
        } 
      } 
      
      
      
      // Unlocks the door.
      void setLockState(bool state, bool doSend){
        if (state) 
           Serial.println("open lock");
        else
           Serial.println("close lock");
        if (doSend)
          send(lockMsg.set(state));
        digitalWrite(lockPin, state);
        saveState(0,state);
        lockStatus = state;
      }
      
      void receive(const MyMessage &message) {
        // We only expect one type of message from controller. But we better check anyway.
        if (message.type==V_LOCK_STATUS) {
           // Change relay state
           setLockState(message.getBool(), false); 
      
           // Write some debug info
           Serial.print("Incoming lock status:");
           Serial.println(message.getBool());
         } 
      }l```
      mfalkviddM Offline
      mfalkviddM Offline
      mfalkvidd
      Mod
      wrote on last edited by
      #2

      change

      void setLockState(bool state, bool doSend){
        if (state) 
           Serial.println("open lock");
        else
           Serial.println("close lock");
        if (doSend)
          send(lockMsg.set(state));
        digitalWrite(lockPin, state);
        saveState(0,state);
        lockStatus = state;
      }
      

      to

      void setLockState(bool state, bool doSend) {
        if (state)
        {
          Serial.println("open lock");
          digitalWrite(lockPin, true);
          wait(3000);
          Serial.println("close lock");
          digitalWrite(lockPin, false);
        }
      }
      
      1 Reply Last reply
      0
      • T Offline
        T Offline
        The-Holgi
        wrote on last edited by
        #3

        Thank you, it work´s perfect.
        greetings Holger

        1 Reply Last reply
        0
        • T Offline
          T Offline
          The-Holgi
          wrote on last edited by
          #4

          Hm,
          now in fhem the state is not transmitted.

          rejoe2R 1 Reply Last reply
          0
          • T The-Holgi

            Hm,
            now in fhem the state is not transmitted.

            rejoe2R Offline
            rejoe2R Offline
            rejoe2
            wrote on last edited by
            #5

            @the-holgi
            That's obvious... as now the state of the lock switches rather fast, the behaviour should be differen. I'd suggest to send both info (on/off) (on before wait(), off after) to get the events in FHEM also.

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

            1 Reply Last reply
            0
            • T Offline
              T Offline
              The-Holgi
              wrote on last edited by
              #6

              I´ve change the code, now the state on transmitted to fhem, but state off not.

              void setLockState(bool state, bool doSend) {
                if (state)
                {
                  Serial.println("open lock");
                  digitalWrite(lockPin, true);
                  wait(5000);
                  Serial.println("close lock");
                if (doSend)
                  send(lockMsg.set(state));
                  digitalWrite(lockPin, false);
                }
              }```
              mfalkviddM 1 Reply Last reply
              0
              • T The-Holgi

                I´ve change the code, now the state on transmitted to fhem, but state off not.

                void setLockState(bool state, bool doSend) {
                  if (state)
                  {
                    Serial.println("open lock");
                    digitalWrite(lockPin, true);
                    wait(5000);
                    Serial.println("close lock");
                  if (doSend)
                    send(lockMsg.set(state));
                    digitalWrite(lockPin, false);
                  }
                }```
                mfalkviddM Offline
                mfalkviddM Offline
                mfalkvidd
                Mod
                wrote on last edited by
                #7

                @the-holgi this should work

                void setLockState(bool state, bool doSend) {
                  if (state)
                  {
                    Serial.println("open lock");
                  if (doSend)
                    send(lockMsg.set(true));
                    digitalWrite(lockPin, true);
                    wait(5000);
                    Serial.println("close lock");
                  if (doSend)
                    send(lockMsg.set(false));
                    digitalWrite(lockPin, false);
                  } else {
                  if (doSend)
                    send(lockMsg.set(state));
                }
                }
                
                1 Reply Last reply
                0
                • T Offline
                  T Offline
                  The-Holgi
                  wrote on last edited by The-Holgi
                  #8

                  Thank you it works perfect.
                  Here the complete sketch:

                  /**
                   * The MySensors Arduino library handles the wireless radio link and protocol
                   * between your home built sensors/actuators and HA controller of choice.
                   * The sensors forms a self healing radio network with optional repeaters. Each
                   * repeater and gateway builds a routing tables in EEPROM which keeps track of the
                   * network topology allowing messages to be routed to nodes.
                   *
                   * Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
                   * Copyright (C) 2013-2015 Sensnology AB
                   * Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
                   *
                   * Documentation: http://www.mysensors.org
                   * Support Forum: http://forum.mysensors.org
                   *
                   * This program is free software; you can redistribute it and/or
                   * modify it under the terms of the GNU General Public License
                   * version 2 as published by the Free Software Foundation.
                   *
                   *******************************
                   *
                   * REVISION HISTORY
                   * Version 1.0 - Henrik Ekblad
                   * 
                   * DESCRIPTION
                   * RFID Lock sensor/actuator
                   * 
                   * Use RFID tag to lock/unlock a door or trigger a scene on your controller.
                   * This example sketch allows you to add an optional relay or solenoid 
                   * which can be activated/opened by RFID or controller.  
                   *
                   * Use the I2C wiring option for your RFID module and connect to the following Arduino pins.
                   * 
                   * RFID       Arduino
                   * -----      -------
                   * GND   ->   GND
                   * VCC   ->   +5V
                   * SCL   ->   A5
                   * SDA   ->   A4
                   * 
                   * Use normal wiring for NRF24L01 radio
                   * 
                   * Attach a optional relay or solonoid lock to pin 4
                   * http://www.mysensors.org/build/rfid 
                   */
                  
                  // Enable debug prints to serial monitor
                  #define MY_DEBUG 
                  
                  // Enable and select radio type attached
                  #define MY_RADIO_NRF24
                  //#define MY_RADIO_RFM69
                  
                  #include <SPI.h>
                  #include <MySensors.h>  
                  #include <Wire.h>
                  #include <PN532_I2C.h>
                  #include <PN532.h>
                  
                  
                  // Add your valid rfid keys here. To find you your key just run sketch; hold your new RFID tag in fron ot the reader; 
                  // and copy the key from serial output of this sketch.
                  const uint8_t maxKeyLength = 7;
                  uint8_t validKeys[][maxKeyLength] = {
                                      { 0xB3, 0xC6, 0xD9, 0x80, 0x00, 0x00, 0x00 },
                                      { 0x5B, 0xFC, 0x7A, 0xD, 0x00, 0x00, 0x00 },    // ADD YOUR KEYS HERE!
                                      { 0xF1, 0x13, 0x54, 0x63, 0x00, 0x00, 0x00 },
                                      { 0xCB, 0x6F, 0x2D, 0xD, 0x00, 0x00, 0x00 },
                                      { 0x9B, 0x33, 0x2C, 0xD, 0x00, 0x00, 0x00 },
                                      { 0x73, 0xFA, 0xF6, 0x1A, 0x00, 0x00, 0x00 },
                                      { 0x83, 0x59, 0x36, 0x1A, 0x00, 0x00, 0x00 },
                                      { 0x83, 0x1A, 0x3C, 0x1A, 0x00, 0x00, 0x00 },
                                      { 0xB6, 0xB2, 0x52, 0xF8, 0x00, 0x00, 0x00 },
                                      { 0x17, 0xE5, 0xD2, 0x2B, 0x00, 0x00, 0x00 },
                                      { 0x79 ,0xF7, 0x88, 0x5A, 0x00, 0x00, 0x00 },
                                      
                                      { 0x2B, 0xDC, 0x49, 0xB, 0x00, 0x00, 0x00 }};
                  int keyCount = sizeof validKeys / maxKeyLength; 
                  
                  #define CHILD_ID 99   // Id of the sensor child
                  
                  
                  // Pin definition
                  const int lockPin = 4;         // (Digital 4) The pin that activates the relay/solenoid lock.
                  bool lockStatus;
                  MyMessage lockMsg(CHILD_ID, V_LOCK_STATUS);
                  PN532_I2C pn532i2c(Wire);
                  PN532 nfc(pn532i2c);
                  
                  void setup() {
                  
                    pinMode(lockPin, OUTPUT);
                  
                    nfc.begin();
                    uint32_t versiondata = nfc.getFirmwareVersion();
                    if (! versiondata) {
                      Serial.print("Couldn't find PN53x board");
                      while (1); // halt
                    }
                    Serial.print("Found NFC chip PN5"); Serial.println((versiondata>>24) & 0xFF, HEX); 
                    Serial.print("Firmware ver. "); Serial.print((versiondata>>16) & 0xFF, DEC); 
                    Serial.print('.'); Serial.println((versiondata>>8) & 0xFF, DEC);
                    // Set the max number of retry attempts to read from a card
                    // This prevents us from waiting forever for a card, which is
                    // the default behaviour of the PN532.
                    nfc.setPassiveActivationRetries(0x3);
                  
                    // configure board to read RFID tags
                    nfc.SAMConfig();
                  
                    lockStatus = loadState(0);    // Read last lock status from eeprom
                    setLockState(lockStatus, true); // Now set the last known state and send it to controller 
                  }
                  
                  void presentation()  {
                    sendSketchInfo("RFID Lock", "1.0");
                    present(CHILD_ID, S_LOCK);
                  }
                  
                  void loop() {
                    bool success;
                    uint8_t key[] = { 0, 0, 0, 0, 0, 0, 0 };  // Buffer to store the returned UID
                    uint8_t currentKeyLength;                        // Length of the UID (4 or 7 bytes depending on ISO14443A card type)
                  
                  
                    // Wait for an ISO14443A type cards (Mifare, etc.).  When one is found
                    // 'uid' will be populated with the UID, and uidLength will indicate
                    // if the uid is 4 bytes (Mifare Classic) or 7 bytes (Mifare Ultralight)
                    success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, &key[0], &currentKeyLength);
                  
                    if (success) {
                      Serial.print("Found tag id: ");
                      for (uint8_t i=0; i < currentKeyLength; i++) 
                      {
                        if (i>0) Serial.print(",");
                        Serial.print("0x");Serial.print(key[i], HEX); 
                      }
                      for (uint8_t i=currentKeyLength; i < maxKeyLength; i++) 
                      {
                        Serial.print(",0x00"); 
                      }
                  
                  
                      Serial.println("");
                  
                      bool valid = false;
                      // Compare this key to the valid once registered here in sketch 
                      for (int i=0;i<keyCount && !valid;i++) {
                        for (int j=0;j<currentKeyLength && !valid;j++) {
                          if (key[j] != validKeys[i][j]) {
                            break;
                          }
                          if (j==currentKeyLength-1) {
                            valid = true;
                          }
                        }
                      }
                      if (valid) {
                        // Switch lock status
                        setLockState(!lockStatus, true);       
                      }
                  
                      // Wait for card/tag to leave reader    
                      while(nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, &key[0], &currentKeyLength));
                    } 
                  } 
                  
                  
                  
                  // Unlocks the door.
                  void setLockState(bool state, bool doSend) {
                    if (state)
                    {
                      Serial.println("open lock");
                    if (doSend)
                      send(lockMsg.set(true));
                      digitalWrite(lockPin, true);
                      wait(5000);
                      Serial.println("close lock");
                    if (doSend)
                      send(lockMsg.set(false));
                      digitalWrite(lockPin, false);
                    } else {
                    if (doSend)
                      send(lockMsg.set(state));
                  }
                  }
                  
                  void receive(const MyMessage &message) {
                    // We only expect one type of message from controller. But we better check anyway.
                    if (message.type==V_LOCK_STATUS) {
                       // Change relay state
                       setLockState(message.getBool(), false); 
                  
                       // Write some debug info
                       Serial.print("Incoming lock status:");
                       Serial.println(message.getBool());
                     } 
                  }
                  T 1 Reply Last reply
                  2
                  • T The-Holgi

                    Thank you it works perfect.
                    Here the complete sketch:

                    /**
                     * The MySensors Arduino library handles the wireless radio link and protocol
                     * between your home built sensors/actuators and HA controller of choice.
                     * The sensors forms a self healing radio network with optional repeaters. Each
                     * repeater and gateway builds a routing tables in EEPROM which keeps track of the
                     * network topology allowing messages to be routed to nodes.
                     *
                     * Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
                     * Copyright (C) 2013-2015 Sensnology AB
                     * Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
                     *
                     * Documentation: http://www.mysensors.org
                     * Support Forum: http://forum.mysensors.org
                     *
                     * This program is free software; you can redistribute it and/or
                     * modify it under the terms of the GNU General Public License
                     * version 2 as published by the Free Software Foundation.
                     *
                     *******************************
                     *
                     * REVISION HISTORY
                     * Version 1.0 - Henrik Ekblad
                     * 
                     * DESCRIPTION
                     * RFID Lock sensor/actuator
                     * 
                     * Use RFID tag to lock/unlock a door or trigger a scene on your controller.
                     * This example sketch allows you to add an optional relay or solenoid 
                     * which can be activated/opened by RFID or controller.  
                     *
                     * Use the I2C wiring option for your RFID module and connect to the following Arduino pins.
                     * 
                     * RFID       Arduino
                     * -----      -------
                     * GND   ->   GND
                     * VCC   ->   +5V
                     * SCL   ->   A5
                     * SDA   ->   A4
                     * 
                     * Use normal wiring for NRF24L01 radio
                     * 
                     * Attach a optional relay or solonoid lock to pin 4
                     * http://www.mysensors.org/build/rfid 
                     */
                    
                    // Enable debug prints to serial monitor
                    #define MY_DEBUG 
                    
                    // Enable and select radio type attached
                    #define MY_RADIO_NRF24
                    //#define MY_RADIO_RFM69
                    
                    #include <SPI.h>
                    #include <MySensors.h>  
                    #include <Wire.h>
                    #include <PN532_I2C.h>
                    #include <PN532.h>
                    
                    
                    // Add your valid rfid keys here. To find you your key just run sketch; hold your new RFID tag in fron ot the reader; 
                    // and copy the key from serial output of this sketch.
                    const uint8_t maxKeyLength = 7;
                    uint8_t validKeys[][maxKeyLength] = {
                                        { 0xB3, 0xC6, 0xD9, 0x80, 0x00, 0x00, 0x00 },
                                        { 0x5B, 0xFC, 0x7A, 0xD, 0x00, 0x00, 0x00 },    // ADD YOUR KEYS HERE!
                                        { 0xF1, 0x13, 0x54, 0x63, 0x00, 0x00, 0x00 },
                                        { 0xCB, 0x6F, 0x2D, 0xD, 0x00, 0x00, 0x00 },
                                        { 0x9B, 0x33, 0x2C, 0xD, 0x00, 0x00, 0x00 },
                                        { 0x73, 0xFA, 0xF6, 0x1A, 0x00, 0x00, 0x00 },
                                        { 0x83, 0x59, 0x36, 0x1A, 0x00, 0x00, 0x00 },
                                        { 0x83, 0x1A, 0x3C, 0x1A, 0x00, 0x00, 0x00 },
                                        { 0xB6, 0xB2, 0x52, 0xF8, 0x00, 0x00, 0x00 },
                                        { 0x17, 0xE5, 0xD2, 0x2B, 0x00, 0x00, 0x00 },
                                        { 0x79 ,0xF7, 0x88, 0x5A, 0x00, 0x00, 0x00 },
                                        
                                        { 0x2B, 0xDC, 0x49, 0xB, 0x00, 0x00, 0x00 }};
                    int keyCount = sizeof validKeys / maxKeyLength; 
                    
                    #define CHILD_ID 99   // Id of the sensor child
                    
                    
                    // Pin definition
                    const int lockPin = 4;         // (Digital 4) The pin that activates the relay/solenoid lock.
                    bool lockStatus;
                    MyMessage lockMsg(CHILD_ID, V_LOCK_STATUS);
                    PN532_I2C pn532i2c(Wire);
                    PN532 nfc(pn532i2c);
                    
                    void setup() {
                    
                      pinMode(lockPin, OUTPUT);
                    
                      nfc.begin();
                      uint32_t versiondata = nfc.getFirmwareVersion();
                      if (! versiondata) {
                        Serial.print("Couldn't find PN53x board");
                        while (1); // halt
                      }
                      Serial.print("Found NFC chip PN5"); Serial.println((versiondata>>24) & 0xFF, HEX); 
                      Serial.print("Firmware ver. "); Serial.print((versiondata>>16) & 0xFF, DEC); 
                      Serial.print('.'); Serial.println((versiondata>>8) & 0xFF, DEC);
                      // Set the max number of retry attempts to read from a card
                      // This prevents us from waiting forever for a card, which is
                      // the default behaviour of the PN532.
                      nfc.setPassiveActivationRetries(0x3);
                    
                      // configure board to read RFID tags
                      nfc.SAMConfig();
                    
                      lockStatus = loadState(0);    // Read last lock status from eeprom
                      setLockState(lockStatus, true); // Now set the last known state and send it to controller 
                    }
                    
                    void presentation()  {
                      sendSketchInfo("RFID Lock", "1.0");
                      present(CHILD_ID, S_LOCK);
                    }
                    
                    void loop() {
                      bool success;
                      uint8_t key[] = { 0, 0, 0, 0, 0, 0, 0 };  // Buffer to store the returned UID
                      uint8_t currentKeyLength;                        // Length of the UID (4 or 7 bytes depending on ISO14443A card type)
                    
                    
                      // Wait for an ISO14443A type cards (Mifare, etc.).  When one is found
                      // 'uid' will be populated with the UID, and uidLength will indicate
                      // if the uid is 4 bytes (Mifare Classic) or 7 bytes (Mifare Ultralight)
                      success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, &key[0], &currentKeyLength);
                    
                      if (success) {
                        Serial.print("Found tag id: ");
                        for (uint8_t i=0; i < currentKeyLength; i++) 
                        {
                          if (i>0) Serial.print(",");
                          Serial.print("0x");Serial.print(key[i], HEX); 
                        }
                        for (uint8_t i=currentKeyLength; i < maxKeyLength; i++) 
                        {
                          Serial.print(",0x00"); 
                        }
                    
                    
                        Serial.println("");
                    
                        bool valid = false;
                        // Compare this key to the valid once registered here in sketch 
                        for (int i=0;i<keyCount && !valid;i++) {
                          for (int j=0;j<currentKeyLength && !valid;j++) {
                            if (key[j] != validKeys[i][j]) {
                              break;
                            }
                            if (j==currentKeyLength-1) {
                              valid = true;
                            }
                          }
                        }
                        if (valid) {
                          // Switch lock status
                          setLockState(!lockStatus, true);       
                        }
                    
                        // Wait for card/tag to leave reader    
                        while(nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, &key[0], &currentKeyLength));
                      } 
                    } 
                    
                    
                    
                    // Unlocks the door.
                    void setLockState(bool state, bool doSend) {
                      if (state)
                      {
                        Serial.println("open lock");
                      if (doSend)
                        send(lockMsg.set(true));
                        digitalWrite(lockPin, true);
                        wait(5000);
                        Serial.println("close lock");
                      if (doSend)
                        send(lockMsg.set(false));
                        digitalWrite(lockPin, false);
                      } else {
                      if (doSend)
                        send(lockMsg.set(state));
                    }
                    }
                    
                    void receive(const MyMessage &message) {
                      // We only expect one type of message from controller. But we better check anyway.
                      if (message.type==V_LOCK_STATUS) {
                         // Change relay state
                         setLockState(message.getBool(), false); 
                    
                         // Write some debug info
                         Serial.print("Incoming lock status:");
                         Serial.println(message.getBool());
                       } 
                    }
                    T Offline
                    T Offline
                    The-Holgi
                    wrote on last edited by The-Holgi
                    #9

                    Hello,
                    now i´m write the sketch to another arduino with rfid and relay.
                    It doesn´t work .
                    From fhem i can trigger the relay and in serial monitor the card´s where shown with serial.
                    In fhem the lockstatus is set to off, when the card is in near of the reader, the lockstatus on doesn´t send to fhem.
                    But the relay doesn´t trigger by card.
                    Where is the mistake?
                    The problem is the lockstate doesnt set to on.

                    /**
                     * The MySensors Arduino library handles the wireless radio link and protocol
                     * between your home built sensors/actuators and HA controller of choice.
                     * The sensors forms a self healing radio network with optional repeaters. Each
                     * repeater and gateway builds a routing tables in EEPROM which keeps track of the
                     * network topology allowing messages to be routed to nodes.
                     *
                     * Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
                     * Copyright (C) 2013-2015 Sensnology AB
                     * Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
                     *
                     * Documentation: http://www.mysensors.org
                     * Support Forum: http://forum.mysensors.org
                     *
                     * This program is free software; you can redistribute it and/or
                     * modify it under the terms of the GNU General Public License
                     * version 2 as published by the Free Software Foundation.
                     *
                     *******************************
                     *
                     * REVISION HISTORY
                     * Version 1.0 - Henrik Ekblad
                     * 
                     * DESCRIPTION
                     * RFID Lock sensor/actuator
                     * 
                     * Use RFID tag to lock/unlock a door or trigger a scene on your controller.
                     * This example sketch allows you to add an optional relay or solenoid 
                     * which can be activated/opened by RFID or controller.  
                     *
                     * Use the I2C wiring option for your RFID module and connect to the following Arduino pins.
                     * 
                     * RFID       Arduino
                     * -----      -------
                     * GND   ->   GND
                     * VCC   ->   +5V
                     * SCL   ->   A5
                     * SDA   ->   A4
                     * 
                     * Use normal wiring for NRF24L01 radio
                     * 
                     * Attach a optional relay or solonoid lock to pin 4
                     * http://www.mysensors.org/build/rfid 
                     */
                    
                    // Enable debug prints to serial monitor
                    #define MY_DEBUG 
                    
                    // Enable and select radio type attached
                    #define MY_RADIO_NRF24
                    //#define MY_RADIO_RFM69
                    
                    #include <SPI.h>
                    #include <MySensors.h>  
                    #include <Wire.h>
                    #include <PN532_I2C.h>
                    #include <PN532.h>
                    
                    
                    // Add your valid rfid keys here. To find you your key just run sketch; hold your new RFID tag in fron ot the reader; 
                    // and copy the key from serial output of this sketch.
                    const uint8_t maxKeyLength = 7;
                    uint8_t validKeys[][maxKeyLength] = {
                                        { 0xB3, 0xC6, 0xD9, 0x80, 0x00, 0x00, 0x00 },
                                        { 0x5B, 0xFC, 0x7A, 0xD, 0x00, 0x00, 0x00 },    // ADD YOUR KEYS HERE!
                                        { 0xF1, 0x13, 0x54, 0x63, 0x00, 0x00, 0x00 },
                                        { 0xCB, 0x6F, 0x2D, 0xD, 0x00, 0x00, 0x00 },
                                        { 0x9B, 0x33, 0x2C, 0xD, 0x00, 0x00, 0x00 },
                                        { 0x73, 0xFA, 0xF6, 0x1A, 0x00, 0x00, 0x00 },
                                        { 0x83, 0x59, 0x36, 0x1A, 0x00, 0x00, 0x00 },
                                        { 0x83, 0x1A, 0x3C, 0x1A, 0x00, 0x00, 0x00 },
                                        { 0xB6, 0xB2, 0x52, 0xF8, 0x00, 0x00, 0x00 },
                                        { 0x17, 0xE5, 0xD2, 0x2B, 0x00, 0x00, 0x00 },
                                        { 0x79 ,0xF7, 0x88, 0x5A, 0x00, 0x00, 0x00 },
                                        { 0x2B, 0xDC, 0x49, 0xB, 0x00, 0x00, 0x00 }};
                    int keyCount = sizeof validKeys / maxKeyLength; 
                    
                    #define CHILD_ID 99   // Id of the sensor child
                    
                    
                    // Pin definition
                    const int lockPin = 4;         // (Digital 4) The pin that activates the relay/solenoid lock.
                    bool lockStatus;
                    MyMessage lockMsg(CHILD_ID, V_LOCK_STATUS);
                    PN532_I2C pn532i2c(Wire);
                    PN532 nfc(pn532i2c);
                    
                    void setup() {
                    
                      pinMode(lockPin, OUTPUT);
                    
                      nfc.begin();
                      uint32_t versiondata = nfc.getFirmwareVersion();
                      if (! versiondata) {
                        Serial.print("Couldn't find PN53x board");
                        while (1); // halt
                      }
                      Serial.print("Found NFC chip PN5"); Serial.println((versiondata>>24) & 0xFF, HEX); 
                      Serial.print("Firmware ver. "); Serial.print((versiondata>>16) & 0xFF, DEC); 
                      Serial.print('.'); Serial.println((versiondata>>8) & 0xFF, DEC);
                      // Set the max number of retry attempts to read from a card
                      // This prevents us from waiting forever for a card, which is
                      // the default behaviour of the PN532.
                      nfc.setPassiveActivationRetries(0x3);
                    
                      // configure board to read RFID tags
                      nfc.SAMConfig();
                    
                      lockStatus = loadState(0);    // Read last lock status from eeprom
                      setLockState(lockStatus, true); // Now set the last known state and send it to controller 
                    }
                    
                    void presentation()  {
                      sendSketchInfo("RFID Lock", "1.0");
                      present(CHILD_ID, S_LOCK);
                    }
                    
                    void loop() {
                      bool success;
                      uint8_t key[] = { 0, 0, 0, 0, 0, 0, 0 };  // Buffer to store the returned UID
                      uint8_t currentKeyLength;                        // Length of the UID (4 or 7 bytes depending on ISO14443A card type)
                    
                    
                      // Wait for an ISO14443A type cards (Mifare, etc.).  When one is found
                      // 'uid' will be populated with the UID, and uidLength will indicate
                      // if the uid is 4 bytes (Mifare Classic) or 7 bytes (Mifare Ultralight)
                      success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, &key[0], &currentKeyLength);
                    
                      if (success) {
                        Serial.print("Found tag id: ");
                        for (uint8_t i=0; i < currentKeyLength; i++) 
                        {
                          if (i>0) Serial.print(",");
                          Serial.print("0x");Serial.print(key[i], HEX); 
                        }
                        for (uint8_t i=currentKeyLength; i < maxKeyLength; i++) 
                        {
                          Serial.print(",0x00"); 
                        }
                    
                    
                        Serial.println("");
                    
                        bool valid = false;
                        // Compare this key to the valid once registered here in sketch 
                        for (int i=0;i<keyCount && !valid;i++) {
                          for (int j=0;j<currentKeyLength && !valid;j++) {
                            if (key[j] != validKeys[i][j]) {
                              break;
                            }
                            if (j==currentKeyLength-1) {
                              valid = true;
                            }
                          }
                        }
                        if (valid) {
                          // Switch lock status
                          setLockState(!lockStatus, true);       
                        }
                    
                        // Wait for card/tag to leave reader    
                        while(nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, &key[0], &currentKeyLength));
                      } 
                    } 
                    
                    
                    
                    // Unlocks the door.
                    void setLockState(bool state, bool doSend) {
                      if (state)
                      {
                        Serial.println("open lock");
                      if (doSend)
                        send(lockMsg.set(true));
                        digitalWrite(lockPin, true);
                        wait(5000);
                        Serial.println("close lock");
                      if (doSend)
                        send(lockMsg.set(false));
                        digitalWrite(lockPin, false);
                      } else {
                      if (doSend)
                        send(lockMsg.set(state));
                    }
                    }
                    
                    void receive(const MyMessage &message) {
                      // We only expect one type of message from controller. But we better check anyway.
                      if (message.type==V_LOCK_STATUS) {
                         // Change relay state
                         setLockState(message.getBool(), false); 
                    
                         // Write some debug info
                         Serial.print("Incoming lock status:");
                         Serial.println(message.getBool());
                       } 
                    }```
                    
                    Hm, with other arduino now it works.
                    1 Reply Last reply
                    0

                    Hello! It looks like you're interested in this conversation, but you don't have an account yet.

                    Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

                    With your input, this post could be even better 💗

                    Register Login
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    11

                    Online

                    12.0k

                    Users

                    11.2k

                    Topics

                    113.4k

                    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