I’m trying to add a UV index to my weather station, being a Pico W running MicroPython.
I can get the json data ok but I’m struggling to break out the UVI number, being the first UVI number in the json string.
Insights appreciated!
# 9/3/25 - UVI from https://currentuvindex.com/api
# v1 - Get the json data.
# v2 - Extract the uvi number
import urequests # Network Request Module
import json # JSON handling module
# Define the API endpoint and your API key
api_url = "https://currentuvindex.com/api/v1/uvi?"
api_key = "" # Replace with your actual API key
# Define the latitude and longitude for Caloundra as got from https://latlong.info/australia/queensland/caloundra
latitude = -26.80346000
longitude = 153.12195000
# Construct the URL
request_url = f"{api_url}latitude={latitude}&longitude={longitude}"
print(request_url)
# Make the API request
response = urequests.get(request_url) # Send a GET request, the return type is the response of the request.
# Check if the request was successful
if response.status_code == 200:
uvi_data = response.json()
print("UVI Data:", uvi_data)
print("Full UVI Data Response:")
print(json.dumps(uvi_data)) # Print the entire JSON response.
print(f'UVI: {uvi_data['uvi']}')
elif response.status_code == 400:
print("Bad request. Please check your parameters and try again.")
elif response.status_code == 401:
print("Unauthorized. Please check your API key.")
elif response.status_code == 404:
print("Resource not found. Please check the URL.")
else:
print("Failed to fetch UVI data. Status code:", response.status_code)
# Close the response to free up resources
response.close()
What is the json data that you are getting? What is the code you are using to convert the json data into a dictionary with the json.loads() method? That should return you a dictionary which you can use to lookup the UVI by label.
import urequests # Network Request Module
import json
import ujson
# Define the API endpoint and your API key
api_url = "https://currentuvindex.com/api/v1/uvi?"
api_key = "" # Replace with your actual API key
# Define the latitude and longitude for Caloundra as got from https://latlong.info/australia/queensland/caloundra
latitude = -26.80346000
longitude = 153.12195000
# Construct the URL
request_url = f"{api_url}latitude={latitude}&longitude={longitude}"
print("URL response is " + request_url)
# Make the API request
response = urequests.get(request_url) # Send a GET request, the return type is the response of the request.
# Check if the request was successful
if response.status_code == 200:
uvi_data = response.json() # Write the response to the variable.
print("UVI Data:", uvi_data) # Print same for testing.
print(f'UVI Now: {uvi_data['now']}') # Print the "now" array for testing.
uvi_data_now = uvi_data['now'] # Get the "now" array.
uvi_data_now_value = uvi_data_now['uvi'] # Get the "uvi" key value.
print("UVI Now Number: " + str(uvi_data_now_value)) # Print the "now", "uvi" key value.
elif response.status_code == 400:
print("Bad request. Please check your parameters and try again.")
elif response.status_code == 401:
print("Unauthorized. Please check your API key.")
elif response.status_code == 404:
print("Resource not found. Please check the URL.")
else:
print("Failed to fetch UVI data. Status code:", response.status_code)
# Close the response to free up resources
response.close()