не работает nRF24L01

dimasinnet
Offline
Зарегистрирован: 18.06.2013

Всем привет, подключил один nRF24L01 к меге, и один к уно... запустил пример GettingStarted, но они чей-то общаться не хотят, пишут в мониторе порта :

Now sending 705605...failed.
Failed, response timed out.
 Помогите разобраться чайнику...
Мегу подключал вот так:
40(CE), 51 (MOSI), 50(MISO), 52(SCK), 53(CSN)
 
ЕЕ скетч:
001#include <SPI.h>
002#include "nRF24L01.h"
003#include "RF24.h"
004#include "printf.h"
005 
006//
007// Hardware configuration
008//
009 
010// Set up nRF24L01 radio on SPI bus plus pins 40 & 53
011 
012RF24 radio(40,53);
013 
014//
015// Topology
016//
017 
018// Radio pipe addresses for the 2 nodes to communicate.
019const uint64_t pipes[2] = { 0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL };
020 
021//
022// Role management
023//
024// Set up role.  This sketch uses the same software for all the nodes
025// in this system.  Doing so greatly simplifies testing. 
026//
027 
028// The various roles supported by this sketch
029typedef enum { role_ping_out = 1, role_pong_back } role_e;
030 
031// The debug-friendly names of those roles
032const char* role_friendly_name[] = { "invalid", "Ping out", "Pong back"};
033 
034// The role of the current running sketch
035role_e role = role_pong_back;
036 
037void setup(void)
038{
039  //
040  // Print preamble
041  //
042 
043  Serial.begin(57600);
044  printf_begin();
045  printf("\n\rRF24/examples/GettingStarted/\n\r");
046  printf("ROLE: %s\n\r",role_friendly_name[role]);
047  printf("*** PRESS 'T' to begin transmitting to the other node\n\r");
048 
049  //
050  // Setup and configure rf radio
051  //
052 
053  radio.begin();
054 
055  // optionally, increase the delay between retries & # of retries
056  radio.setRetries(15,15);
057 
058  // optionally, reduce the payload size.  seems to
059  // improve reliability
060  //radio.setPayloadSize(8);
061 
062  //
063  // Open pipes to other nodes for communication
064  //
065 
066  // This simple sketch opens two pipes for these two nodes to communicate
067  // back and forth.
068  // Open 'our' pipe for writing
069  // Open the 'other' pipe for reading, in position #1 (we can have up to 5 pipes open for reading)
070 
071  //if ( role == role_ping_out )
072  {
073    //radio.openWritingPipe(pipes[0]);
074    radio.openReadingPipe(1,pipes[1]);
075  }
076  //else
077  {
078    //radio.openWritingPipe(pipes[1]);
079    //radio.openReadingPipe(1,pipes[0]);
080  }
081 
082  //
083  // Start listening
084  //
085 
086  radio.startListening();
087 
088  //
089  // Dump the configuration of the rf unit for debugging
090  //
091 
092  radio.printDetails();
093}
094 
095void loop(void)
096{
097  //
098  // Ping out role.  Repeatedly send the current time
099  //
100 
101  if (role == role_ping_out)
102  {
103    // First, stop listening so we can talk.
104    radio.stopListening();
105 
106    // Take the time, and send it.  This will block until complete
107    unsigned long time = millis();
108    printf("Now sending %lu...",time);
109    bool ok = radio.write( &time, sizeof(unsigned long) );
110     
111    if (ok)
112      printf("ok...");
113    else
114      printf("failed.\n\r");
115 
116    // Now, continue listening
117    radio.startListening();
118 
119    // Wait here until we get a response, or timeout (250ms)
120    unsigned long started_waiting_at = millis();
121    bool timeout = false;
122    while ( ! radio.available() && ! timeout )
123      if (millis() - started_waiting_at > 200 )
124        timeout = true;
125 
126    // Describe the results
127    if ( timeout )
128    {
129      printf("Failed, response timed out.\n\r");
130    }
131    else
132    {
133      // Grab the response, compare, and send to debugging spew
134      unsigned long got_time;
135      radio.read( &got_time, sizeof(unsigned long) );
136 
137      // Spew it
138      printf("Got response %lu, round-trip delay: %lu\n\r",got_time,millis()-got_time);
139    }
140 
141    // Try again 1s later
142    delay(1000);
143  }
144 
145  //
146  // Pong back role.  Receive each packet, dump it out, and send it back
147  //
148 
149  if ( role == role_pong_back )
150  {
151    // if there is data ready
152    if ( radio.available() )
153    {
154      // Dump the payloads until we've gotten everything
155      unsigned long got_time;
156      bool done = false;
157      while (!done)
158      {
159        // Fetch the payload, and see if this was the last one.
160        done = radio.read( &got_time, sizeof(unsigned long) );
161 
162        // Spew it
163        printf("Got payload %lu...",got_time);
164 
165    // Delay just a little bit to let the other unit
166    // make the transition to receiver
167    delay(20);
168      }
169 
170      // First, stop listening so we can talk
171      radio.stopListening();
172 
173      // Send the final one back.
174      radio.write( &got_time, sizeof(unsigned long) );
175      printf("Sent response.\n\r");
176 
177      // Now, resume listening so we catch the next packets.
178      radio.startListening();
179    }
180  }
181 
182  //
183  // Change roles
184  //
185 
186  if ( Serial.available() )
187  {
188    char c = toupper(Serial.read());
189    if ( c == 'T' && role == role_pong_back )
190    {
191      printf("*** CHANGING TO TRANSMIT ROLE -- PRESS 'R' TO SWITCH BACK\n\r");
192 
193      // Become the primary transmitter (ping out)
194      role = role_ping_out;
195      radio.openWritingPipe(pipes[0]);
196      radio.openReadingPipe(1,pipes[1]);
197    }
198    else if ( c == 'R' && role == role_ping_out )
199    {
200      printf("*** CHANGING TO RECEIVE ROLE -- PRESS 'T' TO SWITCH BACK\n\r");
201       
202      // Become the primary receiver (pong back)
203      role = role_pong_back;
204      radio.openWritingPipe(pipes[1]);
205      radio.openReadingPipe(1,pipes[0]);
206    }
207  }
208}
209// vim:cin:ai:sts=2 sw=2 ft=cpp

