Problem with coding for sending a message

Hey everyone,
Please see the attached code.
I'm trying to 'transmit' GPS data being received to my receiver which is connected to second arduino.
I've written comment on the place I'm stuck at, please have a look and let me know how can I fix it or if there's an alternative way to transmit the data to the receiver.
Thanks.

#include <VirtualWire.h>
#undef int
#undef abs
#undef double
#undef float
#undef round
#include <SoftwareSerial.h>
#define PMTK_SET_NMEA_UPDATE_1HZ "$PMTK220,10001F" //GPS Speed
#define PMTK_SET_NMEA_OUTPUT_ALLDATA "$PMTK314,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0
28" // GPS Data
#define rxPin 10
#define txPin 7
String message;
char sentvalues[27];
SoftwareSerial mySerial(rxPin, txPin);

void setup()
{
Serial.begin(9600); // Debugging only

Serial.println("UP501 NMEA test!");

Serial.println("setup");

pinMode(rxPin, INPUT);
pinMode(txPin, OUTPUT);
mySerial.begin(9600);
mySerial.println(PMTK_SET_NMEA_OUTPUT_ALLDATA);
mySerial.println(PMTK_SET_NMEA_UPDATE_1HZ);

// Initialise the IO and ISR
vw_set_ptt_inverted(true); // Required for DR3100
vw_set_tx_pin(9); // Tx for transmitter
vw_setup(4000); // Bits per sec
}

void loop()
{
// Following is used to get GPS data

if (mySerial.available())
Serial.print((char)mySerial.read());

if (Serial.available())
mySerial.print((char)Serial.read());

//Following is to Transmit that data in form of a message

message = mySerial.read(); //This is where I'm stuck at as its an invalid conversion from int to constchar
message.toCharArray(sentvalues, 27);

vw_send((uint8_t *)sentvalues, strlen(sentvalues));
vw_wait_tx(); // Wait until the whole message is gone
delay(200);

}

whole2.ino (1.39 KB)

Are you trying to wait until you have received a complete message before you forward it, or send each character immediately it is received?

message = mySerial.read();The message variable is defined as a String. Why ? Serial.read() returns one byte, not a series of bytes and certainly not a String.

PeterH:
Are you trying to wait until you have received a complete message before you forward it, or send each character immediately it is received?

I'm trying to send the whole message so it could be received exactly how GPS receives it.
Later on I'd just make it specific to longitude and latitude but for now I'd just like to send all GPS data (PMTK_SET_NMEA_OUTPUT_ALLDATA)

UKHeliBob:
message = mySerial.read();The message variable is defined as a String. Why ? Serial.read() returns one byte, not a series of bytes and certainly not a String.

Basically I'm really new with coding so I took two different codes which worked fine with Transmitter/Receiver and GPS separately and now this code mentioned is my failed attempt to combine them both :slight_smile:
I can upload both the codes if you'd like to see.
Thanks

Please upload both.

UKHeliBob:
Please upload both.

Below is the code for Transmitter which is used to send a sensors values, but I've just taken the 'Transmitter' bit out and all the commands used to make it work and tried to send GPS data instead.

  #include <VirtualWire.h>  
#include <NewPing.h>
#undef int
#undef abs
#undef double
#undef float
#undef round
#define TRIGGER_PIN  12  
#define ECHO_PIN     11  
#define MAX_DISTANCE 80
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
String speedval;
String distval;
int speedvaltemp;
String message;
char sentvalues[27];
double sensorValue;
int sensorValueint;
int sensorValuedist;
int tempholder;
void setup()
{
  Serial.begin(9600);

  vw_setup(2000);            
  vw_set_tx_pin(3);        
}

void loop()
{
  delay(50); 
  //setting up values to send
  long cm;
  unsigned int uS = sonar.ping();
  sensorValue = analogRead(A0);

  // just to get a resonable 2 digit number from the sensor, not a real world solution for a 
  //higher number of digits unless I reverse the calculation at the other end and lose accuracy
  sensorValue = sensorValue/11;
  //

  cm = uS / US_ROUNDTRIP_CM;
  sensorValueint = (int) round(sensorValue);
  sensorValuedist = (int) round(cm);
  if(sensorValueint < 10){
    speedval = "0";  
    speedval += sensorValueint;
  }
  else {
    speedval += sensorValueint; 
  }

  if(sensorValuedist < 10){
    distval = "0";  
    distval += sensorValuedist;
    if(sensorValuedist > 0){
      tempholder = sensorValuedist; 
    }

  }
  else {
    distval += sensorValuedist; 
    if(sensorValuedist > 0){
      tempholder = sensorValuedist; 
    }
  }

  if(sensorValuedist == 0){
    sensorValuedist = tempholder; 
    distval = "";
    distval += sensorValuedist;
  }

  // stop setting up values

  // construct message
  message = "S" + speedval + "D" + distval; 
  //send to array
  message.toCharArray(sentvalues, 27);

  // reset strings to empty
  speedval = "";
  distval = "";

  //Check message going out
  Serial.print(message);

  // send message
  vw_send((uint8_t *)sentvalues, strlen(sentvalues));
  vw_wait_tx();                                          
  delay(200);
}

Below is the code used for GPS.

 #include <SoftwareSerial.h>

#define PMTK_SET_NMEA_UPDATE_1HZ "$PMTK220,1000*1F"
#define PMTK_SET_NMEA_OUTPUT_ALLDATA "$PMTK314,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0*28"

#define rxPin 8
#define txPin 11

SoftwareSerial mySerial(rxPin, txPin);

void setup() 
{
Serial.begin(9600);
while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
Serial.println("UP501 NMEA test!");

pinMode(rxPin, INPUT);
pinMode(txPin, OUTPUT);

// set the data rate for the SoftwareSerial port
mySerial.begin(9600);


mySerial.println(PMTK_SET_NMEA_OUTPUT_ALLDATA);

// Set the update rate
// 1 Hz update rate
mySerial.println(PMTK_SET_NMEA_UPDATE_1HZ);

}

void loop() 
{

if (mySerial.available()) 
Serial.print((char)mySerial.read());

if (Serial.available()) 
mySerial.print((char)Serial.read());
}

Thanks
SoftwareSerial mySerial(rxPin, txPin);

I thought you said the code was for a GPS? Is your GPS called mySerial?

It would make more sense to name the instance gps, to reflect what is indeed attached to the pins.

PaulS:

SoftwareSerial mySerial(rxPin, txPin);

I thought you said the code was for a GPS? Is your GPS called mySerial?

It would make more sense to name the instance gps, to reflect what is indeed attached to the pins.

You're right, thanks for the advice. Much appreciated.
And please have a look on the issue I'm dealing with as well.
Cheers