RGB LED Sectional Chart Weather

I worked on this project for my brother. It was inspired by something he found online. The idea is to mount a paper aviation sectional chart (it’s like a special map) on the wall and place RGB LEDs at the locations of airports you care about on the chart. Every so often, a Pi checks the weather conditions at those airports and changes the colors of the LEDs, allowing you to see the weather conditions at the various airports in your area at a glance, right there on the wall.

I designed a custom circuit board for interfacing with a NeoPixel light strand I got from Amazon. The circuit uses a level-shifted chip to adjust the Pi’s GPIO output 3v3 voltage to the 5v level expected by the NeoPixels. The circuit also incorporates a barrel-plug adapter and a 5x20mm fuse holder. It’s pictured below and can be viewed on EasyEDA here: https://easyeda.com/ditchwater/sectional-chart-board

I wrote some Python code that will hit the US’s aviation weather web service to get the weather data and update the lights. I’ll post the code here. I made a last-minute untested change to it, but it should be pretty close to working. The code is permissively licensed (https://opensource.org/licenses/MIT). It is based on the CircuitPython library here: https://github.com/adafruit/Adafruit_CircuitPython_NeoPixel.

Note: Make sure you pass the station identifiers to the web service in all capitals. Otherwise, you may get a confusing error saying station_id is not a valid field.

# Copyright 2019 Kyle Hansen
# 
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
# 
# The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

import json
import urllib.request
import urllib.parse
from neopixel import *
import xml.etree.ElementTree as etree
from ledgfx import gfx

PURPLE = (155,0,155)
RED    = (15,0,0)
BLUE   = (0,0,155)
GREEN  = (0,155,0)

# LED strip configuration:
LED_COUNT      = 18      # Number of LED pixels.
LED_PIN        = 18      # GPIO pin connected to the pixels (18 uses PWM!).
#LED_PIN        = 10      # GPIO pin connected to the pixels (10 uses SPI /dev/spidev0.0).
LED_FREQ_HZ    = 800000  # LED signal frequency in hertz (usually 800khz)
LED_DMA        = 5      # DMA channel to use for generating signal (try 10)
LED_BRIGHTNESS = 255     # Set to 0 for darkest and 255 for brightest
LED_INVERT     = False   # True to invert the signal (when using NPN transistor level shift)
LED_CHANNEL    = 0       # set to '1' for GPIOs 13, 19, 41, 45 or 53


def get_color(ceiling, visibility):

    try:
        visibility = float(visibility)
    except Error as e:
        print('ERROR could not parse visibility: %s, defaulting to 10 SM' % visibility)
        visibility = 10.0
        
    if visibility < 1:
        return PURPLE
    elif visibility >= 1 and visibility < 3:
        return RED
    elif visibility >= 3 and visibility < 5:
        return BLUE

    # at this point visibility must be OK, so check ceiling
    if ceiling is None:
        return GREEN
        
    try:
        ceiling = int(ceiling)
    except:
        print('ERROR could not parse ceiling: %s, defaulting to 3000' % ceiling)
        ceiling = 3000
        
    if ceiling < 500:
        return PURPLE
    elif ceiling >= 500 and ceiling < 1000:
        return RED
    elif ceiling >= 1000 and ceiling < 3000:
        return BLUE
    else:
        return GREEN

with open('airports.json') as f:
  airports_json = f.read().upper()
airports = json.loads(airports_json)

request_string = 'https://aviationweather.gov/adds/dataserver_current/httpparam?dataSource=metars&requestType=retrieve&format=xml&stationString=%s&hoursBeforeNow=2&mostRecentForEachStation=constraint&fields=station_id,sky_cover,cloud_base_ft_agl,visibility_statute_mi' % '%20'.join([airport for airport, led_index in airports.items()])
print(request_string)
 
f = urllib.request.urlopen(request_string)
response = f.read().decode('utf-8')
print(response)
root = etree.fromstring(response)

# Create NeoPixel object with appropriate configuration.
strip = Adafruit_NeoPixel(LED_COUNT, LED_PIN, LED_FREQ_HZ, LED_DMA, LED_INVERT, LED_BRIGHTNESS, LED_CHANNEL)
# Intialize the library (must be called once before other functions).
strip.begin()
for i in range(LED_COUNT):
    strip.setPixelColorRGB(i, 0, 0, 0)

for metar in root.iter('METAR'):
    station_id = metar.find('station_id').text.upper()
    print(station_id)
    ceiling = metar.find('sky_condition').get('cloud_base_ft_agl')
    visibility = metar.find('visibility_statute_mi').text
    print('Visibility, sm : %s' % visibility)
    print('Ceiling, ft agl: %s' % ceiling)
    print('Setting light %s to %s' % (airports[station_id], get_color(ceiling, visibility)))
    color = get_color(ceiling, visibility)
    strip.setPixelColorRGB(airports[station_id], color[0], color[1], color[2])

strip.show()