GPS Data Communication with RF433

Hello,
I'm currently working on my project about GPS data communication using RF433 module. The problem is the RX doesn't receive any GPS data when I print it on serial monitor.
Here is the code

TX

#include  <TinyGPS.h>
#include  <RH_ASK.h>//including the library
#include  <SPI.h>// this is not used but needed to compile
#include  <SoftwareSerial.h>

TinyGPS gps;
SoftwareSerial nss(0, 1);
RH_ASK driver(2000, 11, 12, 10, false);

struct dataStruct{
  float DATA1 ; 
  float DATA2;
  unsigned long counter;   
} myData;

byte tx_buf[sizeof(myData)] = {0};

char gpsdata[26] ;
int led = 13;
long lat, lon;
float flat, flon;
unsigned long fix_age, time, date, speed, course;
unsigned long chars;
unsigned short sentences, failed_checksum;
//int year;
//byte month, day, hour, minute, second, hundredths;

void setup()
{
  Serial.begin(115200);                                               //PC<---> arduino 115200 baud
      nss.begin(9600);                                                  //GPS---> arduino at 9600 baud   // Debugging only
        if (!driver.init())
         Serial.println("init failed");
  
              pinMode(led, OUTPUT);  
              Serial.print("GPS Testing");
       
                      myData.DATA1=lat; 
                      myData.DATA2=lon;
    
  //delay(2000);
}

void loop()
{
  while (nss.available())                                                //while gps is connected properly & sending data to arduino
 {
   int c = nss.read();                                                  //Read the GPS serial port
   //Serial.write(c);                                                 //Display NMEA data for debugging purpose
   if (gps.encode(c))                        // Check the GPS data
    {
      getprint_pos(gps);
      //debug_check();
      Serial.print("nilai: ");
      Serial.println(flat);
    } 
}
      memcpy(tx_buf, &myData, sizeof(myData) );
      byte zize=sizeof(myData);

      driver.send((uint8_t *)tx_buf, zize);
      driver.waitPacketSent();
      myData.counter++;
}

void getprint_pos(TinyGPS &gps)                                         //function to check & get valid GPS positional data
    { 
       gps.get_position(&lat, &lon, &fix_age);                          //TinyGPS function for aquiring positional information
         if (fix_age == TinyGPS::GPS_INVALID_AGE )                      //Determine the valid data by checking the fix_age
                Serial.println("No fix ever detected!");
          else if (fix_age > 2000)
                  Serial.println("Data is getting STALE!");
            else{
                   Serial.println("\nLatitude and longitude valid!");
                    flat = lat;                                         //put the data into a float integer 
                    flon = lon;                                         //decimal calculations to be done while printing the value
                       Serial.print(flat/1000000, 6);                   //print latitude
                       if(flat>0) 
                          Serial.println("*N"); 
                          else  
                              Serial.println("*S");
                              Serial.print(flon/1000000, 6);            //print longitude
                              if(flon>0) 
                                  Serial.println("*E");
                              else  
                                  Serial.println("*W");
                }
  }
  void debug_check()                                                      //check n print the data to be sent to RX node for debugging
  {  
    sprintf(gpsdata,"%ld,%ld,%s",lat,lon);                              //put these values in gpsdata
    Serial.println(gpsdata);
  }

RX

#include <RH_ASK.h>//including the library
#include <SPI.h>// this is not used but needed to compile

//So arguments are bitrate, receive pin (rx), transmit pin (tx), 
//ppt pin, pttInverted. The last 2 are not used.
RH_ASK driver(2000, 11, 12, 10, false);

void setup()
{
    Serial.begin(9600);  // Debugging only
    if (!driver.init())
         Serial.println("init failed");
}

struct dataStruct{
  float flat ; 
  float flon;
}myData;

void loop()
{
    uint8_t buf[RH_ASK_MAX_MESSAGE_LEN]; //this is the buffer we use to save the received data
    uint8_t buflen = sizeof(buf); //

    if (driver.recv(buf, &buflen)) // Non-blocking
    {
        // Message with a good checksum received, dump it.
        //driver.printBuffer("Got:", buf, buflen);

        memcpy(&myData, buf, sizeof(myData));

        Serial.println("");
        Serial.print("Latitude: ");
        Serial.print(myData.flat/1000000, 6);
        if(myData.flat>0) 
                  Serial.println("*N"); 
                   else  
                       Serial.println("*S");
                        Serial.print("  Longitude: ");
                          Serial.print(myData.flon/1000000, 6);
                            if(myData.flon>0) 
                                      Serial.println("*E");
                                  else  
                                      Serial.println("*W");
                          delay(2000);
        
        /*
        Serial.print("Received: ");
        for (byte i = 0; i < buflen; i++)
        {
          Serial.print(char(buf[i]));
          //Serial.print(buf[i]);
          //Serial.print(buf[i],HEX);
        }
        Serial.println("");
        */
    }
}

