Guest User

Untitled

a guest
Jun 6th, 2018
82
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. [code]
  2. #include <SPI.h>
  3. #include <SD.h>
  4. #include <Wire.h>
  5. #include <RTClib.h>
  6. #include <Adafruit_PN532.h>
  7.  
  8. // A simple data logger for the Arduino analog pins
  9.  
  10. // how many milliseconds between grabbing data and logging it. 1000 ms is once a second
  11. #define LOG_INTERVAL 1000 // mills between entries (reduce to take more/faster data)
  12.  
  13. // how many milliseconds before writing the logged data permanently to disk
  14. // set it to the LOG_INTERVAL to write each time (safest)
  15. // set it to 10*LOG_INTERVAL to write all data every 10 datareads, you could lose up to
  16. // the last 10 reads if power is lost but it uses less power and is much faster!
  17. #define SYNC_INTERVAL 1000 // mills between calls to flush() - to write data to the card
  18. uint32_t syncTime = 0; // time of last sync()
  19.  
  20. #define ECHO_TO_SERIAL 1 // echo data to serial port
  21. #define WAIT_TO_START 0 // Wait for serial input in setup()
  22.  
  23. // the digital pins that connect to the LEDs
  24. #define redLEDpin 3
  25. #define greenLEDpin 4
  26.  
  27. // RFID pins
  28.  
  29. // If using the breakout or shield with I2C, define just the pins connected
  30. // to the IRQ and reset lines. Use the values below (2, 3) for the shield!
  31. #define PN532_IRQ (2)
  32. #define PN532_RESET (3) // Not connected by default on the NFC Shield
  33.  
  34. // Or use this line for a breakout or shield with an I2C connection:
  35. Adafruit_PN532 nfc(PN532_IRQ, PN532_RESET);
  36.  
  37. #if defined(ARDUINO_ARCH_SAMD)
  38. for Zero, output on USB Serial console, remove line below if using programming port to program the Zero!
  39. also change #define in Adafruit_PN532.cpp library file
  40. #define Serial SerialUSB
  41. #endif
  42.  
  43. RTC_DS1307 RTC; // define the Real Time Clock object
  44.  
  45. // for the data logging shield, we use digital pin 10 for the SD cs line
  46. const int chipSelect = 10;
  47.  
  48. // the logging file
  49. File logfile;
  50.  
  51. void error(char *str)
  52. {
  53. Serial.print("error: ");
  54. Serial.println(str);
  55.  
  56. // red LED indicates error
  57. digitalWrite(redLEDpin, HIGH);
  58.  
  59. while(1);
  60. }
  61.  
  62. void setup(void)
  63. {
  64. Serial.begin(9600);
  65. Serial.println();
  66.  
  67. // use debugging LEDs
  68. pinMode(redLEDpin, OUTPUT);
  69. pinMode(greenLEDpin, OUTPUT);
  70.  
  71. #if WAIT_TO_START
  72. Serial.println("Type any character to start");
  73. while (!Serial.available());
  74. #endif //WAIT_TO_START
  75.  
  76. // initialize the SD card
  77. Serial.print("Initializing SD card...");
  78. // make sure that the default chip select pin is set to
  79. // output, even if you don't use it:
  80. pinMode(10, OUTPUT);
  81.  
  82. // see if the card is present and can be initialized:
  83. if (!SD.begin(chipSelect)) {
  84. error("Card failed, or not present");
  85. return;
  86. }
  87. Serial.println("card initialized.");
  88.  
  89. // create a new file
  90. char filename[] = "LOGGER00.CSV";
  91. for (uint8_t i = 0; i < 100; i++) {
  92. filename[6] = i/10 + '0';
  93. filename[7] = i%10 + '0';
  94. if (! SD.exists(filename)) {
  95. // only open a new file if it doesn't exist
  96. logfile = SD.open(filename, FILE_WRITE);
  97. break; // leave the loop!
  98. }
  99. }
  100.  
  101. if (! logfile) {
  102. error("couldnt create file");
  103. }
  104.  
  105. Serial.print("Logging to: ");
  106. Serial.println(filename);
  107.  
  108. // connect to RTC
  109. Wire.begin();
  110. if (!RTC.begin()) {
  111. logfile.println("RTC failed");
  112. #if ECHO_TO_SERIAL
  113. Serial.println("RTC failed");
  114. #endif //ECHO_TO_SERIAL
  115. }
  116.  
  117.  
  118. logfile.println("millis,time,id");
  119. #if ECHO_TO_SERIAL
  120. Serial.println("millis,time,id");
  121. #endif //ECHO_TO_SERIAL
  122.  
  123. // If you want to set the aref to something other than 5v
  124. //analogReference(EXTERNAL);
  125.  
  126. // Set up RFID reader
  127.  
  128. #ifndef ESP8266
  129. while (!Serial); // for Leonardo/Micro/Zero
  130. #endif
  131. Serial.begin(115200);
  132. Serial.println("Hello!");
  133.  
  134. nfc.begin();
  135.  
  136. uint32_t versiondata = nfc.getFirmwareVersion();
  137. if (! versiondata) {
  138. Serial.print("Didn't find PN53x board");
  139. while (1); // halt
  140. }
  141. // Got ok data, print it out!
  142. Serial.print("Found chip PN5"); Serial.println((versiondata>>24) & 0xFF, HEX);
  143. Serial.print("Firmware ver. "); Serial.print((versiondata>>16) & 0xFF, DEC);
  144. Serial.print('.'); Serial.println((versiondata>>8) & 0xFF, DEC);
  145.  
  146. // configure board to read RFID tags
  147. nfc.SAMConfig();
  148.  
  149. Serial.println("Waiting for an ISO14443A Card ...");
  150.  
  151. }
  152.  
  153. void loop(void)
  154. {
  155. DateTime now;
  156.  
  157. // delay for the amount of time we want between readings
  158. delay((LOG_INTERVAL -1) - (millis() % LOG_INTERVAL));
  159.  
  160. digitalWrite(greenLEDpin, HIGH);
  161.  
  162. // log milliseconds since starting
  163. uint32_t m = millis();
  164. logfile.print(m); // milliseconds since start
  165. logfile.print(", ");
  166. #if ECHO_TO_SERIAL
  167. Serial.print(m); // milliseconds since start
  168. Serial.print(", ");
  169. #endif
  170.  
  171. // fetch the time
  172. now = RTC.now();
  173. // log time
  174. logfile.print(now.unixtime()); // seconds since 1/1/1970
  175. logfile.print(", ");
  176. logfile.print('"');
  177. logfile.print(now.year(), DEC);
  178. logfile.print("/");
  179. logfile.print(now.month(), DEC);
  180. logfile.print("/");
  181. logfile.print(now.day(), DEC);
  182. logfile.print(" ");
  183. logfile.print(now.hour(), DEC);
  184. logfile.print(":");
  185. logfile.print(now.minute(), DEC);
  186. logfile.print(":");
  187. logfile.print(now.second(), DEC);
  188. logfile.print('"');
  189. #if ECHO_TO_SERIAL
  190. Serial.print(now.unixtime()); // seconds since 1/1/1970
  191. Serial.print(", ");
  192. Serial.print('"');
  193. Serial.print(now.year(), DEC);
  194. Serial.print("/");
  195. Serial.print(now.month(), DEC);
  196. Serial.print("/");
  197. Serial.print(now.day(), DEC);
  198. Serial.print(" ");
  199. Serial.print(now.hour(), DEC);
  200. Serial.print(":");
  201. Serial.print(now.minute(), DEC);
  202. Serial.print(":");
  203. Serial.print(now.second(), DEC);
  204. Serial.print('"');
  205. #endif //ECHO_TO_SERIAL
  206.  
  207. //log RFID data
  208.  
  209. int buzzer = 7;
  210. uint8_t success;
  211. uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 }; // Buffer to store the returned UID
  212. uint8_t uidLength; // Length of the UID (4 or 7 bytes depending on ISO14443A card type)
  213.  
  214. // Wait for an ISO14443A type cards (Mifare, etc.). When one is found
  215. // 'uid' will be populated with the UID, and uidLength will indicate
  216. // if the uid is 4 bytes (Mifare Classic) or 7 bytes (Mifare Ultralight)
  217. success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength);
  218.  
  219. if (success) {
  220. // Display some basic information about the card
  221. Serial.println("Found an ISO14443A card");
  222. Serial.print(" UID Length: ");Serial.print(uidLength, DEC);Serial.println(" bytes");
  223. Serial.print(" UID Value: ");
  224. nfc.PrintHex(uid, uidLength);
  225. Serial.println("");
  226.  
  227. if (uidLength == 4)
  228. {
  229. // We probably have a Mifare Classic card ...
  230. Serial.println("Seems to be a Mifare Classic card (4 byte UID)");
  231.  
  232. // Now we need to try to authenticate it for read/write access
  233. // Try with the factory default KeyA: 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF
  234. Serial.println("Trying to authenticate block 4 with default KEYA value");
  235. uint8_t keya[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
  236.  
  237. // Start with block 4 (the first block of sector 1) since sector 0
  238. // contains the manufacturer data and it's probably better just
  239. // to leave it alone unless you know what you're doing
  240. success = nfc.mifareclassic_AuthenticateBlock(uid, uidLength, 4, 0, keya);
  241.  
  242. if (success)
  243. {
  244. Serial.println("Sector 1 (Blocks 4..7) has been authenticated");
  245. uint8_t data[16];
  246.  
  247. // If you want to write something to block 4 to test with, uncomment
  248. // the following line and this text should be read back in a minute
  249. //memcpy(data, (const uint8_t[]){ 'a', 'd', 'a', 'f', 'r', 'u', 'i', 't', '.', 'c', 'o', 'm', 0, 0, 0, 0 }, sizeof data);
  250. // success = nfc.mifareclassic_WriteDataBlock (4, data);
  251.  
  252. // Try to read the contents of block 4
  253. success = nfc.mifareclassic_ReadDataBlock(4, data);
  254.  
  255. if (success)
  256. {
  257. // Data seems to have been read ... spit it out
  258. Serial.println("Reading Block 4:");
  259. nfc.PrintHexChar(data, 16);
  260. Serial.println("");
  261.  
  262. // Wait a bit before reading the card again
  263. delay(1000);
  264. }
  265. else
  266. {
  267. Serial.println("Ooops ... unable to read the requested block. Try another key?");
  268. }
  269. }
  270. else
  271. {
  272. Serial.println("Ooops ... authentication failed: Try another key?");
  273. }
  274. }
  275.  
  276. if (uidLength == 7)
  277. {
  278. // We probably have a Mifare Ultralight card ...
  279. Serial.println("Seems to be a Mifare Ultralight tag (7 byte UID)");
  280.  
  281. // Try to read the first general-purpose user page (#4)
  282. Serial.println("Reading page 4");
  283. uint8_t data[32];
  284. success = nfc.mifareultralight_ReadPage (4, data);
  285. if (success)
  286. {
  287. // Data seems to have been read ... spit it out
  288. nfc.PrintHexChar(data, 4);
  289. Serial.println("");
  290.  
  291. // Wait a bit before reading the card again
  292. delay(1000);
  293. }
  294. else
  295. {
  296. Serial.println("Ooops ... unable to read the requested page!?");
  297. }
  298. }
  299. }
  300.  
  301. pinMode(success, INPUT);
  302. pinMode(buzzer, OUTPUT);
  303.  
  304. if(digitalRead(success) == HIGH){ // If RFID card is detected, turn buzzer ON
  305. digitalWrite(buzzer, HIGH);
  306. delay (500); // Wait for 0.5 seconds
  307. digitalWrite(buzzer, LOW); // Turn off buzzer
  308. }
  309. logfile.print(", ");
  310. logfile.print(success);
  311. #if ECHO_TO_SERIAL
  312. Serial.print(", ");
  313. Serial.print(success);
  314. #endif //ECHO_TO_SERIAL
  315.  
  316.  
  317. digitalWrite(greenLEDpin, LOW);
  318.  
  319. // Now we write data to disk! Don't sync too often - requires 2048 bytes of I/O to SD card
  320. // which uses a bunch of power and takes time
  321. if ((millis() - syncTime) < SYNC_INTERVAL) return;
  322. syncTime = millis();
  323.  
  324. // blink LED to show we are syncing data to the card & updating FAT!
  325. digitalWrite(redLEDpin, HIGH);
  326. logfile.flush();
  327. digitalWrite(redLEDpin, LOW);
  328.  
  329. }
  330.  
  331.  
  332. [/code]
RAW Paste Data Copied