受Radiohead启发的覆盆子皮Lora RFM9X库
raspi-lora的Python项目详细描述
概述
该项目是一个python库,用于使用hoperf rfm95/96/97/98 lora收音机和覆盆子pi。这个设计灵感来自于在基于Arduino的平台上流行的RadioHead项目。radiohead提供了一些方便的特性,包括加密、寻址、确认和重传。这个项目的动机是允许raspberry pis使用RadioHead RF95驱动程序以及RHReliableDatagram和RHEncryptedDriver与设备通信。
用法
安装
需要python>;=3.5。RPi.GPIO和spidev将按要求安装
pip install raspi-lora
开始
下面是一个简单的示例,它可以设置并发送消息:
from raspi_lora import LoRa, ModemConfig
# This is our callback function that runs when a message is received
def on_recv(payload):
print("From:", payload.header_from)
print("Received:", payload.message)
print("RSSI: {}; SNR: {}".format(payload.rssi, payload.snr))
# Use chip select 0. GPIO pin 17 will be used for interrupts
# The address of this device will be set to 2
lora = LoRa(0, 17, 2, modem_config=ModemConfig.Bw125Cr45Sf128, tx_power=14, acks=True)
lora.on_recv = on_recv
lora.set_mode_rx()
# Send a message to a recipient device with address 10
# Retry sending the message twice if we don't get an acknowledgment from the recipient
message = "Hello there!"
status = lora.send_to_wait(message, 10, retries=2)
if status is True:
print("Message sent!")
else:
print("No acknowledgment from recipient")
# And remember to call this as your program exits...
lora.close()
加密
如果要发送和接收加密的数据包,则需要安装PyCryptodome包。如果你使用的设备运行Radiohead与RhencryptedDriver,我建议使用AES密码。
pip install pycryptodome
在您的代码中:
from Crypto.Cipher import AES
crypto = AES.new(b"my-secret-encryption-key", AES.MODE_EAX)
然后在实例化LoRa
对象时传入crypto
。
lora = LoRa(0, 17, 2, crypto=crypto)
配置
初始化
LoRa(channel, interrupt, this_address, freq=915, tx_power=14,
modem_config=ModemConfig.Bw125Cr45Sf128, acks=False, crypto=None)
channel
要使用的SPI信道(如果您的罗兰无线电分别连接到CE0或CE1,则为0或1)
interrupt
用于中断的gpio pin(bcm样式编号)
this_address
发送和接收数据包时,设备将使用的地址号(0-254)。
freq
您的罗兰无线电使用的频率。默认为915MHz
tx_power
传输功率级别从5到23。把这个放低一点。默认为14
model_config
调制解调器配置。见RadioHead docs。默认为BW125CR45SF128。
receive_all
接收消息而不考虑目标地址
acks
如果True
,则在收到消息时发送确认数据包,并在发送消息时等待确认。这相当于使用radiohead的rhreliabledatagram
crypto
pycryptome cipher.aes的一个实例(参见上面的示例)
其他选项:
一个LoRa
实例还可以更改以下属性:
- cad_timeout通道活动检测超时。默认值为0
- 重试超时在尝试重试之前等待确认的时间。默认为0.2秒
- wait_packet_sent_timeout等待数据包传输的超时。默认值为0.2秒
方法
send_to_wait(data, header_to, header_flags=0)
send_to_wait(data, header_to, header_flags=0)
发送消息并阻止,直到收到确认或发生超时。如果成功,返回True
data
您的消息。可以是字符串或字节字符串header_to
收件人地址(0-255)。如果地址是255,则消息将广播到所有设备,并且send_to_wait()
将返回True
,而无需等待确认header_flags
位掩码,可以包含特定于应用程序的标志
send(data, header_to, header_id=0, header_flags=0)
类似于send_to_wait
,但不阻塞或等待确认,并且始终返回True
data
您的消息。可以是字符串或字节字符串header_id
消息的唯一ID(0-255)header_to
收件人地址(0-255)。如果地址是255,则消息将广播到所有设备header_flags
位掩码,可以包含特定于应用程序的标志
set_mode_rx()
将收音机设置为RX连续模式
set_mode_tx()
将收音机设置为Tx模式
set_mode_idle()
将收音机设置为空闲(禁止接收或发送)
sleep()
将收音机设置为低功耗睡眠模式
wait_packet_sent()
阻塞,直到数据包完成传输。如果发生超时,则返回False
s
close()
清理GPIO管脚并关闭SPI连接。这应该在程序退出时调用。
回调
on_recv(payload)
收到消息时运行的回调函数
payload
具有以下属性:
header_from
,header_to
,header_id
,header_flags
,message
,rssi
,snr
资源
RadioHead-无线电头项目。关于使用罗兰无线电的非常有用的信息来源。
Forked version of RadioHead for Raspberry Pi-原始radiohead项目的一个分支,可以更好地容纳覆盆子pi。现在已经过时几年了。
pySX127x-另一个python lora库,允许更多的配置。
Adafruit CircuitPython module for the RFM95/6/7/8-用于circuitpython的lora库