Outils pour utilisateurs

Outils du site


wiki:tutoriels:ateliers:wireless

Ceci est une ancienne révision du document !


Atelier : Communication sans fils

Séance 2 : RfDuino

Séance 1 : NRF24L01

Câblage :

  • NRF24L01 → Arduino
  • 1 GND → GND
  • 2 VCC → 3.3V
  • 3 CE → D8
  • 4 CSN → D10
  • 5 SCK → D13
  • 6 MOSI → D11
  • 7 MISO → D12
  • 8 IRQ → N/A
Exercice 1

Host :

//////////////////////////////////////////////////
// Atelier : Communication sans fils - Séance 1 //
//////////////////////////////////////////////////
 
// Importation de la librarie :
#include <SPI.h>
#include "RH_NRF24.h"
 
// Création d'une instance représentant le module :
RH_NRF24 nrf24;
 
void setup() {
  // Ouverture du port série
  Serial.begin(9600);
  while (!Serial);
 
  // Initialisation du module
  if (!nrf24.init()) {
    Serial.println("Impossible d'initialiser le module");
    while (true);
  }
  // Paramétrage du module :
  if (!nrf24.setRF(RH_NRF24::DataRate250kbps, RH_NRF24::TransmitPowerm18dBm)) {
    Serial.println("Impossible de parametrer le module");
    while (true);
  }
 
  Serial.println("NRF24L01 Ok");
 
  nrf24.setChannel(42);
  nrf24.setModeTx();
}
 
void loop() {
  uint8_t data[] = "Hello World!";
  nrf24.send(data, sizeof(data));
  nrf24.waitPacketSent();
 
  delay(1000);
}

Device :

//////////////////////////////////////////////////
// Atelier : Communication sans fils - Séance 1 //
//////////////////////////////////////////////////
 
// Importation de la librarie :
#include <SPI.h>
#include "RH_NRF24.h"
 
// Création d'une instance représentant le module :
RH_NRF24 nrf24;
 
void setup() {
  // Ouverture du port série
  Serial.begin(9600);
  while (!Serial);
 
  // Initialisation du module
  if (!nrf24.init()) {
    Serial.println("Impossible d'initialiser le module");
    while (true);
  }
  // Paramétrage du module :
  if (!nrf24.setRF(RH_NRF24::DataRate250kbps, RH_NRF24::TransmitPowerm18dBm)) {
    Serial.println("Impossible de parametrer le module");
    while (true);
  }
 
  Serial.println("NRF24L01 Ok");
 
  nrf24.setChannel(42);
  nrf24.setModeRx();
}
 
void loop() {
  if (nrf24.available()) {
    uint8_t buf[RH_NRF24_MAX_MESSAGE_LEN];
    uint8_t len = sizeof(buf);
    if (nrf24.recv(buf, &len)) {
      Serial.println((char*)buf);
    } else {
      Serial.println("Recv failed.");
    }
  }
  delay(100);
}
Exercice 2 : Joystick

Host:

//////////////////////////////////////////////////
// Atelier : Communication sans fils - Séance 1 //
//////////////////////////////////////////////////
 
// Exemple : Une télécommande simple avec un joystick
 
// Importation de la librarie :
#include <SPI.h>
#include "RH_NRF24.h"
 
// Création d'une instance représentant le module :
RH_NRF24 nrf24;
 
struct JoystickData {
  float x;
  float y;
} data;
 
void setup() {
  // Ouverture du port série
  Serial.begin(9600);
  while (!Serial);
 
  // Initialisation du module
  if (!nrf24.init()) {
    Serial.println("Impossible d'initialiser le module");
    while (true);
  }
  // Paramétrage du module :
  if (!nrf24.setRF(RH_NRF24::DataRate250kbps, RH_NRF24::TransmitPowerm18dBm)) {
    Serial.println("Impossible de parametrer le module");
    while (true);
  }
 
  Serial.println("NRF24L01 Ok");
 
  nrf24.setChannel(42);
  nrf24.setModeTx();
}
 
void loop() {
  data.x = analogRead(0);
  data.y = analogRead(1);
  nrf24.send((const uint8_t*)&data, sizeof(data));
  delay(100);
}

Device:

//////////////////////////////////////////////////
// Atelier : Communication sans fils - Séance 1 //
//////////////////////////////////////////////////
 
// Importation de la librarie :
#include <SPI.h>
#include "RH_NRF24.h"
 
// Création d'une instance représentant le module :
RH_NRF24 nrf24;
 
struct JoystickData {
  float x;
  float y;
} data;
 
void setup() {
  // Ouverture du port série
  Serial.begin(9600);
  while (!Serial);
 
  // Initialisation du module
  if (!nrf24.init()) {
    Serial.println("Impossible d'initialiser le module");
    while (true);
  }
  // Paramétrage du module :
  if (!nrf24.setRF(RH_NRF24::DataRate250kbps, RH_NRF24::TransmitPowerm18dBm)) {
    Serial.println("Impossible de parametrer le module");
    while (true);
  }
 
  Serial.println("NRF24L01 Ok");
 
  nrf24.setChannel(42);
  nrf24.setModeRx();
}
 
void loop() {
  if (nrf24.available()>=sizeof(data)) {
    uint8_t len = sizeof(data);
    nrf24.recv((uint8_t*)&data, &len);
    Serial.print("X=");
    Serial.println(data.x);
    Serial.print("Y=");
    Serial.println(data.y);
    Serial.println();
  }
  delay(100);
}

Exemple de projet réalisé avec un NRF24L01 : Analyseur de spectre du pauvre

wiki/tutoriels/ateliers/wireless.1423055782.txt.gz · Dernière modification: 2016/09/11 11:04 (modification externe)