Close

Little Weather Station

A project log for Simple RGB Matrix Display

Simple display with high definition WS2812 matrix

tamcTAMC 08/25/2021 at 12:000 Comments

Change Pico to ESP32 with OpenWeatherMap to display weather!

Code:

import urequests
from irm import IRM
from machine import Pin
from weather_sprites import WEATHER_SPRITES
import time
import json

data = {    "api_key": "YOUR_API_KEY_HERE",    "city_name": "YOUR_CITY",    "units": "metric",
}
weather_url = "https://api.openweathermap.org/data/2.5/weather?q={city_name}&appid={api_key}&units={units}"


irm = IRM(Pin(13), [[1, 2],                    [4, 3]])
irm.brightness = 50

res = urequests.get(url=weather_url.format(**data))
data = json.loads(res.text)
print(data)
weather_icon = data["weather"][0]["icon"]
temp = data["main"]["temp"]
irm.clear()
irm.draw_sprite(0, 0, WEATHER_SPRITES[weather_icon])
irm.draw_text(0, 0, temp, irm.FONT5, [0, 255, 100])
irm.write()

more on  GitLab

To draw all the icons from OpenWeatherMap, I also made a simple editor Matrix Helper from scratch. To help me generating sprites. It grabs the import image, resize it, and generate it into matrix style, then you can edit detail pixel and convert it into code. More about Matrix Helper will be soon on another project. 

Background Story

I am trying ESP32 with OpenWeatherMap to display weather, it's quite easy to use the API. And it also comes with icons too. So I downloaded their icons, and firstly try to draw the icon with code, and it's frustrating! Then I figured I can write a simple python script to help me converting the png icon to 16x16 RGB data for the RGB matrix.

from PIL import Image
import sys

im = Image.open(sys.argv[1])
print(im.size)
im = im.crop((17, 17, 100-17, 100-17))
im = im.resize((16, 16))
px = im.load()
width, height = im.size
temp = []
for y in range(height):    line = []    for x in range(width):        r,g,b,a = px[x, y]        if a == 0:            h = -1        else:            h = (r<<16)+(g<<8)+b        line.append(h)        # print("0x%06X"%h)    temp.append(line)
print(temp)

But what it generates, always have flaws. And it's so hard to fix it after it changes into code. So I was wondering, I should make a tool to edit the pixels after it converts the image! Then here's the Matrix Helper. Then I have more ideas about being open-source, and people can changes the converters to meet their needs for other led matrix even monochrome ones! More on that later.

Discussions