Reading string until keyword found?

Hi,

I was wondering whether there was a function in the Arduino IDE which I can deploy to find a specific keyword in an incoming string and then store the value after the keyword. I am using an ESP8266 running the Arduino firmware and wanted to read a web page and return a specific value (time or weather) after coming across the keyword, for instance, "time: (value)". How can this be best achieved? Would I need to use something like this: StreamReadStringUntil

P.S. I'm using the example sketch BasicHTTPClient for the ESP8266 to read the content.

Thanks! :slight_smile:

Something like a state-machine, where each state is an index into the key value string (note the small 's') ?

Yes, the String class is a terrible, for many subtle reasons. And, Yes, an FSM approach is the most efficient way to watch for a keyword/value pair to “go by”. Because RAM is so limited, you really have to be careful about holding on to an entire “message” (i.e., web page in your case).

Instead, as each character comes in, you use a “current state” variable to track how much of the desired keyword has been “seen”. Initally, NONE of the keyword has been seen. When you receive the first character of the keyword, you increment the state variable. If the next character is the second character of the keyword, you increment the state variable again, etc. If you receive a character that is not the correct nth character of the keyword, you reset the state variable to NONE, and start watching for the first character again.

This approach requires no buffering, and it handles each character exactly once, instead of storing the char in a buffer and doing a string compare “later” (C string, not String).

In addition to recognizing the keyword, the same approach can be used to parse the value as it is received. Start your value at zero, and as digits are received, multiply the value by ten and add the new digit. When you finally receive the delimiter (a comma, space, NL, whatever), the value is “completed”.

That’s a lot of words for what is much easier to visualize as a bubble graph, perhaps something like this:

FSM.jpg

Each bubble is a state, and certain characters change the state and “step” to another bubble (i.e., a state change). That chart is from this thread,. Two other threads here and here (with a class to help).

The structure of this problem can seem “upside-down” from the typical approach. Instead of saving a bunch of characters (e.g., a line, page, or message) and working on them later (e.g., searching for keywords, delimiters and digits), you deal with each character immediately. This requires a state variable to remember what you’ve interpreted so far. It may also require holding on to “values” that are not fully computed until a delimiter is seen.

Cheers,
/dev

Thanks for the response!

Is there a tutorial or site which explains that further? Alternatively, has it been implemented before in some other sketches?

P.S. I am a beginner in the Arduino platform :slight_smile:

Thanks again

There was a recent (last couple of weeks) thread I think, with an implementation using a class

Is there a tutorial or site which explains that further? Alternatively, has it been implemented before in some other sketches?

Uhh... all those underlined words in my post are links. The first is a wikipedia article, and the last 3 are to recent discussions we've had here, all with code and even a simple C++ class library.

Cheers,
/dev

That's strange /dev, I missed your post entirely :confused: must have been a reload issue whilst I was writing my reply.

I'll look through those links now! I also found this in the String tutorials. Could that be of use in this application?

I found that things are a little more difficult since the string I want to analyse has quotation marks and a starting double backslash in the HTML code, interfering with the String function format. Is there a fix for this through string trimming or can the string still be processed when it looks like: "//time: 12:41" (shortened of course)?

Thanks again!

interfering with the String function format. Is there a fix for this or can the string still be processed when it looks like: "time: 12:41"

Are you confusing Strings and strings ? They are not the same thing.

I also found this in the String tutorials. Could that be of use in this application?

String would not be of use in any application, ultimately. It may be easy to start with, but as I said in my first sentence above, the String class is still terrible.

It's easy to confuse the capital-S String class with the lowercase-s strings, also called C strings or char arrays. Most recommendations here will advocate strings, with a singular exception. They are faster and more efficient with RAM, and your sketch is guaranteed to be at least 1.6K smaller than if you were to use String. It will also be more predictable, reliable and stable. Forget String.

can the string still be processed when it looks like: "//time: 12:41"

Yes. Simply design the FSM to watch for whatever you want, including regular expressions, values, conditions and/or multiple lines.

But it seems like you're still thinking about buffering a bunch of characters and then searching them. I would suggest you consider the FSM approach instead, which "handles" each character as it is received. Maybe you should re-read my post, follow those links, and read the code therein. You may have more questions; just ask them here!

Cheers,
/dev

If you are tired of the useless reply churn, maybe the below might be of use. If that doesn't work I've got some old complex code that captures data out of a web page that might be of use.

http://playground.arduino.cc/Code/TextFinder