Read nfc tags and push data to thingspeak and send a push notification to users accordingly.

Dependencies:   EthernetInterface_NFC HTTPClient_NFC LibPN532_NFC mbed-rtos mbed

Fork of PN532_ReadUid by dotnfc Tang

Revision:
1:6e04ab90909f
Parent:
0:54a12c4b19c2
--- a/main.cpp	Tue Sep 13 06:18:59 2016 +0000
+++ b/main.cpp	Sun Apr 22 23:33:45 2018 +0000
@@ -1,142 +1,113 @@
-// Basic example show how to read a tag uid.
-// you can use i2c, spi, hsu interface.
-//
-// Assembled by dotnfc as Arducleo Sample
-// 2016/09/10
-
-
-
+#include <sstream>
 #include "mbed.h"
 #include "PN532.h"
 #include "PN532_HSU.h"
 #include "PN532_SPI.h"
 #include "PN532_I2C.h"
-
-DigitalOut ledBrd (LED1);   // arducleo onboard led
-
-DigitalOut ledNFC (D9);     // status led
+#include "EthernetInterface.h"
+#include "HTTPClient.h"
 
-DigitalOut rstNFC (D4);     // pn532 chip reset control
-
-PwmOut buz(PB_1);
-
-Serial pc(SERIAL_TX, SERIAL_RX);
+char* thingSpeakUrl = "http://api.thingspeak.com/update";
+char* thingSpeakKey = "7QKJFB65MJBCNM3L";
+EthernetInterface eth;
+TCPSocketConnection sock;
+DigitalOut ledBrd (LED1);   // arducleo onboard led
+Serial pc(USBTX, USBRX);
 
 // ----------------------------------------- HSU
 //HardwareSerial pn532_hsu (PC_10, PC_11);
 //PN532_HSU pn532_if (pn532_hsu);
 
 // ----------------------------------------- SPI
-SPI pn532_spi (SPI_MOSI, SPI_MISO, SPI_SCK);
-PN532_SPI pn532_if (pn532_spi, SPI_CS);
+SPI pn532_spi (D11, D12, D13);
+PN532_SPI pn532_if (pn532_spi, D10);
 
 // ----------------------------------------- I2C
 //I2C pn532_i2c (I2C_SDA, I2C_SCL);
 //PN532_I2C pn532_if (pn532_i2c);
 
-
-
 PN532 nfc(pn532_if);
-
-/*==============================================================================
- * \brief reset the pn532 chip
- */
-void reset_chip (void)
-{
-    rstNFC = 0;
-    wait_ms (100);
-    rstNFC = 1;
-}
-
-
-/*==============================================================================
- * \brief init the peripheral
- */
 void setup(void)
 {
     ledBrd = 0;
-    ledNFC = 0;
-    reset_chip ();
-
     uint32_t versiondata = 0;
     pc.baud(115200);
-    pc.printf ("Hello!\n");
-
     while (1) {
         nfc.begin();
-        //nfc.SAMConfig();
         versiondata = nfc.getFirmwareVersion();
         if (! versiondata) {
-            pc.printf("Didn't find PN53x board\n\n");
+            pc.printf("Didn't find PN53x board\n\n\r");
             wait_ms(500);
         } else {
             break;
         }
     }
-
-    // Got ok data, print it out!
-    pc.printf ("Found chip PN5%02X , Firmware ver. %d.%d\n",
+    pc.printf ("\rFound chip PN5%02X , Firmware ver. %d.%d\n",
                (versiondata>>24) & 0xFF,
                (versiondata>>16) & 0xFF,
                (versiondata>>8) & 0xFF);
-
-    // 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(0xFF);
 
     // configure board to read RFID tags
-    nfc.SAMConfig();
-
-    pc.printf ("\nWaiting for an ISO14443A card\n");
-}
+    //nfc.SAMConfig();
 
-
-/*==============================================================================
- * \brief find a tag
- */
+    pc.printf ("\n\rWaiting for an ISO14443A card\n");
+}
 void loop(void)
 {
     bool 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)
-
-    // configure board to read RFID tags
-    nfc.SAMConfig();
-
-    // 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)
+    uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 };
+    uint8_t uidLength;
     success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, &uid[0], &uidLength);
 
-    printf ("\n");
-
     if (success) {
-        tone (buz, 800);        // turn on the buzzer
-        ledNFC = 1;             // led on
-
-        pc.printf("Found a card!\n");
-
-        pc.printf("UID Length: %d bytes\n", uidLength);
+        pc.printf("\n\rFound a card!\n\r");
+        pc.printf("UID Length: %d bytes\n\r", uidLength);
         pc.printf("UID Value: ");
-
-        for (uint8_t i=0; i < uidLength; i++)
+        for (uint8_t i=0; i < uidLength; i++) {
             pc.printf(" 0x%02X", uid[i]);
-
-        pc.printf("\n");
+        }
+        uint8_t data[4];
+        char card[4];
+        memcpy(card, (const char[]) { 'a', 'n', 'i', 'k'
+        }, sizeof card);
+        // 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', 'n', 'i', 'k'}, sizeof data);
+        //success = nfc.mifareclassic_WriteDataBlock (4, data);
 
-        wait_ms (100);
-        tone (buz, 0);          // turn off the buzzer
-
-        // wait until the card is taken away
-        while (nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, &uid[0], &uidLength, 500)) {}
-
-        ledNFC = 0;         // led off
+        // Try to read the contents of block 4
+        success = nfc.mifareclassic_ReadDataBlock(4, data);
+        if (success) {
+            char s = 's';
+            for (uint8_t i = 0; i < 4; i++) {
+                char c = data[i];
+                if(c!=card[i]) {
+                    s='f';
+                }
+            }
+            char str[]="";
+            int value = 0;
+            if(s=='s') {
+                pc.printf("\n\rCard for Aniket Accessed\n\r");
+                strcat(str, "Aniket's Card Accessed");
+                value = 1;
+            } else {
+                pc.printf("\n\rNo access for this card\n\r");
+                strcat(str,"Invalid Card Accessed");
+                value = 404;
+            }
+            HTTPClient http;
+            char buffer[256];
+            sprintf(buffer,"%s?api_key=%s&field1=%d",thingSpeakUrl,thingSpeakKey,value);
+            printf("Send to %s\r\n", buffer);
+            http.get(buffer, buffer , 10);
+        }
+        wait_ms (10000);
     } else {
         // PN532 probably timed out waiting for a card
-        pc.printf("\nTimed out waiting for a card\n");
-        ledNFC = 0;
-        wait_ms (200);
+        pc.printf("\n\rWaiting for a card\n\r");
+        wait_ms (5000);
     }
 }
 
@@ -146,9 +117,14 @@
  */
 int main()
 {
+
+    eth.init(); //Use DHCP
+    eth.connect();
+    printf("\n\rEthernet connected, and IP Address is %s\n\r", eth.getIPAddress());
     setup();
 
     while (1)
         loop ();
+    eth.disconnect();
 }