基于NRF24L01实现两个树莓派和一个Beaglebone Black组网通讯

最近买了几个NRF24L01模块用来做无线通讯,而我手中正好有两个树莓派和一个beaglebone balck,就打算使用nrf24l01来做这三个设备的通讯.可以玩的花样很多,NRF24L01基于SPI接口,普通的单片机就可以使用,所以可以很简单的实现arduino和树莓派等的无线通讯.NRF24L01是基于Zigbee通讯协议的.这里以一台树莓派为主机,一台树莓派为从机,一台beaglebone为从机,实现三台机器的组网通讯.

个人原创,版权所有,转载请注明原文出处,并保留原文链接:

https://www.embbnux.com/2014/12/18/two_raspberry_pi_and_a_beaglebone_black_communicate_on_nrf24l01_network

一 树莓派和beaglebone black和NRF24L01的连线

NRF24l01是基于SPI接口的:

dsd

与树莓派的连线:

NRF24L01 => 树莓派

GND          =>   GND

VCC          =>    3.3V

CE           =>   GPIO25 即22管脚

CSN         =>   CE0(GPIO8) 即 24管脚

SCK         =>   SCLK(GPIO11)即23管脚

MOSI       =>   MOSI(GPIO10)即19管脚

MISO       =>   MISO(GPIO9)即21管脚

IRQ          =>   GPIO18即12管脚

两个树莓派和NRF24L01的连线是一样的

IMG_20141217_151314

Beaglebone black与NRF24L01的连线:

NRF24L01 => beaglebone black

GND          =>   GND

VCC          =>    3.3V

CE           =>   GPIO_48 (P9_15)

CSN         =>   SPI0_CS0(P9_17)

SCK         =>   SPI0_SCLK(P9_22)

MOSI       =>   SPI0_D1(P9_18)

MISO       =>   SPI0_D0(P9_21)

IRQ          =>   GPIO_51(P9_16)

二 树莓派安装NRF24L01的python库

需要先安装树莓派上的gpio python库,没有装的,可以先看我这篇博客:

树莓派raspberry使用python实现GPIO输入输出-按键LED

NRF24L01 这里参考 Learn from fly上的文章:


git clone https://github.com/riyas-org/nrf24pihub
cd nrf24pihub
cd python-spi
sudo python setup.py install
cd ../

    

还需要打开树莓派上的SPI口:


vi /etc/modprobe.d/raspi-blacklist.conf
#blacklist spi-bcm2708 #使用井号注释掉这行
blacklist i2c-bcm2708  #如果要使用i2c就注释掉这行

重启.会在/dev里面多出两个文件:spidev0.0,spidev0.1

三 Beaglebone Black安装NRF24L01的python库

需要先安装Adafruit_GPIO库:


git clone git://github.com/adafruit/adafruit-beaglebone-io-python.git
/usr/bin/ntpdate -b -s -u pool.ntp.org
opkg update && opkg install python-distutils
cd adafruit-beaglebone-io-python
python setup.py install

    

安装NRF24L01库,使用pynrf24:


git clone https://github.com/jpbarraca/pynrf24
cd pynrf24
python setup.py install

打开SPI支持:

用文本编辑器打开uEnv.txt

在里面最后一行加入下面的语句:optargs=capemgr.enable_partno=BB-SPIDEV0

重启.会在/dev里面多出两个文件:spidev1.0,spidev1.1

四 树莓派主机程序:

master_receive.py:


from nrf24 import NRF24
import time
from time import gmtime, strftime

#设定从机的发送地址pipes[0],pipes[1],本机的发送pipes[0]
pipes = [[0x10, 0x10, 0x10, 0x10, 0x20], [0x10, 0x10, 0x10, 0x10, 0x22]]
radio = NRF24()
radio.begin(0, 0,25,18) #set gpio 25 as CE pin,gpio 18 as IRQ pin
radio.setRetries(15,15)
radio.setPayloadSize(32)
radio.setChannel(0x4c)
radio.setDataRate(NRF24.BR_250KBPS)
radio.setPALevel(NRF24.PA_MAX)
radio.setAutoAck(1)

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

radio.startListening()
radio.stopListening()

radio.printDetails()
radio.startListening()

while True:

    pipe = [0] #pipe为接收到的信息的从机从机身份标识
    while not radio.available(pipe, True):
        time.sleep(100/1000000.0)
    recv_buffer = []
    radio.read(recv_buffer)
    out = ''.join(chr(i) for i in recv_buffer)
    print "Message from "+str(pipe[0])+":"+out

五 树莓派从机程序

raspberry_send.py


from nrf24 import NRF24
import time
from time import gmtime, strftime
pipes = [[0x10, 0x10, 0x10, 0x10, 0x20], [0x10, 0x10, 0x10, 0x10, 0x12]]
radio = NRF24()
radio.begin(0, 0,25,18) #set gpio 25 as CE pin
radio.setRetries(15,15)
radio.setPayloadSize(32)
radio.setChannel(0x4c)
radio.setDataRate(NRF24.BR_250KBPS)
radio.setPALevel(NRF24.PA_MAX)
radio.setAutoAck(1)
radio.openWritingPipe(pipes[0])
radio.openReadingPipe(1, pipes[1])

radio.startListening()
radio.stopListening()

radio.printDetails()
radio.startListening()
count = 1
while True:
    pipe = [0]
    send_data = "send count: "+str(count)
    print "start send data..."
    print "send data:"+str(count)
    radio.stopListening()
    ok = 0
    ok_true = 32
    while ok!=ok_true:
        ok = radio.write(send_data)
    time.sleep(2)
    count = count + 1

六 beaglebone black 从机发送程序

beaglebone_send.py:

from nrf24 import NRF24
import time
pipes = [[0x10, 0x10, 0x10, 0x10, 0x22], [0x10, 0x10, 0x10, 0x10, 0x11]]
radio = NRF24()
radio.begin(1, 0, "P9_15", "P9_16") #Set CE and IRQ pins
radio.setRetries(15,15)
radio.setPayloadSize(32)
radio.setChannel(0x4c)
radio.setDataRate(NRF24.BR_250KBPS)
radio.setPALevel(NRF24.PA_MAX)

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

radio.startListening()
radio.stopListening()
radio.printDetails()
buf = ['P', 'I', 'N', 'G']
count = 1
while True:
    send_data = "send:"+str(count)
    print send_data
    ok_true = 32
    while ok!=ok_true:
          ok=radio.write(send_data)
    print ok
    time.sleep(2)
    count = count+1
 

七 启动程序测试

树莓派主机: sudo python master_receive.py

树莓派从机:sudo python raspberry_send.py

beaglebone从机:sudo python beaglebone_send.py

收到的数据:

选区_004

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

Time limit is exhausted. Please reload the CAPTCHA.

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据