Skip to content

Instantly share code, notes, and snippets.

@don
Created December 9, 2015 04:17
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save don/2bf283f81485f89dca13 to your computer and use it in GitHub Desktop.
Save don/2bf283f81485f89dca13 to your computer and use it in GitHub Desktop.
Writing large Peer to Peer NFC messages from Arduino
// generate raw NDEF bytes for cut and paste into an Arduino sketch
//
// $ npm install ndef
// $ node generate_ndef_bytes.js 10
// cut and paste the output into your Arduino sketch
var ndef = require('ndef');
var NUM_CHARS = process.argv[2] || 226;
var data = "";
for (var i = 0; i < NUM_CHARS; i++) {
data += "a";
}
var record = ndef.textRecord(data);
var bytes = ndef.encodeMessage([record]);
console.log('// NDEF message with ' + NUM_CHARS + ' char string (TNF WELL_KNOWN, RTD Text)')
console.log('uint8_t ndef_buffer[] = {' + bytes.join() + '};');
// Sends a NDEF Message to a Peer
// Requires SPI. Tested with Seeed Studio NFC Shield v2
#include "SPI.h"
#include "PN532_SPI.h"
#include "snep.h"
#include "NdefMessage.h"
#define TEXT_SIZE 226
#define LANGUAGE_ENCODING_SIZE 3
#define PAYLOAD_SIZE (TEXT_SIZE + LANGUAGE_ENCODING_SIZE)
#define MESSAGE_ENCODING_SIZE 4
#define NDEF_BUFFER_SIZE (PAYLOAD_SIZE + MESSAGE_ENCODING_SIZE)
PN532_SPI pn532spi(SPI, 10);
SNEP nfc(pn532spi);
uint8_t ndefBuf[NDEF_BUFFER_SIZE];
int messageSize;
void setup() {
Serial.begin(9600);
Serial.println("NFC Peer to Peer Example - Send Message");
NdefMessage message = NdefMessage();
// manually create a record, so Uno doesn't run out of memory
NdefRecord record = NdefRecord();
record.setTnf(TNF_WELL_KNOWN);
uint8_t recordType[] = { 0x54 }; // "T" Text Record
record.setType(recordType, sizeof(recordType));
uint8_t payload[PAYLOAD_SIZE];
// fill buffer with the character "b"
memset(payload, 0x62, sizeof(payload));
// manually add language encoding for TNF_WELL_KNOWN RTD_TEXT
payload[0] = 0x02;
payload[1] = 0x65; // e
payload[2] = 0x6e; // n
record.setPayload(payload, sizeof(payload));
message.addRecord(record);
messageSize = message.getEncodedSize();
if (messageSize != NDEF_BUFFER_SIZE) {
Serial.println("Buffer size is incorrect!");
while (1) {
}
}
message.encode(ndefBuf);
}
void loop() {
Serial.println("Send a message to Peer");
Serial.println(messageSize);
if (0 >= nfc.write(ndefBuf, messageSize)) {
Serial.println("Failed");
} else {
Serial.println("Success");
}
delay(3000);
}
// Sends a NDEF Message to a Peer
// Requires SPI. Tested with Seeed Studio NFC Shield v2
// Requires Arduino Due (or maybe another board with lots of memory)
#include "SPI.h"
#include "PN532_SPI.h"
#include "snep.h"
#include "NdefMessage.h"
PN532_SPI pn532spi(SPI, 10);
SNEP nfc(pn532spi);
uint8_t ndefBuf[233];
int messageSize;
void setup() {
Serial.begin(9600);
Serial.println("NFC Peer to Peer Example - Send Message");
NdefMessage message = NdefMessage();
// 226 character string creates 233 byte message
// need to run on a Due, since encoding the message run an Uno out of memory
message.addTextRecord("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
messageSize = message.getEncodedSize();
if (messageSize > sizeof(ndefBuf)) {
Serial.println("ndefBuf is too small");
while (1) {
}
}
message.encode(ndefBuf);
}
void loop() {
Serial.println("Send a message to Peer");
Serial.println(messageSize);
if (0 >= nfc.write(ndefBuf, messageSize)) {
Serial.println("Failed");
} else {
Serial.println("Success");
}
delay(3000);
}
// Sends a NDEF Message to a Peer
// Requires SPI. Tested with Seeed Studio NFC Shield v2
// Send Raw Bytes to see if the size limit of PN532
#include "SPI.h"
#include "PN532_SPI.h"
#include "snep.h"
PN532_SPI pn532spi(SPI, 10);
SNEP nfc(pn532spi);
// WELL_KNOWN T "hello"
//uint8_t ndef_buffer[] = {209, 1, 8, 84, 2, 101, 110, 104, 101, 108, 108, 111};
// 226 char string works, 227 char string fails
// NDEF message with 226 char string (TNF WELL_KNOWN, RTD Text)
uint8_t ndef_buffer[] = {209,1,229,84,2,101,110,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97};
// NDEF message with 227 char string (TNF WELL_KNOWN, RTD Text)
// uint8_t ndef_buffer[] = {209,1,230,84,2,101,110,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97};
void setup() {
Serial.begin(9600);
Serial.println("NFC Peer to Peer Example - Send Message");
}
void loop() {
Serial.println("Send a message to Peer");
Serial.println(sizeof(ndef_buffer));
if (0 >= nfc.write(ndef_buffer, sizeof(ndef_buffer))) {
Serial.println("Failed");
} else {
Serial.println("Success");
}
delay(3000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment