Test funzionamento Nfr24L01

Ciao a tutti, sono al mio primo post e al mio primo problema che non so come risolvere. ho acquistato diversi moduli nfr24 (il modello con la pista che fa da antenna non spiralata) e sto provando di farli comunicare tra loro utilizzando la libreria Mirf, con gli esempi base il ping sembra funzionare ma modificando il codice i dati ricevuti non sono corretti, quindi penso di aver scritto o del codice sbagliato, o ho dei problemi con gli nfr... Il codice è il seguente, cosa sto sbagliando?

PS: ho provato ad utilizzare anche gli esempi di test della libreria RF24 ma non mi sembra funzionare nemmeno con quelli...

Sender:

#include <SPI.h>
#include <Mirf.h>
#include <nRF24L01.h>
#include <MirfHardwareSpiDriver.h>

unsigned long counter = 0;
unsigned long time = 0;
unsigned long data = 0;
unsigned long blinking_led = 0;

void setup(){
  Serial.begin(9600);
  Mirf.spi = &MirfHardwareSpi;
  Mirf.init();
  Mirf.setRADDR((byte *)"clie1");
  Mirf.setTADDR((byte *)"serv1");
  Mirf.channel = 2;
  Mirf.payload = sizeof(unsigned long);
  Mirf.config();
   Serial.println("Beginning ... "); 
}

void loop(){
  time = millis();
  Serial.print("start sending at ");
  Serial.print(time);
  Serial.println(" ms");
  Mirf.send((byte *)&time); 
  while(Mirf.isSending())
  { 
    if((millis() - blinking_led) > 100)
    {
      Serial.print(".");
      blinking_led = millis();
   }
  }
  Serial.println("Finished sending");

  delay(10);
  while(!Mirf.dataReady()){
    Serial.println("Waiting");
    if ( ( millis() - time ) > 60000 ) {
      Serial.println("Timeout on response from server!");
      return;
    }
  }

  Mirf.getData((byte *) &data);
  
  Serial.print("Sent : ");
  Serial.print(counter);  
  Serial.print(" , received : ");
  Serial.println(data);
  Serial.print("Time Send-Receive : ");
  Serial.print( millis() - time );  
  Serial.println(" ms");
  counter = counter + 1;
  
  delay(1000);
}

Receiver:

#include <SPI.h>
#include <Mirf.h>
#include <nRF24L01.h>
#include <MirfHardwareSpiDriver.h>

unsigned long data = 0;
unsigned long blinking_led = 0;

void setup(){
  Serial.begin(9600);

  Mirf.spi = &MirfHardwareSpi;
  Mirf.init();
  Mirf.setRADDR((byte *)"serv1");
  Mirf.setTADDR((byte *)"clie1");
  Mirf.channel = 2;
  Mirf.payload = sizeof(unsigned long);
  Mirf.config();
  
  Serial.println("Listening..."); 
}

void loop(){
  
   while(!Mirf.dataReady())
   {
     if((millis() - blinking_led) > 100)
     { 
        Serial.print(".");
        blinking_led = millis();
     }
   }

   Mirf.getData((byte *) &data);   
   Serial.print("Got packet : ");
   Serial.println(data);
   Mirf.send((byte *)&data);
   Serial.println("Reply sent.");   
}

E questi sono i risultati stampati sulla seriale:

Beginning ...
start sending at 0 ms
Finished sending
Sent : 0 , received : 0
Time Send-Receive : 86 ms
start sending at 1086 ms
.Finished sending
Sent : 1 , received : 0
Time Send-Receive : 26 ms
start sending at 2119 ms
.Finished sending
Sent : 2 , received : 0
Time Send-Receive : 26 ms
start sending at 3152 ms
.Finished sending
Sent : 3 , received : 0
Time Send-Receive : 26 ms
start sending at 4186 ms
.Finished sending

Listening...
Got packet : 0
Reply sent.
Got packet : 0
Reply sent.
Got packet : 4294967295
Reply sent.
Got packet : 4294967295

