Skip to content

Instantly share code, notes, and snippets.

@hachpai
Last active August 29, 2015 14:12
Show Gist options
  • Save hachpai/7f5ccdddbae9c979fb0e to your computer and use it in GitHub Desktop.
Save hachpai/7f5ccdddbae9c979fb0e to your computer and use it in GitHub Desktop.
RF24: establishing a connection protocol.
/*
Copyright (C) 2011 J. Coliz <maniacbug@ymail.com>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
version 2 as published by the Free Software Foundation.
*/
#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
#include "printf.h"
//
// Hardware configuration
//
// Set up nRF24L01 radio on SPI bus plus pins 9 & 10
RF24 radio(9,10);
// sets the role of this unit in hardware. Connect to GND to be the 'pong' receiver
// Leave open to be the 'ping' transmitter
const int role_pin = 5;
const uint32_t default_borne_id = 0xF0F0F0F0E1LL;
volatile boolean confirmed = false;
volatile bool id_sended = false;
uint32_t remote_bike_id = 0x0; //will be updated in IRQ
const uint32_t local_bike_id= 0xE8E8F0F0E1LL; // bike id, will be send to borne to init
typedef enum { role_borne = 1, role_velo } role_e;
// The debug-friendly names of those roles
const char* role_friendly_name[] = { "invalid", "Borne", "Velo"};
// The role of the current running sketch
role_e role;
void setup(void)
{
//
// Role
//
// set up the role pin
pinMode(role_pin, INPUT);
digitalWrite(role_pin,HIGH);
delay(20); // Just to get a solid reading on the role pin
// read the address pin, establish our role
if ( ! digitalRead(role_pin) )
role = role_borne;
else
role = role_velo;
//
// Print preamble
//
Serial.begin(9600);
printf_begin();
printf("\n\rRF24/examples/pingpair/\n\r");
printf("ROLE: %s\n\r",role_friendly_name[role]);
//
// Setup and configure rf radio
//
radio.begin();
radio.setAutoAck(true);
radio.setPALevel( RF24_PA_MAX ) ;
// Min speed (for better range I presume)
radio.setDataRate( RF24_250KBPS ) ;
// 8 bits CRC
radio.setCRCLength( RF24_CRC_8 ) ;
// optionally, increase the delay between retries & # of retries
radio.setRetries(15,15);
radio.enableAckPayload();
// optionally, reduce the payload size. seems to
// improve reliability
radio.setPayloadSize(8);
//radio.maskIRQ(0,0,0);
//radio.enableAckPayload();
//
// Open pipes to other nodes for communication
//
// This simple sketch opens two pipes for these two nodes to communicate
// back and forth.
// Open 'our' pipe for writing
// Open the 'other' pipe for reading, in position #1 (we can have up to 5 pipes open for reading)
if ( role == role_borne )
{
radio.openReadingPipe(1,default_borne_id);
printf("\n startListening \n");
radio.startListening();
}
else
{
radio.openWritingPipe(default_borne_id);
radio.openReadingPipe(1,local_bike_id);
}
//
// Dump the configuration of the rf unit for debugging
//
radio.printDetails();
delay(50);
attachInterrupt(0, check_radio, FALLING);
}
bool confirmation_sended = false;
void loop(void)
{
//Borne (terminal): wait for a packet with an ID, confirm the authentification with the client by sending back the received ID.
if (role == role_borne)
{
if(remote_bike_id == 0x0){
printf("Now listening...");
delay(1000);
}
else if(!confirmation_sended){
//radio.stopListening();
delay(100);
radio.openWritingPipe(remote_bike_id);
printf("Sending back %lu...",remote_bike_id);
radio.startWrite(&remote_bike_id,sizeof(uint32_t),false);
confirmation_sended=true;
}
}
//
// client: send a packet with own id to try to establish a connection with terminal
//
if ( role == role_velo )
{
radio.startWrite(&local_bike_id,sizeof(uint32_t),false); // Writes 1 payload to the buffers
delay(500);
printf("ID:%lu sended\n\r",local_bike_id);
if(id_sended)
{
printf("id sended to borne: %lu \n\r",local_bike_id);
radio.startListening();
while(!confirmed){
printf("wait confirmation...");
delay(500);
}
printf("confirmed");
}
else{
//printf("send failed");
}
}
}
void check_radio(void)
{
// What happened?
bool tx,fail,rx;
radio.whatHappened(tx,fail,rx);
if(tx){
id_sended = true;
}
if(fail){
id_sended = false;
}
if ( rx || radio.available() )
{
if ( role == role_borne )
{
if(remote_bike_id==0x0){
radio.read(&remote_bike_id,sizeof(uint32_t));
printf("id bike received: %lu\n",remote_bike_id);
}
else
{
printf("To implement...");
}
}
else{
if(!confirmed){
uint32_t confirmation_code=0x0;
radio.read(&confirmation_code,sizeof(uint32_t));
printf("code received:%lu \n",confirmation_code);
confirmed = (confirmation_code == local_bike_id);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment