cancel
Showing results for 
Search instead for 
Did you mean: 

arduino: read NDEF Wi-Fi connection

RMako.2
Associate II

Hi

I am trying to create an application using arduino which will read 4 NDEF text messages where will be stored data needed to establish Wi-Fi connection (host, ssid, password, token).

I did try to run example from stduion ST25 library and thats work fine but only for URL NDEF.

Do you have any working example to show how to read (not need to write) 4 different text NDEF and use those data as I described above?

I would be very gratefull for any help.

thank you!

1 ACCEPTED SOLUTION

Accepted Solutions
Rene Lenerve
ST Employee

Hi @Community member​,

If by updating this value, this allow you to obtain only 2 bytes more, it means for me that you have not enough memory available in your system. You can have some information with the compiler to know more about memory space used by your code.

Another thing to check, that could result in a limitation of bytes to read, is if your i2c read function is limited by a timeout. A read of 42 bytes can spent around 1ms for a speed set to 400kbit/s on I2C.

Hope this can help you.

Kind Regards.

View solution in original post

13 REPLIES 13
Ulysses HERNIOSUS
ST Employee

Hi RMako.2,

I assume you will want to prepare a Wifi pairing message. It is one of the types defined by NFC Forum. You can find an implementation inside ST25 NFC Lib. Inside you can find Middlewares/ST/NDEF with ndef_type_wifi.c,h.

Implementations for using it can be found in the various demos, especially the ndef_rw and the bluetooth_pairing for the ST25R3916.

To my knowledge this has not been ported on stm32duino but you may want to check on their forum. Anyhow I would not expect any problems using the NDEF middleware on stm32duino.

Best Regards, Ulysses

MPale.2
Associate

Hi

thank you for anwser

I went frough demos which you mention but unfortuanately in ndef_rw did not find wifi example

below is my code for wifi and URL - URL is working fine but wifi is ending by foult

int ST25DV::readWIFI(String *s)
{
  uint16_t ret;
  sWifiTokenInfo wifi;
  sRecordInfo_t recordInfo;
 
  ret = NDEF_ReadNDEF(NDEF_Buffer);
  if (ret) {
    return ret;
  }
 
  ret = NDEF_IdentifyBuffer(&recordInfo, NDEF_Buffer);
  if (ret) {
    return ret;
  }
 
  ret = NDEF_ReadWifiToken(&recordInfo, &wifi);
  if (ret) {
    return ret;
  }
  *s = String(wifi.NetworkKey) + String(wifi.NetworkSSID);
 
  return 0;
}
int ST25DV::readURI(String *s)
{
  uint16_t ret;
  sURI_Info uri = {"", "", ""};
  sRecordInfo_t recordInfo;
 
  ret = NDEF_ReadNDEF(NDEF_Buffer);
  if (ret) {
    return ret;
  }
 
  ret = NDEF_IdentifyBuffer(&recordInfo, NDEF_Buffer);
  if (ret) {
    return ret;
  }
 
  ret = NDEF_ReadURI(&recordInfo, &uri);
  if (ret) {
    return ret;
  }
  *s = String(uri.protocol) + String(uri.URI_Message);
 
  return 0;
}

could you tell me what I am doing wrong?

thank you, Robert

Hi Robert,

please describe a bit more what you are doing (e.g. which tag with which content) and also what exactly you mean by "wifi is ending by foult".

BR, Ulrich

Jasper Preston
ST Employee

Hi RMako.2,

The Wifi type defined by the NFC Forum is available in Middlewares/ST/NDEF, see ndef_type_wifi.h and ndef_type_wifi.c. The ST25 NFC Lib provides a bluetooth_pairing demo for ST25R3916 (in the folder X-NUCLEO-NFC06A1), that can be used as a starting point to implement Wifi pairing.

Replace the function demoEncodeBluetoothRecord() with demoEncodeWifiRecord(), as shown in this example:

ReturnCode demoEncodeWifiRecord(uint8_t *buffer, uint32_t length)
{
    ndefRecord record;
    ndefType ndefWifi;
    ReturnCode err;
 
    if (buffer == NULL)
    {
        return ERR_PARAM;
    }
 
    uint8_t ssid[] = { 0x01, 0x02, 0x03, 0x04 };
    ndefConstBuffer bufNetworkSSID = { ssid, sizeof(ssid) };
    uint8_t  key[] = { 0x05, 0x06, 0x07, 0x08 };
    ndefConstBuffer bufNetworkKey  = { key, sizeof(key) };
 
    ndefTypeWifi wifiConfig = {
        .bufNetworkSSID = bufNetworkSSID,
        .bufNetworkKey  = bufNetworkKey,
        .authentication = NDEF_WIFI_AUTHENTICATION_WPAPSK,
        .encryption     = NDEF_WIFI_ENCRYPTION_TKIP
    };
 
    err = ndefWifiInit(&ndefWifi, &wifiConfig);
    if (err != ERR_NONE)
    {
        return err;
    }
 
    err = ndefTypeToRecord(&ndefWifi, &record);
    if (err != ERR_NONE)
    {
        return err;
    }
 
    err = ndefRecordDump(&record, true);
    if (err != ERR_NONE)
    {
        return err;
    }
 
    /* Encode the record starting after the first two bytes that are saved
       to store the NDEF file length */
    ndefBuffer bufRecord = { &buffer[2], length - 2 };
    err = ndefRecordEncode(&record, &bufRecord);
    if (err != ERR_NONE)
    {
        return err;
    }
 
    /* Store the NDEF file length on the two first bytes */
    buffer[0] = bufRecord.length >> 8;
    buffer[1] = bufRecord.length & 0xFF;
 
    return err;
}

