Difference between revisions of "Tutorial:LoRa"
From MobiNetS
Line 7: | Line 7: | ||
*'''Lora''' | *'''Lora''' | ||
===Lora通信实例=== | |||
*使用Arduino进行Lora开发,首先需要环境配置: | *使用Arduino进行Lora开发,首先需要环境配置: | ||
Line 17: | Line 17: | ||
转到tools --> Boards --> Boards Manager,找到Dragino信息并安装它 | 转到tools --> Boards --> Boards Manager,找到Dragino信息并安装它 | ||
这样,我们的Dragino开发板就成功的关联到了IDE里面,可以打开IDE --> Tools的开发板,就能找到Dragino | 这样,我们的Dragino开发板就成功的关联到了IDE里面,可以打开IDE --> Tools的开发板,就能找到Dragino | ||
*要使用Lora通信,需要调用Lora的库文件,当然,我们有现成的库可以用。点击下载Radio_Head,将其拷贝到Arduino安装目录下的libraries里面,就可以从IDE当中直接导包,使用里面提供的接口 | |||
*用数据线将开发板连接到电脑,从IDE选择开发板,然后选择的Tools选择端口(开发板连接的端口)。下面是Lora通信的示例代码: | *用数据线将开发板连接到电脑,从IDE选择开发板,然后选择的Tools选择端口(开发板连接的端口)。下面是Lora通信的示例代码: | ||
Client: | Client: | ||
#include <SPI.h> | #include <SPI.h> //lora跟Arduino之间使用的是SPI通信方式 | ||
#include <RH_RF95.h> | #include <RH_RF95.h> //lora的库文件,提供了关于设置lora发射参数等一系列函数 | ||
RH_RF95 rf95; | RH_RF95 rf95; | ||
float frequency = 433.0; | float frequency = 433.0; //在国内,ISM频段只有433MHz可用,这里需要注意,如果我们买的是868M或者915M的lora板子,是无法使用的 | ||
void setup() | void setup() | ||
{ | { | ||
Serial.begin(9600); | Serial.begin(9600); //设置波特率,为了Arduino开发板跟电脑端的串口通信使用,有时候使用串口助手发现电脑端显示的全是乱码,就很有可能是波特率设置的不一样。 | ||
while (!Serial) ; // Wait for serial port to be available | while (!Serial) ; // Wait for serial port to be available | ||
Serial.println("Start LoRa Client"); | Serial.println("Start LoRa Client"); //串口打印函数 | ||
if (!rf95.init()) | if (!rf95.init()) | ||
Serial.println("init failed"); | Serial.println("init failed"); | ||
// Setup ISM frequency | rf95.setFrequency(frequency); // Setup ISM frequency | ||
rf95. | rf95.setTxPower(18); // Setup Power,dBm | ||
rf95.setSpreadingFactor(12); // Setup Spreading Factor (6 ~ 12) | |||
rf95. | |||
// Setup BandWidth, option: 7800,10400,15600,20800,31200,41700,62500,125000,250000,500000 | // Setup BandWidth, option: 7800,10400,15600,20800,31200,41700,62500,125000,250000,500000 | ||
//Lower BandWidth for longer distance. | //Lower BandWidth for longer distance. | ||
Line 44: | Line 43: | ||
// Setup Coding Rate:5(4/5),6(4/6),7(4/7),8(4/8) | // Setup Coding Rate:5(4/5),6(4/6),7(4/7),8(4/8) | ||
rf95.setCodingRate4(5); | rf95.setCodingRate4(5); | ||
} | //如果我们需要修改lora发射参数,只需要一行代码,非常方便 | ||
} //setup函数体只执行一遍,执行完了过后,开始进入到loop函数里面 | |||
void loop() | void loop() | ||
Line 50: | Line 50: | ||
Serial.println("Sending to LoRa Server"); | Serial.println("Sending to LoRa Server"); | ||
// Send a message to LoRa Server | // Send a message to LoRa Server | ||
uint8_t data[] = "Hello, this is device 1"; | uint8_t data[] = "Hello, this is device 1"; //lora需要发射的数据,都是存放在一个字符数组里面 | ||
rf95.send(data, sizeof(data)); | rf95.send(data, sizeof(data)); //发送函数 | ||
rf95.waitPacketSent(); | rf95.waitPacketSent(); //等待数据发送完成 | ||
// Now wait for a reply | // Now wait for a reply | ||
uint8_t buf[RH_RF95_MAX_MESSAGE_LEN]; | uint8_t buf[RH_RF95_MAX_MESSAGE_LEN]; | ||
Line 59: | Line 59: | ||
} | } | ||
*Server: | *Server: | ||
#include <SPI.h> | #include <SPI.h> | ||
Line 77: | Line 75: | ||
if (!rf95.init()) | if (!rf95.init()) | ||
Serial.println("init failed"); | Serial.println("init failed"); | ||
rf95.setFrequency(frequency); | rf95.setFrequency(frequency); | ||
rf95.setTxPower(13); | rf95.setTxPower(13); | ||
// Defaults BW Bw = 125 kHz, Cr = 4/5, Sf = 128chips/symbol, CRC on | // Defaults BW Bw = 125 kHz, Cr = 4/5, Sf = 128chips/symbol, CRC on | ||
Line 89: | Line 85: | ||
{ | { | ||
if (rf95.available()) | if (rf95.available()) | ||
{ | { | ||
uint8_t buf[RH_RF95_MAX_MESSAGE_LEN]; //开辟一个buffer便于接收lora数据 | |||
uint8_t buf[RH_RF95_MAX_MESSAGE_LEN]; | |||
uint8_t len = sizeof(buf); | uint8_t len = sizeof(buf); | ||
if (rf95.recv(buf, &len)) | if (rf95.recv(buf, &len)) | ||
Line 100: | Line 95: | ||
Serial.println((char*)buf); | Serial.println((char*)buf); | ||
Serial.print("RSSI: "); | Serial.print("RSSI: "); | ||
Serial.println(rf95.lastRssi(), DEC); | Serial.println(rf95.lastRssi(), DEC); //调用现成的接口可以直接获取lora信号的RSSI值 | ||
// Send a reply | // Send a reply |
Revision as of 22:52, 25 September 2019
This page shows the tutorial for LoRa experiments.
Platform and programming environments
Arduino + Lora
- Lora is a wireless communication module,we also need a MCU to contral it like Raspberry Pi(树莓派)、Arduino and so on. (Here we choose Arduino).
- More about Arduino
- Lora
Lora通信实例
- 使用Arduino进行Lora开发,首先需要环境配置:
- 下载和安装Arduino IDE [Arduino下载]
- 在IDE中添加开发板 我们要在IDE上编写代码烧录到板子上运行,首先需要把板子信息加载到IDE里面,这样,IDE才能识别到开发板。以我们使用的Dragino开发板为例:
具体步骤: 在PC上打开IDE --> 点击File --> preference-->在Additional Boards Manager URLs里添加以下URL:http://www.dragino.com/downloads/downloads/YunShield/package_dragino_yun_test_index.json 转到tools --> Boards --> Boards Manager,找到Dragino信息并安装它 这样,我们的Dragino开发板就成功的关联到了IDE里面,可以打开IDE --> Tools的开发板,就能找到Dragino
- 要使用Lora通信,需要调用Lora的库文件,当然,我们有现成的库可以用。点击下载Radio_Head,将其拷贝到Arduino安装目录下的libraries里面,就可以从IDE当中直接导包,使用里面提供的接口
- 用数据线将开发板连接到电脑,从IDE选择开发板,然后选择的Tools选择端口(开发板连接的端口)。下面是Lora通信的示例代码:
Client:
#include <SPI.h> //lora跟Arduino之间使用的是SPI通信方式 #include <RH_RF95.h> //lora的库文件,提供了关于设置lora发射参数等一系列函数
RH_RF95 rf95; float frequency = 433.0; //在国内,ISM频段只有433MHz可用,这里需要注意,如果我们买的是868M或者915M的lora板子,是无法使用的
void setup() { Serial.begin(9600); //设置波特率,为了Arduino开发板跟电脑端的串口通信使用,有时候使用串口助手发现电脑端显示的全是乱码,就很有可能是波特率设置的不一样。 while (!Serial) ; // Wait for serial port to be available Serial.println("Start LoRa Client"); //串口打印函数 if (!rf95.init()) Serial.println("init failed"); rf95.setFrequency(frequency); // Setup ISM frequency rf95.setTxPower(18); // Setup Power,dBm rf95.setSpreadingFactor(12); // Setup Spreading Factor (6 ~ 12) // Setup BandWidth, option: 7800,10400,15600,20800,31200,41700,62500,125000,250000,500000 //Lower BandWidth for longer distance. rf95.setSignalBandwidth(125000); // Setup Coding Rate:5(4/5),6(4/6),7(4/7),8(4/8) rf95.setCodingRate4(5);
//如果我们需要修改lora发射参数,只需要一行代码,非常方便
} //setup函数体只执行一遍,执行完了过后,开始进入到loop函数里面
void loop() { Serial.println("Sending to LoRa Server"); // Send a message to LoRa Server uint8_t data[] = "Hello, this is device 1"; //lora需要发射的数据,都是存放在一个字符数组里面 rf95.send(data, sizeof(data)); //发送函数 rf95.waitPacketSent(); //等待数据发送完成 // Now wait for a reply uint8_t buf[RH_RF95_MAX_MESSAGE_LEN]; uint8_t len = sizeof(buf); delay(1000); }
- Server:
#include <SPI.h> #include <RH_RF95.h>
RH_RF95 rf95; int led = A2; float frequency = 433.0;
void setup() { pinMode(led, OUTPUT); Serial.begin(9600); while (!Serial) ; // Wait for serial port to be available Serial.println("Start Sketch"); if (!rf95.init()) Serial.println("init failed"); rf95.setFrequency(frequency); rf95.setTxPower(13); // Defaults BW Bw = 125 kHz, Cr = 4/5, Sf = 128chips/symbol, CRC on Serial.print("Listening on frequency: "); Serial.println(frequency); }
void loop() { if (rf95.available()) { uint8_t buf[RH_RF95_MAX_MESSAGE_LEN]; //开辟一个buffer便于接收lora数据 uint8_t len = sizeof(buf); if (rf95.recv(buf, &len)) { digitalWrite(led, HIGH); RH_RF95::printBuffer("request: ", buf, len); Serial.print("got request: "); Serial.println((char*)buf); Serial.print("RSSI: "); Serial.println(rf95.lastRssi(), DEC); //调用现成的接口可以直接获取lora信号的RSSI值 // Send a reply uint8_t data[] = "And hello back to you"; rf95.send(data, sizeof(data)); rf95.waitPacketSent(); Serial.println("Sent a reply"); digitalWrite(led, LOW); } else { Serial.println("recv failed"); } } }