Уно подкючал:

9(CE), 11 (MOSI), 12(MISO), 13(SCK), 10(CSN)

ЕЕ Скет:

001#include <SPI.h>
002#include "nRF24L01.h"
003#include "RF24.h"
004#include "printf.h"
005 
006//
007// Hardware configuration
008//
009 
010// Set up nRF24L01 radio on SPI bus plus pins 9 & 10
011 
012RF24 radio(9,10);
013 
014//
015// Topology
016//
017 
018// Radio pipe addresses for the 2 nodes to communicate.
019const uint64_t pipes[2] = { 0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL };
020 
021//
022// Role management
023//
024// Set up role.  This sketch uses the same software for all the nodes
025// in this system.  Doing so greatly simplifies testing. 
026//
027 
028// The various roles supported by this sketch
029typedef enum { role_ping_out = 1, role_pong_back } role_e;
030 
031// The debug-friendly names of those roles
032const char* role_friendly_name[] = { "invalid", "Ping out", "Pong back"};
033 
034// The role of the current running sketch
035role_e role = role_pong_back;
036 
037void setup(void)
038{
039  //
040  // Print preamble
041  //
042 
043  Serial.begin(57600);
044  printf_begin();
045  printf("\n\rRF24/examples/GettingStarted/\n\r");
046  printf("ROLE: %s\n\r",role_friendly_name[role]);
047  printf("*** PRESS 'T' to begin transmitting to the other node\n\r");
048 
049  //
050  // Setup and configure rf radio
051  //
052 
053  radio.begin();
054 
055  // optionally, increase the delay between retries & # of retries
056  radio.setRetries(15,15);
057 
058  // optionally, reduce the payload size.  seems to
059  // improve reliability
060  //radio.setPayloadSize(8);
061 
062  //
063  // Open pipes to other nodes for communication
064  //
065 
066  // This simple sketch opens two pipes for these two nodes to communicate
067  // back and forth.
068  // Open 'our' pipe for writing
069  // Open the 'other' pipe for reading, in position #1 (we can have up to 5 pipes open for reading)
070 
071  //if ( role == role_ping_out )
072  {
073    //radio.openWritingPipe(pipes[0]);
074    radio.openReadingPipe(1,pipes[1]);
075  }
076  //else
077  {
078    //radio.openWritingPipe(pipes[1]);
079    //radio.openReadingPipe(1,pipes[0]);
080  }
081 
082  //
083  // Start listening
084  //
085 
086  radio.startListening();
087 
088  //
089  // Dump the configuration of the rf unit for debugging
090  //
091 
092  radio.printDetails();
093}
094 
095void loop(void)
096{
097  //
098  // Ping out role.  Repeatedly send the current time
099  //
100 
101  if (role == role_ping_out)
102  {
103    // First, stop listening so we can talk.
104    radio.stopListening();
105 
106    // Take the time, and send it.  This will block until complete
107    unsigned long time = millis();
108    printf("Now sending %lu...",time);
109    bool ok = radio.write( &time, sizeof(unsigned long) );
110     
111    if (ok)
112      printf("ok...");
113    else
114      printf("failed.\n\r");
115 
116    // Now, continue listening
117    radio.startListening();
118 
119    // Wait here until we get a response, or timeout (250ms)
120    unsigned long started_waiting_at = millis();
121    bool timeout = false;
122    while ( ! radio.available() && ! timeout )
123      if (millis() - started_waiting_at > 200 )
124        timeout = true;
125 
126    // Describe the results
127    if ( timeout )
128    {
129      printf("Failed, response timed out.\n\r");
130    }
131    else
132    {
133      // Grab the response, compare, and send to debugging spew
134      unsigned long got_time;
135      radio.read( &got_time, sizeof(unsigned long) );
136 
137      // Spew it
138      printf("Got response %lu, round-trip delay: %lu\n\r",got_time,millis()-got_time);
139    }
140 
141    // Try again 1s later
142    delay(1000);
143  }
144 
145  //
146  // Pong back role.  Receive each packet, dump it out, and send it back
147  //
148 
149  if ( role == role_pong_back )
150  {
151    // if there is data ready
152    if ( radio.available() )
153    {
154      // Dump the payloads until we've gotten everything
155      unsigned long got_time;
156      bool done = false;
157      while (!done)
158      {
159        // Fetch the payload, and see if this was the last one.
160        done = radio.read( &got_time, sizeof(unsigned long) );
161 
162        // Spew it
163        printf("Got payload %lu...",got_time);
164 
165    // Delay just a little bit to let the other unit
166    // make the transition to receiver
167    delay(20);
168      }
169 
170      // First, stop listening so we can talk
171      radio.stopListening();
172 
173      // Send the final one back.
174      radio.write( &got_time, sizeof(unsigned long) );
175      printf("Sent response.\n\r");
176 
177      // Now, resume listening so we catch the next packets.
178      radio.startListening();
179    }
180  }
181 
182  //
183  // Change roles
184  //
185 
186  if ( Serial.available() )
187  {
188    char c = toupper(Serial.read());
189    if ( c == 'T' && role == role_pong_back )
190    {
191      printf("*** CHANGING TO TRANSMIT ROLE -- PRESS 'R' TO SWITCH BACK\n\r");
192 
193      // Become the primary transmitter (ping out)
194      role = role_ping_out;
195      radio.openWritingPipe(pipes[0]);
196      radio.openReadingPipe(1,pipes[1]);
197    }
198    else if ( c == 'R' && role == role_ping_out )
199    {
200      printf("*** CHANGING TO RECEIVE ROLE -- PRESS 'T' TO SWITCH BACK\n\r");
201       
202      // Become the primary receiver (pong back)
203      role = role_pong_back;
204      radio.openWritingPipe(pipes[1]);
205      radio.openReadingPipe(1,pipes[0]);
206    }
207  }
208}
209// vim:cin:ai:sts=2 sw=2 ft=cpp

 

 