Pay attention to turn on Wifi support in ndef_config_custom.h:

#define NDEF_TYPE_WIFI_SUPPORT		true

This will create a Wifi pairing record.

I hope it will help, please let me know if you face any issue.

Best regards,

Jasper

RMako.2
Associate II

Hello

Thank you for all your support.

I have suceed to read and wright wifi, text and URL NDEF but problem is that I can read only one of them

Program is working fine if I have got only one NDEF in memory for example wifi - then readung is ok, but if I have got 3 (each different type) program is crash and do not read anything.

Can you suggest what is wrong?

0693W00000NpZe0QAF.pngIn this situation cannot read - I need to read all of them

0693W00000NpZeFQAV.pngWhen there is only one NDF - can read without any problem

  sRecordInfo_t record;
  NDEF_ReadNDEF(NDEF_Buffer);
  NDEF_IdentifyBuffer(&record, NDEF_Buffer);
  NDEF_ParseSP(&record *pRecordStruct);
 
// READ TEXT NDEF
  NDEF_Text_info_t Text;
  NDEF_ReadText( &record, &Text );
  text_read = String(Text.text);
 
// READ URL NDEF
  sURI_Info uri = {"", "", ""};
  NDEF_ReadURI(&record, &uri);
  uri_read = String(uri.protocol) + String(uri.URI_Message);
 
 // READ WIFI NDEF
  sWifiTokenInfo wps;
  NDEF_ReadWifiToken(&record, &wps);
  wifi_read_ssid = String(wps.NetworkSSID);
  wifi_read_pass = String(wps.NetworkKey);

Brian TIDAL
ST Employee

Hi,

can you share more details about your HW setup:

  • X-NUCLEO-NFC04A1 connected via I2C to a MCU or X-NUCLEO-NFC06 in card emulation mode? or custom board?
  • what is your MCU?
  • etc.

Thanks in advance

Rgds

BT

In order to give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
Rene Lenerve
ST Employee

Hi @Community member​,

If i understand well, you are using package from arduino mbed which is using older NDEF library than the one available in ST25 NFC Lib (I think that this last one is not available for arduino mbed).

I've listed some points that may fix some issues you encounter.

  • You are using NDEF_ParseSP() function which is not a public function. This function is used to parse dedicated SmartPoster NDEF file which is not what you seemed to have recorded in your tag.
  • In the capture you have first a wifi record, followed by an URI and then a Text record. In your code the order is not the same.
  • In your code you are parsing the same record 3 times. You may add an offset of your pointer after each read of the record.

Here is an example that might help you:

      sRecordInfo_t record[3];
      uint32_t recordlength;
      uint8_t *pNDEFtmp;
 
      NDEF_ReadNDEF(NDEF_Buffer);
      pNDEFtmp = NDEF_Buffer;
      NDEF_IdentifyBuffer(&record[0], pNDEFtmp);
      recordlength = NDEF_GetRecordLength( &record[0] );
      pNDEFtmp += recordlength;
      NDEF_IdentifyBuffer(&record[1], pNDEFtmp);
      recordlength = NDEF_GetRecordLength( &record[1] );
      pNDEFtmp += recordlength;
      NDEF_IdentifyBuffer(&record[2], pNDEFtmp);
      
      // READ WIFI NDEF
      sWifiTokenInfo wps;
      NDEF_ReadWifiToken(&record[0], &wps);
      wifi_read_ssid = String(wps.NetworkSSID);
      wifi_read_pass = String(wps.NetworkKey);
 
      // READ URL NDEF
      sURI_Info uri = {"", "", ""};
      NDEF_ReadURI(&record[1], &uri);
      uri_read = String(uri.protocol) + String(uri.URI_Message);
    
    // READ TEXT NDEF
      NDEF_Text_info_t Text;
      NDEF_ReadText( &record[2], &Text );
      text_read = String(Text.text);

Hope this will help you.

Kind Regards.

RMako.2
Associate II

thank you that helps a lot, but stil it do not work as I want.

All is ok when I have got shor maeeage as below

0693W00000NqPXVQA3.png 

but if I add a longger message data are not read correctly

0693W00000NqPXaQAN.pngI tried to manipulate with NDEFBuffer size and with calculation of buffer size but I stucked

Can you suggest something (Arduino has not debbug capability so it is hard to find what is wrong...)

thank you

Rene Lenerve
ST Employee

Hi @Community member​,

I'm not sure to understand well what you are trying to do. Could you give us more details about the step you are doing?

Is the message you are editing an NDEF Wifi or an NDEF Text?

And what tools do you use for that?

Could you give us the raw Tag memory contents to see what you are trying to decode?

kind regards.