I try to make a HTTP GET request with data read from an NDEF formatted MIFARE NFC tag. I fail to convert the byte array data from the tag into a format that works with the Ethernet client print() function.
Hardware setup is an Arduino Uno with a seeedstudio NFC Shield and an Arduino ethernet shield. I make use of the ethernet, the PN532, and the NfcAdapter library.
I tried several types of conversion using char * and char[] instead of the String object with no success.
To pinpoint the problem I picked a case where Serial.print() gives the expected result but client.print() does not.
Code is based on the PN532 NDEF library example 'ReadTagExtended'.
void loop(void)
{
if (nfc.tagPresent()) // Do an NFC scan to see if an NFC tag is present
{
NfcTag tag = nfc.read(); // read the NFC tag
if (tag.hasNdefMessage())
{
NdefMessage message = tag.getNdefMessage();
for (int i = 0; i < message.getRecordCount(); i++)
{
NdefRecord record = message.getRecord(i);
int payloadLength = record.getPayloadLength();
byte payload[payloadLength];
record.getPayload(payload);
String tag_content = "";
for(int i = 0; i<payloadLength; i++) {
tag_content += (char)payload[i];
}
Serial.println(tag_content); // prints the correct string
request(tag_content);
}
}
}
}
void request(String data) {
EthernetClient client;
// if you get a connection, report back via serial:
if (client.connect(remote, 8080)) {
client.print("GET /subaddress");
client.print("?data=");
client.print(data); // unfortunately empty
client.println();
client.println();
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.print(c);
}
}
client.stop();
Serial.println(" OK");
delay(100);
} else {
Serial.println("ERR");
delay(100);
}
}
With the above setup, I get the expected output using Serial.println(). Yet, in the (successfull) request, data is empty.
From the comments (summarized):
Printing the record type (record.getType()) gives the letter 'U'. payloadLength is 4 for a tag containing the string "def".
record.getType(...)? DoesSerial.println(data);print anything when you call it from within therequest()function? What's the value ofpayloadLengthand what data string value is printed "correctly"? IspayloadLengtha few bytes longer than the actual string? - Michael Roland