LMIC-node 项目教程
项目介绍
LMIC-node 是一个 LoRaWAN 节点的示例应用程序,适用于 The Things Network。它展示了如何发送上行消息、接收下行消息、实现下行命令,并提供了有用的状态信息。使用 LMIC-node,可以快速让一个节点运行起来。LMIC-node 支持许多流行的(LoRa)开发板,使用 Arduino 框架、LMIC LoRaWAN 库和 PlatformIO。
项目快速启动
基本步骤
- 选择支持的板子:在
platformio.ini
文件中选择一个支持的开发板。 - 选择 LoRaWAN 区域:在
platformio.ini
文件中选择你的 LoRaWAN 区域。 - 提供 LoRaWAN 密钥:在
lorawan-keys.h
文件中提供你的节点的 LoRaWAN 密钥。 - 编译和上传固件:编译并上传固件,然后就可以开始使用了。
示例代码
[env:your_board]
platform = ststm32
board = your_board
framework = arduino
lib_deps =
mcci-catena/MCCI LoRaWAN LMIC library@3.3.0
[platformio]
default_envs = your_board
#include <lmic.h>
#include <hal/hal.h>
#include <SPI.h>
// LoRaWAN 密钥
static const u1_t PROGMEM DEVEUI[8] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
void os_getDevEui (u1_t* buf) { memcpy_P(buf, DEVEUI, 8); }
static const u1_t PROGMEM APPEUI[8] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
void os_getArtEui (u1_t* buf) { memcpy_P(buf, APPEUI, 8); }
static const u1_t PROGMEM APPKEY[16] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
void os_getDevKey (u1_t* buf) { memcpy_P(buf, APPKEY, 16); }
static uint8_t mydata[] = "Hello, world!";
static osjob_t sendjob;
const unsigned TX_INTERVAL = 60;
void onEvent (ev_t ev) {
Serial.print(os_getTime());
Serial.print(": ");
switch(ev) {
case EV_JOINED:
Serial.println("EV_JOINED");
break;
case EV_TXCOMPLETE:
Serial.println("EV_TXCOMPLETE (includes waiting for RX windows)");
if (LMIC.dataLen) {
// 数据已接收
Serial.println("Received data");
}
// 计划下一次传输
os_setTimedCallback(&sendjob, os_getTime() + sec2osticks(TX_INTERVAL), do_send);
break;
default:
Serial.println(ev);
break;
}
}
void do_send(osjob_t* j) {
// 检查是否可以发送
if (LMIC.opmode & OP_TXRXPEND) {
Serial.println("OP_TXRXPEND, not sending");
} else {
// 发送数据
LMIC_setTxData2(1, mydata, sizeof(mydata) - 1, 0);
Serial.println("Packet queued");
}