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();
}
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.
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?