1
0
mirror of https://github.com/sandeepmistry/arduino-LoRa.git synced 2025-04-19 13:02:14 +03:00

Some tweaks to new examples

This commit is contained in:
Sandeep Mistry 2017-05-28 18:13:21 -04:00
parent b894e71202
commit 21ace81a59
5 changed files with 97 additions and 71 deletions

View File

@ -1,26 +1,30 @@
/* /*
LoRa register dump LoRa register dump
context: Arduino
This examples shows how to inspect and output the LoRa radio's
registers on the Serial interface
*/ */
#include <SPI.h> // include libraries #include <SPI.h> // include libraries
#include <LoRa.h> #include <LoRa.h>
void setup() { void setup() {
while (!Serial);
Serial.begin(9600); // initialize serial Serial.begin(9600); // initialize serial
Serial.println("LoRa Receiver"); while (!Serial);
LoRa.setPins(7, 6, 1); // set CS, reset, IRQ pin
if (!LoRa.begin(915E6)) { // initialize ratio at 915Mhz Serial.println("LoRa Dump Registers");
// override the default CS, reset, and IRQ pins (optional)
// LoRa.setPins(7, 6, 1); // set CS, reset, IRQ pin
if (!LoRa.begin(915E6)) { // initialize ratio at 915 MHz
Serial.println("LoRa init failed. Check your connections."); Serial.println("LoRa init failed. Check your connections.");
while (true); // if failed, do nothing while (true); // if failed, do nothing
} }
LoRa.sleep();
LoRa.dumpRegisters(Serial); LoRa.dumpRegisters(Serial);
} }
void loop() { void loop() {
} }

View File

