Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
3.8 kB
5
Indexable
Never

#include <Arduino.h>
#include <Wire.h>
#include <PN532_I2C.h>
#include <PN532.h>
#include <NfcAdapter.h>

PN532_I2C pn532_i2c(Wire);
NfcAdapter nfc = NfcAdapter(pn532_i2c);
#define configButton 2
#define sigRed 13
#define sigGreen 15
#define sigBlue 12

int READ_THRESHOLD = 1000;
enum SystemStatus {
    INITIALIZING,
    READY,
    OFF,
    READ_SUCCESS,
    READ_ERROR
};



void updateSystemStatus(SystemStatus status) {
    // Turn off all LEDs initially
    digitalWrite(sigRed, LOW);
    digitalWrite(sigGreen, LOW);
    digitalWrite(sigBlue, LOW);

    switch (status) {
        case INITIALIZING:
        
            digitalWrite(sigBlue, HIGH);
            digitalWrite(sigRed, HIGH);
            digitalWrite(sigGreen, HIGH);

            break;
        case READY:

            digitalWrite(sigGreen, HIGH);
            delay(500);
            yield();
            digitalWrite(sigGreen, LOW);  
            delay(500);
            yield();
            digitalWrite(sigGreen, HIGH);
            delay(500);
            yield();
            digitalWrite(sigGreen, LOW);  
            delay(500);
            yield();
    

            break;
        case OFF:    
            break;
        case READ_SUCCESS:
            digitalWrite(sigBlue, HIGH);
            digitalWrite(sigRed, HIGH);

            break;
        case READ_ERROR:
            digitalWrite(sigRed, HIGH);
            break;

}}

void setup() {
    updateSystemStatus(INITIALIZING);
    

    Serial.begin(9600); //Baud rate
    Serial.println("NDEF Writer");
    pinMode(configButton, OUTPUT);
    pinMode(sigRed, OUTPUT);
    pinMode(sigGreen, OUTPUT);
    pinMode(sigBlue, OUTPUT);

   
    nfc.begin();
    updateSystemStatus(READY);

}

unsigned long lastReadTime = 0;
String lastCardId = "";


void processCard(NfcTag& tag) {
    NdefMessage message = tag.getNdefMessage();
    int recordCount = message.getRecordCount();

    bool foundMimeMessage = false;

    for (int i = 0; i < recordCount; i++) {
        NdefRecord record = message.getRecord(i);

        // Check if the record is a MIME media record
        if (record.getTnf() == TNF_MIME_MEDIA) {
            String mimeType = record.getType();

            // Check if the MIME type matches the one we're looking for
            if (mimeType == "application/redk.card.data") {
                foundMimeMessage = true;
                uint8_t payload[record.getPayloadLength()];

                // Get the payload
                record.getPayload(payload);
                updateSystemStatus(READ_SUCCESS);
                yield();

                // Convert the payload to a string
                String payloadStr = "";
                for (int i = 0; i < record.getPayloadLength(); i++) {
                    payloadStr += (char)payload[i];
                }

                Serial.println(payloadStr);
            }
        }
        yield();
    }

    if (!foundMimeMessage) {
        updateSystemStatus(READ_ERROR);
    }
}

void loop() {
    updateSystemStatus(OFF);

    if (nfc.tagPresent()) {
        NfcTag tag = nfc.read();
        if (!tag.hasNdefMessage()) {
            updateSystemStatus(READ_ERROR);  
            return;
        }

        String currentCardId = tag.getUidString();
        if (currentCardId == "") {
            Serial.println("Failed to read card ID");
            return;
        }

        unsigned long currentTime = millis();

        if (currentCardId == lastCardId && currentTime - lastReadTime < READ_THRESHOLD) {
           // Serial.println("Same card still on the reader");
        } else {
         //   Serial.println("New card detected");
            processCard(tag);  
        }

        lastCardId = currentCardId;
        lastReadTime = currentTime;
    }
}
Leave a Comment