Arduino récepteur réseau avec bibliothèque RF24


Voici une version de communication avec le nRF24L01 identique à celui du Raspberry. Permet de voir facilement ce qui se passe sur le réseau sans fil.


Télécharger la bibliothèque : https://github.com/stanleyseow/RF24 ou ici


Installer la bibliothèque.


Montage:


Raccordement sur Arduino UNO:


NRF24   Arduino

1 GND-> GND

2 VCC-> 3.3V

3 CE -> 6

4 CSN-> 7

5 SCK -> 13

6 MOSI -> 11

7 MISO -> 12

8 IRQ-> 2






Programme:


/*

*

*  

*

*  This program makes the RPi as a hub listening to all six pipes from the remote

*  sensor nodes ( usually Arduino  or RPi ) and will return the packet back to the

*  sensor on pipe0 so that the sender can calculate the round trip delays

*  when the payload matches.

*  

*  Refer to RF24/examples/rpi_hub_arduino/ for the corresponding Arduino sketches

* to work with this code.

*  

*  CE is connected to pin 6

*  CSN is connected to pin 7

*

*  Refer to RPi docs for GPIO numbers

*

*  Author : Stanley Seow

*  e-mail : stanleyseow@gmail.com

*  date   : 4th Apr 2013

*

*Modifier par Christophe CARON

*www.caron.ws

*26/01/2014

*/


#include <SPI.h>

#include "nRF24L01.h"

#include "RF24.h"

#include "printf.h"


#define RF_SETUP 0x17


// Radio pipe addresses for the 2 nodes to communicate.

// First pipe is for writing, 2nd, 3rd, 4th, 5th & 6th is for reading...

// Pipe0 in bytes is "serv1" for mirf compatibility

const uint64_t pipes[6] = { 0x7365727631LL, 0xF0F0F0F0E1LL, 0xF0F0F0F0E2LL, 0xF0F0F0F0E3LL, 0xF0F0F0F0E4LL, 0xF0F0F0F0E5LL };

//const uint64_t pipes[3] = { 0x7365727631LL, 0xF0F0F0F0E1LL, 0xF0F0F0F0E2LL};

// CE 6 and 7 CSN

RF24 radio(6,7);  



void setup(void)

{

        Serial.begin(57600);

printf_begin();

       // Refer to RF24.h or nRF24L01 DS for settings

       radio.begin();

       radio.enableDynamicPayloads();

       radio.setAutoAck(1);

       radio.setRetries(15,15);

       radio.setDataRate(RF24_250KBPS);

       radio.setPALevel(RF24_PA_MAX);

       radio.setChannel(120);

       radio.setCRCLength(RF24_CRC_16);


       // Open 6 pipes for readings ( 5 plus pipe0, also can be used for reading )

       radio.openWritingPipe(pipes[0]);

       radio.openReadingPipe(1,pipes[1]);

       radio.openReadingPipe(2,pipes[2]);

       radio.openReadingPipe(3,pipes[3]);

       radio.openReadingPipe(4,pipes[4]);

       radio.openReadingPipe(5,pipes[5]);


       //

       // Dump the configuration of the rf unit for debugging

       //


       // Start Listening

       radio.startListening();


       radio.printDetails();

       printf("\n\rOutput below : \n\r");

       delay(1000);

}


void loop()

{

       char receivePayload[32]="";

       uint8_t pipe = 0;

       


        while ( radio.available( &pipe ) ) {

               

               uint8_t len = radio.getDynamicPayloadSize();

               radio.read( receivePayload, len );


               // Display it on screen

               printf("Recv: size=%i payload=%s pipe=%i",len,receivePayload,pipe);


               // Send back payload to sender

               radio.stopListening();



               // if pipe is 7, do not send it back

               if ( pipe != 7 ) {

                       // Send back using the same pipe

                       // radio.openWritingPipe(pipes[pipe]);

                       radio.write(receivePayload,len);


                       receivePayload[len]=0;

                       printf("\t Send: size=%i payload=%s pipe:%i\n\r",len,receivePayload,pipe);

                 } else {

                       printf("\n\r");

               }


               // Enable start listening again

               radio.startListening();


       // Increase the pipe outside the while loop

       pipe++;

       // reset pipe to 0

       if ( pipe > 5 ) pipe = 0;

       }


       delay(20);

}



Téléchargement du programme ici.



Résultat avec le Raspberry:



Résultat avec l'Arduino:





Mise à jour 08/02/2015

Créé avec HelpNDoc Personal Edition: Création d'aide CHM, PDF, DOC et HTML d'une même source