Robofun 機器人論壇

標題: arduino+nrf24L01 收發問題 [打印本頁]

作者: Frnak_LIU    時間: 2016-3-29 20:53
標題: arduino+nrf24L01 收發問題
大家好:我按照網路的範例程式  想要使用收發器
但不管我參照哪種方法  顯示的結果都是發不出去
我CLIENT是使用MEGA版  SERVER端是UNO版
想請問到底哪邊出問題  為啥總是出現nrf24.init()
在這裡面我挑選一個我最能理解的程式碼  CLIENT端如下:
#include <SPI.h>
#include <RH_NRF24.h>

// Singleton instance of the radio driver
RH_NRF24 nrf24;
// RH_NRF24 nrf24(8, 7); // use this to be electrically compatible with Mirf
// RH_NRF24 nrf24(8, 10);// For Leonardo, need explicit SS pin
// RH_NRF24 nrf24(8, 7); // For RFM73 on Anarduino Mini

void setup()
{
  Serial.begin(9600);
  while (!Serial)
    ; // wait for serial port to connect. Needed for Leonardo only
  if (!nrf24.init())
    Serial.println("init failed");
  // Defaults after init are 2.402 GHz (channel 2), 2Mbps, 0dBm
  if (!nrf24.setChannel(1))
    Serial.println("setChannel failed");
  if (!nrf24.setRF(RH_NRF24::DataRate2Mbps, RH_NRF24::TransmitPower0dBm))
    Serial.println("setRF failed");   
}


void loop()
{
  Serial.println("Sending to nrf24_server");
  // Send a message to nrf24_server
  uint8_t data[] = "Hello World!";
  nrf24.send(data, sizeof(data));

  nrf24.waitPacketSent();
  // Now wait for a reply
  uint8_t buf[RH_NRF24_MAX_MESSAGE_LEN];
  uint8_t len = sizeof(buf);

  if (nrf24.waitAvailableTimeout(500))
  {
    // Should be a reply message for us now   
    if (nrf24.recv(buf, &len))
    {
      Serial.print("got reply: ");
      Serial.println((char*)buf);
    }
    else
    {
      Serial.println("recv failed");
    }
  }
  else
  {
    Serial.println("No reply, is nrf24_server running?");
  }
  delay(400);
}




SERVER端如下:
#include <SPI.h>
#include <RH_NRF24.h>

// Singleton instance of the radio driver
RH_NRF24 nrf24;
// RH_NRF24 nrf24(8, 7); // use this to be electrically compatible with Mirf
// RH_NRF24 nrf24(8, 10);// For Leonardo, need explicit SS pin
// RH_NRF24 nrf24(8, 7); // For RFM73 on Anarduino Mini

void setup()
{
  Serial.begin(9600);
  while (!Serial)
    ; // wait for serial port to connect. Needed for Leonardo only
  if (!nrf24.init())
    Serial.println("init failed");
  // Defaults after init are 2.402 GHz (channel 2), 2Mbps, 0dBm
  if (!nrf24.setChannel(1))
    Serial.println("setChannel failed");
  if (!nrf24.setRF(RH_NRF24::DataRate2Mbps, RH_NRF24::TransmitPower0dBm))
    Serial.println("setRF failed");   
}

void loop()
{
  if (nrf24.available())
  {
    // Should be a message for us now   
    uint8_t buf[RH_NRF24_MAX_MESSAGE_LEN];
    uint8_t len = sizeof(buf);
    if (nrf24.recv(buf, &len))
    {
//      NRF24::printBuffer("request: ", buf, len);
      Serial.print("got request: ");
      Serial.println((char*)buf);

      // Send a reply
      uint8_t data[] = "And hello back to you";
      nrf24.send(data, sizeof(data));
      nrf24.waitPacketSent();
      Serial.println("Sent a reply");
    }
    else
    {
      Serial.println("recv failed");
    }
  }
}
作者: 超新手    時間: 2016-3-29 21:39
mega 是接 50、51、52、53嗎?
作者: Frnak_LIU    時間: 2016-3-29 22:38
對~  接腳部分我有再三確認
作者: Frnak_LIU    時間: 2016-3-29 22:43
nrf24.init()
我不懂為啥我光最基本的通訊線問題
線路也都確定無誤
作者: Frnak_LIU    時間: 2016-3-29 22:46
http://randomnerdtutorials.com/n ... odule-with-arduino/
這是我參考的網站~
作者: 超新手    時間: 2016-3-30 06:50
本帖最後由 超新手 於 2016-3-30 08:02 編輯

>>為啥總是出現nrf24.init()
這句話是什麼意思?
是指  序列監控視窗會顯示 init failed 嗎?
作者: Frnak_LIU    時間: 2016-3-30 13:21
對~
作者: 超新手    時間: 2016-3-30 15:17
要不要試看看官方推薦的
http://tmrh20.github.io/RF24/
下載
https://github.com/TMRh20/RF24/archive/master.zip
你用的那版, 有人反應和你一樣的問題
http://forum.arduino.cc/index.php?topic=269190.0
作者: Frnak_LIU    時間: 2016-3-30 19:51
我使用裡面的GETTINGSTART
但傳送端一樣只出現
Now sending
failed
Failed, response timed out.

接收端則是
Failed, response timed out.


之前這我也試過了但就是無法
作者: 超新手    時間: 2016-3-30 20:39
本帖最後由 超新手 於 2016-3-30 21:26 編輯

所以你有分別用role=1和role=0下載到各別板子嗎?
奇怪, Failed, response timed out.這個錯誤
應該是role=1才會發生的錯誤
為什麼兩個板子都顯示相同錯誤

另外, 除了 CE 和 CSN你也有接嗎?
作者: Frnak_LIU    時間: 2016-3-31 10:18
都有接~  我有發現我UNO版能收跟發
但MEGA版卻只能收
作者: Frnak_LIU    時間: 2016-3-31 10:23
radioNumber
但我看介紹  接收端值設定為0   傳送段值設定為1

我也有更改但就是無法  MEGA版需要更改其他設定嗎
作者: 超新手    時間: 2016-3-31 12:49
本帖最後由 超新手 於 2016-3-31 12:50 編輯

>>我有發現我UNO版能收跟發
>>但MEGA版卻只能收
你是說, 至少已經可以一邊正常工作了嗎?
記得收發互換要去改 ROLE 值
或是直接用 序列監控視窗 按 T 或 R 去切換收發模式
那個程式可以支援動態切換收發模式

>>radioNumber
>>但我看介紹  接收端值設定為0   傳送段值設定為1
這個值不用改才對吧?
改 ROLE 就可了, 或是用軟體切

>>我也有更改但就是無法  MEGA版需要更改其他設定嗎
應該不用才對, 接腳不同而已
作者: cc1357    時間: 2022-3-12 23:07

谢谢大大分享




歡迎光臨 Robofun 機器人論壇 (https://www.robofun.net/forum/) Powered by Discuz! X3.2