🌐Get weather

Learn how Misty can interact with the Web, request data and analyze them!

In this project, Misty will use the command send_external request to connect with Weatherstack and request your city's data.

from mistyPy.Robot import Robot
import json

misty = Robot("YOUR_ROBOT_IP_ADDRESS")
misty.set_default_volume(20)
misty.change_led(0, 255, 0)
misty.move_head(0, 0, 0)
misty.display_image("e_Joy.jpg")

access_key = "<your-weatherstack-key>" #follow instructions to get your API key
query = "<your_city>" #for example: Stockholm

data = misty.send_external_request("GET", "http://api.weatherstack.com/current?access_key="+ access_key+"&query="+ query)
if data.status_code == 200: # success code
    parsed = json.loads(data.text) # transform the result in json

    #locate your variables
    weather_descriptions = parsed["current"]["weather_descriptions"][0]
    city = parsed["location"]["name"]
    temperature = parsed["current"]["temperature"]

    #create Misty animations with your data!
    print(f"Just letting you know it's {temperature} and {weather_descriptions} in {city}")
    misty.speak(f"Just letting you know it's {temperature} and {weather_descriptions} in {city}")
else:
    print("Failed to get data")

In the first lines of Misty's code, we import the usual libraries and the JSON one.

We need to import this library because the data from Weatherstack are sent in that format.

After initializing the Misty robot and the other variables, like the access_key and the query we run the misty.send_external_request command that will use the GET method to request the weather data in your city.

The following statement tests if the request succeeded and if yes proceeds to organize the data and assign the value to the variables that you'll use in your Misty's animations. Otherwise, you'll get an error message and Misty's skill will finish.

Weatherstack key

Weatherstack is an online weather data API service that provides real-time weather information, historical data, and weather forecasts for various locations around the world. It is commonly used by developers to integrate weather data into applications and websites. The service is known for its reliability, ease of use, and wide coverage, making it popular for various weather-based applications and projects.

  • Navigate in Weatherstack

  • Click on "START USING THE API"

  • Sign up with your credentials and your plan (we used the free version, which gives only 250 API requests)

  • Click on "VISIT DASHBOARD"

  • and you'll see your API Key with many other information.

Weatherstack data

In this example, we requested the weather in Stockholm.

There are three main areas:

  • request: contains the information about your request

  • location: contains the information about your location like the name of the city, latitude, longitude and local time when the request was sent

  • current: contains the information about the weather in your city like temperature, weather descriptions, wind details, humidity and many more data

{
  "request": {
    "type": "City",
    "query": "Stockholm, Sweden",
    "language": "en",
    "unit": "m"
  },
  "location": {
    "name": "Stockholm",
    "country": "Sweden",
    "region": "Stockholms Lan",
    "lat": "59.333",
    "lon": "18.050",
    "timezone_id": "Europe/Stockholm",
    "localtime": "2024-10-04 09:52",
    "localtime_epoch": 1728035520,
    "utc_offset": "2.0"
  },
  "current": {
    "observation_time": "07:52 AM",
    "temperature": 10,
    "weather_code": 113,
    "weather_icons": [
      "https://cdn.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0001_sunny.png"
    ],
    "weather_descriptions": [
      "Sunny"
    ],
    "wind_speed": 6,
    "wind_degree": 297,
    "wind_dir": "WNW",
    "pressure": 1020,
    "precip": 0,
    "humidity": 67,
    "cloudcover": 6,
    "feelslike": 9,
    "uv_index": 3,
    "visibility": 10,
    "is_day": "yes"
  }
}

You can refer to the data in Python in this way:

parsed = json.loads(data.text)

variable = parsed["<area_name>"]["detail_name"]

If you want to extrapolate the weather description, that's an array, you can refer to the position index for that element. In this case, the weather description has only one element in the array so we'll refer to the first element (position index = 0)

weather_descriptions = parsed["current"]["weather_descriptions"][0]

Last updated