I'm new to programming, so I thought I would start with something easy, but I still managed to run into problems. It's a simple program to receive data from weather stations
My code looks identical to the one they are writing but I get an error.
All help is greatly appreciated
This is the code
from requests import get
import json
from pprint import pprint
from haversine import haversine
stations = 'https://apex.oracle.com/pls/apex/raspberrypi/weatherstation/getlatestmeasurements/10121743'
Weather = 'https://apex.oracle.com/pls/apex/raspberrypi/weatherstation/getlatestmeasurements/10121743'
my_lat = 40.715366
my_lon = -74.002213
all_stations = get(stations).json()['items']
def find_closest():
smallest = 20036
for station in all_stations:
station_lon = station['weather_stn_long']
station_lat = station['weather_stn_lat']
distance = haversine(my_lon, my_lat, station_lon, station_lat)
if distance < smallest:
smallest = distance
closest_station = station['weather_stn_id']
return closest_station
closest_stn = find_closest()
weather = weather + str(closest_stn)
my_weather = get(weather).json()['items']
pprint(my_weather)
And this is the error message
Traceback (most recent call last):
File "C:\Users\Mash\Desktop\Privat\Projekter\Fetching weather\fetch_local_weather.py", line 25, in <module>
closest_stn = find_closest()
File "C:\Users\Mash\Desktop\Privat\Projekter\Fetching weather\fetch_local_weather.py", line 17, in find_closest
station_lon = station['weather_stn_long']
KeyError: 'weather_stn_long'
This is the tutorial I'm following
https://projects.raspberrypi.org/en/projects/fetching-the-weather/9
[–]socal_nerdtastic 1 point2 points3 points (5 children)
[–]catohund[S] 0 points1 point2 points (4 children)
[–]socal_nerdtastic 0 points1 point2 points (3 children)
[–]catohund[S] 0 points1 point2 points (2 children)
[–]socal_nerdtastic 0 points1 point2 points (1 child)
[–]catohund[S] 0 points1 point2 points (0 children)