lfurze
Posts: 10
Joined: Sun Apr 21, 2019 9:21 am

Python script for blinkt with openweathermap API

Sun Apr 21, 2019 11:12 am

Hi all,

Totally new to python and just messing around with a Blinkt! to see what it can do. Apologies for any inadvertent forum faux pas. I have been mucking around with the openweathermap API and the example blinkt_thermo.py and have thus far made a thermometer which changes colour based on temperature, and then cycles to show different data, for example a humidity gauge.

I would like to colour code the ‘description’ (weather condition) so that, for example, if weather.description is “overcast clouds” the blinkt lights up all white, light blue for a clear day, etc.

As I said I’ve fudged together a few bits of code from different places so what I have is not at all elegant. At the moment, I can get it to display the white colour bar for the overcast day with:

Code: Select all

def draw_desc(desc):
    if desc == 'overcast clouds':
        blinkt.set_all(255, 255, 255, 0.1)
        blinkt.show()


Where desc is a previously defined global variable added in to the original blinkt_thermo.py update_weather function as desc = weather_data['weather'][0]['description']

I suspect a better way to achieve what I am after would be to use the weather.id rather than .description, (https://openweathermap.org/weather-conditions) so that for example anything with the id in the range 200-232 (thunderstorm) would show purple, or whatever.

I can’t figure out a way to do this with my limited knowledge beyond using a whopping great bunch of if-elif statements like:

Code: Select all

def draw_id(id):
    if id in range(200, 232): #thunderstorms
        blinkt.set_all(128, 0, 128, 0.1)
        blinkt.show()
    elif id in range(300, 321): #drizzle
        blinkt.set_all(0, 0, 255, 0.1)
        blinkt.show()
    elif
.........

And so on.

As per the website there are seven groups of weather conditions that would need “colour coding”.

Any suggestions, other than “why bother?”?

Andyroo
Posts: 4223
Joined: Sat Jun 16, 2018 12:49 am
Location: Lincs U.K.

Re: Python script for blinkt with openweathermap API

Sun Apr 21, 2019 2:33 pm

Take a deep breath for this one :lol:

I would use a Python dictionary https://www.tutorialspoint.com/python/p ... ionary.htm

The keys are the type of weather - either values or text would be fine
The entry is the value you need

To keep it simple to start with (best for me as well) create three dictionaries - one for each colour with the same keys.
When you get more comfortable with dictionaries you can move onto tuples and use them as the entry and go down to one dictionary.

Have a Google and shout up if you get stuck :)

By the way - worry not about elegant code (not seen anyone complain that examples are bad which is nice) - that will come with practise and reading other code. Just like any spoken or written language practice helps.
Need Pi spray - these things are breeding in my house...

lfurze
Posts: 10
Joined: Sun Apr 21, 2019 9:21 am

Re: Python script for blinkt with openweathermap API

Sun Apr 21, 2019 11:05 pm

Cheers Andyroo,

I went with tuples for the numerical id codes in the end:

Code: Select all

#tuples to store weather id codes
thunder = (200, 201, 202, 210, 211, 212, 221, 230, 231, 232)
drizzle = (300, 301, 302, 310, 311, 312, 313, 314, 321)
rain = (500, 501, 502, 503, 504, 511, 520, 521, 522, 531)
snow = (600, 601, 602, 611, 612, 613, 615, 616, 620, 621, 622)
atmos = (701, 711, 721, 731, 741, 751, 761, 762, 771, 781)
clear = (800,)
clouds = (801, 802, 803, 804)
and then a series of if-elif statements to check the weather id (wid):

Code: Select all

def draw_wid(wid):
    if wid in thunder:
        blinkt.set_all(102, 0, 204)
    elif wid in drizzle:
        blinkt.set_all(204, 255, 255)
    elif wid in rain:
        blinkt.set_all(0, 0, 255)
etc.

This works, but I expect there's a more efficient way to do it... should I be making a dictionary of tuples? A tuple of tuples? Something else entirely?

Anyway like I say it works, so on with the next project!

Andyroo
Posts: 4223
Joined: Sat Jun 16, 2018 12:49 am
Location: Lincs U.K.

Re: Python script for blinkt with openweathermap API

Sun Apr 21, 2019 11:33 pm

I would go for a dictionary of tuples :lol:

But then again it works which is more than my current callback function :twisted: :lol: :twisted: so go with it!
Need Pi spray - these things are breeding in my house...

Return to “Python”