I'm having a problem getting GPS coordinations while sending them using nrf24l01 to another Arduino. The wireless communication works fine, but I can't get a GPS fix. When I don't send any data (I comment this part of code radio.write(&char_array, sizeof(char_array));
) I can get the GPS coords in a few seconds, but not when using the nrf24l01 module.
This is my code:
#include <TinyGPS++.h>
#include <SoftwareSerial.h>
#include <nRF24L01.h>
#include <RF24.h>
//create an RF24 object
RF24 radio(9, 8); // CE, CSN
//address through which two modules communicate.
const byte address[6] = "00001";
int gpsRXPin = 3;
int gpsTXPin = 2;
int GPSBaud = 9600;
float gps_lat = 0;
float gps_lng = 0;
float gps_sat = 0;
const int radiorepeat = 1000;
unsigned long startTime;
TinyGPSPlus gps;
SoftwareSerial gpsSerial(gpsRXPin, gpsTXPin);
void setup() {
Serial.begin(9600);
gpsSerial.begin(GPSBaud);
startTime = millis();
//2,4GHz radio
radio.begin();
radio.openWritingPipe(address);
// radio.setRetries(3,5); // delay, count
radio.stopListening();
}
//sent gps coords over 2.4GHz
void radiosent(String output_message){
//convert string to char array
int str_len = output_message.length() + 1;
char char_array[str_len];
output_message.toCharArray(char_array, str_len);
radio.write(&char_array, sizeof(char_array));
}
void consolelog(){
Serial.println("--------------");
Serial.print("GPS: ");
Serial.print("Lat=");
Serial.print(gps_lat);
Serial.print(" lng=");
Serial.print(gps_lng);
Serial.print(" sat=");
Serial.println(gps_sat);
}
void loop() {
while (gpsSerial.available() > 0){
gps.encode(gpsSerial.read());
if (gps.location.isUpdated()){
gps_lat = gps.location.lat();
gps_lng = gps.location.lng();
gps_sat = gps.satellites.value();
}
}
//prepare string to send over 2.4Ghz (SY1-gps_lat-gps_lng)
String output_message = "SY1-";
output_message = output_message + String(gps_lat, 6);
output_message = output_message + "-";
output_message = output_message + String(gps_lng, 6);
//runs every second
if(millis() > startTime + radiorepeat){
consolelog();
radiosent(output_message);
startTime = millis();
}
}