Skip to content

Instantly share code, notes, and snippets.

@MZachmann
Last active March 16, 2018 02:13
Show Gist options
  • Save MZachmann/d9f0e640c2b91fd17918e3c0fc3cdbf8 to your computer and use it in GitHub Desktop.
Save MZachmann/d9f0e640c2b91fd17918e3c0fc3cdbf8 to your computer and use it in GitHub Desktop.
LoRa High Level Examples using LightLora
include "LoraUtil.h"
unsigned long _SendTime = 0;
unsigned long _SendInterval = 10000; // 10 seconds allowed for send
int packetnum = 0; // packet counter
setup()
{
_Lru = new LoraUtil();
}
loop()
{
// Send a packet if we've not received anything in too long
if ((millis() - _SendTime) > _SendInterval)
{
// Send a message
_Lru->sendString("Wake up!"); // ping
_SendTime = millis();
}
// Do we have a packet available?
if (_Lru->isPacketAvailable())
{
// Should be a message for us now
LoraPacket* pkt = _Lru->readPacket();
if(pkt != NULL)
{
// print what we've received
String reply = pkt->msgTxt;
String getrssi = "RS:" + String(pkt->rssi) + "_" + String(pkt->snr);
delete pkt; // we own it... so delete it
Serial.println("Got packet: " + reply); // show reply
Serial.println(getrssi); // and stats
// send a reply
delay(200); // let the receiver power up
_Lru->sendString(getrssi); // pong
_SendTime = millis();
Serial.println("Waiting for reply...");
packetnum++;
}
else
{
Serial.println("Receive failed");
}
}
else
{
delay(50); // next loop, nothing's happening
}
}
import time
from LightLora import lorautil
# to start running a loop test. Ctrl-C to stop.
_Lru = lorautil.LoraUtil() # the LoraUtil object
_SendTime = 0
_SendInterval = 10000 # 10 seconds allowed for send
packetnum = 0 # packet counter
while True:
if (time.time() - _SendTime) > _SendInterval:
_Lru.sendPacket(0xff, 0x41, 'Wake up!'.encode())
_SendTime = time.time()
if _Lru.isPacketAvailable():
pkt = None
try:
pkt = _Lru.readPacket()
if pkt and pkt.msgTxt:
getrssi = 'RS:' + str(pkt.rssi) + '_' + str(pkt.snr)
_Lru.sendPacket(0xff, 0x41, getrssi.encode())
_SendTime = time.time()
print('Waiting for reply')
packetnum = packetnum + 1
except Exception as ex:
print(str(ex))
else:
time.sleep(.05)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment