I had a post Arduino OpenWeatherMap Client several years ago which documents an OpenWeather client on Arduino UNO. After so many years, people are still interested in reading that post. This encourages me to re-implement it on a more powerful platform - LEGO EV3 Intelligent Brick using MicroPython.

This project demonstrates how to turn LEGO EV3 into an IoT device and change LED light color. Please refer to my previous post LEGO EV3 MicroPython - Random Shapes on how to install MicroPython image for LEGO EV3.

Connect to the Internet

To turn your EV3 Intelligent brick into an IoT device, the simplest route is to use a Wi-Fi adapter connected through the EV3’s USB port. The official EV3 MicroPython image is derived from ev3dev.org. You can read their networking documentation for detailed settings and configurations.

I am fortunate enough to have my USB Wi-Fi dongle worked out of the box. I assume the ev3dev Linux kernel, which is at 4.14.117 as of this writing, should support most of the dongles on the market. The wireless setup steps are very similar to connecting your phone or tablet to your router. The steps are as follows using the Brickman interface:

  • Navigate to Wireless and Networks in the main menu
  • Select Wi-Fi
  • Select Powered
  • Press Start Scan
  • Find your wireless network using the Up and Down buttons
  • Press Connect
  • You will be prompted with a dialog and press the Middle button
  • Type your password and select OK
  • Turn on Connect Automatically

You can also setup Wi-Fi using the command line which is documented in ev3dev tutorial.

MicroPython Libraries

If you had a chance SSH into your EV3 Intelligent Brick with the official MicroPython image running, you will find a complete set of MicroPython libraries under /usr/lib/pybricks-micropython. There is another set under /usb/lib/micropython but I am pretty sure the Pybricks version is what we are using. The complete source code can be found at pybricks-micropython GitHub repository.

Some of my favorite libraries are also included, for example:

  • micropython-urequests. MicroPython’s requests module which will be used to fetch OpenWeather Current Weather Data.
  • micropython-uasyncio. MicroPython’s asynchronous scheduling library, roughly modeled after CPython’s asyncio.
  • Many other useful libraries…

Talk to OpenWeather

With micropython-urequests, it is simple to send the HTTP GET request to OpenWeather Current Weather Data API endpoint.

import urequests

url = 'https://api.openweathermap.org/data/2.5/weather?q=YOUR_CITY&appid=YOUR_APPID&units=metric'
resp = urequests.get(url)
current_weather = resp.json()

Please replace YOUR_CITY and YOUR_APPID with the city you want to query and your API key after subscribing OpenWeather service. The request and response structures are well documented in the API documentation.

Parse Response

With the OpenWeather response converted to JSON, it is time to get the weather data you need. Besides showing the weather icon on the screen, I am also using the LED lights as an indication of the current weather condition.

weather = current_weather['weather'][0]
main = current_weather['main']

description = weather['description']
feels_like = int(main['feels_like'])
humidity = main['humidity']
icon = weather['icon']
pressure = main['pressure']
temp = int(main['temp'])
temp_max = int(main['temp_max'])
temp_min = int(main['temp_min'])
weather_id = weather['id']

# change led lights
if 800 <= weather_id < 900:
    # group 800: clear
    # group 80x: clouds
    ev3.light.on(Color.GREEN)
elif 200 <= weather_id < 600:
    # group 2xx: thunderstorm
    # group 3xx: drizzle
    # group 5xx: rain
    ev3.light.on(Color.YELLOW)
else:
    # group 6xx: snow
    # group 7xx: atmosphere
    ev3.light.on(Color.ORANGE)

The OpenWeather response contains way more data than I need for this project so I only extract a small portion from it.

EV3Brick Class Reference

light.on(color)

Turns on the light at the specified color.

light.off()

Turns off the light.