"NFC read" stops the program (PN532 and NDEF libraries)

Hello,

I have a problem with PN532.h and NDEF.h libraries. I want to write a program that will test the NFC P2P connection with the phone (If there is a NFC connection then do something, if there is no connection then program will go further) I cant find any function to test connection. In examples there is:

int msgSize = nfc.read(ndefBuf, sizeof(ndefBuf));

if (msgSize > 0) {

...

} else {

...

}

But this makes my program to wait here and do nothing until there will be any connection.

Final program should do a lot of calucations about pressure etc but if there will be NFC message from Smartphone then some parameters should change.

I would be very grateful for your help.

I have a problem with PN532.h and NDEF.h libraries.

If I had the problem, I would have shared a link to the libraries.

Ohh sorry, I thought that these libraries are widely known in NFC using.

There are links:

There are links:

No. Those are URLs. Links can actually be clicked on. And, they are trivial to properly insert in posts.

For homework, hover over ALL of the icons above the text entry field, and learn what each of them do.

And, post ALL of your code. The Adafruit-PN532 library has a method, isReady() that seems to be what you are looking for, but that method is private. I'd make it public, and test to see whether it does what you want. If I had the hardware...

My code is HUGE and there is no reason to post all code. I just want to have:

IF THERE IS NFC CONNECTION
{
DO A
}

ELSE
{
DO B
}

Sorry for "links".

I will try to make this method useful. Thanks a lot for a tip.

Sorry i posted wrong PN532 library. Correct is here:

I still dont know how to solve my problem. The testing code for reading NFC is here:

#include "SPI.h"
#include "PN532_SPI.h"
#include "snep.h"
#include "NdefMessage.h"

#include "Wire.h"
#include "LiquidCrystal.h"

PN532_SPI pn532spi(SPI, 10);
SNEP nfc(pn532spi);
uint8_t ndefBuf[128];

// Connect via i2c, default address #0 (A0-A2 not jumpered)
LiquidCrystal lcd(8, 9, 4, 5, 6, 7); // Define LCD with selected pins

void setup() {
    Serial.begin(9600);
    // set up the LCD's number of rows and columns: 
    lcd.begin(16, 2);
}

void loop() {
    Serial.println("Before read");
    
    //the program stops here and waiting for NFC comunication!
    int msgSize = nfc.read(ndefBuf, sizeof(ndefBuf));     
    
    Serial.println("after read");

    if (msgSize > 0) {
        NdefMessage msg  = NdefMessage(ndefBuf, msgSize);
      //  msg.print();
            
        NdefRecord record = msg.getRecord(0);

        int payloadLength = record.getPayloadLength();
        byte payload[payloadLength];
        record.getPayload(payload);        
        
        // The TNF and Type are used to determine how your application processes the payload
        // There's no generic processing for the payload, it's returned as a byte[]
        int startChar = 0;        
        if (record.getTnf() == TNF_WELL_KNOWN && record.getType() == "T") { // text message
          // skip the language code
          startChar = payload[0] + 1;
        } else if (record.getTnf() == TNF_WELL_KNOWN && record.getType() == "U") { // URI
          // skip the url prefix (future versions should decode)
          startChar = 1;
        }
        // Force the data into a String
        String payloadAsString = "";
        
        for (int c = startChar; c < payloadLength; c++) {
          payloadAsString += (char)payload[c];
        }
        int payloadAsInt = payloadAsString.toInt(); 
        double payloadAsDouble = (double)payloadAsInt;   
        // print on the LCD display
       lcd.setCursor(0, 0);
        lcd.print(payloadAsString);
        Serial.print("WIADOMOSC: ");
        Serial.println(payloadAsString);
        Serial.print("LICZBA: ");
        Serial.println(payloadAsInt);
        Serial.println("\n");
    } else {
        Serial.println("Failed"); 
    // delay(1000);
    }
}

Anyone can help? The method "isReady" is not returning "TRUE" when comunication is "ON" but only when the NFC module is corectly connected.

If you look at the read() Source header its states:

    /**
    * @brief    read a SNEP packet, the packet will be less than (255 - 2 - 3) bytes
    * @param    buf     the buffer to contain the packet
    * @param    len     lenght of the buffer
    * @param    timeout max time to wait, 0 means no timeout
    * @return   >=0     length of the packet 
    *           <0      failed
    */
    int16_t read(uint8_t *buf, uint8_t len, uint16_t timeout = 0);

Notice the 3rd parameter with default value 0 and the comment:
==> timeout max time to wait, 0 means no timeout

If I were you i would play with That

Thanks a lot!
It is what i was looking for! This is time in ms after which program goes on to the next line.

:slight_smile: