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