查看: 8395|回复: 1
收起左侧

[Arduino] raspberrypi 与 arduino 使用 nRF24L01+ 通信 -- raspberry pi为发送端(转载)

2013-8-15 17:35:51 | 显示全部楼层 |阅读模式
nRF24L01+ 通过gpio与树梅派链接,按着网上能找到的所有方法基本上都不顺利,从Python方案到c方案都不行,尝试了很长时间,终于成功,基本上,每个人都会碰到各种各样的问题。
修改系统配置
  1. $ sudo nano /etc/modprobe.d/raspi-blacklist.conf
复制代码
都注释掉,修改为:
  1. # blacklist spi and i2c by default (many users don't need them)

  2. #blacklist spi-bcm2708
  3. #blacklist i2c-bcm2708
复制代码
修改加载模块
  1. $ sudo nano /etc/modules
复制代码
改为:
  1. snd-bcm2835
  2. i2c-dev
  3. spidev
复制代码
主要是增加 spidev
重启之后,/dev/中会多出两个设备 spidev0.0 与 spidev0.1, 没有出现的话请google排错。
下载源码  https://github.com/gnulnulf/RF24
打包下载到树梅派
编译rf24库
解压下载好的源码,进入目录RF24-master/librf24-rpi/librf24
编译
  1. $ make
复制代码
如果出现缺少某些编译工具的提示,google搜搜然后apt安装就是了 安装
  1. $ sudo make install
复制代码
编译源码
  1. $ make
复制代码
安装
  1. $ sudo make install
复制代码
连接 nRF24L01+ 模块
QQ截图20130815173340.png
  1. rf24        rasp
  2. 3.3v        3.3v (不能使用5v)
  3. GND         GND
  4. CE          GPIO 18 (右排往下第六个)
  5. CSN         GPIO 8 (右排往下倒数第二个)
  6. SCK         GPIO 11 (左排往下倒数第二个)  
  7. MOSI        GPIO 10 (左排往下倒数第四个)
  8. MISO        GPIO 9  (左排往下倒数第三个)
复制代码
编写发送端程序
源码中已经有丰富的示例程序,我们只需要改改源码就可以跑通
进入librf24-rpi/librf24/examples
更改 pingtest.cpp 代码为:
  1. /**
  2. * Example RF Radio Ping Pair
  3. *
  4. * This is an example of how to use the RF24 class.  Write this sketch to two different nodes,
  5. * connect the role_pin to ground on one.  The ping node sends the current time to the pong node,
  6. * which responds by sending the value back.  The ping node can then see how long the whole cycle
  7. * took.
  8. */

  9. #include <cstdlib>
  10. #include <iostream>

  11. #include "../RF24.h"

  12. /*
  13.     连接方法

  14.     rf24        rasp
  15.     3.3v        3.3v (不能使用5v)
  16.     GND         GND
  17.     CE          GPIO 18 (右排往下第六个)
  18.     CSN         GPIO 8 (右排往下倒数第二个)
  19.     SCK         GPIO 11 (左排往下倒数第二个)  
  20.     MOSI        GPIO 10 (左排往下倒数第四个)
  21.     MISO        GPIO 9  (左排往下倒数第三个)

  22. */

  23. //
  24. // 硬件配置
  25. // spi设备、CSN速率、CSN引脚 GPIO 8
  26. RF24 radio("/dev/spidev0.0",8000000 , 8);

  27. // 设置数据通道地址
  28. const uint64_t pipes[2] = { 0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL };

  29. // 配置rf24
  30. void setup(void) {

  31.     printf("\n\rRF24/examples/pingpair/\n\r");
  32.     printf("ROLE: Ping out\n\r");

  33.     radio.begin();

  34.     // 开启动态有效信息长度
  35.     radio.enableDynamicPayloads();

  36.     // 设置重传次数以及每次重传的延迟
  37.     //radio.setRetries(15,15);

  38.     // 设置传输速率
  39.     radio.setDataRate(RF24_1MBPS);

  40.     // 设置功放级别,有四种级别:
  41.     // RF24_PA_MIN=-18dBm
  42.     // RF24_PA_LOW=-12dBm
  43.     // RF24_PA_MED=-6dBM
  44.     // RF24_PA_HIGH=0dBm
  45.     radio.setPALevel(RF24_PA_HIGH);

  46.     // 设置信道(0-127)
  47.     radio.setChannel(110);

  48.     // 设置crc校验长度
  49.     // 两种 8位RF24_CRC_8 和 16位RF24_CRC_16
  50.     radio.setCRCLength(RF24_CRC_16);

  51.     radio.openWritingPipe(pipes[0]);
  52.     radio.openReadingPipe(1,pipes[1]);


  53.     //
  54.     // 开始监听
  55.     //

  56.     radio.startListening();

  57.     // 打印配置信息
  58.     radio.printDetails();
  59. }

  60. void loop(void) {

  61.     // 首先停止监听
  62.     radio.stopListening();

  63.     // 获取时间,并发送时间
  64.     unsigned long time = __millis();
  65.     printf("Now sending %lu...",time);

  66.     // 是否发送成功
  67.     bool ok = radio.write( &time, sizeof(unsigned long) );

  68.     if (ok)
  69.         printf("ok...");
  70.     else
  71.         printf("failed.\n\r");

  72.     // 继续监听
  73.     radio.startListening();

  74.     // 等待对方返回数据,超时时间 250ms
  75.     unsigned long started_waiting_at = __millis();

  76.     bool timeout = false;
  77.     while ( !radio.available() && !timeout ) {

  78.         //稍微延迟一下,等待radio.available()检测有效数据
  79.         __msleep(5);
  80.         if (__millis() - started_waiting_at > 200 )
  81.         timeout = true;
  82.     }

  83.     // 是否超时
  84.     if ( timeout ) {

  85.         printf("Failed, response timed out.\n\r");

  86.     } else {
  87.         // 读取返回信息,并打印出来
  88.         unsigned long got_time;
  89.         radio.read( &got_time, sizeof(unsigned long) );

  90.         printf("Got response %lu, round-trip delay: %lu\n\r",got_time,__millis()-got_time);
  91.     }

  92.     //延迟一会儿
  93.     sleep(1);
  94. }

  95. int main(int argc, char** argv) {
  96.     setup();

  97.     while(1)
  98.         loop();

  99.     return 0;
  100. }
