I have a small code to read a RFID tag and send the tag data alone to another unit via nRF24L01 module. The setup works as expected but with a problem ... the radio.write(const void *buf, uint8_t len) function never returns TRUE even though it does send the RFID tag info to the receiver. COnsequently i am not able to reset the RFID tag information - if this Bool returns true, then i read the AckPayload and using the information in it can reset the RFID tag. Also I use this to light up a RF_OK led
I notice that the function returns TRUE if i remove the MFRC522 from circuit and the RF-OK led remains lit as long as the communication is alive.
( I am making sure that the RFID functionality is blocked when RF24 comm is active. But that does not help )
So what else could be the issue ??
#include <SPI.h>
#include <RF24.h>
#include <nRF24L01.h>
#include <MFRC522.h>
/****************** User Config ***************************/
/* Hardware configuration: Set up nRF24L01 radio on SPI bus plus pins 7,8 */
RF24 radio(7, 8);
/**********************************************************/
/* RFID interface configuration apart from SPI bus */
#define RfidRst 9 // RFID reset pin
#define RfidSel 10 // RFID select pin
/*********************************************************/
byte rf_Address[6] = {"3_PTX"} ; // Radio pipe refernce of this RFIDReader,
MFRC522 mfrc522(RfidSel, RfidRst); // Instantiate the reader MFRC522
byte RFID_Data[4]; // Data to Rx unit
bool resetRFID; // Reset data frrom Rx
unsigned long prevMillisA;
unsigned long intervalA = 500;
unsigned long rfStatusTimeOut;
byte rfidTxCount ;
bool blockMfrc522;
byte RF_OK_Out = 3;
byte RFID_OK_Out = 4;
void setup() {
Serial.begin(9600);
Serial.println(F("*** STARTING TRANSMITTER *** "));
SPI.begin();
mfrc522.PCD_Init(); // Initialize the MFRC522 reader
// Setup and configure radio
radio.begin();
radio.setChannel(108); // Above most Wifi Channels
radio.setDataRate( RF24_250KBPS );
radio.setRetries(5, 10); // Delay, count
radio.enableAckPayload(); // Allow optional ack payloads
radio.enableDynamicPayloads(); // Ack payloads are dynamic payload
pinMode(RF_OK_Out, OUTPUT);
pinMode(RFID_OK_Out, OUTPUT);
}
void loop(void)
{
if ( !blockMfrc522) getRFID(); // Read the RFID tag... when not transmitting
if (RFID_Data[0] == 0 && RFID_Data[1] == 0 && RFID_Data[2] == 0 && RFID_Data[3] == 0 )
{
digitalWrite(RFID_OK_Out, LOW); // Handle the RFID led..
}
else
{
digitalWrite(RFID_OK_Out, HIGH);
}
//******************************************************
if ( millis() - prevMillisA > intervalA )
{
prevMillisA = millis();
blockMfrc522 = true;
send_rfid(); // Transmitt the last read RFID
blockMfrc522 = false;
}
//******************************************************
if ( millis() - rfStatusTimeOut > 2000) // Update RF24 comm status
{
digitalWrite( RF_OK_Out, LOW);
}
else
{
digitalWrite( RF_OK_Out, HIGH);
}
}
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
void send_rfid()
{
radio.openWritingPipe(rf_Address); // Open the writing pipe with own reference
bool rfTxStatus = (radio.write(&RFID_Data, sizeof(RFID_Data))); // Send the RFID info
if (rfTxStatus) // PROBLEM : THIS BOOL NEVER RETRUNS TRUE
{
if ( radio.isAckPayloadAvailable() ) // Sent. Now read the Ack Pay load from Rx module..
{
radio.read(&resetRFID, sizeof(resetRFID)); // If ackPayLoad is True , reset RFID data
}
if (resetRFID)
{
RFID_Data[0] = 0;
RFID_Data[1] = 0;
RFID_Data[2] = 0;
RFID_Data[3] = 0;
}
rfStatusTimeOut = millis(); // Hold the RF_OK LED
}
}
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
// FUNCTION TO READ RFID TAG AND CONVERT IT TO A 8 BYTE DATA
int getRFID()
{
// Look for new cards
if ( ! mfrc522.PICC_IsNewCardPresent()) {
return;
}
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial()) {
return;
}
for (int i = 0; i < mfrc522.uid.size; i++) {
RFID_Data[i] = mfrc522.uid.uidByte[i];
}
char scannedRFID[10];
sprintf ( scannedRFID, "%02X%02X%02X%02X", RFID_Data[0], RFID_Data[1], RFID_Data[2], RFID_Data[3]);
Serial.println(scannedRFID);
mfrc522.PICC_HaltA();
return 1;
}