Use Seeeduino Arch(or Arch Pro) NFC Shield to exchange NDEF messages with Android 4.0
Fork of PN532_P2P by
Code for NFC breakout board by Elechouse to communicate with Android phone
main.cpp@2:367b193b7540, 2014-04-08 (annotated)
- Committer:
- Margulan
- Date:
- Tue Apr 08 18:55:37 2014 +0000
- Revision:
- 2:367b193b7540
- Parent:
- 1:f228fdc41598
ndef
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
yihui | 0:21ac4fe1ccf8 | 1 | #include "mbed.h" |
yihui | 1:f228fdc41598 | 2 | #include "PN532_SPI.h" |
yihui | 1:f228fdc41598 | 3 | #include "snep.h" |
yihui | 1:f228fdc41598 | 4 | #include "NdefMessage.h" |
Margulan | 2:367b193b7540 | 5 | #include "NdefRecord.h" |
yihui | 0:21ac4fe1ccf8 | 6 | |
Margulan | 2:367b193b7540 | 7 | InterruptIn irq(PTD5); |
Margulan | 2:367b193b7540 | 8 | AnalogIn LM35(PTC2); |
Margulan | 2:367b193b7540 | 9 | |
Margulan | 2:367b193b7540 | 10 | #define LOG(args...) pc.printf(args)//LOG of connection |
Margulan | 2:367b193b7540 | 11 | SPI spi(PTD2, PTD3, PTC5); // SPI(mosi, miso, clk) |
Margulan | 2:367b193b7540 | 12 | PN532_SPI pn532spi(spi, PTD0); // SPI (ss) |
Margulan | 2:367b193b7540 | 13 | Serial pc(USBTX, USBRX); //serial connection to PC |
Margulan | 2:367b193b7540 | 14 | DigitalOut led1(LED_RED); //digital output |
Margulan | 2:367b193b7540 | 15 | DigitalOut led2(LED_GREEN); //indicators |
yihui | 0:21ac4fe1ccf8 | 16 | |
Margulan | 2:367b193b7540 | 17 | SNEP nfc(pn532spi); //simple NDEF Exchange Protocol |
yihui | 1:f228fdc41598 | 18 | uint8_t ndefBuf[128]; |
yihui | 0:21ac4fe1ccf8 | 19 | |
yihui | 0:21ac4fe1ccf8 | 20 | int main() |
Margulan | 2:367b193b7540 | 21 | { |
Margulan | 2:367b193b7540 | 22 | float temp; //measured temperature |
Margulan | 2:367b193b7540 | 23 | while(1){temp=LM35.read(); |
Margulan | 2:367b193b7540 | 24 | #if 1 //initiator mode: starts connection with Android phone and |
Margulan | 2:367b193b7540 | 25 | //sends NDEF message (text, url, MimeMedia or Application package) |
Margulan | 2:367b193b7540 | 26 | led1=1; //switch off RGB LED |
Margulan | 2:367b193b7540 | 27 | led2=1; |
Margulan | 2:367b193b7540 | 28 | |
Margulan | 2:367b193b7540 | 29 | LOG("\rBring close an NFC-enabled phone\n"); |
Margulan | 2:367b193b7540 | 30 | int msgSize = nfc.read(ndefBuf, sizeof(ndefBuf)); |
Margulan | 2:367b193b7540 | 31 | if (msgSize > 0) { |
Margulan | 2:367b193b7540 | 32 | NdefMessage msg = NdefMessage(ndefBuf, msgSize); |
Margulan | 2:367b193b7540 | 33 | LOG("\r Connection is successful.\n"); |
Margulan | 2:367b193b7540 | 34 | int recordCount = msg.getRecordCount(); |
Margulan | 2:367b193b7540 | 35 | for (int i = 0; i < recordCount; i++) |
Margulan | 2:367b193b7540 | 36 | { |
Margulan | 2:367b193b7540 | 37 | LOG("\r NDEF Record %d\n",i+1); |
Margulan | 2:367b193b7540 | 38 | NdefRecord record = msg.getRecord(i); |
Margulan | 2:367b193b7540 | 39 | LOG("\r TNF: %u", record.getTnf()); |
Margulan | 2:367b193b7540 | 40 | LOG("\n\r Type: %s", record.getType()); |
Margulan | 2:367b193b7540 | 41 | |
Margulan | 2:367b193b7540 | 42 | // The TNF and Type should be used to determine how your application processes the payload |
Margulan | 2:367b193b7540 | 43 | int payloadLength = record.getPayloadLength(); |
Margulan | 2:367b193b7540 | 44 | uint8_t payload[payloadLength]; |
Margulan | 2:367b193b7540 | 45 | record.getPayload(payload); |
Margulan | 2:367b193b7540 | 46 | |
Margulan | 2:367b193b7540 | 47 | // Force the data into a String (might work depending on the content) |
Margulan | 2:367b193b7540 | 48 | string payloadAsString = ""; |
Margulan | 2:367b193b7540 | 49 | for (int c = 0; c < payloadLength; c++) { |
Margulan | 2:367b193b7540 | 50 | payloadAsString += (char)payload[c]; |
Margulan | 2:367b193b7540 | 51 | } |
Margulan | 2:367b193b7540 | 52 | if (payloadLength > temp*330){ |
Margulan | 2:367b193b7540 | 53 | led2=0;//scenario 3 |
Margulan | 2:367b193b7540 | 54 | LOG("\r\nTemperature is normal"); |
Margulan | 2:367b193b7540 | 55 | wait(3); |
Margulan | 2:367b193b7540 | 56 | led2=1; |
Margulan | 2:367b193b7540 | 57 | } |
Margulan | 2:367b193b7540 | 58 | else{led1=0; |
Margulan | 2:367b193b7540 | 59 | LOG("\r\nTemperature is too high"); |
Margulan | 2:367b193b7540 | 60 | wait(2); |
Margulan | 2:367b193b7540 | 61 | led1=1;} |
Margulan | 2:367b193b7540 | 62 | LOG("\n\r Received message '%s'\n", payloadAsString); |
Margulan | 2:367b193b7540 | 63 | LOG("\r Command length %d\n", payloadLength); |
Margulan | 2:367b193b7540 | 64 | printf("\r Temperature %.2f C\n", temp*330); |
Margulan | 2:367b193b7540 | 65 | } |
Margulan | 2:367b193b7540 | 66 | |
Margulan | 2:367b193b7540 | 67 | } else { |
Margulan | 2:367b193b7540 | 68 | LOG("\rConnection failed. Try again.\n"); |
Margulan | 2:367b193b7540 | 69 | led1=0; |
Margulan | 2:367b193b7540 | 70 | wait(2); |
Margulan | 2:367b193b7540 | 71 | } |
yihui | 1:f228fdc41598 | 72 | #endif |
Margulan | 2:367b193b7540 | 73 | led1=1; |
Margulan | 2:367b193b7540 | 74 | led2=1; |
Margulan | 2:367b193b7540 | 75 | wait(2); //polling cycle is 2 sec |
Margulan | 2:367b193b7540 | 76 | } |
Margulan | 2:367b193b7540 | 77 | } |