85 lines
2.7 KiB
C++
85 lines
2.7 KiB
C++
/*
|
|
Heltec.LoRa Duplex communication with Spreading Factor
|
|
|
|
Sends a message every half second, and polls continually
|
|
for new incoming messages. Sets the Heltec.LoRa radio's spreading factor.
|
|
|
|
Spreading factor affects how far apart the radio's transmissions
|
|
are, across the available bandwidth. Radios with different spreading
|
|
factors will not receive each other's transmissions. This is one way you
|
|
can filter out radios you want to ignore, without making an addressing scheme.
|
|
|
|
Spreading factor affects reliability of transmission at high rates, however,
|
|
so avoid a hugh spreading factor when you're sending continually.
|
|
|
|
See the Semtech datasheet, http://www.semtech.com/images/datasheet/sx1276.pdf
|
|
for more on Spreading Factor.
|
|
|
|
by Aaron.Lee from HelTec AutoMation, ChengDu, China
|
|
成都惠利特自动化科技有限公司
|
|
www.heltec.cn
|
|
|
|
this project also realess in GitHub:
|
|
https://github.com/Heltec-Aaron-Lee/WiFi_Kit_series
|
|
*/
|
|
#include "heltec.h"
|
|
#define BAND 433E6 //you can set band here directly,e.g. 868E6,915E6
|
|
|
|
|
|
byte msgCount = 0; // count of outgoing messages
|
|
int interval = 2000; // interval between sends
|
|
long lastSendTime = 0; // time of last packet send
|
|
|
|
void setup()
|
|
{
|
|
//WIFI Kit series V1 not support Vext control
|
|
Heltec.begin(true /*DisplayEnable Enable*/, true /*Heltec.Heltec.LoRa Disable*/, true /*Serial Enable*/, true /*PABOOST Enable*/, BAND /*long BAND*/);
|
|
|
|
|
|
LoRa.setSpreadingFactor(8); // ranges from 6-12,default 7 see API docs
|
|
Serial.println("Heltec.LoRa init succeeded.");
|
|
}
|
|
|
|
void loop()
|
|
{
|
|
if (millis() - lastSendTime > interval)
|
|
{
|
|
String message = "Heltec.LoRa World! "; // send a message
|
|
message += msgCount;
|
|
sendMessage(message);
|
|
Serial.println("Sending " + message);
|
|
lastSendTime = millis(); // timestamp the message
|
|
interval = random(2000) + 1000; // 2-3 seconds
|
|
msgCount++;
|
|
}
|
|
|
|
// parse for a packet, and call onReceive with the result:
|
|
onReceive(LoRa.parsePacket());
|
|
}
|
|
|
|
void sendMessage(String outgoing)
|
|
{
|
|
LoRa.beginPacket(); // start packet
|
|
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:
|
|
String incoming = "";
|
|
|
|
while (LoRa.available())
|
|
{
|
|
incoming += (char)LoRa.read();
|
|
}
|
|
|
|
Serial.println("Message: " + incoming);
|
|
Serial.println("RSSI: " + String(LoRa.packetRssi()));
|
|
Serial.println("Snr: " + String(LoRa.packetSnr()));
|
|
Serial.println();
|
|
}
|