Transmitter Code:
//config
#include <SPI.h>
#include <LoRa.h>
#include <Wire.h>
#include <RTClib.h>
// Pin setup
//#define LORA_SCK 18 //lora SCK pin
//#define LORA_MISO 19 //lora MISO pin
//#define LORA_MOSI 23 //lora MOSI pin
#define LORA_CS 5 //lora NSS pin
#define LORA_DIO0 26 //lora DIO0 pin
#define LORA_RST 27 //lora RESET pin
int counter = 0;
void setup()
{
Serial.begin(115200);
delay(1000);
Serial.println("Boot starting...");
// Initialize LoRa
LoRa.setPins(LORA_CS, LORA_RST, LORA_DIO0);
if (!LoRa.begin(433E6)) // Set frequency to 915MHz =>915E6
{
Serial.println("LoRa init failed!");
while (1);
}
//LoRa.setSyncWord(0xF3);
Serial.println("LoRa init success!");
}
void loop()
{
// Transmitting a message
Serial.print("::Sending packet::");
Serial.println(counter);
String message = "Hello";
//Send LoRa packet to receiver
LoRa.beginPacket();
LoRa.print(message);
LoRa.print(counter);
LoRa.endPacket();
Serial.println(message +" "+ counter);
counter++;
delay(5000); //Send message after 10 seconds delay
}
Transmitter Output:
14:43:48.175 -> ::Sending packet::42
14:43:48.175 -> Hello42
14:43:53.205 -> ::Sending packet::43
14:43:53.205 -> Hello43
14:43:58.236 -> ::Sending packet::44
14:43:58.236 -> Hello44
14:44:03.237 -> ::Sending packet::45
14:44:03.294 -> Hello45
14:44:08.298 -> ::Sending packet::46
14:44:08.298 -> Hello46
14:44:13.321 -> ::Sending packet::47
14:44:13.368 -> Hello47
14:44:18.352 -> ::Sending packet::48
14:44:18.400 -> Hello48
14:44:23.389 -> ::Sending packet::49
14:44:23.389 -> Hello49
14:44:28.402 -> ::Sending packet::50
14:44:28.435 -> Hello50
Receiver Code:
#include <SPI.h>
#include <LoRa.h>
//define the pins used by the transceiver module
#define LORA_CS 5 //lora NSS pin
#define LORA_DIO0 GPIO_NUM_26 //lora DIO0 pin
#define LORA_RST 27 //lora RESET pin
void setup() {
Serial.begin(115200);
delay(5000);
Serial.println("\n\nESP32 LoRa RFM996 Receiver");
// void setPins(int ss = LORA_DEFAULT_SS_PIN, int reset = LORA_DEFAULT_RESET_PIN, int dio0 = LORA_DEFAULT_DIO0_PIN);
//LoRa.setPins(8, 4, 7); // for Lora 32u4
LoRa.setPins(5, 27, 26);
if (!LoRa.begin(433E6)) {
Serial.println("Starting LoRa failed!");
while (1)
;
}
Serial.println("Starting LoRa OK!");
}
// enable LoRa receiver, goto light sleep on wakeup read packet
void loop() {
LoRa.receive();
// set GPIO pin to wake on high pulse from RFM95 DIO0
gpio_wakeup_enable(LORA_DIO0, GPIO_INTR_HIGH_LEVEL);
esp_sleep_enable_gpio_wakeup();
Serial.println("goto light sleep");
delay(1000);
esp_light_sleep_start(); // light sleep!!
Serial.begin(115200);
while (!Serial) { delay(500); }
Serial.println("wakeup from ligh sleep");
// now read received packet
int packetSize = LoRa.parsePacket(); // packet available
if (packetSize) {
Serial.print("Received packet '");
while (LoRa.available()) { // yes, read it
Serial.print((char)LoRa.read());
}
// print RSSI of packet
Serial.print("' with RSSI ");
Serial.println(LoRa.packetRssi());
}
}
Receiver Output:
14:42:00.999 ->
14:42:00.999 -> rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
14:42:00.999 -> configsip: 0, SPIWP:0xee
14:42:00.999 -> clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
14:42:00.999 -> mode:DIO, clock div:1
14:42:00.999 -> load:0x3fff0030,len:4832
14:42:00.999 -> load:0x40078000,len:16460
14:42:00.999 -> load:0x40080400,len:4
14:42:00.999 -> load:0x40080404,len:3504
14:42:00.999 -> entry 0x400805cc
14:42:06.155 ->
14:42:06.155 ->
14:42:06.155 -> ESP32 LoRa RFM996 Receiver
14:42:06.155 -> Starting LoRa OK!
14:42:06.155 -> goto light sleep