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...