Dependencies:   PN532 mbed

Fork of PN532_P2P by Yihui Xiong

Code for NFC breakout board by Elechouse to communicate with Android phone

Files at this revision

API Documentation at this revision

Comitter:
Margulan
Date:
Tue Apr 08 18:55:37 2014 +0000
Parent:
1:f228fdc41598
Commit message:
ndef

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Thu Nov 21 08:27:37 2013 +0000
+++ b/main.cpp	Tue Apr 08 18:55:37 2014 +0000
@@ -2,53 +2,76 @@
 #include "PN532_SPI.h"
 #include "snep.h"
 #include "NdefMessage.h"
-
-#if 1                           // Using Seeeduino Arch
-#define LOG(args...)
-SPI spi(P1_22, P1_21, P1_20);  // SPI(mosi, miso, clk)
-PN532_SPI pn532spi(spi, P0_2);
+#include "NdefRecord.h"
 
-#else                           // Using Arch Pro
-#define LOG(args...)        pc.printf(args)
-Serial pc(USBTX, USBRX);
-SPI spi(P0_18, P0_17, P0_15);
-PN532_SPI pn532spi(spi, P0_6);
-#endif
+InterruptIn irq(PTD5);
+AnalogIn LM35(PTC2);
+                   
+#define LOG(args...)    pc.printf(args)//LOG of connection
+SPI spi(PTD2, PTD3, PTC5);  // SPI(mosi, miso, clk)
+PN532_SPI pn532spi(spi, PTD0);  // SPI (ss)
+Serial pc(USBTX, USBRX); //serial connection to PC
+DigitalOut led1(LED_RED);   //digital output
+DigitalOut led2(LED_GREEN); //indicators
 
-DigitalOut led(LED1);
-SNEP nfc(pn532spi);
+SNEP nfc(pn532spi);   //simple NDEF Exchange Protocol
 uint8_t ndefBuf[128];
 
 int main()
-{
-    while (1) {
-#if 1
-        LOG("Send a message to Android");
-        NdefMessage message = NdefMessage();
-        message.addUriRecord("http://www.seeedstudio.com");
-        int messageSize = message.getEncodedSize();
-        if (messageSize > sizeof(ndefBuf)) {
-            LOG("ndefBuf is too small");
-            while (1) {
-            }
-        } 
-        message.encode(ndefBuf);
-        if (0 >= nfc.write(ndefBuf, messageSize)) {
-            LOG("Failed\n");
-        } else {
-            LOG("Success\n");
-        }
-#else
-        LOG("Get a message from Android");
-        int msgSize = nfc.read(ndefBuf, sizeof(ndefBuf));
-        if (msgSize > 0) {
-            NdefMessage msg  = NdefMessage(ndefBuf, msgSize);
-            msg.print();
-            LOG("\nSuccess");
-        } else {
-            LOG("failed");
-        }
+{  
+float temp;  //measured temperature
+while(1){temp=LM35.read();
+#if 1    //initiator mode: starts connection with Android phone and 
+         //sends NDEF message (text, url, MimeMedia or Application package)
+        led1=1; //switch off RGB LED
+        led2=1;
+   
+            LOG("\rBring close an NFC-enabled phone\n");
+            int msgSize = nfc.read(ndefBuf, sizeof(ndefBuf));
+            if (msgSize > 0) {   
+            NdefMessage msg = NdefMessage(ndefBuf, msgSize);
+            LOG("\r     Connection is successful.\n");
+            int recordCount = msg.getRecordCount();
+            for (int i = 0; i < recordCount; i++)
+               {
+               LOG("\r  NDEF Record %d\n",i+1);
+               NdefRecord record = msg.getRecord(i);
+               LOG("\r  TNF: %u", record.getTnf());
+               LOG("\n\r  Type: %s", record.getType());
+              
+               // The TNF and Type should be used to determine how your application processes the payload
+               int payloadLength = record.getPayloadLength();
+               uint8_t payload[payloadLength];
+               record.getPayload(payload);
+
+               // Force the data into a String (might work depending on the content)
+               string payloadAsString = "";
+               for (int c = 0; c < payloadLength; c++) {
+               payloadAsString += (char)payload[c];
+               }
+               if (payloadLength > temp*330){
+                   led2=0;//scenario 3
+                   LOG("\r\nTemperature is normal");
+                   wait(3);
+                   led2=1;
+                   }
+                   else{led1=0;
+                   LOG("\r\nTemperature is too high");
+                   wait(2);
+                   led1=1;}
+               LOG("\n\r  Received message '%s'\n", payloadAsString);
+               LOG("\r  Command length %d\n", payloadLength);    
+               printf("\r  Temperature %.2f C\n", temp*330);
+               }   
+               
+           } else {
+               LOG("\rConnection failed. Try again.\n");
+               led1=0;
+               wait(2);
+               }
 #endif
-        wait(5);
-    }
-}
\ No newline at end of file
+        led1=1;
+        led2=1;
+        wait(2); //polling cycle is 2 sec
+}
+}