Weather Assistant | Learn Python with Graphical Programming for Beginners 14

0 2994 Medium

Checking the weather and choosing appropriate clothing are essential for travel preparations. Today, we will use the UNIHIKER to create a "Weather Assistant" that can help you check the weather in the city you plan to visit.

Goal

Display the current city, date, weather code, and temperature on the UNIHIKER screen.

projectImage

Get to Know

1. The basics of API in programming

2. The usage of dictionary in programming

3. How to obtain weather information

Preparations

Hardware:

HARDWARE LIST
1 UNIHIKER
1 Type-C&Micro 2-in-1 USB Cable

Software: Mind+

Download address: https://www.mindplus.cc

projectImage

Hands-on Practice

The main function of the Weather Assistant is to obtain and display the weather and temperature of a city through the Internet. How can we achieve this? Next, we will complete two tasks to accomplish this.

Task 1: Obtaining Weather Data

In this task, we will learn how to obtain and display the current weather condition using the "Open-Meteo" platform.

Task 2: Displaying Specified Weather Information

Based on the obtained weather data, we will further learn about dictionary, complete data filtering, and display the required weather information.

Task 1: Obtaining Weather Data

1. Hardware Setup

Connect the UNIHIKER to the computer using a USB cable.

projectImage
2. Software Preparation

Open Mind+, and complete the software preparation according to the figure below.

projectImage
3. Programming

The weather data displayed on mobile phones is obtained through the internet, and the same is true for the UNIHIKER. Next, let's learn about the methods of obtaining weather data.

Weather data is obtained through an "interface." In this lesson, we will use "Open-Meteo" to obtain weather data.

Note: For information about "interfaces," please refer to the "Knowledge Base."

