Маленькие RFID считыватели могут быть размещены практически в любом месте. За стеной или дверью, чтобы дать доступ к открытию двери, когда считается правильная метка.
Также следует помнить, что многие смартфоны имеют функцию отправки RFID, которые могут быть использованы с RFID-считывателем.
В приведенном примере можно запрограммировать какие метки должны разблокировать (включать) реле. Чтобы узнать уникальный идентификатор метки просто запустить пример, и смотрите монитор порта. Держите метку перед считывателем и ваш идентификатор должен считаться. Скопируйте его и добавьте его в массив validKeys в программе. Затем повторно скомпилируйте скетч, чтобы новый ключ доступа был добавлен.
Подключение
Начните с подключения радио.
Сейчас подключим RFID модуль.
Сенсор | Arduino |
---|---|
GND | GND |
VCC | +5V |
SCL | A5 (аналоговый вход) |
SDA | A4(аналоговый вход) |
Пример
В этом примере используются внешние библиотеки PN532 и PN532_I2C найти их можно здесь. Пожалуйста, установите их и перезагрузите Arduino IDE, прежде чем пытаться компилировать скетч.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
/** * 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_RF24 //#define MY_RADIO_RFM69 #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], ¤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(""); 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], ¤tKeyLength)); } } // 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()); } } |
Документация
Перевёл Антон Вотчицев