The float type is not implemented in sprintf on most arduinos, you would need to use dtostrf() instead.
< edit >
You can avoid using dtostrf() by converting the float to an integer first, then using that with sprintf(). Here is an example where the float is converted to an integer that represents the speed in hundredths of a unit, then prints out the whole number and fractional parts separated by a period (decimal point). You do need to be careful that the maximum possible number will fit within an int, otherwise use a long, and I am assuming speed is always a positive number. Notice that you can also embed the double dashes into the format string for sprintf().
struct {
float speed;
} GPS;
uint32_t timer = 0;
void setup()
{
Serial.begin(115200);
}
void loop() // run over and over again
{
GPS.speed = random(32768) / 100.0; //dummy value for speed
if (millis() - timer > 200) {
timer = millis();
int speed = (GPS.speed + .005) * 100; //round GPS.speed and convert to hundredths as an integer
char radiopacket[100];
snprintf(radiopacket, sizeof(radiopacket), "%i.%02i, --, --, --, --, ", speed / 100, speed % 100);
Serial.println(GPS.speed);
Serial.println(radiopacket);
}
}
Here's some working code! Not yet tested at speed, but I plan on doing that very soon
#include <Adafruit_GPS.h>
#include <SoftwareSerial.h>
#include <SPI.h>
#include <RH_RF95.h>
// what's the name of the hardware serial port?
#define GPSSerial Serial1
SoftwareSerial blueconn(11,12);
// Connect to the GPS on the hardware port
Adafruit_GPS GPS(&GPSSerial);
// Set GPSECHO to 'false' to turn off echoing the GPS data to the Serial console
// Set to 'true' if you want to debug and listen to the raw GPS sentences
#define GPSECHO false
#define RFM95_CS 4
#define RFM95_RST 2
#define RFM95_INT 3
// Change to 434.0 or other frequency, must match RX's freq!
#define RF95_FREQ 434.0
// Singleton instance of the radio driver
RH_RF95 rf95(RFM95_CS, RFM95_INT);
uint32_t timer = millis();
void setup()
{
blueconn.begin(9600);
//while (!Serial); // uncomment to have the sketch wait until Serial is ready
// connect at 115200 so we can read the GPS fast enough and echo without dropping chars
// also spit it out
Serial.begin(115200);
// 9600 NMEA is the default baud rate for Adafruit MTK GPS's- some use 4800
GPS.begin(9600);
// uncomment this line to turn on RMC (recommended minimum) and GGA (fix data) including altitude
GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCONLY);
// uncomment this line to turn on only the "minimum recommended" data
//GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCONLY);
// For parsing data, we don't suggest using anything but either RMC only or RMC+GGA since
// the parser doesn't care about other sentences at this time
// Set the update rate
GPS.sendCommand(PMTK_SET_NMEA_UPDATE_5HZ); // 1 Hz update rate
// For the parsing code to work nicely and have time to sort thru the data, and
// print it out we don't suggest using anything higher than 1 Hz
// Request updates on antenna status, comment out to keep quiet
GPS.sendCommand(PGCMD_ANTENNA);
delay(1000);
// Ask for firmware version
GPSSerial.println(PMTK_Q_RELEASE);
pinMode(RFM95_RST, OUTPUT);
digitalWrite(RFM95_RST, HIGH);
while (!Serial);
delay(100);
Serial.println("Arduino LoRa TX Test!");
// manual reset
digitalWrite(RFM95_RST, LOW);
delay(10);
digitalWrite(RFM95_RST, HIGH);
delay(10);
while (!rf95.init()) {
Serial.println("LoRa radio init failed");
delay(500);
}
Serial.println("LoRa radio init OK!");
// Defaults after init are 434.0MHz, modulation GFSK_Rb250Fd250, +13dbM
if (!rf95.setFrequency(RF95_FREQ)) {
Serial.println("setFrequency failed");
while (1);
}
Serial.print("Set Freq to: "); Serial.println(RF95_FREQ);
// Defaults after init are 434.0MHz, 13dBm, Bw = 125 kHz, Cr = 4/5, Sf = 128chips/symbol, CRC on
// The default transmitter power is 13dBm, using PA_BOOST.
// If you are using RFM95/96/97/98 modules which uses the PA_BOOST transmitter pin, then
// you can set transmitter powers from 5 to 23 dBm:
rf95.setTxPower(23, false);
}
int speed = 0;
int b1;
int b2;
int b3;
int b4;
void loop() // run over and over again
{
// read data from the GPS in the 'main loop'
char c = GPS.read();
// if you want to debug, this is a good time to do it!
if (GPSECHO)
if (c) Serial.print(c);
// if a sentence is received, we can check the checksum, parse it...
if (GPS.newNMEAreceived()) {
// a tricky thing here is if we print the NMEA sentence, or data
// we end up not listening and catching other sentences!
// so be very wary if using OUTPUT_ALLDATA and trying to print out data
//Serial.print(GPS.lastNMEA()); // this also sets the newNMEAreceived() flag to false
if (!GPS.parse(GPS.lastNMEA())) // this also sets the newNMEAreceived() flag to false
return; // we can fail to parse a sentence in which case we should just wait for another
}
// approximately every 2 seconds or so, print out the current stats
if (millis() - timer > 200) {
timer = millis(); // reset the timer
if (GPS.fix) {
speed = GPS.speed * 100;
}
blueconn.print(speed);
float then = millis();
char radiopacket[100];
snprintf(radiopacket, sizeof(radiopacket), "%i,%i,%i,%i,%i,%i,", speed, b1, b2, b3, b4, GPS.fix);
Serial.println(radiopacket);
const char *radiopacketCharArray = radiopacket;
rf95.send((uint8_t *)radiopacketCharArray, strlen(radiopacketCharArray));
rf95.waitPacketSent();
//Serial.println((millis() - then));
}
}