Jump to content
Sign in to follow this  
jim

Arduino NFC send data on card tap

Recommended Posts

Hey guys

I am trying to send some data to my php script when a mifare classic card is tapped on my seeedstudio v2 NFC shield. I can read and write the data, now I would like to take that data and ship it over to a php script. does anyone know a way I could do that? I am open to other suggestions or methods to achieving this.

Before I got a pcduino, I was using an Arduino Uno with an adafruit nfc shield and Arduino's ethernet shield. The ethernet shield came with a library that allowed me to print and http request to the client. Here is my code that I used on the arduino uno.

#include <SPI.h>
#include <Ethernet.h>

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

PN532_I2C pn532_i2c(Wire);
NfcAdapter nfc = NfcAdapter(pn532_i2c);

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress server(198,189,195,46);

void setup(void) {
  delay(1000);
  Ethernet.begin(mac);
  Serial.begin(9600);
  Serial.println("NDEF Reader");
  nfc.begin();
}

void loop(void) {
  Serial.println("\nScan a NFC tag\n");

  if (nfc.tagPresent())
  {
    NfcTag tag = nfc.read();
    Serial.println(tag.getTagType());
    Serial.print("UID: ");Serial.println(tag.getUidString());

    if (tag.hasNdefMessage()) // every tag won't have a message
    {

      NdefMessage message = tag.getNdefMessage();
      Serial.print("\nStudent Data Found!");
      if (message.getRecordCount() != 1) {
        Serial.print("s");
      }

      // cycle through the records, printing some info from each
        NdefRecord record = message.getRecord(0);
        // NdefRecord record = message[i]; // alternate syntax


        // The TNF and Type should be used to determine how your application processes the payload
        // There's no generic processing for the payload, it's returned as a byte[]
        int payloadLength = record.getPayloadLength();
        byte payload[payloadLength];
        record.getPayload(payload);

        // Force the data into a String (might work depending on the content)
        // Real code should use smarter processing
        String payloadAsString;
        for (int c = 1; c < payloadLength; c++) {
          payloadAsString += (char)payload[c];
        }
        Serial.print("  Payload (as String): ");
        Serial.println(payloadAsString);
        

        // id is probably blank and will return ""
        String uid = record.getId();
        if (uid != "") {
          Serial.print("  ID: ");Serial.println(uid);
        }
        sendData(payloadAsString);
    }
  }
}

void sendData(String studentInfo){
  EthernetClient client;
  if(client.connect(server, 80)){
      
      Serial.println("Connected to ego...");
      Serial.println(studentInfo);
      client.print("GET /ego/addPointNFC.php?student=");
      client.print(studentInfo);
      client.print(" HTTP/1.1\r\n");
      client.println("HOST: localhost\r\n");
      client.print("");  
      client.stop();
      
      Serial.println("Point Added!");                       
  }
  else{
     Serial.println("Connection Failed.");
     Serial.println();
  }
  delay(1000);
}

Everything worked great! I would like to achieve this same functionality on the pcduino, but this time using pcduino's wireless module.

Thank you!

Share this post


Link to post
Share on other sites

Hi there,

I'm very interested to hear your experience with the hardware you have mentioned. I have an Arduino Uno and Arduino's ethernet shield and I'm after buying the adafruit nfc shield (enroute by courier at the moment). I have an urgent project that requires these 3 pieces to work together - can you tell me your experiences with them and if they're reliable? Have you had any issues or conflicts with the mentioned hardware and any pitfalls to watch out for?

Thanks in advance!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×