Difference between revisions of "Tutorial:LoRa"
From MobiNetS
Line 2: | Line 2: | ||
===Platform and programming environments=== | ===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). | *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). | ||
*[http://mobinets.org/index.php?title=Tutorial:Arduino&action=edit&redlink=1 More about '''Arduino'''] | *[http://mobinets.org/index.php?title=Tutorial:Arduino&action=edit&redlink=1 More about '''Arduino'''] | ||
*'''Lora''' | *'''Lora''' | ||
====Lora通信实例==== | ====Lora通信实例==== |
Revision as of 22:39, 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
- 用数据线将开发板连接到电脑,从IDE选择开发板,然后选择的Tools选择端口(开发板连接的端口)。下面是Lora通信的示例代码:
Client:
#include <SPI.h> #include <RH_RF95.h>
RH_RF95 rf95; float frequency = 433.0;
void setup() { Serial.begin(9600); while (!Serial) ; // Wait for serial port to be available Serial.println("Start LoRa Client"); if (!rf95.init()) Serial.println("init failed"); // Setup ISM frequency rf95.setFrequency(frequency); // Setup Power,dBm rf95.setTxPower(18); // Setup Spreading Factor (6 ~ 12) rf95.setSpreadingFactor(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); }
void loop() { Serial.println("Sending to LoRa Server"); // Send a message to LoRa Server uint8_t data[] = "Hello, this is device 1"; 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"); // Setup ISM frequency rf95.setFrequency(frequency); // Setup Power,dBm 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()) { // Should be a message for us now uint8_t buf[RH_RF95_MAX_MESSAGE_LEN]; 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); // 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"); } } }