Last active
September 11, 2019 07:53
-
-
Save jenschr/ec45a7b27b5d16897f1d5a8a61653312 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
LoRa Duplex communication | |
Sends a message every half second, and polls continually | |
for new incoming messages. Implements a one-byte addressing scheme, | |
with 0xFF as the broadcast address. | |
Uses readString() from Stream class to read payload. The Stream class' | |
timeout may affect other functuons, like the radio's callback. For an | |
created 28 April 2017 | |
by Tom Igoe | |
*/ | |
#include <SPI.h> // include libraries | |
#include <LoRa.h> | |
#define SCK 5 // GPIO5 -- SX1278's SCK | |
#define MISO 19 // GPIO19 -- SX1278's MISO | |
#define MOSI 27 // GPIO27 -- SX1278's MOSI | |
#define SS 18 // GPIO18 -- SX1278's CS | |
#define RST 14 // GPIO14 -- SX1278's RESET | |
#define DI0 26 // GPIO26 -- SX1278's IRQ(Interrupt Request) | |
const long frequency = 866E6; // LoRa Frequency | |
String outgoing; // outgoing message | |
byte msgCount = 0; // count of outgoing messages | |
byte localAddress = 0xB2; // address of this device | |
byte destination = 0xFF; // destination to send to | |
long lastSendTime = 0; // last send time | |
int interval = 5000; // interval between sends | |
void setup() { | |
Serial.begin(9600); // initialize serial | |
while (!Serial); | |
Serial.println("LoRa Duplex"); | |
SPI.begin(SCK, MISO, MOSI, SS); | |
LoRa.setPins(SS, RST, DI0); | |
if (!LoRa.begin(frequency)) { // initialize ratio at 915 MHz | |
Serial.println("LoRa init failed. Check your connections."); | |
while (true); // if failed, do nothing | |
} | |
Serial.println("LoRa init succeeded."); | |
Serial.print("My address: 0x"); | |
Serial.print(localAddress, HEX); | |
} | |
void loop() { | |
if (millis() - lastSendTime > interval) { | |
String message = "HeLoRa World!"; // send a message | |
sendMessage(message); | |
Serial.println("Sending " + message); | |
lastSendTime = millis(); // timestamp the message | |
interval = random(2000) + 6000; // 2-3 seconds | |
} | |
// parse for a packet, and call onReceive with the result: | |
onReceive(LoRa.parsePacket()); | |
} | |
void sendMessage(String outgoing) { | |
LoRa.beginPacket(); // start packet | |
LoRa.write(destination); // add destination address | |
LoRa.write(localAddress); // add sender address | |
LoRa.write(msgCount); // add message ID | |
LoRa.write(outgoing.length()); // add payload length | |
LoRa.print(outgoing); // add payload | |
LoRa.endPacket(); // finish packet and send it | |
msgCount++; // increment message ID | |
} | |
void onReceive(int packetSize) { | |
if (packetSize == 0) return; // if there's no packet, return | |
// read packet header bytes: | |
byte recipient = LoRa.read(); // recipient address | |
byte sender = LoRa.read(); // sender address | |
byte incomingMsgId = LoRa.read(); // incoming msg ID | |
byte incomingLength = LoRa.read(); // incoming msg length | |
String incoming = ""; | |
while (LoRa.available()) { | |
incoming += (char)LoRa.read(); | |
} | |
if (incomingLength != incoming.length()) { // check length for error | |
Serial.println("error: message length does not match length"); | |
return; // skip rest of function | |
} | |
// if the recipient isn't this device or broadcast, | |
if (recipient == localAddress && sender == destination ) { | |
// if message is for this device, or broadcast, print details: | |
Serial.println("Received from: 0x" + String(sender, HEX)); | |
Serial.println("Sent to: 0x" + String(recipient, HEX)); | |
Serial.println("Message ID: " + String(incomingMsgId)); | |
Serial.println("Message length: " + String(incomingLength)); | |
Serial.println("Message: " + incoming); | |
Serial.println("RSSI: " + String(LoRa.packetRssi())); | |
Serial.println("Snr: " + String(LoRa.packetSnr())); | |
Serial.println(); | |
} else { | |
Serial.println("This message is not for me."); | |
return; // skip rest of function | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment