How to extract value from a string: thingspeak​.getFieldV​alue(3));

Hi,
I built, with a NodeMCU ESP8266, a receiver of the data transmitted by a weather station connected to ThingSpeak, based on the project by Daniel Eichhorn (http://blog.squix.ch).
From the string "display-> drawString (40 + x, 15 + y, thingspeak.getFieldValue (3) + "m/s");" of the sketch I get the value of the wind speed expressed in m/s, while I would need the value expressed in Km/h. I can't find a solution to "extract" the numeric value from the string <thingspeak.getFieldValue (3)> to then multiply it by 3.6, and get the value in Km/h.
Could anyone suggest me a solution?
Thank you.
Giuseppe

댓글 수: 4

create a variable before that line.
float myFieldValue = thingspeak.getFieldValue (3) ;
myConvertedValue = myfieldValue * conversionFactor;
then
display-> drawString (40 + x, 15 + y, String(myConvertedValue) + "m/s");
First of all I want to thank you, Christopher, for your prompt reply.
I corrected my sketch exactly as you suggested, but from Arduino IDE I get this error message:
"cannot convert 'String' to 'float' in initialization".
To provide more information about my sketch, these are the lines to show the data on the display:
void drawThingspeak1(OLEDDisplay *display, OLEDDisplayUiState* state, int16_t x, int16_t y) {
display->setTextAlignment(TEXT_ALIGN_CENTER);
display->setFont(ArialMT_Plain_10);
display->drawString(64 + x, 0 + y, "Kalena");
display->setFont(ArialMT_Plain_16);
float myFieldValue = thingspeak.getFieldValue (3);
myConvertedValue = myFieldValue * 3.6;
display-> drawString (40 + x, 15 + y, String(myConvertedValue) + "m/s");
//display->drawString(40 + x, 15 + y, thingspeak.getFieldValue(3)+ " m/s");
I hope you can help me.
Have a good night.
Is the error on this line?
float myFieldValue = thingspeak.getFieldValue (3);
?
It is probably becasue getFieldValue(3) returns a string. So read it into a string and then use the .toFloat function to turn it into a float. Then convert it, then trun it back to a string to display.
Thank you very much.
I wrote these line of code and now it works:
String stringVal = thingspeak.getFieldValue(3);
float myFieldValue = stringVal.toFloat();
float conversionFactor = 3.6;
float myConvertedValue = myFieldValue * conversionFactor;
display-> drawString (40 + x, 15 + y, String(myConvertedValue) + " Km/h");

댓글을 달려면 로그인하십시오.

답변 (0개)

커뮤니티

더 많은 답변 보기:  ThingSpeak 커뮤니티

카테고리

도움말 센터File Exchange에서 ThingSpeak에 대해 자세히 알아보기

제품

질문:

2020년 4월 15일

댓글:

2020년 4월 18일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by