LilyGo-LoRa-Series/examples/ArduinoLoRa/LoRaReceiver/LoRaReceiver.ino

99 lines
2.4 KiB
Arduino
Raw Normal View History

2024-05-15 17:58:37 +08:00
// Only supports SX1276/SX1278
2022-06-16 14:01:20 +08:00
#include <LoRa.h>
2024-05-15 17:58:37 +08:00
#include "LoRaBoards.h"
2022-06-16 14:01:20 +08:00
2024-08-03 14:19:31 +08:00
#ifndef CONFIG_RADIO_FREQ
2025-05-10 14:39:13 +08:00
#define CONFIG_RADIO_FREQ 868.0
2024-08-03 14:19:31 +08:00
#endif
#ifndef CONFIG_RADIO_OUTPUT_POWER
2025-05-10 14:39:13 +08:00
#define CONFIG_RADIO_OUTPUT_POWER 17
2024-08-03 14:19:31 +08:00
#endif
#ifndef CONFIG_RADIO_BW
2025-05-10 14:39:13 +08:00
#define CONFIG_RADIO_BW 125.0
2024-08-03 14:19:31 +08:00
#endif
#if !defined(USING_SX1276) && !defined(USING_SX1278)
2025-05-10 14:39:13 +08:00
#error "LoRa example is only allowed to run SX1276/78. For other RF models, please run examples/RadioLibExamples
2024-08-03 14:19:31 +08:00
#endif
2022-06-16 14:01:20 +08:00
void setup()
{
2024-05-15 17:58:37 +08:00
setupBoards();
2022-06-16 14:01:20 +08:00
// When the power is turned on, a delay is required.
delay(1500);
Serial.println("LoRa Receiver");
2024-05-15 17:58:37 +08:00
#ifdef RADIO_TCXO_ENABLE
pinMode(RADIO_TCXO_ENABLE, OUTPUT);
digitalWrite(RADIO_TCXO_ENABLE, HIGH);
#endif
2025-05-10 14:25:50 +08:00
#ifdef RADIO_CTRL
Serial.println("Turn on LAN, Enter Rx mode.");
/*
* BPF LoRa LAN Control ,set HIGH turn on LAN ,RX Mode
* */
digitalWrite(RADIO_CTRL, HIGH);
#endif /*RADIO_CTRL*/
2022-12-26 16:53:44 +08:00
LoRa.setPins(RADIO_CS_PIN, RADIO_RST_PIN, RADIO_DIO0_PIN);
2024-08-03 14:19:31 +08:00
if (!LoRa.begin(CONFIG_RADIO_FREQ * 1000000)) {
2022-06-16 14:01:20 +08:00
Serial.println("Starting LoRa failed!");
while (1);
}
2024-08-03 14:19:31 +08:00
LoRa.setTxPower(CONFIG_RADIO_OUTPUT_POWER);
LoRa.setSignalBandwidth(CONFIG_RADIO_BW * 1000);
LoRa.setSpreadingFactor(10);
LoRa.setPreambleLength(16);
LoRa.setSyncWord(0xAB);
LoRa.disableCrc();
LoRa.disableInvertIQ();
LoRa.setCodingRate4(7);
// put the radio into receive mode
LoRa.receive();
2022-06-16 14:01:20 +08:00
}
void loop()
{
// try to parse packet
int packetSize = LoRa.parsePacket();
if (packetSize) {
// received a packet
Serial.print("Received packet '");
String recv = "";
// read packet
while (LoRa.available()) {
recv += (char)LoRa.read();
}
Serial.println(recv);
// print RSSI of packet
Serial.print("' with RSSI ");
Serial.println(LoRa.packetRssi());
if (u8g2) {
u8g2->clearBuffer();
char buf[256];
u8g2->drawStr(0, 12, "Received OK!");
u8g2->drawStr(0, 26, recv.c_str());
snprintf(buf, sizeof(buf), "RSSI:%i", LoRa.packetRssi());
u8g2->drawStr(0, 40, buf);
snprintf(buf, sizeof(buf), "SNR:%.1f", LoRa.packetSnr());
u8g2->drawStr(0, 56, buf);
u8g2->sendBuffer();
}
}
}