Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [code]
- #include <SPI.h>
- #include <SD.h>
- #include <Wire.h>
- #include <RTClib.h>
- #include <Adafruit_PN532.h>
- // A simple data logger for the Arduino analog pins
- // how many milliseconds between grabbing data and logging it. 1000 ms is once a second
- #define LOG_INTERVAL 1000 // mills between entries (reduce to take more/faster data)
- // how many milliseconds before writing the logged data permanently to disk
- // set it to the LOG_INTERVAL to write each time (safest)
- // set it to 10*LOG_INTERVAL to write all data every 10 datareads, you could lose up to
- // the last 10 reads if power is lost but it uses less power and is much faster!
- #define SYNC_INTERVAL 1000 // mills between calls to flush() - to write data to the card
- uint32_t syncTime = 0; // time of last sync()
- #define ECHO_TO_SERIAL 1 // echo data to serial port
- #define WAIT_TO_START 0 // Wait for serial input in setup()
- // the digital pins that connect to the LEDs
- #define redLEDpin 3
- #define greenLEDpin 4
- // RFID pins
- // If using the breakout or shield with I2C, define just the pins connected
- // to the IRQ and reset lines. Use the values below (2, 3) for the shield!
- #define PN532_IRQ (2)
- #define PN532_RESET (3) // Not connected by default on the NFC Shield
- // Or use this line for a breakout or shield with an I2C connection:
- Adafruit_PN532 nfc(PN532_IRQ, PN532_RESET);
- #if defined(ARDUINO_ARCH_SAMD)
- for Zero, output on USB Serial console, remove line below if using programming port to program the Zero!
- also change #define in Adafruit_PN532.cpp library file
- #define Serial SerialUSB
- #endif
- RTC_DS1307 RTC; // define the Real Time Clock object
- // for the data logging shield, we use digital pin 10 for the SD cs line
- const int chipSelect = 10;
- // the logging file
- File logfile;
- void error(char *str)
- {
- Serial.print("error: ");
- Serial.println(str);
- // red LED indicates error
- digitalWrite(redLEDpin, HIGH);
- while(1);
- }
- void setup(void)
- {
- Serial.begin(9600);
- Serial.println();
- // use debugging LEDs
- pinMode(redLEDpin, OUTPUT);
- pinMode(greenLEDpin, OUTPUT);
- #if WAIT_TO_START
- Serial.println("Type any character to start");
- while (!Serial.available());
- #endif //WAIT_TO_START
- // initialize the SD card
- Serial.print("Initializing SD card...");
- // make sure that the default chip select pin is set to
- // output, even if you don't use it:
- pinMode(10, OUTPUT);
- // see if the card is present and can be initialized:
- if (!SD.begin(chipSelect)) {
- error("Card failed, or not present");
- return;
- }
- Serial.println("card initialized.");
- // create a new file
- char filename[] = "LOGGER00.CSV";
- for (uint8_t i = 0; i < 100; i++) {
- filename[6] = i/10 + '0';
- filename[7] = i%10 + '0';
- if (! SD.exists(filename)) {
- // only open a new file if it doesn't exist
- logfile = SD.open(filename, FILE_WRITE);
- break; // leave the loop!
- }
- }
- if (! logfile) {
- error("couldnt create file");
- }
- Serial.print("Logging to: ");
- Serial.println(filename);
- // connect to RTC
- Wire.begin();
- if (!RTC.begin()) {
- logfile.println("RTC failed");
- #if ECHO_TO_SERIAL
- Serial.println("RTC failed");
- #endif //ECHO_TO_SERIAL
- }
- logfile.println("millis,time,id");
- #if ECHO_TO_SERIAL
- Serial.println("millis,time,id");
- #endif //ECHO_TO_SERIAL
- // If you want to set the aref to something other than 5v
- //analogReference(EXTERNAL);
- // Set up RFID reader
- #ifndef ESP8266
- while (!Serial); // for Leonardo/Micro/Zero
- #endif
- Serial.begin(115200);
- Serial.println("Hello!");
- nfc.begin();
- uint32_t versiondata = nfc.getFirmwareVersion();
- if (! versiondata) {
- Serial.print("Didn't find PN53x board");
- while (1); // halt
- }
- // Got ok data, print it out!
- Serial.print("Found 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);
- // configure board to read RFID tags
- nfc.SAMConfig();
- Serial.println("Waiting for an ISO14443A Card ...");
- }
- void loop(void)
- {
- DateTime now;
- // delay for the amount of time we want between readings
- delay((LOG_INTERVAL -1) - (millis() % LOG_INTERVAL));
- digitalWrite(greenLEDpin, HIGH);
- // log milliseconds since starting
- uint32_t m = millis();
- logfile.print(m); // milliseconds since start
- logfile.print(", ");
- #if ECHO_TO_SERIAL
- Serial.print(m); // milliseconds since start
- Serial.print(", ");
- #endif
- // fetch the time
- now = RTC.now();
- // log time
- logfile.print(now.unixtime()); // seconds since 1/1/1970
- logfile.print(", ");
- logfile.print('"');
- logfile.print(now.year(), DEC);
- logfile.print("/");
- logfile.print(now.month(), DEC);
- logfile.print("/");
- logfile.print(now.day(), DEC);
- logfile.print(" ");
- logfile.print(now.hour(), DEC);
- logfile.print(":");
- logfile.print(now.minute(), DEC);
- logfile.print(":");
- logfile.print(now.second(), DEC);
- logfile.print('"');
- #if ECHO_TO_SERIAL
- Serial.print(now.unixtime()); // seconds since 1/1/1970
- Serial.print(", ");
- Serial.print('"');
- Serial.print(now.year(), DEC);
- Serial.print("/");
- Serial.print(now.month(), DEC);
- Serial.print("/");
- Serial.print(now.day(), DEC);
- Serial.print(" ");
- Serial.print(now.hour(), DEC);
- Serial.print(":");
- Serial.print(now.minute(), DEC);
- Serial.print(":");
- Serial.print(now.second(), DEC);
- Serial.print('"');
- #endif //ECHO_TO_SERIAL
- //log RFID data
- int buzzer = 7;
- uint8_t success;
- uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 }; // Buffer to store the returned UID
- uint8_t uidLength; // 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, uid, &uidLength);
- if (success) {
- // Display some basic information about the card
- Serial.println("Found an ISO14443A card");
- Serial.print(" UID Length: ");Serial.print(uidLength, DEC);Serial.println(" bytes");
- Serial.print(" UID Value: ");
- nfc.PrintHex(uid, uidLength);
- Serial.println("");
- if (uidLength == 4)
- {
- // We probably have a Mifare Classic card ...
- Serial.println("Seems to be a Mifare Classic card (4 byte UID)");
- // Now we need to try to authenticate it for read/write access
- // Try with the factory default KeyA: 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF
- Serial.println("Trying to authenticate block 4 with default KEYA value");
- uint8_t keya[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
- // Start with block 4 (the first block of sector 1) since sector 0
- // contains the manufacturer data and it's probably better just
- // to leave it alone unless you know what you're doing
- success = nfc.mifareclassic_AuthenticateBlock(uid, uidLength, 4, 0, keya);
- if (success)
- {
- Serial.println("Sector 1 (Blocks 4..7) has been authenticated");
- uint8_t data[16];
- // If you want to write something to block 4 to test with, uncomment
- // the following line and this text should be read back in a minute
- //memcpy(data, (const uint8_t[]){ 'a', 'd', 'a', 'f', 'r', 'u', 'i', 't', '.', 'c', 'o', 'm', 0, 0, 0, 0 }, sizeof data);
- // success = nfc.mifareclassic_WriteDataBlock (4, data);
- // Try to read the contents of block 4
- success = nfc.mifareclassic_ReadDataBlock(4, data);
- if (success)
- {
- // Data seems to have been read ... spit it out
- Serial.println("Reading Block 4:");
- nfc.PrintHexChar(data, 16);
- Serial.println("");
- // Wait a bit before reading the card again
- delay(1000);
- }
- else
- {
- Serial.println("Ooops ... unable to read the requested block. Try another key?");
- }
- }
- else
- {
- Serial.println("Ooops ... authentication failed: Try another key?");
- }
- }
- if (uidLength == 7)
- {
- // We probably have a Mifare Ultralight card ...
- Serial.println("Seems to be a Mifare Ultralight tag (7 byte UID)");
- // Try to read the first general-purpose user page (#4)
- Serial.println("Reading page 4");
- uint8_t data[32];
- success = nfc.mifareultralight_ReadPage (4, data);
- if (success)
- {
- // Data seems to have been read ... spit it out
- nfc.PrintHexChar(data, 4);
- Serial.println("");
- // Wait a bit before reading the card again
- delay(1000);
- }
- else
- {
- Serial.println("Ooops ... unable to read the requested page!?");
- }
- }
- }
- pinMode(success, INPUT);
- pinMode(buzzer, OUTPUT);
- if(digitalRead(success) == HIGH){ // If RFID card is detected, turn buzzer ON
- digitalWrite(buzzer, HIGH);
- delay (500); // Wait for 0.5 seconds
- digitalWrite(buzzer, LOW); // Turn off buzzer
- }
- logfile.print(", ");
- logfile.print(success);
- #if ECHO_TO_SERIAL
- Serial.print(", ");
- Serial.print(success);
- #endif //ECHO_TO_SERIAL
- digitalWrite(greenLEDpin, LOW);
- // Now we write data to disk! Don't sync too often - requires 2048 bytes of I/O to SD card
- // which uses a bunch of power and takes time
- if ((millis() - syncTime) < SYNC_INTERVAL) return;
- syncTime = millis();
- // blink LED to show we are syncing data to the card & updating FAT!
- digitalWrite(redLEDpin, HIGH);
- logfile.flush();
- digitalWrite(redLEDpin, LOW);
- }
- [/code]
Add Comment
Please, Sign In to add comment