We live under the approach of the Raleigh-Durham airport. Planes fly over around 2300 feet - we can hear them clearly. Why wouldn’t I want to know what flight it is?
It takes two things.
One, a very simple (and re-usable) piston that takes a text argument and speaks over Alexa Speaks.
Two is a python script that runs on a Raspberry Pi, below.
-Edit the first url boundary box to be the GPS coordinates you care about (it’s a box, so takes 2 lattitude and 2 longitude)
-Edit the second url to be the execute url of your Webcore announcement piston.
-Edit the airport code from RDU to the airport by you
-Edit the three file paths if the script is running from anywhere besides /home/pi/Documents/
Set it up in crontab to run every two minutes. Attached. Oh, and grab the airport-codes.csv from github: https://github.com/datasets/airport-codes/blob/master/data/airport-codes.csv
and put in the same directory as the script.
import json
import requests
import time
import csv
##get flight data for a bounding box over our house in Raleigh
##bounds=north,south,west,east
url = 'https://data-live.flightradar24.com/zones/fcgi/feed.js?bounds=35.9881,35.9116,-78.7739,-78.6480,-78.7739&faa=1&mlat=1&flarm=1&adsb=1&gnd=0&air=1&vehicles=0&estimated=0&gliders=0&stats=0'
s = requests.Session()
s.headers.update({'User-Agent':'Mozilla/5.0','referer':'http://foo.bar.horse:3000/'})
response=s.get(url)
data = json.loads(response.text)
##iterate through results; ignore the full_count and version;
##everything else is a flight; creates a list of lists
flightslist = []
for key in data:
if key != "full_count" and key != "version":
value = data[key]
flightslist.append(value)
##read the translation file to change airport codes to city name
with open("/home/pi/Documents/airports.json", "r") as read_file:
airportcodes = json.load(read_file)
#load previous flight history into dict
flighthistoryold = {}
with open("/home/pi/Documents/flights.csv") as f:
for line in f:
(key, val) = line.split(',')
flighthistoryold[key] = val.rstrip()
print(flighthistoryold)
#create dict for flights in this current run
flighthistorynew = {}
##iterate through the flights in flight list; items 11-13 are
##airport code from, airport code to, and flight number
for flight in flightslist:
flightnumber = flight[13]
airportfrom = flight[11]
airportto = flight[12]
##filter down to just the ones that are commercial and landing at RDU
if airportto == "RDU" and flightnumber != "" and airportfrom != "":
#add to flighthistorynew dict so we don't repeat announcements
flighthistorynew[flightnumber] = time.time()
#check if this flight number was seen last time
if flightnumber not in flighthistoryold:
##parses the airport translation file down to airport code matching departure
cityfrom = [x for x in airportcodes if x['iata'] == airportfrom]
##there can be only one matching airport code, so grab the city from first record
cityfrom = cityfrom[0]['city']
#build the announcement string; listify the stringcolonate flightnumber
#to pronounce better from Alexa
announcement = "Here comes flight " + ":".join(flightnumber) + " arriving from " + cityfrom
print(announcement)
##call webCore piston; pass announcement as argument
url = 'https://graph-na02-useast1.api.smartthings.com/api/token/xxxxxxsmartapps/installations/yyyyyyyyy/execute/:0zzzzzzzzzzz:'
response=requests.get(url + "?text='" + announcement + "'")
#write out new flights - or lack thereof - into a file
w = csv.writer(open("/home/pi/Documents/flights.csv", "w"))
for key, val in flighthistorynew.items():
w.writerow([key, val])
End result: around the time I hear a plane flying over, I hear Alexa say something like “Here comes flight C27508 arriving from Wilmington”. Because why the heck wouldn’t I?
Enjoy!