Outils pour utilisateurs

Outils du site


issue186:micro-ci

It’s been a while since I’ve been able to write a ‘Micro This Micro That’ article. I apologize that it’s been so long. Too many things have been in my way, but at least for this month, I can provide you with something. I also know that I had promised a long time ago that we would complete the compass app and I still intend to do that, but so much has changed in the Microcontroller world, I felt that I needed to discuss some of the new things. With any luck, the next ‘Micro This Micro That’ article will finish that. For this month, we’ll be discussing the new Raspberry Pi Pico board, the Pico-W. You can find out a good bit of information from https://www.raspberrypi.com/documentation/microcontrollers/raspberry-pi-pico.html. This webpage covers the entire RPi Pico family, the Original board, the Pico-H, and the Pico-W. We’ll ignore the Pico-H for now, other than to say that the Pico-H is pretty much the same board as the original Pico, but it has a three-pin keyed socket for the Serial wire debug connections. Here is the pinout for the Pico-W

Cela fait un moment que je n'ai pas pu écrire un article dans la rubrique « Micro-ci micro-là ». Je m'excuse d'avoir été si longtemps absent. Trop de choses se sont mises en travers de mon chemin, mais, au moins pour ce mois-ci, je peux vous fournir quelque chose.

Je sais aussi que j'avais promis il y a longtemps que nous terminerions l'application de boussole et j'ai toujours l'intention de le faire, mais tant de choses ont changé dans le monde des microcontrôleurs que j'ai senti que je devais présenter certaines des nouveautés. Et, avec un peu de chance, le prochain article de « Micro-ci micro-là » finira cela.

Ce mois-ci, nous allons parler de la nouvelle carte Raspberry Pi Pico, la Pico-W. Vous pouvez trouver un bon nombre d'informations sur https://www.raspberrypi.com/documentation/microcontrollers/raspberry-pi-pico.html. Cette page Web couvre toute la famille RPi Pico, la carte originale, la Pico-H et la Pico-W. Nous allons ignorer la Pico-H pour l'instant, sauf pour dire qu'elle est à peu près la même carte que la Pico originale, mais qu'elle a une prise à trois broches avec verrouillage pour les connexions de débogage de la liaison série. Voici le brochage du Pico-W (à gauche).

You can see that the pinout is pretty much the same as the original Pico, so, for the most part, you can use the software you’ve already developed, but you need to get the latest MicroPython version that is designed to support the Pico-W. You can get that from the website above or from the MicroPython.org site. The process of flashing the board is the same as the original board, so I won’t bore you with that. One of the new things that the Pico-W brings to the table is that the onboard LED pin is not connected to a GPIO pin on the board, but is connected to a GPIO pin on the wireless module (and the MicroPython version has been modified to handle it). So it makes it much easier to access it in your code. Here is a quick demo program that shows how to do it. import machine import time led=machine.Pin(“LED”,machine.Pin.OUT) led.on() time.sleep(3) led.off()

Vous pouvez voir que le brochage est à peu près le même que celui de la Pico originale ; ainsi, dans l'ensemble, vous pouvez utiliser les logiciels que vous avez déjà développés, mais vous devez obtenir la dernière version de MicroPython qui est conçue pour supporter la Pico-W. Vous pouvez l'obtenir sur le site Web ci-dessus ou sur le site MicroPython.org. Le processus de flashage de la carte est le même que pour la carte originale et je ne vous ennuierai pas avec ça.

L'une des nouveautés du Pico-W est que le contact de la LED intégrée n'est pas connecté à un picot GPIO de la carte, mais à une broche GPIO du module sans fil (et la version de MicroPython a été modifiée pour le gérer). Il est donc beaucoup plus facile d'y accéder dans votre code. Voici un programme de démonstration rapide qui montre comment faire :

import machine

import time

led=machine.Pin(“LED”,machine.Pin.OUT)

led.on()

time.sleep(3)

led.off()

As you can see, it is very simple. We define the led object, then we simply call the led.on() and led.off methods to turn on and off the led. You can use this to provide a visual indication that you have a wireless connection. Since the pinout of the Pico-W is pretty much the same as the original, we can use our original i2cscan program to probe the i2c bus and provide the address of all the i2c devices attached to our Pico-W (shown previous page, top right). As I usually do, I connected the sda (data) line to physical pin 8 and the scl (clock) line to physical pin 9. When I ran the program I got the following output. 0x19 25 0x1e 30 0x40 64 Address 0x19 is the lsm303 accelerator and 0x1e is the lsm303 magnetometer. Address 0x40 is the SI7021 temperature/humidity sensor. (See, I AM going to finish the compass project!). Since there are now three RPi Pico boards, how can you tell which device you are working with (other than looking at it)?

Comme vous pouvez le voir, c'est très simple. Nous définissons l'objet led, puis nous appelons simplement les méthodes led.on() et led.off() pour allumer et éteindre la LED. Vous pouvez l'utiliser pour fournir une indication visuelle que vous avez une connexion sans fil.

Puisque le brochage de la Pico-W est à peu près le même que celui de l'original, nous pouvons utiliser notre programme i2cscan original pour sonder le bus i2c et fournir l'adresse de tous les périphériques i2c connectés à notre Pico-W (voir page précédente, en haut à droite).

Comme je le fais habituellement, j'ai connecté la ligne sda (data) à la broche physique 8 et la ligne scl (clock) à la broche physique 9. Lorsque j'ai lancé le programme, j'ai obtenu la sortie suivante :

0x19 25 0x1e 30 0x40 64

L'adresse 0x19 est l'accéléromètre lsm303 et 0x1e est le magnétomètre lsm303. L'adresse 0x40 est le capteur de température/humidité SI7021. (Vous voyez, je vais finir le projet de la boussole !)

Puisqu'il y a maintenant trois cartes RPi Pico, comment pouvez-vous savoir avec quel périphérique vous travaillez (autrement qu'en le regardant) ?

The micropython versions for the Pico boards now support a sys method named implementation (shown top right). Of course you have to import the sys module first. I threw together a simple program that shows how to access the information. When you run the program, here’s what it returns. (name='micropython', version=(1, 19, 1), _machine='Raspberry Pi Pico W with RP2040', _mpy=4102) Raspberry Pi Pico W with RP2040 Pico with Wireless So, which1 returns a tuple that holds the fact that we are using Micropython, the version of Micropython installed, the type of machine, and a value for _mpy. The important part of this is the value located at position 2 of the tuple (remember this is zero based). Which1[2] returns a string which we can use the in operator of Python. So by checking for “Pico W” within the string, if we are running on a Pico-W, we will get back a True. This whole thing can be distilled down to a simple function (shown bottom right). And we get back True. I’ve saved this as WhichPico2.py. Now, let’s look at how to work with the wireless portion of the Pico-W. It is very similar to what we’ve done with the ESP-32 and the ESP-8266 microcontrollers. As a minimum, you need to import the network, time and a special file named secret.py.

Les versions de Micropython pour les cartes Pico supportent maintenant dans sys une méthode nommée implementation (montrée en haut à droite). Bien sûr, vous devez d'abord importer le module sys. J'ai créé un programme simple qui montre comment accéder à l'information.

Lorsque vous exécutez le programme, voici ce qu'il retourne :

(name='micropython', version=(1, 19, 1), _machine='Raspberry Pi Pico W with RP2040', _mpy=4102) Raspberry Pi Pico W with RP2040 Pico with Wireless

Ainsi, which1 renvoie un tuple qui contient le fait que nous utilisons Micropython, la version de Micropython installée, le type de machine et une valeur pour _mpy. La partie importante est la valeur située à la position 2 du tuple (rappelez-vous que la première vaut zéro). Which1[2] renvoie une chaîne de caractères sur laquelle nous pouvons utiliser l'opérateur in de Python. Ainsi, en vérifiant la présence de « Pico W » dans la chaîne, si nous fonctionnons sur un Pico-W, nous obtiendrons un True.

Tout cela peut se résumer à une simple fonction (en bas à droite).

Et on obtient un True. J'ai enregistré cette fonction sous le nom de WhichPico2.py.

Maintenant, regardons comment travailler avec la partie sans fil du Pico-W. C'est très similaire à ce que nous avons fait avec les microcontrôleurs ESP-32 et ESP-8266.

Au minimum, vous devez importer le réseau, l'heure et un fichier spécial nommé secret.py.

Secret.py contains # secret.py # ============= SSID = “” PASSWORD = “” This way, you can keep the wireless access point and your password secret and have to provide only this sample file to anyone you share your code with. Be sure to modify the two values on your Pico-W before trying to run the program. Once you have the imports section complete, you need to add only the following code (previous page, middle right). We create the wlan object and set the active method to True in order to connect. Then we read the SSID and PASSWORD values from the secrets.py file and pass these to the connect method. We then need to wait until we get a True value from the isconnected method and a status greater than 0. This can sometimes take a few seconds, so be patient. Once we are connected, we print the status and the ifconfig information, and we can carry on with what we need to do. Here is the output from the program 3 ('192.168.1.195', '255.255.255.0', '192.168.1.1', '192.168.1.1')

Secret.py contient

# secret.py #

SSID = “” PASSWORD = “”

De cette façon, vous pouvez garder secrets le point d'accès sans fil et votre mot de passe et n'avoir à fournir que cet exemple de fichier à toute personne avec qui vous partagez votre code. Veillez à modifier les deux valeurs sur votre Pico-W avant d'essayer d'exécuter le programme.

Une fois la section des importations terminée, il ne vous reste plus qu'à ajouter le code suivant (page précédente, au milieu à droite).

Nous créons l'objet wlan et mettons la méthode active à True afin de nous connecter. Ensuite, nous lisons les valeurs SSID et PASSWORD du fichier secrets.py et les passons à la méthode connect. Nous devons ensuite attendre jusqu'à ce que nous obtenions une valeur True de la méthode isconnected et un statut supérieur à 0. Cela peut parfois prendre quelques secondes, alors soyez patient. Une fois connectés, nous imprimons l'état et les informations de ifconfig, et nous pouvons poursuivre ce que nous avons à faire.

Voici le résultat du programme :

3

('192.168.1.195', '255.255.255.0', '192.168.1.1', '192.168.1.1')

The status value of 3 means that we are properly connected to the wireless network. The ifconfig information is (from left to right) IP address, netmask, gateway, DNS. Here are all the possible values for the status method. // Return value of cyw43_wifi_link_status #define CYW43_LINK_DOWN (0) #define CYW43_LINK_JOIN (1) #define CYW43_LINK_NOIP (2) #define CYW43_LINK_UP (3) #define CYW43_LINK_FAIL (-1) #define CYW43_LINK_NONET (-2) #define CYW43_LINK_BADAUTH (-3) I’ve created a simple program that rolls all of the test programs we’ve used into one. I’ve named it net1a.py. I’ve changed most of the comments into explanation lines here, to make it easier for the editors of FCM to include the full code. import machine import network import time import secret import sys Check that the board is a Pico-W with wireless support (previous page, top right).

La valeur d'état 3 signifie que nous sommes correctement connectés au réseau sans fil. Les informations d'ifconfig sont (de gauche à droite) l'adresse IP, le masque de réseau, la passerelle, le DNS. Voici toutes les valeurs possibles pour la méthode status :

// Valeur retournée par cyw43_wifi_link_status #define CYW43_LINK_DOWN (0) #define CYW43_LINK_JOIN (1) #define CYW43_LINK_NOIP (2) #define CYW43_LINK_UP (3) #define CYW43_LINK_FAIL (-1) #define CYW43_LINK_NONET (-2) #define CYW43_LINK_BADAUTH (-3)

J'ai créé un programme simple qui regroupe tous les programmes de test que nous avons utilisés en un seul. Je l'ai nommé net1a.py. J'ai changé la plupart des commentaires en lignes d'explication ici, pour faciliter l'inclusion du code complet par les éditeurs du FCM.

import machine import network import time import secret import sys

Vérifiez que la carte est une Pico-W avec support sans-fil (page précédente, en haut à droite).

First, set up the led object and turn off the led (just in case). Next, set up all the things we need to do for the wireless support, including getting the SSID and PASSWORD from the secret.py file, and call the wlan.connect with these values (previous page, bottom right). In the event the board is NOT a Pico-W, print that we can’t deal with wireless on this board and end the program (top right). So that’s using the RPi Pico-W networking in a nutshell. The site I mentioned earlier has a very nice PDF on using the wireless portion of the Pico-W at https://datasheets.raspberrypi.com/picow/connecting-to-the-internet-with-pico-w.pdf, as well as other important information about the board. I’ve set up a github repository for the code that we’ve used this month. You can find it at https://github.com/gregwa1953/FCM-185_MicroThisMicroThat. Until next time, as always; stay safe, healthy, positive and creative!

Tout d'abord, configurez l'objet led et éteignez la LED (juste au cas où). Ensuite, configurez tout ce que nous devons faire pour le support du sans-fil, y compris récupérer SSID et PASSWORD dans le fichier secret.py, et appelez wlan.connect avec ces valeurs (page précédente, en bas à droite).

Dans le cas où la carte n'est pas une Pico-W, indiquez que nous ne pouvons pas gérer le sans-fil sur cette carte et terminez le programme (en haut à droite).

Voilà donc en quelques mots comment utiliser le réseau de la RPi Pico-W. Le site que j'ai mentionné précédemment contient un très bon PDF sur l'utilisation de la partie sans fil de la Pico-W à https://datasheets.raspberrypi.com/picow/connecting-to-the-internet-with-pico-w.pdf, ainsi que d'autres informations importantes sur la carte.

J'ai mis en place un dépôt github pour le code que nous avons utilisé ce mois-ci. Vous pouvez le trouver à https://github.com/gregwa1953/FCM-185_MicroThisMicroThat.

Jusqu'à la prochaine fois, comme toujours, restez en sécurité, en bonne santé, positif et créatif !

Dans les encadrés : p 35, en haut : If the board actually is a Pico-W, we can try to connect to the wireless network: Si la carte est vraiment une Pico-W, nous pouvons essayer de nous connecter au résau sans fil :

p 53, en bas : Now loop until we get connected: Maintenant, boucler jusqu'à être connecté :

Print the status value and the ifconfig values, and turn the onboard LED on: Imprimer la valeur d'état et les valeurs de ifconfig, puis allumer la LED intégrée :

p 36 : When the program runs (assuming it is a Pico-W board), here is the output from my machine. Quand le programme tourne (en supposant que c'est une carte Pico-W), voici la sortie de ma machine.

issue186/micro-ci.txt · Dernière modification : 2022/11/03 12:03 de andre_domenech