复制代码
然后编译:
  1. $ make
复制代码
之后,这几个示例都会编译出来。运行pingtest程序
  1. $ sudo ./pingtest
复制代码
rasp的输出:
  1. RF24/examples/pingpair/
  2. ROLE: Ping out
  3. SPI device   = /dev/spidev0.0
  4. SPI speed    = 8000000
  5. CE GPIO  = 8
  6. STATUS       = 0x0e RX_DR=0 TX_DS=0 MAX_RT=0 RX_P_NO=7 TX_FULL=0
  7. RX_ADDR_P0-1     = 0xf0f0f0f0e1 0xf0f0f0f0d2
  8. RX_ADDR_P2-5     = 0xe2 0xe3 0xf1 0xf2
  9. TX_ADDR      = 0xf0f0f0f0e1
  10. RX_PW_P0-6   = 0x20 0x20 0x20 0x20 0x20 0x20
  11. EN_AA        = 0x3f
  12. EN_RXADDR    = 0x3e
  13. RF_CH        = 0x6e
  14. RF_SETUP     = 0x04
  15. CONFIG       = 0x0f
  16. DYNPD/FEATURE    = 0x3f 0x04
  17. Data Rate    = 1MBPS
  18. Model        = nRF24L01+
  19. CRC Length   = 16 bits
  20. PA Power     = PA_HIGH
  21. Now sending 1607486530...failed.
  22. Got response 1607486430, round-trip delay: 156
  23. Now sending 1607487589...failed.
  24. Got response 1607487489, round-trip delay: 158
  25. Now sending 1607488650...failed.
复制代码
ps: 本人用arduino uno充当接收端时,发送端总是提示发送失败failed,但是,双方通信是没问题的。换成了lilypad就没有这个情况。
上面的是配置信息,如果大部分数据都是ffffff,那么硬件没有配置成功,这样很麻烦,只能求助与google了。
下面的 sending那些东西,是发送给arduino,以及arduino返回的数,arduino会给每个数减去100,并返回。
arduino的输出:
  1. RF24/examples/pingpair/
  2. ROLE: Pong back
  3. STATUS       = 0x0e RX_DR=0 TX_DS=0 MAX_RT=0 RX_P_NO=7 TX_FULL=0
  4. RX_ADDR_P0-1    = 0xf0f0f0f0d2 0xf0f0f0f0e1
  5. RX_ADDR_P2-5    = 0xc3 0xc4 0xc5 0xc6
  6. TX_ADDR      = 0xf0f0f0f0d2
  7. RX_PW_P0-6      = 0x20 0x20 0x00 0x00 0x00 0x00
  8. EN_AA        = 0x3f
  9. EN_RXADDR       = 0x03
  10. RF_CH        = 0x6e
  11. RF_SETUP        = 0x05
  12. CONFIG       = 0x0f
  13. DYNPD/FEATURE   = 0x3f 0x04
  14. Data Rate    = 1MBPS
  15. Model        = nRF24L01+
  16. CRC Length   = 16 bits
  17. PA Power     = LA_MED
  18. Got payload 1607938782...Sent response.
  19. Got payload 1607939839...Sent response.
  20. Got payload 1607940898...Sent response.
复制代码
补充

http://wenku.baidu.com/view/6c779635eefdc8d376ee3256.html nRF24L01中文手册

http://maniacbug.github.io/RF24/classRF24.html RF24的api手册,适用于rasp和arduino

其他 rf24与rasp和arduino相关的资源:

http://www.raspberrypi.org/phpBB3/viewtopic.php?t=17061 官方讨论

https://github.com/maniacbug/RF24 rf24库

https://github.com/gnulnulf/RF24 包含rasp的rf24库

https://bitbucket.org/Amanoo/rf24rp/wiki/Home BeagleBone的rf24库移植的rasp库

http://www.e-risingstar.com/wordpress/?p=543 另一个rasp与arduino的互通指南,基于上面那个BeagleBone移植过来的库

https://plus.google.com/100198871098258549028/posts/Hn1JpyUWKKo rasp的python rasp库

http://arduino-info.wikispaces.com/Nrf24L01-2.4GHz-HowTo arduino的rf24指南

http://maniacbug.wordpress.com/2011/11/02/getting-started-rf24/ 另一个arduino的rf24指南

转自:http://www.cnblogs.com/hangxin19 ... /05/01/3053467.html
回复

使用道具 举报

2017-11-3 10:00:55 | 显示全部楼层
你好,请问这是用树莓派的SPI口往寄存器里读写数据的吗?
我看您资料里的头文件代码,#define MASK_RX_DR  6 树莓派这样定义不就成了定义gpio口了吗?
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注我们,了解更多

官方微信

服务时间:10:00-16:00

13714503811

公司地址:深圳市龙岗区南湾街道东门头路8号

Copyright © 2012-2020 Powered by 树莓派论坛 2019.4  粤ICP备15075382号-1
快速回复 返回列表 返回顶部