Data types and conversions

Hi there

I have a board connected to my arduino delivering data. I know that the data received always starts with a “$” and ends with a “*” and is a fixed length of 46 characters.

I have written the following so far to try and capture the data. The data recevied contains weather information ie pressure, temperature etc. I am stuck at how to extract certain characters from the string to give me a floating point value or int. I seem to get stuck because I dont see how to extract say 4 characters from the nmeaweather char array and convert this to a float or int.

Can someone suggest anything?

David

int i;
char nmeaweather[46];
char temp;
int humidity;
float dewpoint;
float pressure;
float lightlevel;

while (Serial1.available() > 0)
  {
    temp = Serial1.read();
    if (temp = '

)
    {
      for (i = 0; i < 46; i++) {
        nmeaweather[i] = Serial.read();
      }
     
      humidity = ???
    }
  }

Can you show us an example line of the data you get from your device? Even better would be a datasheet or anything comparable, if you have that.

const int DATALENGTH = 46;
char nmeaweather[DATALENGTH];

int humidity;
float dewpoint;
float pressure;
float lightlevel;

void loop()
{
static int index = 0;
if (Serial1.available())
  {
  char c = Serial1.read();
  if (c == '

)
     {
     index = 0;
     }
 else
 if (c == “" && index == DATALENGTH)
     {
     ProcessString();  // Received "$DATALENGTH characters

     }
 else
 if (index < DATALENGTH)
     {
     nmeaweather[index++] = c;
     }
 else
    {
   index++;  // This is how we check for >DATALENGTH characters between $ and *
   }
  }
}
     
void ProcessString()
    {
    humidity = ???;  // You have not said anything about the format of the data.
    }

You can copy data from any position in nmeaweather to another array. All you need to know is where in nmeaweather to start and how many characters to copy.

Once you have copied the data to another array, and NULL terminated that array, atof() or atoi() can be used to convert the string to a float or int.

Hi there

Thanks for your replies. I have the arduino connected to a USB weather board from sparkfun here is the datasheet - http://dlnmh9ip6v2uc.cloudfront.net/datasheets/Sensors/Weather/USB_Weather_Board_V3_datasheet_110615.pdf

The bottom right of page 5 shows the format of the data I am getting in NMEA.

If I have the NMEA string stored in a char array how can I get pressure data as a float for example from this? ie I know humidity is always characters 8 and 9 of the array. How do I get this into an int?

David

The data is comma separated values. Use the strtok() function to extract each token, with ',' as the delimiter.

The 1st token is the header. The 2nd is the temperature. The 3rd is the humidity.

Each token can be passed to atof() or atoi() as appropriate.