Raspberry Pi Project 5: LED Weather Alert

This project uses OpenWeatherMap's API to displaythe weather forecast via 4 LEDs.

This project integrates your Raspberry Pi with an Application Programming Interface (API) to display the current weather forecast. An API is an online service that returns data in a format that can easily be read by a script.

The API we will be using in this project has been created by OpenWeatherMap and provides weather forecast information.

The project uses the same circuit that we made in tutorial 2:

Raspberry Pi Project 5: LED Weather Alert Circuit Diagram Schematic

Open IDLE on your Raspberry Pi (Menu > Programming > Python 2 (IDLE)) and open a new project (File > New File).  Then type the following:

import requests
import time
import RPi.GPIO as GPIO


LEDa=17
LEDb=18
LEDc=22
LEDd=23
GPIO.setmode(GPIO.BCM)
GPIO.setup(LEDa, GPIO.OUT)
GPIO.setmode(GPIO.BCM)
GPIO.setup(LEDb, GPIO.OUT)
GPIO.setmode(GPIO.BCM)
GPIO.setup(LEDc, GPIO.OUT)
GPIO.setmode(GPIO.BCM)
GPIO.setup(LEDd, GPIO.OUT)

apikey="Insert Your API Key Here"
town="Nottingham"
town=town.replace(" ","+")
country="uk"
wurl="http://api.openweathermap.org/data/2.5/weather?q="+town+","+country+"&appid="+apikey  #zip
update=60 #seconds

PrecX="600,601,602,611,612,615,616,620,621,622,"
Prec="903,906,200,201,202,210,211,212,221,230,231,232,300,301,302,310,311,312,313,314,321,500,501,502,503,504,511,520,521,522,531,"
WindX="900,901,902,960,961,962,781,905,"
Wind="952,953,954,955,956,957,958,959,"
Cloud="801,802,803,804,"
Fog="701,711,721,731,741,751,761,762,771,"


while True:
    r = requests.get(wurl).content
    GPIO.output(LEDa, False)
    GPIO.output(LEDb, False)
    GPIO.output(LEDc, False)
    GPIO.output(LEDd, False)

    if r.find("\"weather\":")>=0:
        #print(r)
        #print(wurl)
        wc=r.split("\"weather\":")[1]
        wc=wc.split("\"id\":")[1]
        wc=wc.split(",")[0]
        city=r.split("\"name\":\"")[1]
        city=city.split("\"")[0]
        weather=r.split("\"description\":\"")[1]
        weather=weather.split("\"")[0]
        print("City: "+city+" Weather: "+weather)
        if Cloud.find(wc)>=0:
            GPIO.output(LEDb, True)
        if Prec.find(wc)>=0:
            GPIO.output(LEDd, True)
        if Wind.find(wc)>=0:
            GPIO.output(LEDc, True)
        if Fog.find(wc)>=0:
            GPIO.output(LEDa, True)    
        #flash
        stime=time.time()
        while stime+update>=time.time():
            #extreme
            if PrecX.find(wc)>=0:
                GPIO.output(LEDd, False)
            if WindX.find(wc)>=0:
                GPIO.output(LEDc, False)
            time.sleep(1)
            if PrecX.find(wc)>=0:
                GPIO.output(LEDd, True)
            if WindX.find(wc)>=0:
                GPIO.output(LEDc, True)
                time.sleep(1)
    else:
        print("Error: "+r)

Save your project as weather.py (File > Save As) in your Documents folder.

Go to https://home.openweathermap.org/users/sign_up and create an account. You may need to answer two question that appear in a pop window. Now go to https://home.openweathermap.org/api_keys.

Find the api key in the page (this will be a jumble of letters and numbers) and copy this into line 19 between the two quotation marks.

Change the city name on line 20 to your nearest city and save the script (File > Save).

Now open Terminal (Menu > Accessories > Terminal) and type the following command:

python weather.py

The script will check the OpenWeatherMap data service every 60 seconds to get a weather report for your area. It will then display the current weather forecast on your screen and light up the LEDs as follows:

LED 1: This will signal if Fog or Misty weather is forecasted.

LED 2: This will signal if Cloudy weather is forecasted.

LED 3: This will signal if Wind is forecasted. The LED will blink if Extreme Wind is forecasted.

LED 4: This will signal if Rain is forecasted. The LED will blink if SnowSleet or Hale is forecasted.