Teensy 4.1 + nRF24L01 Serial port issue

tbursoy

New member
Hello,

I'm new in the forum. Send greetings to everyone contributing knowledge here.

I'm making a project sending data beetween two Teensy 4.1 using nRF24L01 modules.

Everything was going well until I decided to make some improvements in my code.

The project is, one Teensy collects data from GPIO expanders and sends to another Teensy via nRF24L01 modules. The receiver Teensy sends received data to PLC using native ethernet.

The strange thing is, when I open Arduino IDE serial port screen or RealTerm etc.of receiver Teensy, stops receiving data and printing to serial port about 1 or 2 seconds and restarts. It occurs again in 1 or 2 minutes and occurs again.

I'm not sure that I wrote codes about printing serial ports to right place. The sender Teensy sends 17 bytes every 500 ms to receiver Teensy. So, it is a bit fast.

I suspect that, while receiver Teensy trying to print serial port previous data, the sender Teensy sends the new data and receiver stucks. But, I'm not sure.

I will be appreciate if anyone helps me solve this issue. Thank you.

Receiver Code :

Code:
#include <SPI.h>
#include <RF24.h>
#include <nRF24L01.h>
#include <NativeEthernet.h>
#include <Metro.h>

Metro serialMetro = Metro(50);          

RF24 radio(7, 10);                      

IPAddress plcIp(192,168,0,1);          

IPAddress slaveIp(192,168,0,3);         

EthernetClient client;                

const byte address[6]="00001";                              
const int tcpStatusLedGreen=29;                             
const int rfDataLedGreen=25;                             
long cycle,previousCycle;                                   
byte slaveMac[]={ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };     
uint8_t gelenData[17],previousGelenData;                                                       
bool ledState=false;                                           
      
void setup() {
  
  Serial.begin(115200);                             
  
  radio.begin();                         
  radio.openReadingPipe(1, address);
  radio.setPALevel(RF24_PA_MIN);
  radio.setDataRate(RF24_1MBPS);
  radio.startListening();               

  pinMode(tcpStatusLedGreen,OUTPUT);     
  pinMode(rfDataLedGreen,OUTPUT);
   
  digitalWrite(tcpStatusLedGreen,LOW);   
  digitalWrite(rfDataLedGreen,LOW);
    
  Ethernet.begin(slaveMac, slaveIp);     
  delay(2000);                           
  
}

void loop() 
{

  if (radio.available())                
  {
        
    if(gelenData[0] == 1)                          
    {

      previousCycle = millis();   
    
    }
      
    radio.read(&gelenData, sizeof(gelenData));     

    if(Serial)
    {
      for(int j=1 ; j<17 ; j++)
      {                                        
        for(int k=0 ; k<8 ; k++)                   
         {
             Serial.print(bitRead(gelenData[j],k));                   
         }     
      } 

      Serial.print("-Output_No_");                   
      Serial.println(gelenData[0]);
     }
    
     sendDataTcp();

     if(gelenData[0] == 128)
     {
       cycle = millis() - previousCycle;            

        if(Serial)
        {
          Serial.print("Cycle_Time_");
          Serial.print(cycle);
          Serial.println("_ms");
        }    
     }  
   }

   if (serialMetro.check() == 1)                 
   { 

      tcpLinkCheckRestart();                     

      if(gelenData[0] != previousGelenData)       
      {

        if(ledState == false)
        {
          ledState = true;
        }
        else 
        {
          ledState = false;
        }
        
        digitalWrite(rfDataLedGreen,ledState); 
          
      }
      
      if(gelenData[0] == previousGelenData)      
      {
        
        digitalWrite(rfDataLedGreen,LOW); 
              
      }
      
      previousGelenData = gelenData[0];
      
   }

}

void sendDataTcp()                        
{
  if(!client.connected())                 
  {
    
    digitalWrite(tcpStatusLedGreen,LOW);
    
    client.stop();                       
    delay(2000);                          
    client.connect(plcIp, 2000);          
    delay(2000);
    
  }
  
  else                                    
  { 
    
    digitalWrite(tcpStatusLedGreen,HIGH);
    
    client.write(gelenData,17);
               
  }    
}

void tcpLinkCheckRestart()                      
{

    if (Ethernet.linkStatus() == LinkOFF) 
    {

      if(Serial)
      {
          Serial.println("Ethernet kablosu bagli degil");
      }
           
      digitalWrite(tcpStatusLedGreen,LOW);
      delay(2000);   
      Ethernet.begin(slaveMac, slaveIp);      
      delay(2000);   
       
    }

    if (Ethernet.linkStatus() == LinkON) 
    {

      digitalWrite(tcpStatusLedGreen,HIGH);
       
    }  
}
 
not sure why the strange behavior, but a few things jump out at me

1) is gelenData an array or data struct? if so you sure about the .read? I use serial devices and send structs and it's thing.readBytes((uint8_t*) &Data, sizeof(Data));

2) odd how if(gelenData[0] == 1) but data is read a few lines later. Not sure this matters, but testing a value before radio.read seems wrong

3) probably not an issue but long to measure millis() should be unsigned long (to give larger duration before rollover)

I'm probably not much help, but when I get into tough bugs I start commenting out code until the culprit is found.
 
Back
Top