Skip to content

Instantly share code, notes, and snippets.

@shirish47
Last active March 30, 2016 06:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shirish47/1c082b26217708ba3f8ee7f8e7b45f5f to your computer and use it in GitHub Desktop.
Save shirish47/1c082b26217708ba3f8ee7f8e7b45f5f to your computer and use it in GitHub Desktop.
available returns true even after reading.
#include <nRF24L01.h>
//#include <printf.h>
#include <RF24.h>
#include <RF24_config.h>
// This is test code I am trying out using library
#include <nRF24L01.h>
//#include <printf.h>
#include <RF24.h>
#include <RF24_config.h>
#include <SPI.h>
#include "Packet.h" //My own data structures.
RF24 nrf24(A2,A0);
uint8_t addresses[][6]={"Node1","Node2"};
#define TX 0//has been set as tx
Advertise* adv;
Advertise a;
void setup() {
Serial.begin(4800);
Serial.println("Hello");
if(!nrf24.begin())
{
Serial.println("failed to intialise");
}
nrf24.setCRCLength(RF24_CRC_8);
nrf24.setDataRate(RF24_250KBPS);
nrf24.setChannel(10);
nrf24.setPALevel(RF24_PA_MAX);
#if (TX == 1)
a.dev_Id=143;
a.token_Id=7676;
a.adv_type= PINGTEST;
adv=&a;
nrf24.openWritingPipe(addresses[1]);
nrf24.setPayloadSize(sizeof(Advertise));
#else //reveiver pipe setup
nrf24.openReadingPipe(1,addresses[1]);
nrf24.setPayloadSize( sizeof(Advertise));
#endif
nrf24.setAutoAck(false);
Serial.println("Setup done");
}
void loop() {
#if (TX == 1) //transmitter part
Serial.println("sending Adv..");
nrf24.write( (uint8_t*)adv, sizeof(Advertise));
// nrf24.send((uint8_t*)adv,sizeof(Advertise));
// Serial.println("read failed");
Serial.print("Dev ID: ");
Serial.println(adv->dev_Id);
Serial.print("Token ID: ");
Serial.println(adv->token_Id);
Serial.print("Adv type: ");
Serial.println(adv->adv_type);
#else //receiver part
nrf24.startListening();
Serial.println("waiting..");
Advertise a;
uint8_t len= sizeof(a);
// while(!nrf24.available())
// {
// nrf24.read( &a, len );
// }
unsigned long started_waiting_at =millis();
bool timeout=false;
while ( !nrf24.available() ){ // While nothing is received
if ((millis() - started_waiting_at) > 5000 ){ // If waited longer than 200ms, indicate timeout and exit while loop
timeout = true;
break;
}else
timeout=false;
}
if ( timeout ){ // Describe the results
Serial.println(F("Failed, response timed out."));
}else{
Advertise a;
uint8_t len= sizeof(a);
while ( nrf24.available() ){
Serial.println("woow some data .. **************************");
nrf24.read( &a, len );
Serial.print("Dev ID: ");
Serial.println(a.dev_Id);
Serial.print("Token ID: ");
Serial.println(a.token_Id);
Serial.print("Adv type: ");
Serial.println(a.adv_type);
a.dev_Id=0;
a.token_Id=0;
a.adv_type= 0;
Serial.println("___________has the buffered erased?____________");
Serial.print("Dev ID: ");
Serial.println(a.dev_Id);
Serial.print("Token ID: ");
Serial.println(a.token_Id);
Serial.print("Adv type: ");
Serial.println(a.adv_type);
Serial.println("___________are all these zeroed?____________");
}
}
Serial.print("nrf available test: "); Serial.println(nrf24.available());
// Serial.print("timeout :");
// Serial.println(timeout);
//nrf24.stopListening();
#endif
delay(2000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment