issue167:python
Différences
Ci-dessous, les différences entre deux révisions de la page.
Prochaine révision | Révision précédente | ||
issue167:python [2021/03/28 11:26] – créée auntiee | issue167:python [2021/04/04 15:38] (Version actuelle) – andre_domenech | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
- | First things first. Last month, I gave you a quick overview of the RTK.GPIO board, so I thought I’d give you a quick update. I tried some “normal” GPIO type tests, and they worked well. However, when I tried an I2C program to a simple I2C device, it failed to run. I’ll try to keep playing and let you know. | + | **First things first. Last month, I gave you a quick overview of the RTK.GPIO board, so I thought I’d give you a quick update. I tried some “normal” GPIO type tests, and they worked well. However, when I tried an I2C program to a simple I2C device, it failed to run. I’ll try to keep playing and let you know. |
- | Now, on to the Raspberry Pi Pico board. Yes it did come in. The darn thing is so tiny, and I’m so old with bad eyesight, that it was hard for me to solder the pins for the breadboard. I did it, without any shorts (or melting the board), but it took me twice as long as it would have 4 years ago. It worked pretty well, I must say. There are lots of possibilities for the RP2040 chipset and I can see many powerful microcontroller boards coming out in the future based on it. As they say in Texas, “They done did GOOD!” (I don’t say that, but ‘they’ do). | + | Now, on to the Raspberry Pi Pico board. Yes it did come in. The darn thing is so tiny, and I’m so old with bad eyesight, that it was hard for me to solder the pins for the breadboard. I did it, without any shorts (or melting the board), but it took me twice as long as it would have 4 years ago. It worked pretty well, I must say. There are lots of possibilities for the RP2040 chipset and I can see many powerful microcontroller boards coming out in the future based on it. As they say in Texas, “They done did GOOD!” (I don’t say that, but ‘they’ do). ** |
- | This month, we’ll revisit free weather APIs on the Internet. Why? Well many of them have either closed down or gone to a full pay model, so the choices have changed. When I’m sitting in my living room, I want to be able to check the weather outside and get the forecast for my location. Since I live in an apartment, a proper freestanding weather station is just not possible, so I have to rely on an outside source. | + | Commençons par le commencement. Le mois dernier, je vous ai donné un rapide aperçu de la carte RTK.GPIO ; j'ai pensé maintenant vous donner une rapide mise à jour. J'ai essayé quelques tests de type « normaux » sur les GPIO, et ils ont bien fonctionné. Cependant, lorsque j'ai essayé un programme I2C vers un simple périphérique I2C, il n'a pas fonctionné. Je vais essayer de continuer à jouer et je vous tiendrai au courant. |
+ | |||
+ | Maintenant, passons à la carte Raspberry Pi Pico. Oui, elle est arrivée. Cette satanée chose est si petite, et je suis si vieux avec une mauvaise vue, qu'il était difficile pour moi de souder les broches pour le circuit d' | ||
+ | |||
+ | |||
+ | **This month, we’ll revisit free weather APIs on the Internet. Why? Well many of them have either closed down or gone to a full pay model, so the choices have changed. When I’m sitting in my living room, I want to be able to check the weather outside and get the forecast for my location. Since I live in an apartment, a proper freestanding weather station is just not possible, so I have to rely on an outside source. | ||
I did a quick check the other day and found one that has a plan that I can get behind. It’s called Weather API, and you can visit their homepage at https:// | I did a quick check the other day and found one that has a plan that I can get behind. It’s called Weather API, and you can visit their homepage at https:// | ||
- | They offer return data in either XML or JSON format. We’ll explore both formats, and some of the data that is returned. | + | They offer return data in either XML or JSON format. We’ll explore both formats, and some of the data that is returned.** |
- | Before you can get anything, you need to sign up for a free API key. Point your favorite browser to https:// | + | Ce mois-ci, nous allons revenir sur les API météo gratuites sur Internet. Pourquoi ? Eh bien, beaucoup d' |
+ | |||
+ | J'ai fait une vérification rapide l' | ||
+ | |||
+ | Les retours de données sont au format XML ou JSON. Nous allons explorer les deux formats, ainsi que certaines des données qui sont renvoyées. | ||
+ | |||
+ | |||
+ | **Before you can get anything, you need to sign up for a free API key. Point your favorite browser to https:// | ||
As we have done before, this API has you build the http query with the query type, the location you want, and your API key. Here is what the simplest format (with my API key obscured) looks like... | As we have done before, this API has you build the http query with the query type, the location you want, and your API key. Here is what the simplest format (with my API key obscured) looks like... | ||
+ | http:// | ||
+ | |||
+ | Avant de pouvoir obtenir quoi que ce soit, vous devez vous inscrire pour avoir une clé API gratuite. Dirigez votre navigateur préféré vers https:// | ||
+ | |||
+ | Comme nous l' | ||
http:// | http:// | ||
- | Here’s the breakdown of the URL: | + | |
+ | **Here’s the breakdown of the URL: | ||
Base URL Address: | Base URL Address: | ||
Ligne 27: | Ligne 45: | ||
The URL can easily be constructed from variables and an “f-string”... | The URL can easily be constructed from variables and an “f-string”... | ||
+ | |||
+ | Base = ‘http:// | ||
+ | |||
+ | Query = ‘current.json’ | ||
+ | |||
+ | Key = ‘? | ||
+ | |||
+ | Location = ‘& | ||
+ | |||
+ | link = f" | ||
+ | |||
+ | Voici le détail de l'URL : | ||
+ | |||
+ | Adresse URL de base : http:// | ||
+ | |||
+ | Type de requête : current.json | ||
+ | Votre clé API : ? | ||
+ | |||
+ | Emplacement : & | ||
+ | |||
+ | Paramètres supplémentaires (le cas échéant) | ||
+ | |||
+ | L'URL peut facilement être créée à partir de variables et d'une « f-string »... | ||
Base = ‘http:// | Base = ‘http:// | ||
Ligne 38: | Ligne 79: | ||
link = f" | link = f" | ||
- | The location can be “US Zipcode, UK Postcode, Canada Postalcode, IP address, Latitude/ | + | |
+ | **The location can be “US Zipcode, UK Postcode, Canada Postalcode, IP address, Latitude/ | ||
You can paste the URL into the browser of your choice, and the result looks something like the code shown right. | You can paste the URL into the browser of your choice, and the result looks something like the code shown right. | ||
Ligne 44: | Ligne 86: | ||
You can ask for the data to be returned in JSON format (as we did here), or XML format. | You can ask for the data to be returned in JSON format (as we did here), or XML format. | ||
- | Here is the link to the API docs: https:// | + | Here is the link to the API docs: https:// |
- | Before we get into code, let’s take a quick look at the forecast call. | + | L' |
+ | |||
+ | Vous pouvez coller l'URL dans le navigateur de votre choix, et le résultat ressemble au code illustré à droite. | ||
+ | |||
+ | Vous pouvez demander que les données soient renvoyées au format JSON (comme nous l' | ||
+ | |||
+ | Voici le lien vers la documentation de l'API : https:// | ||
+ | |||
+ | |||
+ | **Before we get into code, let’s take a quick look at the forecast call. | ||
http:// | http:// | ||
Ligne 54: | Ligne 105: | ||
So the big changes from our previous current-weather query is the “forecast.json” instead of “current.json” string, and the addition of days, aqi=yes (aqi stands for Air Quality Index), and the alerts=yes. There is also a language option that you can use, but you should look at the documentation to get the correct parameter. | So the big changes from our previous current-weather query is the “forecast.json” instead of “current.json” string, and the addition of days, aqi=yes (aqi stands for Air Quality Index), and the alerts=yes. There is also a language option that you can use, but you should look at the documentation to get the correct parameter. | ||
- | When we send this query to the API, you’ll get back a LARGE amount of data. I’ll just paste a portion here (below). | + | When we send this query to the API, you’ll get back a LARGE amount of data. I’ll just paste a portion here (below).** |
- | You get the location information and current condition information, | + | Avant d' |
+ | |||
+ | http:// | ||
+ | |||
+ | Pour le compte API gratuit, vous pouvez avoir une prévision pour un maximum de 3 jours. Le nombre de jours dépend du niveau de compte auquel vous avez souscrit. | ||
+ | |||
+ | Les principaux changements par rapport à notre précédente requête de météo actuelle sont donc la chaîne « forecast.json » au lieu de « current.json », l' | ||
+ | |||
+ | Lorsque nous envoyons cette requête à l'API, nous obtenons en retour une GRANDE quantité de données. Je vais en coller juste une partie ici (ci-dessous). | ||
+ | |||
+ | |||
+ | **You get the location information and current condition information, | ||
Next comes the forecast information. Under the general [‘forecast’] header, you get a forecastday group of data for each of the days that you requested (3 in this case), each of which starts with “date” and “date_epoch”, | Next comes the forecast information. Under the general [‘forecast’] header, you get a forecastday group of data for each of the days that you requested (3 in this case), each of which starts with “date” and “date_epoch”, | ||
- | It’s a lot of data – which is why I used JSON format for the ease of picking out what I want on any call. | + | It’s a lot of data – which is why I used JSON format for the ease of picking out what I want on any call.** |
- | The Code | + | Vous obtenez les informations de localisation et les informations sur les conditions actuelles, tout comme lorsque nous avons effectué la demande des conditions actuelles. En plus de cela, vous obtenez les informations sur la qualité de l'air, puisque nous les avons demandées (en bas à gauche). |
+ | |||
+ | Viennent ensuite les informations sur les prévisions. Sous l' | ||
+ | |||
+ | Cela fait beaucoup de données, c'est pourquoi j'ai utilisé le format JSON pour faciliter la sélection de ce que je veux à chaque appel. | ||
+ | |||
+ | |||
+ | **The Code | ||
Now we can concentrate on the code. Luckily, it is very similar to the logic that we’ve used before. As always, we start with our imports... | Now we can concentrate on the code. Luckily, it is very similar to the logic that we’ve used before. As always, we start with our imports... | ||
Ligne 76: | Ligne 145: | ||
That’s it. Very simple and easily modifiable to add or delete elements as you wish – to customize the output to your needs. | That’s it. Very simple and easily modifiable to add or delete elements as you wish – to customize the output to your needs. | ||
- | I’ve placed the code on my github repository at https:// | + | I’ve placed the code on my github repository at https:// |
- | One final thing. Last month, I promised that I would give my first impressions of the Raspberry Pi Pico microcontroller. I had originally intended doing the update here, but even before I got the Pico board in, I was so excited that I talked to Ronnie about creating another series of articles under a separate title. He said that it would be no problem, so I will now be trying to do two articles a month, one on “standard Python” and one on working with Microcontrollers using MicroPython and CircuitPython. I’m sure that every once-in-a-while, | + | Le code |
+ | |||
+ | Nous pouvons maintenant nous concentrer sur le code. Heureusement, | ||
+ | |||
+ | import requests | ||
+ | |||
+ | import json | ||
+ | |||
+ | Nous définissons maintenant certaines de nos variables. | ||
+ | |||
+ | Lorsque nous exécutons le programme, la sortie ressemblera à l' | ||
+ | |||
+ | C'est tout. Très simple et facilement modifiable pour ajouter ou supprimer des éléments comme vous le souhaitez, pour adapter le résultat à vos besoins. | ||
+ | |||
+ | J'ai placé le code sur mon dépôt github à https:// | ||
+ | |||
+ | |||
+ | **One final thing. Last month, I promised that I would give my first impressions of the Raspberry Pi Pico microcontroller. I had originally intended doing the update here, but even before I got the Pico board in, I was so excited that I talked to Ronnie about creating another series of articles under a separate title. He said that it would be no problem, so I will now be trying to do two articles a month, one on “standard Python” and one on working with Microcontrollers using MicroPython and CircuitPython. I’m sure that every once-in-a-while, | ||
Ligne 84: | Ligne 170: | ||
- | As always, until next time; stay safe, healthy, positive and creative! | + | As always, until next time; stay safe, healthy, positive and creative!** |
+ | |||
+ | Une dernière chose. Le mois dernier, j'ai promis que je donnerais mes premières impressions sur le microcontrôleur Raspberry Pi Pico. J' | ||
+ | |||
+ | |||
+ | Ne manquez pas de consulter ma nouvelle série d' | ||
+ | |||
+ | |||
+ | Comme toujours, jusqu' | ||
issue167/python.1616923594.txt.gz · Dernière modification : 2021/03/28 11:26 de auntiee