dimasinnet
Offline
Зарегистрирован: 18.06.2013

а как понять эту строчку? где выбрать плату в качестве передатчика? или я что-то не понял О_о

dimasinnet
Offline
Зарегистрирован: 18.06.2013

Разобралсо... на моем китаезном датчике CE и CSN  местами перепутаны...

Puhlyaviy
Puhlyaviy аватар
Offline
Зарегистрирован: 22.05.2013

ну так и понимайте, что у вас оба скеча одинаковые :) 

что бы один начал пинговать а второй отвечать нужно задать им роли через сериал...одному послать R а второму T

Bubaylo
Offline
Зарегистрирован: 19.06.2013

Тоже столкнулся с непоняткой... только у меня nrf24l01+

Скачал mirf библиотеку, на ардуино уно залил пинг-сервер:

001/**
002 * An Mirf example which copies back the data it recives.
003 *
004 * Pins:
005 * Hardware SPI:
006 * MISO -> 12
007 * MOSI -> 11
008 * SCK -> 13
009 *
010 * Configurable:
011 * CE -> 8
012 * CSN -> 7
013 *
014 */
015 
016#include <SPI.h>
017#include <Mirf.h>
018#include <nRF24L01.h>
019#include <MirfHardwareSpiDriver.h>
020 
021void setup(){
022  Serial.begin(9600);
023   
024  /*
025   * Set the SPI Driver.
026   */
027 
028  Mirf.spi = &MirfHardwareSpi;
029   
030  /*
031   * Setup pins / SPI.
032   */
033    
034  Mirf.init();
035   
036  /*
037   * Configure reciving address.
038   */
039    
040  Mirf.setRADDR((byte *)"serv1");
041   
042  /*
043   * Set the payload length to sizeof(unsigned long) the
044   * return type of millis().
045   *
046   * NB: payload on client and server must be the same.
047   */
048    
049  Mirf.payload = sizeof(unsigned long);
050   
051  /*
052   * Write channel and payload config then power up reciver.
053   */
054    
055  Mirf.config();
056   
057  Serial.println("Listening...");
058}
059 
060void loop(){
061  /*
062   * A buffer to store the data.
063   */
064    
065  byte data[Mirf.payload];
066   
067  /*
068   * If a packet has been recived.
069   *
070   * isSending also restores listening mode when it
071   * transitions from true to false.
072   */
073    
074  if(!Mirf.isSending() && Mirf.dataReady()){
075    Serial.println("Got packet");
076     
077    /*
078     * Get load the packet into the buffer.
079     */
080      
081    Mirf.getData(data);
082     
083    /*
084     * Set the send address.
085     */
086      
087      
088    Mirf.setTADDR((byte *)"clie1");
089     
090    /*
091     * Send the data back to the client.
092     */
093      
094    Mirf.send(data);
095     
096    /*
097     * Wait untill sending has finished
098     *
099     * NB: isSending returns the chip to receving after returning true.
100     */
101       
102    Serial.println("Reply sent.");
103  }
104}

