Skip to main content
Visitor II
April 26, 2022
Solved

arduino: read NDEF Wi-Fi connection

  • April 26, 2022
  • 12 replies
  • 4410 views

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!

    This topic has been closed for replies.
    Best answer by Rene Lenerve

    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.

    12 replies

    Technical Moderator
    April 27, 2022

    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

    Visitor II
    April 27, 2022

    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

    Technical Moderator
    April 28, 2022

    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

    ST Employee
    April 28, 2022

    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.2Author
    Visitor II
    May 8, 2022

    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);

    Technical Moderator
    May 10, 2022

    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

    ST Employee
    May 12, 2022

    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.2Author
    Visitor II
    May 21, 2022

    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

    ST Employee
    May 23, 2022

    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.

    RMako.2Author
    Visitor II
    May 23, 2022

    Hi

    What I am trying to do is just to read from ST25DV04K Nucleo board 3 NDEF message - WiFi, URI and TXT, using ST library for arduino.

    I am using code from your previous post.

    All is ok if NDEF message are short, but if I will try to read longer message I have a trouble with decode.

    MCU is STM32L4, I2C communication, what you can see bove is just printf to Termite console

    To modify NDEF message I am using ST android application ST25 from google store

    0693W00000NqU3BQAV.jpg

    ST Employee
    May 24, 2022

    Hi @Community member​,

    Ok, the error is due to a buffer too small in the code.

    In the libNDEF, there is a define that set the buffer size of text record in the file lib_NDEF_Text.h.

    This size is set to :

    #define NDEF_TEXT_MAX_LENGTH 40

    So, it allows text record of 40 bytes max. If you need to work with bigger Text record, you can increase this value to your needs.

    Hope this can help you.

    Kind Regards.