我尝试使用 2 个 Arduino Unos、2 个 LoRa 芯片(SX1278 433MHz)、2 个天线和 2 个 Arduino IDE 实例发送和接收数据。
问题出在接收数据命令上。
这是发送命令的代码:
#include <SPI.h>
#include <LoRa.h>
int counter = 0;
const int ss = 10;
const int reset = 9;
const int dio0 = 2;
void setup() {
Serial.begin(9600);
LoRa.begin(433E6);
LoRa.setPins(ss, reset, dio0);
while (!Serial);
Serial.println("LoRa Sender");
}
void loop() {
Serial.print("Sending packet: ");
Serial.println(counter);
// send packet
LoRa.beginPacket();
LoRa.print("hello ");
LoRa.print(counter);
LoRa.endPacket();
counter++;
delay(5000);
}
在串口监视器上,我成功发送包,但没有接收。这是接收代码:
#include <SPI.h>
#include <LoRa.h>
const int ss = 10;
const int reset = 9;
const int dio0 = 2;
void setup() {
Serial.begin(9600);
LoRa.begin(433E6);
LoRa.setPins(ss, reset, dio0);
while (!Serial);
Serial.println("LoRa Receiver");
}
void loop() {
// try to parse packet
int packetSize = LoRa.parsePacket();
if (packetSize) {
// received a packet
Serial.print("Received packet '");
// read packet
while (LoRa.available()) {
Serial.print((char)LoRa.read());
Serial.print("hello ");
}
// print RSSI of packet
Serial.print("' with RSSI ");
Serial.println(LoRa.packetRssi());
}
}
我使用了此 git 页面中有关连接的说明: https://github.com/sandeepmistry/arduino-LoRa
为了使这个板工作,我必须显式初始化SPI
SPI.begin(/*sck*/ 5, /*miso*/ 19, /*mosi*/ 27, /*ss*/ cs);
对你来说可能也是一样。另外,你应该正确初始化 Lora:
while (!LoRa.begin(433E6)) {
Serial.print(".");
delay(500);
}
Serial.println("LoRa Initializing OK!");
使用您发布的代码,您并不真正知道它是否正确初始化或者是否实际上正在发送任何数据。
首先我没有使用过 Lora 库。我使用 SX1278 库。所以我可以帮助你。 首先是库的链接 - Lora SX1278.h 库
现在您可能会问为什么我不使用原始 GitHub 存储库中的库。好吧,我遇到了该库的 qn 问题,问题是这样的:
修改了 sx1278::getPacket() 库函数以稳定 Lora 接收功能。有一个错误导致 esp 恐慌。未检查从 REG_FIFO 寄存器读取的有效负载长度是否有有效值,这导致读取 REG_FIFO 寄存器的次数超过 65000 次。此外,在该函数的耗时部分添加了yield()。
这就是我使用这个自定义库的原因。无论如何,对你来说: 您可以使用此功能发送数据包:
T_packet_state = sx1278.sendPacketTimeoutACK(ControllerAddress, message); //T_packet_state is 0 if the packet data is sent successful.
还可以使用此功能接收数据:
R_packet_state = sx1278.receivePacketTimeoutACK(); //R_packet_state is 0 if the packet data is received successfully.
在代码的开头只需定义一些内容:
//Lora SX1278:
#define LORA_MODE 10 //Add your suitable mode from the library
#define LORA_CHANNEL CH_6_BW_125 //Ch number and Bandwidth
#define LORA_ADDRESS 3 //Address of the device from which you will send data
uint8_t ControllerAddress = 5; //Address of receiving end device
我在 StackOverflow 上的第一个像样的答案。手指交叉。