на клиент(на нано):

01/**
02 * A Mirf example to test the latency between two Ardunio.
03 *
04 * Pins:
05 * Hardware SPI:
06 * MISO -> 12
07 * MOSI -> 11
08 * SCK -> 13
09 *
10 * Configurable:
11 * CE -> 8
12 * CSN -> 7
13 *
14 * Note: To see best case latency comment out all Serial.println
15 * statements not displaying the result and load
16 * 'ping_server_interupt' on the server.
17 */
18 
19#include <SPI.h>
20#include <Mirf.h>
21#include <nRF24L01.h>
22#include <MirfHardwareSpiDriver.h>
23 
24void setup(){
25  Serial.begin(9600);
26  /*
27   * Setup pins / SPI.
28   */
29    
30  /* To change CE / CSN Pins:
31   *
32   * Mirf.csnPin = 9;
33   * Mirf.cePin = 7;
34   */
35  /*
36  Mirf.cePin = 7;
37  Mirf.csnPin = 8;
38  */
39  Mirf.spi = &MirfHardwareSpi;
40  Mirf.init();
41   
42  /*
43   * Configure reciving address.
44   */
45    
46  Mirf.setRADDR((byte *)"clie1");
47   
48  /*
49   * Set the payload length to sizeof(unsigned long) the
50   * return type of millis().
51   *
52   * NB: payload on client and server must be the same.
53   */
54    
55  Mirf.payload = sizeof(unsigned long);
56   
57  /*
58   * Write channel and payload config then power up reciver.
59   */
60    
61  /*
62   * To change channel:
63   *
64   * Mirf.channel = 10;
65   *
66   * NB: Make sure channel is legal in your area.
67   */
68    
69  Mirf.config();
70   
71  Serial.println("Beginning ... ");
72}
73 
74void loop(){
75  unsigned long time = millis();
76   
77  Mirf.setTADDR((byte *)"serv1");
78   
79  Mirf.send((byte *)&time);
80   
81  while(Mirf.isSending()){
82  }
83  Serial.println("Finished sending");
84  delay(10);
85  while(!Mirf.dataReady()){
86    //Serial.println("Waiting");
87    if ( ( millis() - time ) > 1000 ) {
88      Serial.println("Timeout on response from server!");
89      return;
90    }
91  }
92   
93  Mirf.getData((byte *) &time);
94   
95  Serial.print("Ping: ");
96  Serial.println((millis() - time));
97   
98  delay(1000);
99}

И тишина... что такое - не пойму. есессно, я в настройсках выбрал для каждого свою плату.

Bubaylo
Offline
Зарегистрирован: 19.06.2013

Вроде заработал пинг... Вот только почему-то очень много пропусков.... растояние между передатчиками примерно пол метра. с 2х разных ЮСБ портов.

dimasinnet
Offline
Зарегистрирован: 18.06.2013

Puhlyaviy пишет:

ну так и понимайте, что у вас оба скеча одинаковые :) 

что бы один начал пинговать а второй отвечать нужно задать им роли через сериал...одному послать R а второму T

Это я понимал...