Using Web APIs with Python – Read Weather Forecast and Send to Your Own IoT Device

Photo by Jaxon StevensDo you want get weather forecast, BitCoin vs EUR valuation and know your power consumption? A lot of data is available on free APIs. Your program can just make a regular HTTP GET request – it’s just like loading a web page with Firefox.
In this example, we use Python 3 to read weather from one API and write it to another API for an IoT device.

Following this tutorial requires some familiarity with Linux command line interface and Python. API for IoT is currently in invite only beta. Join mailing list to get an invitation on the next round of invitations. For Open Weather Map API, sample API keys are used. For real data, a registration is needed. Techniques used here are easy to apply to many other APIs, too.

Install and Start iPython

Popular Linuxes already have Python. To have a very nice interactive console, we’ll install iPython (aka Jupyter).

$ sudo apt-get update
$ sudo apt-get -y install ipython3

Then run it. You can type a calculation (or any Python commands) to see immediate results.

$ ipython3
In [1]: 2+2
Out[1]: 4

Let’s use “requests” library to make HTTP GET requests (i.e. loading web pages).

In [2]: from requests import get

We load a sample JSON file form Open Weather Map. Yes, you can just click a link to view it in your browser.

In [3]: r=get(",fi&appid=b1b15e88fa797225412429c1c50c122a1")

Convert the text to Python dictionary, a native Python type. This will make it easy to extract the temperature.

In [4]: d=r.json()

We can play with d. Just add once component at the time, and we’ll extract temperature in no time.

In [5]: d.keys()
Out[5]: dict_keys(['message', 'cod', 'cnt', 'city', 'list'])

It seems to be Kelvin, got to minus 273 to get Celcius.

In [6]: t=d["list"][0]["main"]["temp"]-273.15

Now t contains temperature in Celcius, and t is conveniently a floating point.

In [7]: t
Out[7]: 13.520000000000039
In [8]: t=round(t,1)
In [9]: t
Out[9]: 13.5

To upload the data to API for our IoT devices, we just load a web page with “?x=13.5” in the end.

In [10]: r=get("" % t)
In [11]: r.text
Out[11]: 'OK Added value to server database.\n'

The last text “OK Added…” was printed by the server.
Well done, you’ve now walked trough a simple example of using web APIs with Python3.

What Next?

Write your program into a file and run it with ‘python3’.
Put your program into cron (‘crontab -e’) to run it automatically every hour.
Subscribe mailing list for IoT, Linux and robot related tips and offers.

References API one
Python Requests homepage
Winter photo by Jaxon Stevens.
Open Weather Map 5 day forecast API documentation
IoT.Botbook.Com – Support Material for Method for IoT prototyping
Tested with Xubuntu 14.04 LTS amd64. Updates: Fixed typos, added photo, improved copy, keywords.

Posted in Uncategorized | Tagged , , , , , , , , , , , | Comments Off on Using Web APIs with Python – Read Weather Forecast and Send to Your Own IoT Device

Comments are closed.