#include #include #include #include PN532_I2C pn532_i2c(Wire); NfcAdapter nfc = NfcAdapter(pn532_i2c); #define Content_Type "text/admin" String username = "myUsername"; #define LED_PIN 8 // pin for the LED boolean led = LOW; boolean p_state = false; void setup() { Serial.begin(9600); nfc.begin(); // initialize the NFC reader pinMode(LED_PIN, OUTPUT); // make the led pin an output digitalWrite(LED_PIN, led); // set it low turn off the led } void loop() { String text = readTag(); if (text == username && !p_state) { lock(); p_state = true; } else if (text == "" && p_state) { p_state = false; } } String readTag() { String text = ""; if (nfc.tagPresent()) { // if there's a tag present NfcTag tag = nfc.read(); if (tag.hasNdefMessage()) { // every tag won't have a message NdefMessage message = tag.getNdefMessage(); NdefRecord record = message.getRecord(0); if (record.getTnf() == TNF_MIME_MEDIA && record.getType() == Content_Type) { // Get the length of the payload: int payloadLength = record.getPayloadLength(); byte payload[payloadLength]; // make a byte array to hold the payload record.getPayload(payload); // convert the payload to a String for (int c = 0; c < payloadLength; c++) { if (isprint((char)payload[c])) { text += (char)payload[c]; } } Serial.println("OK"); } } } return text; } boolean lock() { led = !led; digitalWrite(LED_PIN, led); Serial.print("LED state: "); Serial.println(led); }