The results on the serial monitor for RX would be like this.

Latitude: 0.00
Longitude: 0.00

I got this code from TinyGPS data parsing & viewing it at the RF433 RX node - Programming Questions - Arduino Forum then I modified it. I am not so good in programming so I am perhaps missing something. Please give me guidance and any help would be appreciated.

Thank you very much!

Never mind. I actually have found the solution

I hope it was this:

SoftwareSerial nss(0, 1);    <-- never on HardwareSerial pins 0 & 1

          and

    sprintf(gpsdata,"%ld,%ld,%s",lat,lon);   <-- bad format string

:slight_smile:

BTW, there are several better alternatives to SoftwareSerial. SoftwareSerial is very inefficient, because it disables interrupts for long periods of time. This can interfere with other parts of your sketch or with other libraries. In your sketch, receiving GPS characters could interfere with the RH_ASK library. Each GPS character will block everything else for 1ms.

The alternatives:

  • AltSoftSerial on two specific pins (8 & 9 on an UNO), or

  • NeoSWSerial on any two pins, but only at baud rates 9600, 19200 or 38400.

More description here.

Are you sure you want to send a radio message as fast as you can? This will keep the transmitter on almost constantly, draining a battery. You should probably send one message per second, when new GPS data is available. Here's a version of your sketch that uses NeoGPS to send one message per second:

#include  <RH_ASK.h>
#include  <SPI.h>   // this is not used but needed to compile

#include  <NMEAGPS.h>
NMEAGPS gps;
gps_fix fix;

#include  <AltSoftSerial.h>
AltSoftSerial gpsPort; // two specific pins

//#include <NeoSWSerial.h>  or use this on any two pins
//NeoSWSerial gpsPort( 3, 4 );

RH_ASK driver(2000, 11, 12, 10, false);

struct dataStruct{
  float DATA1 ; 
  float DATA2;
  unsigned long counter;   
} myData;

byte tx_buf[sizeof(myData)] = {0};

const int LED = 13;


void setup()
{
  Serial.begin(115200);          // PC<---> arduino 115200 baud

  gpsPort.begin(9600);           // GPS---> arduino at 9600 baud

  if (!driver.init())
    Serial.println("init failed");

  pinMode(LED, OUTPUT);  
  Serial.print("GPS Testing");

  setData();
}


void loop()
{
  if (gps.available( gpsPort )) 
  {
    fix = gps.read();

    getprint_pos();

    memcpy(tx_buf, &myData, sizeof(myData) );
    byte zize=sizeof(myData);

    driver.send((uint8_t *)tx_buf, zize);
    driver.waitPacketSent();
    myData.counter++;
  }
}


void getprint_pos()           //function to check & get valid GPS positional data
{ 
  if (not fix.valid.location)
    Serial.println("No fix ever detected!");
  else {
    Serial.println("\nLatitude and longitude valid!");
    
    setData();
    debug_check();
  }
}


void setData()
{
  myData.DATA1=fix.latitude ();
  myData.DATA2=fix.longitude();
}


void debug_check()     //check n print the data to be sent to RX node
{  
  Serial.print( fix.latitude(), 6 );
  if (fix.latitude() > 0.0)
    Serial.println("*N"); 
  else  
    Serial.println("*S");

  Serial.print( fix.longitude(), 6);
  if (fix.longitude() > 0.0) 
    Serial.println("*E");
  else  
    Serial.println("*W");
}

It's amazing how white space and indentation can make the sketch easier to read... and to debug. You can press control-T in the IDE editor to format your code automatically.

It uses AltSoftSerial to listen to the GPS device. This loop structure will send the message when the GPS device is quiet for a little while (read more here about the GPS "quiet" time). This helps prevent interference between reading GPS characters and sending radio messages.

All of these libraries are available from the IDE Library Manager, under the menu Sketch -> Include Library -> Manage Libraries.

Cheers,
/dev

hello! can you post the solution? please. thank you!