How to write text message to NFC

I'm a newbie to NFC and am using a Sparkfun ST25DV64KC. My microcontroller is a ESP32 (Firebeetle). I'm using the ST25DV64KC Arduino Library provide by sparkfun and specifically I'm starting with "Example 10-NDEF_Text" Listed below. On my apple phone I'm using the NFC_tools app. Everything works correctly when I upload and then use NFC_tools to read the text record/s.

What I'm wanting to do, and cannot figure it out, is instead of in the payload of the record display Hex values, instead I would like to display a string of text (perhaps using an char array) followed by the current value of a variable in my program, something like the following:

Days user has taken their medication = 3

where "Days user has taken their medication" is the string and 3 is the variable. I do not need to store the string or the variable in eeprom. Using the Example 10-NDEF_Text as a basis, can someone give me guidance on how to do this?

/*
  ST25DV64KC Example
  By: Ricardo Ramos and Paul Clark
  SparkFun Electronics
  Date: August, 2022
  License: MIT. Please see the license file for more information but you can
  basically do whatever you want with this code.

  This example shows how to set up the ST25DV64KC's Capability Container (CC)
  and create a NDEF UTF-8 Text record

  Feel like supporting open source hardware?
  Buy a board from SparkFun!
  SparkFun Qwiic RFID Tag - ST25DV64KC : https://www.sparkfun.com/products/19035

  Hardware Connections:
  Plug a Qwiic cable into the Qwiic RFID Tag and a RedBoard
  If you don't have a platform with a Qwiic connection use the SparkFun Qwiic Breadboard Jumper (https://www.sparkfun.com/products/14425)
  Open the serial monitor at 115200 baud to see the output
*/

#include <SparkFun_ST25DV64KC_Arduino_Library.h> // Click here to get the library:  http://librarymanager/All#SparkFun_ST25DV64KC

SFE_ST25DV64KC_NDEF tag;

void setup()
{
  delay(1000);

  Serial.begin(115200);
  Wire.begin();

  Serial.println(F("ST25DV64KC example."));

  if (!tag.begin(Wire))
  {
    Serial.println(F("ST25 not detected. Freezing..."));
    while (1) // Do nothing more
      ;
  }

  Serial.println(F("ST25 connected."));

  // -=-=-=-=-=-=-=-=-

  // The previous examples will have left the memory write-enabled.
  // We should not need to open a security session here...
  /*
  Serial.println(F("Opening I2C security session with default password (all zeros)."));
  uint8_t password[8] = {0x0}; // Default password is all zeros
  tag.openI2CSession(password);

  Serial.print(F("I2C session is "));
  Serial.println(tag.isI2CSessionOpen() ? "opened." : "closed.");
  */

  // -=-=-=-=-=-=-=-=-

  // Clear the first 256 bytes of user memory
  uint8_t tagMemory[256];
  memset(tagMemory, 0, 256);

  Serial.println("Writing 0x0 to the first 256 bytes of user memory.");
  tag.writeEEPROM(0x0, tagMemory, 256);

  // -=-=-=-=-=-=-=-=-

  // Write the Type 5 CC File - starting at address zero
  Serial.println(F("Writing CC_File"));
  tag.writeCCFile8Byte();

  // -=-=-=-=-=-=-=-=-

  // Write two NDEF UTF-8 Text records
  uint16_t memLoc = tag.getCCFileLen();

  Serial.println(F("Writing the first NDEF Text record"));
  tag.writeNDEFText("Hello, World!", &memLoc, true, false); // MB=1, ME=0
  
  Serial.println(F("Writing the second NDEF Text record"));
  tag.writeNDEFText("All your base are belong to us", &memLoc, false, true); // MB=0, ME=1

  // -=-=-=-=-=-=-=-=-

  // Read back the second NDEF UTF-8 Text record
  Serial.println(F("Reading the second NDEF Text record:"));
  char theText[40];
  if (tag.readNDEFText(theText, 40, 2))
    Serial.println(theText);
  else
    Serial.println(F("Read failed!"));

  // -=-=-=-=-=-=-=-=-

  // Read back the memory contents
  Serial.println(F("The first 256 bytes of user memory are:"));
  tag.readEEPROM(0x0, tagMemory, 256);
  prettyPrintChars(tagMemory, 256);
}

