Separate Values from a String
Separate Values from a String
I use a nodemcu with a tft-display to show "weatherdata" (humidity= 2 digits, airpressure= 4 digits and temperature=3 digits) - I get those three values from my Arduino webserver as one String which contains all three values! What I need to do is to separate the three values and assign each of them to a different variable! This would give me the possibility to locate them in different spots of the display - the whole thing would be more attractive and better readable!! There are lots of string functions in Arduino but my problem is that I don´t know which of them I should use. And - since I never did this before - I honestly don´t know how this is to be done!!
Would it be helpful to post my sketch?
I would really appreciate your help!
Would it be helpful to post my sketch?
I would really appreciate your help!
Re: Separate Values from a String
If you are certain there are always 2, 4 and 3 characters respectively (even if the temperature is negative for example), simply use the substring function to extract the three readings as strings . . . . .
(I'm not sure of the format of your data string)
Code: Select all
String data = "humidity=89, airpressure=1023 and temperature=-05";
void setup() {
Serial.begin(115200);
Serial.println(data.substring(9,11));
Serial.println(data.substring(25,29));
Serial.println(data.substring(46,49));
}
Re: Separate Values from a String
Thank you very much for your quick answer - now I´ve got that...and it works!!!!!!!!!!!!!!!!!!!!!!
There´s one more question on my mind - is it possible to assign variables to the three substrings? This woud be very important for future designs!!
void tft_show_weatherdata() //(air pressure, temperature, humidity)
{
tft.setTextColor(ILI9340_YELLOW, ILI9340_BLACK );
tft.setTextSize(4);
tft.setCursor(5, 5);
tft.print(weatherdata.substring(0,4));// air pressure
tft.setTextSize(13);
tft.setCursor(5, 80);
tft.print(weatherdata.substring(4,8));// temperature
tft.setTextSize(4);
tft.setCursor(5, 205);
tft.print(weatherdata.substring(8,10));// humidity
}
There´s one more question on my mind - is it possible to assign variables to the three substrings? This woud be very important for future designs!!
Code: Select all
{
tft.setTextColor(ILI9340_YELLOW, ILI9340_BLACK );
tft.setTextSize(4);
tft.setCursor(5, 5);
tft.print(weatherdata.substring(0,4));// air pressure
tft.setTextSize(13);
tft.setCursor(5, 80);
tft.print(weatherdata.substring(4,8));// temperature
tft.setTextSize(4);
tft.setCursor(5, 205);
tft.print(weatherdata.substring(8,10));// humidity
}
Code: Select all
Re: Separate Values from a String
Use toInt() . . . . , .
toFloat() is also available if you need it (reference).String data = "1023-01289";
int h,p,t;
void setup() {
Serial.begin(115200);
p = data.substring(0,4).toInt();
t = data.substring(4,8).toInt();
h = data.substring(8,10).toInt();
Serial.println(p);
Serial.println(t);
Serial.println(h);
}
Re: Separate Values from a String
Thank you again - you´re really kind!!
One last question - let´s say the air pressure drops from 1000 to 998 or so - then this would dislocate the values shown on the display! Is there a possibility to assign a minimum length to each of the three parameters on my webserver so that the stringlength of each value always stays the same !! Here is the part of my webserver where the three parameters "pressure", "temperature" and "humidity" are readout.
One last question - let´s say the air pressure drops from 1000 to 998 or so - then this would dislocate the values shown on the display! Is there a possibility to assign a minimum length to each of the three parameters on my webserver so that the stringlength of each value always stays the same !! Here is the part of my webserver where the three parameters "pressure", "temperature" and "humidity" are readout.
Code: Select all
// output in plain text
void printText(EthernetClient client) {
Serial.println("Textmode");
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/plain; charset=utf-8");
client.println();
client.print(pressure,0);
client.print(temperature,1);
client.print(humidity,0);
Re: Separate Values from a String
That's why in my first post I wrote "If you are certain there are always 2, 4 and 3 characters respectively"
I assume client.print does not put spaces between sucessive values. I therefore suggest you insert commas (or some other character).
In the code below, I make use of the fact that toInt() and toFloat() stops the conversion when it finds an invalid character such as a comma.
If you cannot insert the commas for some reason, we would have to check if the first chararacter is 9 to determine whether the pressure is 3 digits or 4 digits. Then we would have the find the location (index) of the decimal point to determine the end of the temperature value.
I assume client.print does not put spaces between sucessive values. I therefore suggest you insert commas (or some other character).
In the code below, I make use of the fact that toInt() and toFloat() stops the conversion when it finds an invalid character such as a comma.
Code: Select all
String data="998,-1.2,89";
int humidity, pressure;
float temperature;
int firstComma, secondComma;
void setup() {
Serial.begin(115200);
pressure = data.toInt(); // will read up to first comma
firstComma = data.indexOf(",");
temperature = data.substring(firstComma+1).toFloat(); //will read up to second comma
secondComma = data.indexOf(",",firstComma+1);
humidity = data.substring(secondComma+1).toInt(); //will read to end of string
Serial.println(pressure);
Serial.println(temperature,1);
Serial.println(humidity);
}
Re: Separate Values from a String
Hello Archibald,
I implemented your code and it works - of course. My webserver now sends "1017,23.8,28".
But there is one strange phenomenon The value "humidity" has a strange "frame" now that wasn´t there before.
BTW: Excuse me for all those questions and misunderstandings - I´m good at hardware but I´m relatively new to C
I implemented your code and it works - of course. My webserver now sends "1017,23.8,28".
But there is one strange phenomenon The value "humidity" has a strange "frame" now that wasn´t there before.
BTW: Excuse me for all those questions and misunderstandings - I´m good at hardware but I´m relatively new to C
Code: Select all
void tft_show_weatherdata() //(air pressure, temperature, humidity)
{
pressure = weatherdata.toInt(); // will read up to first comma
firstComma = weatherdata.indexOf(",");
temperature = weatherdata.substring(firstComma+1).toFloat(); //will read up to second comma
secondComma = weatherdata.indexOf(",",firstComma+1);
humidity = weatherdata.substring(secondComma+1).toInt(); //will read to end of string
Serial.println(pressure);
Serial.println(temperature,1);
Serial.println(humidity);
tft.setTextColor(ILI9340_YELLOW, ILI9340_BLACK );
tft.setTextSize(4);
tft.setCursor(5, 5);
tft.print(pressure);
//tft.print(weatherdata.substring(0,4));// air pressure
tft.setTextSize(13);
tft.setCursor(5, 80);
tft.print(temperature);
//tft.print(weatherdata.substring(4,8));// temperature
tft.setTextSize(4);
tft.setCursor(5, 205);
tft.print(humidity);
//tft.print(weatherdata.substring(8,10));// humidity
}
Re: Separate Values from a String
I think you included an out of date version of your code; otherwise I would have expected a comma or two to show up on the display!
Because the number of characters representing each value can vary, you need to print the integer and float values to the display, not the substrings. Also, because the number of characters varies and the width of characters varies, I would expect you to have to clear an area of display before printing each new value to the display.
Regarding the 'framing', sorry I don't know anything about that display and its library. It may be best to start a new forum thread for that.
By the way: "hpa" should be "hPa" .
You can delete the console Serial.print statements (unless you want them for debugging).
Because the number of characters representing each value can vary, you need to print the integer and float values to the display, not the substrings. Also, because the number of characters varies and the width of characters varies, I would expect you to have to clear an area of display before printing each new value to the display.
Regarding the 'framing', sorry I don't know anything about that display and its library. It may be best to start a new forum thread for that.
By the way: "hpa" should be "hPa" .
You can delete the console Serial.print statements (unless you want them for debugging).
Re: Separate Values from a String
Thanks again for all you did for me - and of course I´ve changed "hpa" to "hPa"
BTW: I do print the integer and float values to the display - the old "substring-code" is commented out!
all the best!!!!!!!
Peter
BTW: I do print the integer and float values to the display - the old "substring-code" is commented out!
Code: Select all
//tft.print(weatherdata.substring(0,4));// air pressure
Peter
Re: Separate Values from a String
UPDATE!!!!!
When I declare "int temperature" instead of "float temperature" the phenomenon does not appear - see picture!!!
When I declare "int temperature" instead of "float temperature" the phenomenon does not appear - see picture!!!
- Attachments
-
- Display.JPG (110.45 KiB) Viewed 19621 times
Who is online
Users browsing this forum: No registered users and 74 guests