Arduino Nano 33 IoT float value and float to String converter

Hello,
I'm writing a program to take the Latitude and longitude values, convert to the understandable format into float and then publish the same to MQTT.
I've been able to take the values only upto 2 digits from the forum help but could not convert it to 4 decimal value. I want the 4 digit decimal float so as to get the meaningful data.

Also, after getting the float value, I cannot publish the latitude and Longitude into a single string.
Please find the code for GPS float vale as below:

Serial.begin(9600);
  Serial1.begin(9600);
  while (Serial1.available()) 
  {
    inChar = Serial1.read();
    gpsData += inChar;
    if (inChar == '$') 
    {
      gpsData = Serial1.readStringUntil('\n');
      break;
    }
  }
  //Serial.println(gpsData);
  sGPRMC = gpsData.substring(0, 5);
  if (sGPRMC == "GPRMC") 
  {
    Serial.flush();
    
    latt = gpsData.substring(18, 28);
    la = gpsData.substring(29, 30);
    lonn = gpsData.substring(31, 42);
    lo = gpsData.substring(43, 44);
    
    lattt = latt.toFloat();
    lonnn = lonn.toFloat();

    if (la == "N" and lo == "E") 
    {

      latDeg = float(int(lattt / 100));
      latMin = float(lattt - (latDeg * 100));
      latMin = float(latMin / 60);
        
      lonDeg = float(int(lonnn / 100));
      lonMin = float(lonnn - (lonDeg * 100));
      lonMin = float(lonMin / 60);

      latttt = latDeg + latMin;
      lonnnn = lonDeg + lonMin;
      LATval = float(latttt);
      LNGval = float(lonnnn);
      //Serial.print(LATval);
      //Serial.println(LATval);
      //Serial.print(LNGval);
      //Serial.println(LNGval);
    }
  }
  delay(1000);

Please find the code for MQTT as below:

void senddataonmqtt()
{
  unsigned long currentMillis = millis(); 
  if (currentMillis - previousMillis >= interval) 
  {
    // save the last time a message was sent
    previousMillis = currentMillis;
    //record random value from A0, A1 and A2
    int Rvalue = degreesX + degreesY;
    Serial.print("Sending message to topic: ");
    Serial.println(topic);
    Serial.println(Rvalue);
    Serial.println(topic1);
    Serial.println(LATval);
    Serial.println(topic2);
    Serial.println(LNGval);
    // send message, the Print interface can be used to set the message contents
    mqttClient.beginMessage(topic);
    mqttClient.print(Rvalue);
    mqttClient.beginMessage(topic1);
    mqttClient.print(LATval);
    mqttClient.beginMessage(topic2);
    mqttClient.print(LNGval);
    mqttClient.endMessage();
    Serial.println();
  }
}

I want the data to be published on MQTT as:
XYZ: 123 , latitude: 82.36 , Longitude: 77.23

Thank you in advance.

why do you want to loose precision in position?

if you do

    mqttClient.print(LATval);

that's why you get only 2 digits

if you want 4 (but really why?) then do

    mqttClient.print(LATval,4);

same goes for the longitude

Thank you for the reply sir.
Actually, I want the precise position. Hence I want 4 digit after decimal.

Also, I've tried the mqttClient.print(LATval,4); but it gave me error. hence I did not write that into code.

what error did you get? is LATval a String or a float?

4 decimal digit is NOT a precise GPS position... you need all you can get
for example this is where the Eiffel Tower is in Paris:
➜ 48.85848301905653, 2.2945027564091065

if you go to ➜ 48.8584, 2.2945 you are a bit off :slight_smile:

The data that I get is 17.46 as an example.
But if I want the precision, then I need the data as 17.462578 etc. It may be 4 or 6 digit after decimal.

Also, I get the error as (Compilation error: no matching function for call to 'MqttClient::print(arduino::String&, int)')

ah, so you don't have a float but a String... if you only get 2 digits then it means this is all you have in the String...

when you do

    latt = gpsData.substring(18, 28);
    lonn = gpsData.substring(31, 42);
...
    lattt = latt.toFloat();
    lonnn = lonn.toFloat();

if the format is always the same then you get the full resolution in lattt and lonnn (stupid variable names - you could use latValue and lonValue for example and for the other latString and lonString)

why don't you just print the float values and not create an extra String? then you can use 4 or 6 to get the number of decimals you need

Thank you so much sir.
Got the code right. Please check the result as (latitude: 18.474730 Longitude: 73.796349)

Can you please teach me how to publish the latitude and longitude values into single topic?

Thank you in advance.

which MQTT library are you using?

#include <ArduinoMqttClient.h>
WiFiClient wifiClient;
MqttClient mqttClient(wifiClient);
const char broker[] = "broker.hivemq.com";
int port = 1883;`

a link to ArduinoMqttClient.h would have been better :slight_smile:

is it that one ? GitHub - arduino-libraries/ArduinoMqttClient: ArduinoMqttClient Library for Arduino

if so, it seems the publishing is simple:

you begin your message on a topic and then print whet you want to publish

    // send message, the Print interface can be used to set the message contents
    mqttClient.beginMessage(topic);
    mqttClient.print(xxx);
    mqttClient.print(xxx);
    mqttClient.print(xxx);
    mqttClient.print(xxx);
    mqttClient.print(xxx);
    mqttClient.endMessage();

so if you want to see Latitude: 18.474730 Longitude: 73.796349

    mqttClient.beginMessage(topic);
    mqttClient.print(F("Latitude: "));
    mqttClient.print(latValue, 6);
    mqttClient.print(F(" Longitude: "));
    mqttClient.print(lonValue, 6);
    mqttClient.endMessage();

Dear Sir,
Thank you so much. The issue is now resolved.
Thank you for your valuable guidance.

have fun with your arduino!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.