Hi, I recently wanted to connect an ESP32 with an Adafruit Lora RFM9X to receive data from an MKR with Lora and GPS. Searching on the Adafruit site I can't find anything about libraries or codes for Arduino. I was wondering if anyone could explain to me how I can do it or if you already have a working project. Thank you
also have a look at the Arduino LoRa library which supports the RFM9x modules
it the other device a MKRWAN 1300?
There is a LoRa library, which works with RFM9X, that has lots of working examples of using LoRa, including a set of GPS tracker transmitter and receiver examples.
I currently have a mkr that is transmitting gps data and it works with this code:
/* SCRIPT PER VERIFICARE SE IL GPS È SBLOCCATO*/
#include <Arduino_MKRGPS.h>
#include <SPI.h>
#include <SD.h>
#include <Wire.h>
#include <LoRa.h>
float lat_previous = 0;
float long_previous = 0;
float alt_previous = 0;
byte destination = 0x3D;
void setup() {
Serial.begin(9600);
Wire.begin();
if (!GPS.begin(GPS_MODE_SHIELD)) {
Serial.println("Failed to initialize GPS!");
while (1);
}
if (!SD.begin(4)) {
Serial.println("Card failed, or not present");
while (1);
}
if (!LoRa.begin(868E6)) {
Serial.println("Starting LoRa failed!");
while (1);
}
LoRa.setTxPower(17); //17 dB (defoult)(max power)
LoRa.setSpreadingFactor(10); //7 defoult (can be set from 6 to 12)
LoRa.setSignalBandwidth(62.5E3); //125E3 defoult (7.8 10.4 15.6 20.8 31.25 41.7 62.5 125 250 500)
LoRa.setCodingRate4(8); //5 defoult (can be from 5 to 8 that correspond to 4/5 and 4/8)
}
void loop() {
if (GPS.available()) {
// read GPS values
unsigned long time = GPS.getTime();
float latitude = GPS.latitude();
float longitude = GPS.longitude();
float altitude = GPS.altitude();
float speed = GPS.speed();
int satellites = GPS.satellites();
// store GPS value in case GPS signal is lost
lat_previous = latitude;
long_previous = longitude;
alt_previous = altitude;
// print GPS values, IN ORDER AS ABOVE
Serial.print("£ , ");
Serial.print(time);
Serial.print(" , ");
Serial.print(latitude, 7);
Serial.print(" , ");
Serial.print(longitude, 7);
Serial.print(" , ");
Serial.print(altitude);
Serial.print(" , ");
Serial.print(speed);
Serial.print(" , ");
Serial.print(satellites);
Serial.println(" , $");
// store in the SD card
File log = SD.open("datalog.txt", FILE_WRITE);
log.print("£ , ");
log.print(time);
log.print(" , ");
log.print(latitude, 7);
log.print(" , ");
log.print(longitude, 7);
log.print(" , ");
log.print(altitude);
log.print(" , ");
log.print(speed);
log.print(" , ");
log.print(satellites);
log.println(" , $");
log.close();
// transmission of data with LORA
LoRa.beginPacket();
LoRa.write(destination);
LoRa.print("£ , ");
LoRa.print(latitude, 7);
LoRa.print(" , ");
LoRa.print(longitude, 7);
LoRa.print(" , ");
LoRa.print(altitude);
LoRa.print(" , ");
LoRa.print(speed);
LoRa.print(" , $");
LoRa.endPacket();
}
//if GPS is lost, the last known position is sent
// if(!GPS.available()){
// LoRa.beginPacket();
// LoRa.write(destination);
// LoRa.print(" £ , ");
// LoRa.print(lat_previous, 7);
// LoRa.print(" , ");
// LoRa.print(long_previous, 7);
// LoRa.print(" , ");
// LoRa.print(alt_previous);
// LoRa.print(" , $");
// LoRa.endPacket();
// Serial.println("Sent");
// }
}
while as a receiver I have an esp32 connected with an adafruit rfm9x Lora, which seems to work but does not receive anything, the code on the receiver is :
#include <SPI.h>
#include <RH_RF95.h>
#define RFM95_CS 4
#define RFM95_RST 15
#define RFM95_INT 5
#define RF95_FREQ 868.0 // Frequenza di operazione corrispondente al transmitter
RH_RF95 rf95(RFM95_CS, RFM95_INT);
#define LED 13
void setup() {
pinMode(LED, OUTPUT);
pinMode(RFM95_RST, OUTPUT);
digitalWrite(RFM95_RST, HIGH);
Serial.begin(9600);
while (!Serial) {
; // attendi che la porta seriale si connetta. Necessario solo per le schede con USB nativo
}
delay(1000); // aspetta un po' per l'inizializzazione del seriale
Serial.println("Arduino LoRa RX Test!");
digitalWrite(RFM95_RST, LOW);
delay(10);
digitalWrite(RFM95_RST, HIGH);
delay(10);
while (!rf95.init()) {
Serial.println("LoRa radio init failed");
while (1);
}
if (!rf95.setFrequency(RF95_FREQ)) {
Serial.println("setFrequency failed");
while (1);
}
// Configura le impostazioni LoRa per corrispondere al transmitter
rf95.setTxPower(17, false); // 17 dBm
rf95.setSpreadingFactor(10); // SF10
rf95.setSignalBandwidth(62500); // 62.5 kHz
rf95.setCodingRate4(8); // 4/8
Serial.println("LoRa radio init OK!");
}
void loop() {
if (rf95.available()) {
// Dovrebbe esserci un messaggio per noi ora
uint8_t buf[RH_RF95_MAX_MESSAGE_LEN];
uint8_t len = sizeof(buf);
if (rf95.recv(buf, &len)) {
digitalWrite(LED, HIGH);
Serial.print("Ricevuto: ");
for (int i = 0; i < len; i++) { // Stampare tutto il messaggio
Serial.print((char)buf[i]);
}
Serial.println();
digitalWrite(LED, LOW);
} else {
Serial.println("Ricezione fallita");
}
}
}
I only receive this on the serial line:
01:25:03.397 -> Arduino LoRa RX Test!
01:25:03.475 -> LoRa radio init OK!
what am I doing wrong ? can you give me a hand please? I'm not good at these modules...
This should set the frequency:
if (!LoRa.begin(868E6)) {
Try disconnecting the SD card module, and removing all the SD code. Many SD modules do not share the SPI bus correctly, and that could prevent the transmitter from functioning properly.
You would be better off using identical libraries for the LoRa radios on the two platforms. All modulation parameters must be set identically for successful radio communications.
how do you know it is transmitting if
the receiver is not receiving?
agree with @jremington regarding using the identical libraries for both LoRa modules - getting different LoRa modules to communicate can be difficult even when using identical libraries
the SD is not a problem and I also know that the transmission from the Lora of the MKR works because I have already done a test with another Lora MKR which receives with this code:
#include <SPI.h>
#include <LoRa.h>
byte localAddress = 0x5D;
void setup() {
Serial.begin(115200);
if (!LoRa.begin(868E6)) {
Serial.println("Starting LoRa failed!");
while (1);
}
LoRa.setTxPower(17); //17 dB (defoult)(max power)
LoRa.setSpreadingFactor(10); //7 defoult (can be set from 6 to 12)
LoRa.setSignalBandwidth(62.5E3); //125E3 defoult (7.8 10.4 15.6 20.8 31.25 41.7 62.5 125 250 500)
LoRa.setCodingRate4(8); //5 defoult (can be from 5 to 8 that correspond to 4/5 and 4/8)
}
void loop() {
// try to parse packet
int packetSize = LoRa.parsePacket();
int password = LoRa.read();
if (packetSize) {
if (password != localAddress) {
return;
}
// received a packet
// read packet
while (LoRa.available()) {
Serial.print((char)LoRa.read());
}
// print information of packet
Serial.println("");
// Serial.print(LoRa.packetRssi());
// Serial.print(" , ");
// Serial.print(LoRa.packetSnr());
// Serial.print(" , ");
// Serial.println(packetSize);
}}
so the only problem is the Adafruit RFM95 module set incorrectly and I don't know what I'm doing wrong, it seems to me that I've set everything correctly but it doesn't receive... if I used this code on the Adafruit I'm wondering how to specify the Adafruit pins instead (since in the Lora MKR it is not needed as it is internal to the board, the library works as shown without setting anything regarding Pin CS or INT, RST etc.
if I try to load the working code of the receiver directly onto the ESP32 + Adafruit Lora RFM95 I get this error:
22:58:57.196 -> rst:0x8 (TG1WDT_SYS_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
22:58:57.196 -> configsip: 0, SPIWP:0xee
22:58:57.196 -> clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
22:58:57.196 -> mode:DIO, clock div:1
22:58:57.196 -> load:0x3fff0030,len:1184
22:58:57.196 -> load:0x40078000,len:13260
22:58:57.196 -> load:0x40080400,len:3028
22:58:57.244 -> entry 0x400805e4
22:58:57.957 -> ets Jul 29 2019 12:21:46
Adafruit posts working examples for all their radios. Study those carefully. I've lately been using the Feather M0 LoRa module and strongly recommend it.
As others have pointed out, it can be very difficult to mix LoRa modules and libraries, so your problems are not a surprise. It is also extremely unlikely that any forum members have your particular combination of parts, so they are not able to repeat your experiments.
Probably a reboot due to inadequate power supply.
I solved it and was able to make the Adafruit RFM95 work by adding only these parts to the receiver:
const int ss = 4; // Pin CS (Chip Select)
const int rst = 15; // Pin RST (Reset)
const int dio0 = 5; // Pin IRQ (Interrupt Request)
// Inizializza il modulo LoRa con i pin specifici
LoRa.setPins(ss, rst, dio0);
I send the complete working code:
#include <SPI.h>
#include <LoRa.h>
// Definisci i pin per il modulo LoRa
const int ss = 5; // Pin CS (Chip Select)
const int rst = 14; // Pin RST (Reset)
const int dio0 = 2; // Pin IRQ (Interrupt Request)
void setup() {
Serial.begin(115200);
while (!Serial);
// Inizializza il modulo LoRa con i pin specifici
LoRa.setPins(ss, rst, dio0);
if (!LoRa.begin(868E6)) {
Serial.println("Starting LoRa failed!");
while (1);
}
LoRa.setTxPower(17);
LoRa.setSpreadingFactor(10);
LoRa.setSignalBandwidth(62.5E3);
LoRa.setCodingRate4(8);
}
void loop() {
// il resto del tuo codice
}
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.