Tuesday 13 November 2018

Getting Weather Data

After talking about the project in last weeks session, it was decided that it would be great if the virtual forest could react in real time to some data inputs. In the real world, external factors from the environment such as wind have an instantly visible effect on trees, other factors including temperature & humidity take time to realise.


In theory, the installation could include several sensors for measuring environmental parameters using physical hardware sensors, however, it would also be interesting too position the virtual forest in different real-world environments. i.e. what would this forest look like right now in California or Syberia?

RSS


As defined by Digital trends [1], RSS stands for 'really simple syndication'. At their heart, they are just simple text files with basic updated information — news pieces, articles, that sort of thing. In this instance, an RSS feed can be used to get up to date and succinct information of the current weather forecast. 


Example output from a BBC weather RSS feed.


So the next question was how do I get an RSS data feed into a C++ program. A quick search of GitHub revealed a number of open source libraries that would have been suitable, however, I really wanted something simple and not full of external dependencies.

Some more hacking

Making use of what I already knew I was able to hack together a fairly simple application in C++ that could get the current weather report. I shall now talk through the code line-by-line.


The first line of code simply makes a call to system, followed by the curl command, which is used to pull down the content of the RSS feed and pipe it into a plain text file.


The program then waits before trying to open the file.



Make sure the file is actually open before trying to parse it.


Next up we simply go over the file line-by-line 


We then use the following code to check to see if we have got to the weather description part.



Once there we make a call to the stringBetween function to pull out various bits of information, the std::stoi converts a string into an integer.


Finally, we can print out the information we extracted.


The entire program looks like this:



I can now think about putting the various parts of the project together!


[1] https://www.digitaltrends.com/computing/what-is-an-rss-feed/


No comments:

Post a Comment