Matteo benvenuto. :slight_smile:
Ti invitiamo a presentarti qui: Re: Presentazioni nuovi iscritti, fatevi conoscere da tutti! (Part 1) - Generale - Arduino Forum
e a leggere il regolamento: [REGOLAMENTO] Come usare questa sezione del forum - Italiano - Arduino Forum

Io ho patito molto solo per riuscire a fare il ping-pong della libreria rf24.
Dopo aver letto qui , http://arduino-info.wikispaces.com/Nrf24L01-2.4GHz-HowTo , ho risolto problemi di invio mettendo un condensatore come indicato. Senza a volte andava, a volte no.
Purtroppo da un po' di mesi il tempo è tiranno e ho pochissimo tempo per andare avanti con la conoscenza degli nrf24l01+ .
Spero aiuti.

prova con questo

TX

#include <SPI.h>
#include <Mirf.h>
#include <nRF24L01.h>
#include <MirfHardwareSpiDriver.h>

char data[5] = { '0', 'x', 'c', '7'};
char rec[4];

const int switchPin = 22;     // pushbutton pin
const int ledPin =  23;      //  LED pin

void setup(){
  Serial.begin(57600);
  Mirf.spi = &MirfHardwareSpi;
  Mirf.init();
  Mirf.setRADDR((byte *)"clie1");
  Mirf.configRegister( FEATURE, 1<<EN_DPL );         //disable dynamic payload
  Mirf.payload = 4;
  Mirf.config();
  Serial.println("initialized");
}

void loop(){
  if (digitalRead(switchPin) == HIGH) { 
    digitalWrite(ledPin, HIGH); 
    sendAlarm(); 
    delay(500);
  } 
  else {
    digitalWrite(ledPin, LOW); 
//    Mirf.powerDown();
  }

  //  delay(3000);
} 


void sendAlarm() {
//  Mirf.configRegister(CONFIG, 1<<1 );          //power up module
resend:
  Serial.print(F("sending: "));

  for (int i = 0; i < 4; i++) {
    Serial.print(data[i]);
  }
  Serial.println();

  Mirf.setTADDR((byte *)"serv1");
  Mirf.send((byte *) &data);

  while(Mirf.isSending()){
    //    Serial.print("sending...");
  }
  Serial.println();
  //  delay(10);
  while(!Mirf.dataReady()){
    //    Serial.print("waiting for reply...");
    delay(100);
    break;
  }

  Mirf.getData((byte *) &rec);

  Serial.print(F("reply: "));
  for (int i = 0; i < 4; i++) {
    Serial.print(rec[i]);
    if(data[i] != rec[i]  ) {
      goto resend;
    }
  }

//  Mirf.flushTx(); 
//  Mirf.flushRx(); 
//  Mirf.powerDown();
  Serial.println();
  Serial.println(F("---------------------"));
}

RX

#include <SPI.h>
#include <Mirf.h>
#include <nRF24L01.h>
#include <MirfHardwareSpiDriver.h>

char data[4];

void setup(){
  Serial.begin(9600);
  //  pinMode(4, OUTPUT);

  Mirf.spi = &MirfHardwareSpi;
  Mirf.init();

  Mirf.setRADDR((byte *)"serv1");
  Mirf.payload = 4;

  Mirf.config();

  Serial.println("Listening..."); 

  //Standby-I mode
}

void loop(){
  
  if(!Mirf.isSending() && Mirf.dataReady()){
    Serial.print("Got packet: ");
    Mirf.getData((byte *) &data);        //Get load the packet into the buffer.

    for (int i = 0; i < 4; i++) {
      Serial.print(data[i]);
    }
    Serial.println();
    Mirf.setTADDR((byte *)"clie1");          //Set the send address.
    Mirf.send((byte *) &data);        //Send the data back to the client.

    while(Mirf.isSending()){
      //    Serial.print("sending...");
    }

    Serial.println("Reply sent.");
    Mirf.flushTx(); 
    Mirf.flushRx(); 
  }
}

il codice è un pò da rivedere, nel TX c'è un goto che è meglio eliminare, ma nel complesso dovrebbe funzionare.