@ -13,11 +13,13 @@
*/ */
#include <SPI.h> // include libraries #include <SPI.h> // include libraries
#include <LoRa.h> #include <LoRa.h>
const int csPin = 7; // LoRa radio chip select const int csPin = 7; // LoRa radio chip select
const int resetPin = 6; // LoRa radio reset const int resetPin = 6; // LoRa radio reset
const int irqPin = 1; // change for your board; must be a hardware interrupt pin const int irqPin = 1; // change for your board; must be a hardware interrupt pin
String outgoing; // outgoing message String outgoing; // outgoing message
byte msgCount = 0; // count of outgoing messages byte msgCount = 0; // count of outgoing messages
byte localAddress = 0xBB; // address of this device byte localAddress = 0xBB; // address of this device
byte destination = 0xFF; // destination to send to byte destination = 0xFF; // destination to send to
@ -25,32 +27,33 @@ long lastSendTime = 0; // last send time
int interval = 2000; // interval between sends int interval = 2000; // interval between sends
void setup() { void setup() {
while (!Serial);
Serial.begin(9600); // initialize serial Serial.begin(9600); // initialize serial
while (!Serial);
Serial.println("LoRa Duplex"); Serial.println("LoRa Duplex");
// override the default CS, reset, and IRQ pins (optional)
LoRa.setPins(csPin, resetPin, irqPin);// set CS, reset, IRQ pin LoRa.setPins(csPin, resetPin, irqPin);// set CS, reset, IRQ pin
if (!LoRa.begin(915E6)) { // initialize ratio at 915Mhz if (!LoRa.begin(915E6)) { // initialize ratio at 915 MHz
Serial.println("LoRa init failed. Check your connections."); Serial.println("LoRa init failed. Check your connections.");
while (true); // if failed, do nothing while (true); // if failed, do nothing
} }
LoRa.setTimeout(10); // set Stream timeout of 10ms Serial.println("LoRa init succeeded.");
Serial.println("LoRa init succeeded.");
} }
void loop() { void loop() {
if (millis() - lastSendTime > interval) { if (millis() - lastSendTime > interval) {
String message = "HeLoRa World!"; // send a message String message = "HeLoRa World!"; // send a message
sendMessage(message); sendMessage(message);
Serial.println("Sending " + message); Serial.println("Sending " + message);
lastSendTime = millis(); // timestamp the message lastSendTime = millis(); // timestamp the message
interval = random(2000) + 1000; // 2-3 seconds interval = random(2000) + 1000; // 2-3 seconds
} }
// parse for a packet, and call onReceive with the result: // parse for a packet, and call onReceive with the result:
onReceive(LoRa.parsePacket()); onReceive(LoRa.parsePacket());
} }
void sendMessage(String outgoing) { void sendMessage(String outgoing) {
@ -64,7 +67,6 @@ void sendMessage(String outgoing) {
msgCount++; // increment message ID msgCount++; // increment message ID
} }
void onReceive(int packetSize) { void onReceive(int packetSize) {
if (packetSize == 0) return; // if there's no packet, return if (packetSize == 0) return; // if there's no packet, return
@ -73,10 +75,13 @@ void onReceive(int packetSize) {
byte sender = LoRa.read(); // sender address byte sender = LoRa.read(); // sender address
byte incomingMsgId = LoRa.read(); // incoming msg ID byte incomingMsgId = LoRa.read(); // incoming msg ID
byte incomingLength = LoRa.read(); // incoming msg length byte incomingLength = LoRa.read(); // incoming msg length
String incoming = ""; String incoming = "";
while (LoRa.available()) { while (LoRa.available()) {
incoming += (char)LoRa.read(); incoming += (char)LoRa.read();
} }
if (incomingLength != incoming.length()) { // check length for error if (incomingLength != incoming.length()) { // check length for error
Serial.println("error: message length does not match length"); Serial.println("error: message length does not match length");
return; // skip rest of function return; // skip rest of function
@ -89,13 +94,13 @@ void onReceive(int packetSize) {
} }
// if message is for this device, or broadcast, print details: // if message is for this device, or broadcast, print details:
Serial.println("Received from:" + String(sender, HEX)); Serial.println("Received from: 0x" + String(sender, HEX));
Serial.println("Sent to:" + String(recipient, HEX)); Serial.println("Sent to: 0x" + String(recipient, HEX));
Serial.println("Message ID:" + String(incomingMsgId)); Serial.println("Message ID: " + String(incomingMsgId));
Serial.println("Message length:" + String(incomingLength)); Serial.println("Message length: " + String(incomingLength));
Serial.println("Message:" + incoming); Serial.println("Message: " + incoming);
Serial.println("RSSI:" + String(LoRa.packetRssi())); Serial.println("RSSI: " + String(LoRa.packetRssi()));
Serial.println("Snr:" + String(LoRa.packetSnr())); Serial.println("Snr: " + String(LoRa.packetSnr()));
Serial.println(); Serial.println();
} }

View File

@ -14,6 +14,7 @@
*/ */
#include <SPI.h> // include libraries #include <SPI.h> // include libraries
#include <LoRa.h> #include <LoRa.h>
const int csPin = 7; // LoRa radio chip select const int csPin = 7; // LoRa radio chip select
const int resetPin = 6; // LoRa radio reset const int resetPin = 6; // LoRa radio reset
const int irqPin = 1; // change for your board; must be a hardware interrupt pin const int irqPin = 1; // change for your board; must be a hardware interrupt pin
@ -26,12 +27,15 @@ long lastSendTime = 0; // last send time
int interval = 2000; // interval between sends int interval = 2000; // interval between sends
void setup() { void setup() {
while (!Serial);
Serial.begin(9600); // initialize serial Serial.begin(9600); // initialize serial
while (!Serial);
Serial.println("LoRa Duplex with callback"); Serial.println("LoRa Duplex with callback");
// override the default CS, reset, and IRQ pins (optional)
LoRa.setPins(csPin, resetPin, irqPin);// set CS, reset, IRQ pin LoRa.setPins(csPin, resetPin, irqPin);// set CS, reset, IRQ pin
if (!LoRa.begin(915E6)) { // initialize ratio at 915Mhz if (!LoRa.begin(915E6)) { // initialize ratio at 915 MHz
Serial.println("LoRa init failed. Check your connections."); Serial.println("LoRa init failed. Check your connections.");
while (true); // if failed, do nothing while (true); // if failed, do nothing
} }
@ -42,14 +46,14 @@ void setup() {
} }
void loop() { void loop() {
if (millis() - lastSendTime > interval) { if (millis() - lastSendTime > interval) {
String message = "HeLoRa World!"; // send a message String message = "HeLoRa World!"; // send a message
sendMessage(message); sendMessage(message);
Serial.println("Sending " + message); Serial.println("Sending " + message);
lastSendTime = millis(); // timestamp the message lastSendTime = millis(); // timestamp the message
interval = random(2000) + 1000; // 2-3 seconds interval = random(2000) + 1000; // 2-3 seconds
LoRa.receive(); // go back into receive mode LoRa.receive(); // go back into receive mode
} }
} }
void sendMessage(String outgoing) { void sendMessage(String outgoing) {
@ -63,7 +67,6 @@ void sendMessage(String outgoing) {
msgCount++; // increment message ID msgCount++; // increment message ID
} }
void onReceive(int packetSize) { void onReceive(int packetSize) {
if (packetSize == 0) return; // if there's no packet, return if (packetSize == 0) return; // if there's no packet, return
@ -72,10 +75,13 @@ void onReceive(int packetSize) {
byte sender = LoRa.read(); // sender address byte sender = LoRa.read(); // sender address
byte incomingMsgId = LoRa.read(); // incoming msg ID byte incomingMsgId = LoRa.read(); // incoming msg ID
byte incomingLength = LoRa.read(); // incoming msg length byte incomingLength = LoRa.read(); // incoming msg length
String incoming = ""; // payload of packet String incoming = ""; // payload of packet
while (LoRa.available()) { // can't use readString() in callback, so while (LoRa.available()) { // can't use readString() in callback, so
incoming += (char)LoRa.read(); // add bytes one by one incoming += (char)LoRa.read(); // add bytes one by one
} }
if (incomingLength != incoming.length()) { // check length for error if (incomingLength != incoming.length()) { // check length for error
Serial.println("error: message length does not match length"); Serial.println("error: message length does not match length");
return; // skip rest of function return; // skip rest of function
@ -88,13 +94,13 @@ void onReceive(int packetSize) {
} }
// if message is for this device, or broadcast, print details: // if message is for this device, or broadcast, print details:
Serial.println("Received from:" + String(sender, HEX)); Serial.println("Received from: 0x" + String(sender, HEX));
Serial.println("Sent to:" + String(recipient, HEX)); Serial.println("Sent to: 0x" + String(recipient, HEX));
Serial.println("Message ID:" + String(incomingMsgId)); Serial.println("Message ID: " + String(incomingMsgId));
Serial.println("Message length:" + String(incomingLength)); Serial.println("Message length: " + String(incomingLength));
Serial.println("Message:" + incoming); Serial.println("Message: " + incoming);
Serial.println("RSSI:" + String(LoRa.packetRssi())); Serial.println("RSSI: " + String(LoRa.packetRssi()));
Serial.println("Snr:" + String(LoRa.packetSnr())); Serial.println("Snr: " + String(LoRa.packetSnr()));
Serial.println(); Serial.println();
} }

View File

@ -12,7 +12,7 @@
Spreading factor affects reliability of transmission at high rates, however, Spreading factor affects reliability of transmission at high rates, however,
so avoid a hugh spreading factor when you're sending continually. so avoid a hugh spreading factor when you're sending continually.
See the Semtech datasheete, http://www.semtech.com/images/datasheet/sx1276.pdf See the Semtech datasheet, http://www.semtech.com/images/datasheet/sx1276.pdf
for more on Spreading Factor. for more on Spreading Factor.
created 28 April 2017 created 28 April 2017
@ -20,6 +20,7 @@
*/ */
#include <SPI.h> // include libraries #include <SPI.h> // include libraries
#include <LoRa.h> #include <LoRa.h>
const int csPin = 7; // LoRa radio chip select const int csPin = 7; // LoRa radio chip select
const int resetPin = 6; // LoRa radio reset const int resetPin = 6; // LoRa radio reset
const int irqPin = 1; // change for your board; must be a hardware interrupt pin const int irqPin = 1; // change for your board; must be a hardware interrupt pin
@ -29,15 +30,19 @@ int interval = 2000; // interval between sends
long lastSendTime = 0; // time of last packet send long lastSendTime = 0; // time of last packet send
void setup() { void setup() {
while (!Serial);
Serial.begin(9600); // initialize serial Serial.begin(9600); // initialize serial
Serial.println("LoRa Duplex"); while (!Serial);
LoRa.setPins(csPin, resetPin, irqPin);// set CS, reset, IRQ pin
if (!LoRa.begin(915E6)) { // initialize ratio at 915Mhz Serial.println("LoRa Duplex - Set spreading factor");
// override the default CS, reset, and IRQ pins (optional)
LoRa.setPins(csPin, resetPin, irqPin); // set CS, reset, IRQ pin
if (!LoRa.begin(915E6)) { // initialize ratio at 915 MHz
Serial.println("LoRa init failed. Check your connections."); Serial.println("LoRa init failed. Check your connections.");
while (true); // if failed, do nothing while (true); // if failed, do nothing
} }
LoRa.setSpreadingFactor(8); // ranges from 6-12,default 7 see API docs LoRa.setSpreadingFactor(8); // ranges from 6-12,default 7 see API docs
Serial.println("LoRa init succeeded."); Serial.println("LoRa init succeeded.");
} }
@ -64,18 +69,19 @@ void sendMessage(String outgoing) {
msgCount++; // increment message ID msgCount++; // increment message ID
} }
void onReceive(int packetSize) { void onReceive(int packetSize) {
if (packetSize == 0) return; // if there's no packet, return if (packetSize == 0) return; // if there's no packet, return
// read packet header bytes: // read packet header bytes:
String incoming = ""; String incoming = "";
while (LoRa.available()) { while (LoRa.available()) {
incoming += (char)LoRa.read(); incoming += (char)LoRa.read();
} }
Serial.println("Message:" + incoming);
Serial.println("RSSI:" + String(LoRa.packetRssi())); Serial.println("Message: " + incoming);
Serial.println("Snr:" + String(LoRa.packetSnr())); Serial.println("RSSI: " + String(LoRa.packetRssi()));
Serial.println("Snr: " + String(LoRa.packetSnr()));
Serial.println(); Serial.println();
} }

View File

@ -25,16 +25,20 @@ int interval = 2000; // interval between sends
long lastSendTime = 0; // time of last packet send long lastSendTime = 0; // time of last packet send
void setup() { void setup() {
while (!Serial);
Serial.begin(9600); // initialize serial Serial.begin(9600); // initialize serial
Serial.println("LoRa Duplex"); while (!Serial);
Serial.println("LoRa Duplex - Set sync word");
// override the default CS, reset, and IRQ pins (optional)
LoRa.setPins(csPin, resetPin, irqPin);// set CS, reset, IRQ pin LoRa.setPins(csPin, resetPin, irqPin);// set CS, reset, IRQ pin
if (!LoRa.begin(915E6)) { // initialize ratio at 915Mhz if (!LoRa.begin(915E6)) { // initialize ratio at 915 MHz
Serial.println("LoRa init failed. Check your connections."); Serial.println("LoRa init failed. Check your connections.");
while (true); // if failed, do nothing while (true); // if failed, do nothing
} }
LoRa.setSyncWord(F3); // ranges from 0-0xFF, default 0x34, see API docs
LoRa.setSyncWord(0xF3); // ranges from 0-0xFF, default 0x34, see API docs
Serial.println("LoRa init succeeded."); Serial.println("LoRa init succeeded.");
} }
@ -60,18 +64,19 @@ void sendMessage(String outgoing) {
msgCount++; // increment message ID msgCount++; // increment message ID
} }
void onReceive(int packetSize) { void onReceive(int packetSize) {
if (packetSize == 0) return; // if there's no packet, return if (packetSize == 0) return; // if there's no packet, return
// read packet header bytes: // read packet header bytes:
String incoming = ""; String incoming = "";
while (LoRa.available()) { while (LoRa.available()) {
incoming += (char)LoRa.read(); incoming += (char)LoRa.read();
} }
Serial.println("Message:" + incoming);
Serial.println("RSSI:" + String(LoRa.packetRssi())); Serial.println("Message: " + incoming);
Serial.println("Snr:" + String(LoRa.packetSnr())); Serial.println("RSSI: " + String(LoRa.packetRssi()));
Serial.println("Snr: " + String(LoRa.packetSnr()));
Serial.println(); Serial.println();
} }