From 2776e5fca9ba3406b491ac8744372720d981d91c Mon Sep 17 00:00:00 2001 From: Henrik Ekblad Date: Sun, 17 Aug 2014 23:56:53 +0200 Subject: [PATCH] Added a new RFID Lock Sensor example. --- .../RFIDLockSensor/RFIDLockSensor.ino | 161 ++++++++++++++++++ .../{iso14443a_uid.pde => iso14443a_uid.ino} | 0 ...tndef.pde => mifareclassic_formatndef.ino} | 0 ..._memdump.pde => mifareclassic_memdump.ino} | 0 ...ic.pde => mifareclassic_ndeftoclassic.ino} | 0 ...endef.pde => mifareclassic_updatendef.ino} | 0 .../{readMifare.pde => readMifare.ino} | 0 7 files changed, 161 insertions(+) create mode 100644 libraries/MySensors/examples/RFIDLockSensor/RFIDLockSensor.ino rename libraries/PN532/examples/iso14443a_uid/{iso14443a_uid.pde => iso14443a_uid.ino} (100%) rename libraries/PN532/examples/mifareclassic_formatndef/{mifareclassic_formatndef.pde => mifareclassic_formatndef.ino} (100%) rename libraries/PN532/examples/mifareclassic_memdump/{mifareclassic_memdump.pde => mifareclassic_memdump.ino} (100%) rename libraries/PN532/examples/mifareclassic_ndeftoclassic/{mifareclassic_ndeftoclassic.pde => mifareclassic_ndeftoclassic.ino} (100%) rename libraries/PN532/examples/mifareclassic_updatendef/{mifareclassic_updatendef.pde => mifareclassic_updatendef.ino} (100%) rename libraries/PN532/examples/readMifare/{readMifare.pde => readMifare.ino} (100%) diff --git a/libraries/MySensors/examples/RFIDLockSensor/RFIDLockSensor.ino b/libraries/MySensors/examples/RFIDLockSensor/RFIDLockSensor.ino new file mode 100644 index 0000000..96f7e25 --- /dev/null +++ b/libraries/MySensors/examples/RFIDLockSensor/RFIDLockSensor.ino @@ -0,0 +1,161 @@ +/* + RFID Lock sensor/actuator + + Henrik Ekblad + + 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 + +*/ + +#include +#include +#include +#include +#include + + +// 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 }, + { 0, 0, 0, 0, 0, 0, 0 }, + { 0, 0, 0, 0, 0, 0, 0 }, + { 0, 0, 0, 0, 0, 0, 0 }}; +int keyCount = sizeof validKeys / maxKeyLength; + + +#define CHILD_ID 99 // Id of the sensor child + +/*Pin definitions*/ +const int lockPin = 4; // (Digital 4) The pin that activates the relay/solenoid lock. + +bool lockStatus; +MySensor gw; +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(); + + // Init mysensors library + gw.begin(incomingMessage); + + gw.sendSketchInfo("RFID Lock", "1.0"); + gw.present(CHILD_ID, S_LOCK); + + lockStatus = gw.loadState(0); // Read last lock status from eeprom + setLockState(lockStatus, true); // Now set the last known state and send it to controller +} + +void loop() { + boolean 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) + + gw.process(); // Process incomming messages + + // 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], ¤tKeyLength); + + 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(""); + + boolean valid = false; + // Compare this key to the valid once registered here in sketch + for (int i=0;i