Save value to variable

hi,

I am new to coding. I have a sketch that reliably prints weather data i.a. the wind speed from openweathermap to serial console.

This is the respective part of the sketch:

Serial.print(F("Wind speed: "));
Serial.print(str.substring(pos, str.indexOf(split, pos)));
Serial.println(F(" m/s"));

And this is the output on the console:

08:07:44.067 -> Wind speed: 1.54 m/s

Now I want to save the wind speed to a variable.
I tried the following code, but I results in a value of 0.0

float windspeed;
str.substring(pos, str.indexOf(split, pos)) = windspeed;

Any help would be appreciated.

Kind regards, C

str.substring(pos, str.indexOf(split, pos)) = windspeed;

The first thing I see is that you appear to have the assignment back to front. The target variable should be on the left of the assignment. As an example you would never do

12.5 = windspeed;

would you ?

The second problem is that you have declared windspeed as a float but you are trying to assign a substring of a String to it and the data types are different

So, what to do ?

Although it looks very clever to do everything in a single statement I suggest that you break down the conversion so that you can test each stage

  • use substring() to extract the value as a new String
  • print the new String to verify that it is correct
  • use toFloat() to convert the new String to a float and assign it to a variable
  • print the variable to verify that it is correct

first of all a warning:

variabletype String the type with a capital "S" String is dangerous to use if you don't know the limits.
Variable-type String can eat up all memory and then causes very hard to find program-crashes.

You should use array of char or the library SafeString which could be installed with the library-manager in the Arduino-IDE.

for assigning a value to a variable it has to have this pattern
the variabe is on the left side
myVariable = value
myVariable = expression

the type of the value / expression must be of the same type as the variable

int myInteger;
myIntegerVar = 1.23; // does not work. Assigning a float-value to an integer means different types

float myFloatVar;
myFloatVar = "A character-sequence"; // does not work. different types

of course there are functions to convert values from almost any type to almost any other type

Depending on what you want to do with the strored value in the variable different variabletype would be sufficient.

For Just quoting the value "1.54" a string would be sufficient
If you want to do real calculations you must do a typecasting

"classical" but dangerous type String:

@the C++-purists: If you have a different opinon post a full blown tutorial about how to use c_str and arrays of char

SafeString offers assigning all kinds of values straight-forward without typecasting to variables of type SafeString

#include <SafeString.h>
createSafeString(myDemoStr, 20); // a SafeString object for 20 characters

myDemoStr = 1.54; // assign a float-value DIRECTLY to a SafeString 

//and offers functions for typecasting 
float myFloatVar;
// here the target-variable must be used as a parameter = inside parenthesis ()
myDemoStr.toFloat(myFloatVar); 

the name is program: SafeStrings are safe to use.
best regards Stefan

I am impressed by your prompt and comprehesive answers. Many thanky!
The following lines worked for me:

    String windspeedstring = str.substring(pos, str.indexOf(split, pos));
    float windspeedms = windspeedstring.toFloat() ;
    Serial.print(windspeedms);

Problem solved.

well for just printing the windspeed
this is sufficient:

String windspeedstring = str.substring(pos, str.indexOf(split, pos));
Serial.print(windspeedstring);

Serial.print() is able to "print" all variabletypes

If you would like to calculate km/h from m/s the conversion to a float must be done

  String windspeedstring = str.substring(pos, str.indexOf(split, pos));
  float windspeedms = windspeedstring.toFloat() ;
  float windspeedkm_h = windspeedms * 3.6;
  Serial.print(windspeedkm_h);

best regards Stefan

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