6. The WeatherStation Code Explained
In this chapter we will have a look at the building blocks of the WeatherStation. This project
is a relatively complex piece of code and I hope to improve this chapter over time with new
details.
A. The JSON Streaming Parser
You might not know it but the most important puzzle piece for the WeatherStation project is
a thing called a streaming parser. What is a streaming parser? You are most certainly using
parsers every day. A parser is a piece of code that analyses an input (text, document) by
reading in its content. To do that the parser has knowledge about the structure of the text,
sometimes called a syntax. The syntax is like the grammar of your natural language. A web
browser you are using to read news uses an HTML parser to understand the tags that are
downloaded from the webserver and then put into a visualization with formatted text, pictures
and links.
So now that we roughly understand what a parser is the next question would be what is a
streaming parser? With a modern smartphone or desktop computer we often don't need
streaming parsers anymore, we use document object model parsers (DOM) parsers instead.
A DOM parser creates a tree-like structure of the document it parses, keeps this structure in
memory and makes it available for the code that does something meaningful with it. DOM
parsers are very easy to use, fast and convenient. But this convenience comes at the price of
memory requirements. The DOM parser needs a lot of memory, since it keeps the whole
document in memory until it is no longer used. If you have a lot of RAM and your documents
are not that big this is perfectly fine. But if the documents are big compared to the available
(heap) memory you might run into a serious problem.
Imagine the parser to be something like a water meter and we are comparing now two
different types of meters. A water meter which works like a DOM parser needs a bucket and
measures the amount of water by filling the bucket and then measuring the weight of the
water in the bucket. If there is a lot of water then the bucket must be big. A water meter which
works like a streaming parser measures the water while it flows through and doesn't care what
happens to the water afterwards. The bucket in this analogy is the heap or working memory
of your microcontroller, the water is the stream of bits and bytes that you receive, either from
the file system or from a remote server. And the parser does not just measure the amount of
Need help?
Do you have a question about the ESP8266 and is the answer not in the manual?