必要准备
要控制LoRa模块,你需要依赖于一些Python库,比如pyserial来与串口进行通信。下面提供了一个示例代码,用于设置树莓派4B上的LoRa模块。在使用以下代码之前,请确保你已经正确连接了LoRa模块,并且树莓派的串口已经被正确配置和启用。可以参考之前文章树莓派4B串口通信配置
源码下载,无需积分
树莓派控制LoRa模块
安装库
首先,安装必要的库(如果还没有安装的话):
pip install pyserial
代码
import serial
import time
import RPi.GPIO as GPIO
# LoRa模块的MD0引脚连接到树莓派的哪个GPIO引脚(BCM编号)
MD0_PIN = 23 # 假设MD0连接到GPIO 23,请根据实际情况修改
# 初始化GPIO
GPIO.setmode(GPIO.BCM) # 使用BCM编号
GPIO.setup(MD0_PIN, GPIO.OUT) # 将MD0_PIN设置为输出
# 配置串口
ser = serial.Serial(
port='/dev/ttyAMA2',
baudrate=115200, # 根据您的LoRa模块配置,可能需要调整波特率
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout=1
)
# 控制MD0引脚的函数
def control_md0(state):
GPIO.output(MD0_PIN, state)
# 发送配置命令到LoRa模块的函数
def send_lora_config():
# 配置命令,包括了您提供的命令
config_commands = [
"AT+UART=7,0", # 配置串口波特率等参数
"AT+WLRATE=23,5", # 配置LoRa速率
"AT+TPOWER=3", # 配置发射功率
"AT+TMODE=0", # 配置工作模式
"AT+WLTIME=0", # 配置等待时间
"AT+CWMODE=0", # 配置操作模式
"AT+ADDR=00,00" # 配置设备地址
]
# 进入AT模式
control_md0(GPIO.HIGH) # MD0置高
time.sleep(1) # 等待模块反应
# 发送AT命令配置LoRa模块
for command in config_commands:
ser.write((command + "\r\n").encode())
time.sleep(0.1) # 等待命令执行
while ser.in_waiting:
print(ser.read(ser.in_waiting).decode(), end='') # 打印响应
# 退出AT模式
control_md0(GPIO.LOW) # MD0置低
time.sleep(1) # 等待模块反应
# 主函数
def main():
try:
send_lora_config() # 发送配置命令
finally:
ser.close() # 关闭串口
GPIO.cleanup() # 清理GPIO
if __name__ == "__main__":
main()
代码加入LoRa返回值显示
import serial
import time
import RPi.GPIO as GPIO
# LoRa模块的MD0引脚连接到树莓派的哪个GPIO引脚(BCM编号)
MD0_PIN = 21 # 假设MD0连接到GPIO 23,请根据实际情况修改
# 初始化GPIO
GPIO.setmode(GPIO.BCM) # 使用BCM编号
GPIO.setup(MD0_PIN, GPIO.OUT) # 将MD0_PIN设置为输出
# 配置串口
ser = serial.Serial(
port='/dev/ttyAMA2',
baudrate=115200, # 根据您的LoRa模块配置,可能需要调整波特率
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout=1
)
# 控制MD0引脚的函数
def control_md0(state):
GPIO.output(MD0_PIN, state)
# 读取串口直到遇到预期的响应(OK或ERROR)
def read_until_response(expected_responses, timeout=2):
end_time = time.time() + timeout
response = ''
while time.time() < end_time:
if ser.in_waiting:
response += ser.read(ser.in_waiting).decode()
for expected in expected_responses:
if expected in response:
return response
time.sleep(0.1)
return response # 返回所收到的响应,即使它不包含预期的结果
# 发送AT命令并等待OK或ERROR响应
def send_command_and_wait_for_response(command, expected_responses=["OK", "ERROR"]):
ser.write((command + "\r\n").encode())
response = read_until_response(expected_responses)
print(f"Sent: {command}, Received: {response}")
# 发送配置命令到LoRa模块的函数
def send_lora_config():
# 配置命令,包括了您提供的命令
config_commands = [
"AT+UART=7,0", # 配置串口波特率等参数
"AT+WLRATE=23,5", # 配置LoRa速率
"AT+TPOWER=3", # 配置发射功率
"AT+TMODE=0", # 配置工作模式
"AT+WLTIME=0", # 配置等待时间
"AT+CWMODE=0", # 配置操作模式
"AT+ADDR=00,00" # 配置设备地址
]
# 进入AT模式
control_md0(GPIO.HIGH) # MD0置高
time.sleep(1) # 等待模块反应
print("Enter AT mode\r\n")
# 发送AT命令配置LoRa模块
for command in config_commands:
send_command_and_wait_for_response(command)
# 退出AT模式
control_md0(GPIO.LOW) # MD0置低
time.sleep(1) # 等待模块反应
print("Exit AT mode\r\n")
# 主函数
def main():
try:
send_lora_config() # 发送配置命令
finally:
ser.close() # 关闭串口
GPIO.cleanup() # 清理GPIO
if __name__ == "__main__":
main()
透传模式下发送hello代码
import serial
import time
import RPi.GPIO as GPIO
# LoRa模块的MD0引脚连接到树莓派的哪个GPIO引脚(BCM编号)
MD0_PIN = 21 # 根据实际情况修改MD0连接到的GPIO引脚
# 初始化GPIO
GPIO.setmode(GPIO.BCM) # 使用BCM编号
GPIO.setup(MD0_PIN, GPIO.OUT) # 将MD0_PIN设置为输出
# 配置串口
ser = serial.Serial(
port='/dev/ttyAMA2',
baudrate=115200, # 根据LoRa模块的配置,可能需要调整波特率
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout=1
)
# 控制MD0引脚的函数
def control_md0(state):
GPIO.output(MD0_PIN, state)
# 读取串口直到遇到预期的响应(OK或ERROR)
def read_until_response(expected_responses, timeout=2):
end_time = time.time() + timeout
response = ''
while time.time() < end_time:
if ser.in_waiting:
response += ser.read(ser.in_waiting).decode()
for expected in expected_responses:
if expected in response:
return response
time.sleep(0.1)
return response # 返回所收到的响应,即使它不包含预期的结果
# 发送AT命令并等待OK或ERROR响应
def send_command_and_wait_for_response(command, expected_responses=["OK", "ERROR"]):
ser.write((command + "\r\n").encode())
response = read_until_response(expected_responses)
print(f"Sent: {command}, Received: {response}")
# 发送配置命令到LoRa模块的函数
def send_lora_config():
# 配置命令,包括了您提供的命令
config_commands = [
"AT+UART=7,0", # 配置串口波特率等参数
"AT+WLRATE=23,5", # 配置LoRa速率
"AT+TPOWER=3", # 配置发射功率
"AT+TMODE=0", # 配置工作模式
"AT+WLTIME=0", # 配置等待时间
"AT+CWMODE=0", # 配置操作模式
"AT+ADDR=00,00" # 配置设备地址
]
# 进入AT模式
control_md0(GPIO.HIGH) # MD0置高
time.sleep(1) # 等待模块反应
print("Enter AT mode\r\n")
# 发送AT命令配置LoRa模块
for command in config_commands:
send_command_and_wait_for_response(command)
# 退出AT模式
control_md0(GPIO.LOW) # MD0置低
time.sleep(1) # 等待模块反应
print("Exit AT mode\r\n")
# 发送数据到LoRa模块的函数
def send_data(data):
ser.write(data.encode())
# 主函数
def main():
try:
send_lora_config() # 发送配置命令
# 在这里发送"hello world"到LoRa模块
send_data("hello world")
print("Data sent: hello world\r\n")
finally:
ser.close() # 关闭串口
GPIO.cleanup() # 清理GPIO
if __name__ == "__main__":
main()
透传模式下发送hello和时间代码
import serial
import time
import RPi.GPIO as GPIO
from datetime import datetime
# LoRa模块的MD0引脚连接到树莓派的哪个GPIO引脚(BCM编号)
MD0_PIN = 21 # 根据实际情况修改MD0连接到的GPIO引脚
# 初始化GPIO
GPIO.setmode(GPIO.BCM) # 使用BCM编号
GPIO.setup(MD0_PIN, GPIO.OUT) # 将MD0_PIN设置为输出
# 配置串口
ser = serial.Serial(
port='/dev/ttyAMA2',
baudrate=115200, # 根据LoRa模块的配置,可能需要调整波特率
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout=1
)
# 控制MD0引脚的函数
def control_md0(state):
GPIO.output(MD0_PIN, state)
# 读取串口直到遇到预期的响应(OK或ERROR)
def read_until_response(expected_responses, timeout=2):
end_time = time.time() + timeout
response = ''
while time.time() < end_time:
if ser.in_waiting:
response += ser.read(ser.in_waiting).decode()
for expected in expected_responses:
if expected in response:
return response
time.sleep(0.1)
return response # 返回所收到的响应,即使它不包含预期的结果
# 发送AT命令并等待OK或ERROR响应
def send_command_and_wait_for_response(command, expected_responses=["OK", "ERROR"]):
ser.write((command + "\r\n").encode())
response = read_until_response(expected_responses)
print(f"Sent: {command}, Received: {response}")
# 发送配置命令到LoRa模块的函数
def send_lora_config():
# 配置命令,包括了您提供的命令
config_commands = [
"AT+UART=7,0", # 配置串口波特率等参数
"AT+WLRATE=23,5", # 配置LoRa速率
"AT+TPOWER=3", # 配置发射功率
"AT+TMODE=0", # 配置工作模式
"AT+WLTIME=0", # 配置等待时间
"AT+CWMODE=0", # 配置操作模式
"AT+ADDR=00,00" # 配置设备地址
]
# 进入AT模式
control_md0(GPIO.HIGH) # MD0置高
time.sleep(1) # 等待模块反应
print("Enter AT mode\r\n")
# 发送AT命令配置LoRa模块
for command in config_commands:
send_command_and_wait_for_response(command)
# 退出AT模式
control_md0(GPIO.LOW) # MD0置低
time.sleep(1) # 等待模块反应
print("Exit AT mode\r\n")
# 发送数据到LoRa模块的函数
def send_data(data):
ser.write(data.encode())
# 主函数
def main():
try:
send_lora_config() # 发送配置命令
while True:
# 在这里发送"hello world"到LoRa模块
send_data("hello world\r\n")
print("Data sent: hello world\r\n")
current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S\r\n")
send_data(current_time)
print(f"Data sent: {current_time}\r\n")
time.sleep(2)
finally:
ser.close() # 关闭串口
GPIO.cleanup() # 清理GPIO
if __name__ == "__main__":
main()