void loop()
{
  // Nothing to do here
}

void prettyPrintChars(uint8_t *theData, int theLength) // Pretty-print char data in HEX and ASCII format
{
  Serial.println();

  for (int i = 0; i < theLength; i += 16)
  {
    if (i < 10000)
      Serial.print(F("0"));
    if (i < 1000)
      Serial.print(F("0"));
    if (i < 100)
      Serial.print(F("0"));
    if (i < 10)
      Serial.print(F("0"));
    Serial.print(i);

    Serial.print(F(" 0x"));

    if (i < 0x1000)
      Serial.print(F("0"));
    if (i < 0x100)
      Serial.print(F("0"));
    if (i < 0x10)
      Serial.print(F("0"));
    Serial.print(i, HEX);

    Serial.print(F(" "));

    int j;
    for (j = 0; ((i + j) < theLength) && (j < 16); j++)
    {
      if (theData[i + j] < 0x10)
        Serial.print(F("0"));
      Serial.print(theData[i + j], HEX);
      Serial.print(F(" "));
    }

    if (((i + j) == theLength) && (j < 16))
    {
      for (int k = 0; k < (16 - (theLength % 16)); k++)
      {
        Serial.print(F("   "));
      }
    }

    for (j = 0; ((i + j) < theLength) && (j < 16); j++)
    {
      if ((theData[i + j] >= 0x20) && (theData[i + j] <= 0x7E))
        Serial.write(theData[i + j]);
      else
        Serial.print(F("."));
    }

    Serial.println();
  }

  Serial.println();
}

what is NFC?

@gcjr "near field communication"

As I see in the code , you already tried to output the text via writeNDEFText . Is it works?
If so, what the problem just to replace one text to another?

Yes, after studying the example I think I understand that the way NDEF works is to write to the Payload I must "stream" bytes using Serial.write() - at lease that is my understanding. I was looking for a more simple way to do it. If my assumption is correct, then I will attempt to create an array for my text message/s and use the array in the i and j loops to "build" message to serialize.

wouldn't you just change this to the message you want?

char buffer[40];
int days = 3;
sprintf(buffer,"Days user has taken their medication = %d", days);
tag.writeNDEFText(buffer, &memLoc, false, true); // MB=0, ME=1

This could work for me to just make it part of the Record tag as you've shown. It does compile and upload, but unfortunately your character buffer and format does not work in either the serial monitor or on the mobile app NFC_tools reader.

Reading the second NDEF Text record:

Read failed!

The first 256 bytes of user memory are:

00000 0x0000 E2 40 00 01 00 00 03 FF 51 55 2B 54 02 65 6E 44 .@......QU+T.enD

00016 0x0010 61 79 73 20 75 73 65 72 20 68 61 73 20 74 61 6B ays user has tak

00032 0x0020 65 6E 20 74 68 65 69 72 20 6D 65 64 69 63 61 74 en their medicat

00048 0x0030 69 6F 6E 20 3D 20 33 51 01 21 54 02 65 6E 41 6C ion = 3Q.!T.enAl

00064 0x0040 6C 20 79 6F 75 72 20 62 61 73 65 20 61 72 65 20 l your base are

00080 0x0050 62 65 6C 6F 6E 67 20 74 6F 20 75 73 FE 00 00 00 belong to us....

00096 0x0060 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................

00112 0x0070 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................

00128 0x0080 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................

00144 0x0090 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................

00160 0x00A0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................

00176 0x00B0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................

00192 0x00C0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................

00208 0x00D0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................

00224 0x00E0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................

00240 0x00F0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................

I think the tag.writeNDEFText() does not accept the buffer in place of quotations ?

Answered here:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.