I'm using TinyGPS to grab latitude coordinates. I am then trying to send those coordinates to a RF device using the Radiohead library. This is done using the following function:
driver.send((uint8_t *)cstr, strlen(cstr));
The problem is, the full latitude coordinate is not being received by my receiver. Only the whole number. TinyGPS grabs and prints a coordinate such as 36.778313 for Latitude while on the transmitter.
The receiver only prints "36". How do I send and receive the entire 36.778313 number?
Transmitter code where I think I am having the problem:
#include <TinyGPS.h>
#include <RH_ASK.h>
#include <itoa.h>
const byte TPin = PB9;
RH_ASK driver(2000, 2, TPin, 5); // set transmit to PB9
// I'm using an ESP8266
TinyGPS gps;
void setup()
{
//Receive from the GPS device (the NMEA sentences) - Green wire
pinMode(2, INPUT);
//Transmit to the GPS device - Yellow wire
pinMode(3, OUTPUT);
Serial.begin(9600);
if (!driver.init())
Serial.println("init failed");
Serial2.begin(4800);
}
void loop()
{
// GPS
bool newData = false;
unsigned long chars;
unsigned short sentences, failed;
// For one second we parse GPS data and report some key values
for (unsigned long start = millis(); millis() - start < 1000;)
{
while (Serial2.available())
{
char c = Serial2.read();
// Serial.write(c); // uncomment this line if you want to see the GPS data flowing
if (gps.encode(c)) // Did a new valid sentence come in?
newData = true;
}
}
if (newData)
{
float flat, flon;
unsigned long age;
gps.f_get_position(&flat, &flon, &age);
Serial.print("LAT=");
Serial.print(flat == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : flat, 6);
// Radio Latitude
// need to convert lat coord into Char Array
Serial.print("My Print Test= ");
Serial.print(flat, 6); // prints something such as 36.778313 for Latitude
/* Below is where I am having problems */
char cstr[20];
itoa(flat, cstr, 10); // base 10 for decimal value
driver.send((uint8_t *)cstr, strlen(cstr));
driver.waitPacketSent();
/* ^^ This does not work properly. Only numbers before the decimal place are printed.
* For example 33 instead of the full 33.6355...
* If I Serial.print(flat); is will print 33.63 but no remaining numbers.
*
*/
}
gps.stats(&chars, &sentences, &failed);
Serial.println(failed);
if (chars == 0)
Serial.println("** No characters received from GPS: check wiring **");
}
Receiver code:
#include <RH_ASK.h>
#include <SPI.h> // Not actualy used but needed to compile
// RH_ASK driver;
RH_ASK driver(2000, 4, 4, 5);
/// Which will initialise the driver at 2000 bps, receive on GPIO4, transmit on GPIO4, PTT on GPIO5.
void setup()
{
Serial.begin(115200); // Debugging only
if (!driver.init())
Serial.println("init failed");
}
void loop()
{
uint8_t buf[10];
uint8_t buflen = sizeof(buf);
if (driver.recv(buf, &buflen)) // Non-blocking
{
int i;
// Message with a good checksum received, dump it.
Serial.print("Message: ");
Serial.println((char*)buf);
}
}