Difference between revisions of "Tutorial:LoRa"
From MobiNetS
(→配置环境) |
|||
Line 7: | Line 7: | ||
*'''Lora''' | *'''Lora''' | ||
=== | ===Configuration Environment=== | ||
====Lora通信实例==== | ====Lora通信实例==== | ||
*使用Arduino进行Lora开发,首先需要环境配置: | |||
# | #下载和安装Arduino IDE [[https://www.arduino.cc/en/Main/Software Arduino下载]] | ||
# | # | ||
# | # | ||
# | # | ||
*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"); | |||
} | |||
} | |||
} | |||
===Transmit a package=== | ===Transmit a package=== |
Revision as of 21:09, 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
Configuration Environment
Lora通信实例
- 使用Arduino进行Lora开发,首先需要环境配置:
- 下载和安装Arduino IDE [Arduino下载]
- 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"); } } }