1.First, visit the Open-Meteo website (https://open-meteo.com/); after entering the website, click "Documentation" to enter the weather forecast API interface.

projectImage

2. After entering the weather forecast interface, scroll down the page and find "API URL (Open in new tab)" below "Settings". This is the API URL that we need to use.

projectImage

We can select the desired weather data by changing the parameters of the API URL endpoint. The necessary endpoint parameters can be found in the "API Documentation" section.

projectImage
projectImage

For example, the API endpoint for the current weather conditions of the current location.

Original:

https://api.open-meteo.com/v1/forecast?latitude=52.52&longitude=13.41&hourly=temperature_2m

Modified:

https://api.open-meteo.com/v1/forecast?latitude=52.52&longitude=13.41¤t_weather=true

To change the weather data for a different city, simply modify the latitude and longitude parameters in the API endpoint. There are two ways to find the latitude and longitude of a city:

Method 1: Select the "Select Coordinates or City" option at the top and click "Select city". Then enter the name of the city you want to find in the search box. For example, if you enter "Washington," the related city locations will automatically appear. Click on the desired location and the latitude and longitude will automatically change to the coordinates of that city.

projectImage

The latitude and longitude of the interface address will also automatically switch to the latitude and longitude of that city.

projectImage

Clicking "Detect GPS Position" will directly switch to the longitude and latitude information of the city you are in.

projectImage

Method 2: Use Google Maps. Enter the name of the location you want to find, such as "Washington D.C.", in Google Maps. After clicking on the search button, the latitude and longitude coordinates will appear in the website address.

projectImage

So the URL for the current weather conditions in Washington is:

https://api.open-meteo.com/v1/forecast?latitude=38.90&longitude=-77.04¤t_weather=true; You can also copy and access this URL to view the weather data for this city. Can you understand the meaning of the data in the information?

projectImage

The weather data has been obtained. How can it be incorporated into the program? This requires sending a request to Open-Meteo through the "Requests module" to obtain the data.

Click on "Extensions" → "Official Library", locate "Requests" and add it.

projectImage

Next, you can find the corresponding command under the "Requests" category. In this lesson, we will use the make a get request command to send a request to Open-Meteo and the parse request content as format to transform the retrieved data into the format we saw on the webpage earlier.

projectImage

Note: For the block parse request content as format, there are three available formats: text format as a string, binary format as a binary string (bytes type), and JSON format as a packaged data format that can be found using keywords. For ease of data processing, we choose JSON format here.

To better understand the code, we can create three variables, url, response, and data, to respectively store the API URL, request response, and weather data.

projectImage

In order to more conveniently modify the city and weather data that we want to obtain, we need to separate the main part of the API URL, the city's latitude and longitude information, and the endpoint parameters. We can use two join commands to combine the main part of the API URL, city latitude and longitude information, and endpoint parameters.

projectImage

Next, you can directly display the data variable on the UNIHIKER board. To see the complete data of the current weather condition, you can set the font size and text width for display. The complete reference code is as follows:

projectImage
4. Run the program

Before running the code, you need to use the method from the previous class to connect the UNIHIKER board to a wireless network.

Click "run" and wait for the data to be successfully obtained. The data on the current weather conditions will be displayed in the center of the screen.

projectImage

Task 2: Display the specified weather information

1. Write a Program

In Task 1, we have successfully obtained weather data, but the displayed content is too complex. Actually, we only need "city", "date", "weathercode", and "temperature". So we need to filter out the information we need from the variable data. How can we do this?

Well, the data format you see is "dictionary" in Python. It is a data type that binds the contents before and after the ":". You can obtain the "value" after the ":" through the "key" before the ":".

Note: For knowledge about "dictionary" in programs, please refer to the "Knowledge Base".

projectImage

You can find the dictionary-related blocks under "Dictionary" category in the command area. To obtain the "value", we need to use the command dictionary {} key "" value and fill in the contents of the "dictionary" and "key". For example, to obtain the date in the figure above, we need to first create a variable data_weather and use dictionary {} key "Age" value to obtain the value under the "current_weather" category.

projectImage

Then, use the dictionary {} key "Age" value to obtain the value of "time".

projectImage

Then, you can display the obtained date information on the UNIHIKER screen.

projectImage

Similarly, the weather, temperature information, and weather code of the city can be obtained using the keys "time", "weathercode", and "temperature". And you can also add some text prompts when displaying. The complete reference code is as follows:

projectImage
2. Run the Program

Check the connection status of the UNIHIKER, click "Run", wait a moment, and the city name, date, weather code, and temperature filled in the code will be displayed on the screen.

projectImage

You can learn about the current weather conditions according to the Weather variable documentation. The weather code in the figure above is 1, corresponding to weather conditions of "Mainly clear, partly cloudy, and overcast".

projectImage

Knowledge Base

1. Interfaces in Programs

An application programming interface (API) is a program that provides some functionality or data. You can think of it as a defined function, where you do not need to access the source code or know the implementation details. Instead, you simply provide the required parameters and call the function, which will return the data you need. For example, the website address for "Open-Meteo" corresponds to the parameters. By entering the website address to access the website and calling the corresponding function, you can obtain weather data.

projectImage

The purpose of API is to make it easier for programmers to implement more applications. As a result, input parameters and coding conventions for interfaces need to be more standardized. In fact, interfaces can be used in many scenarios, such as using system-defined functions and commands within a program, or downloading and installing libraries written by other companies or developers. You can even remotely call more powerful functions through a network platform, just like in this lesson, where we used the "Weather API" to obtain weather data.

2. Dictionary in Programs

A dictionary is one of the basic data types in Python programs, and it is often used to represent a one-to-one relationship between data, such as city and latitude/longitude, name and ID number, or skills and damage points in a game. A dictionary is defined by curly braces "{}", and when there is nothing inside the braces, it represents an empty dictionary. When there is data, the data is in the form of "key-value" pairs, with each pair written as "key: value", and multiple pairs separated by commas.

Note: All symbols are English symbols.

projectImage

In the blocks, find the dictionary operation blocks under the "Dictionary" category in the command area. For the example in the figure above, we can use the initialize dictionary { : } and my variable to create a dictionary, and then use the dictionary my variable key 'Washington DC" value to read the latitude and longitude of "Washington DC" shown in the figure from the "my variable" variable.

projectImage

3. Command Learning

In this project, we mainly use network request and dictionary-related commands. Next, we will study them in detail.

projectImage

Challenge

In this project, you have completed the task of obtaining weather information for different cities. Now try designing the interface layout and completing the following tasks:

(1) Add the button for switching cities to realize the effect: the screen displays the weather information for the current city at the beginning, and when you switch to a different city, updates to show the data for the newly selected city once button A is pressed;

(2) Use the images in the image source folder to match different weather conditions with different images. Of course, you can also display different information on your UNIHIKER screen. For the interface design, you can refer to the figure below.

projectImage

Tip: To display the city names and weather images, you can use dictionary variables to match numbers with city names and weather codes with image names (for weather codes, see the figure Weather variable documentation in Task 2). You can refer to the figure below to complete the initial setting of the two variables:

projectImage
icon Lesson 14. Weather Assistant.rar 1.13MB Download(19)
License
All Rights
Reserved
licensBg
0