issue204:micro-ci_micro-la
Différences
Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédente | ||
issue204:micro-ci_micro-la [2024/04/28 07:42] – d52fr | issue204:micro-ci_micro-la [2024/05/01 11:41] (Version actuelle) – andre_domenech | ||
---|---|---|---|
Ligne 10: | Ligne 10: | ||
So let’s talk about some of the possible remedies available to us.** | So let’s talk about some of the possible remedies available to us.** | ||
+ | |||
+ | Plus de plaisir avec les dates et l' | ||
+ | |||
+ | Salut mes amis. Une fois de plus, je viens à vous depuis un endroit tenu secret, quelque part dans le temps et l' | ||
+ | |||
+ | Quoi qu'il en soit, le mois dernier, nous avons abordé le sujet du changement d' | ||
+ | |||
+ | Tout d' | ||
+ | |||
+ | J'ai aussi parlé du fait qu'il y a deux fonctions qui fournissent l' | ||
+ | |||
+ | Parlons donc des remèdes possibles qui s' | ||
+ | |||
**In my search for an easy solution, I came across the fact that the MicroPython group has been working on creating a large library of modules that are useful for programmers writing MicroPython programs. Many of them attempt to mirror standard libraries in the Python world. One of these modules is the datetime module. MicroPython has suffered from the lack of this being in the builds. Well, now, the wait is over (at least for the most part). | **In my search for an easy solution, I came across the fact that the MicroPython group has been working on creating a large library of modules that are useful for programmers writing MicroPython programs. Many of them attempt to mirror standard libraries in the Python world. One of these modules is the datetime module. MicroPython has suffered from the lack of this being in the builds. Well, now, the wait is over (at least for the most part). | ||
Ligne 16: | Ligne 29: | ||
Of course you have to have the datetime module “installed” on your device. There are few ways of doing this. One is my preferred way of getting the entire source code package and copying the necessary modules onto my device. By doing this, I can make sure that any of the dependencies are available and I can examine the code to see how that programmer (or group of programmers) got their code to work. Also if there are any errors being thrown, I can track it down much quicker than relying on a library where I can’t see the source code. You can find the package at https:// | Of course you have to have the datetime module “installed” on your device. There are few ways of doing this. One is my preferred way of getting the entire source code package and copying the necessary modules onto my device. By doing this, I can make sure that any of the dependencies are available and I can examine the code to see how that programmer (or group of programmers) got their code to work. Also if there are any errors being thrown, I can track it down much quicker than relying on a library where I can’t see the source code. You can find the package at https:// | ||
+ | |||
+ | Dans ma recherche d'une solution facile, je suis tombé sur le fait que le groupe MicroPython a travaillé à la création d'une grande bibliothèque de modules utiles pour les programmeurs qui écrivent des programmes en MicroPython. Beaucoup d' | ||
+ | |||
+ | En utilisant le module datetime de micropython-lib dans nos programmes, nous pouvons, avec quelques modifications, | ||
+ | |||
+ | Bien sûr, il faut que le module datetime soit « installé » sur votre appareil. Il y a plusieurs façons de le faire. La première, que je préfère, consiste à obtenir l' | ||
+ | |||
**Copy the datetime.py to your Microcontroller device and put it into the /lib folder or, if you don’t have it, either create the folder and place it there, or just drop it into your main folder on the device. (It’s better to put it into the /lib folder.) | **Copy the datetime.py to your Microcontroller device and put it into the /lib folder or, if you don’t have it, either create the folder and place it there, or just drop it into your main folder on the device. (It’s better to put it into the /lib folder.) | ||
Ligne 30: | Ligne 50: | ||
If you are a “regular” Python programmer, you will recognize the second line, which uses the datetime.datetime.now() method.** | If you are a “regular” Python programmer, you will recognize the second line, which uses the datetime.datetime.now() method.** | ||
+ | |||
+ | Copiez le fichier datetime.py sur votre microcontrôleur et placez-le dans le dossier /lib, ou, si vous ne l'avez pas, créez le dossier et placez-le dedans, ou placez-le simplement dans votre dossier principal sur le microcontrôleur. (Il est préférable de le placer dans le dossier /lib). | ||
+ | |||
+ | L' | ||
+ | |||
+ | Quoi qu'il en soit, je n'ai pas eu l' | ||
+ | |||
+ | my_timezone = timezone(timedelta(hours=10)) | ||
+ | |||
+ | current_time = datetime.datetime.now(my_timezone) | ||
+ | |||
+ | Bien entendu, vous devez importer la bibliothèque avant de pouvoir l' | ||
+ | |||
+ | Si vous êtes un programmeur Python « normal », vous reconnaîtrez la deuxième ligne, qui utilise la méthode datetime.datetime.now(). | ||
+ | |||
**So, for this month’s main project, we’ll connect to the Internet (be sure you’re using a RPi Pico-W), use Peter Hinch’s portable NTPTime module, get the network time, set the Real Time clock on the Pico-W, and then start printing the time in the REPL once every 10 seconds. I’ve named the program ‘timetestFCM1.py’. Make sure that you have the datetime.py or datetime.mpy file either in your main device’s folder or in the lib folder. All of the other libraries needed are “standard” libraries, except for Peter’s portable NTPtime library. I’ve got that in the repository listed at the end of this article. | **So, for this month’s main project, we’ll connect to the Internet (be sure you’re using a RPi Pico-W), use Peter Hinch’s portable NTPTime module, get the network time, set the Real Time clock on the Pico-W, and then start printing the time in the REPL once every 10 seconds. I’ve named the program ‘timetestFCM1.py’. Make sure that you have the datetime.py or datetime.mpy file either in your main device’s folder or in the lib folder. All of the other libraries needed are “standard” libraries, except for Peter’s portable NTPtime library. I’ve got that in the repository listed at the end of this article. | ||
Ligne 43: | Ligne 78: | ||
Really simple huh?** | Really simple huh?** | ||
+ | |||
+ | Ainsi, pour le projet principal de ce mois, nous allons nous connecter à Internet (assurez-vous que vous utilisez un RPi Pico-W), utiliser le module portable NTPTime de Peter Hinch, obtenir l' | ||
+ | |||
+ | Le code | ||
+ | |||
+ | Tout d' | ||
+ | |||
+ | # secret.py | ||
+ | # ========================== | ||
+ | SSID = {Votre nom de réseau ici} | ||
+ | PASSWORD = {votre mot de passe réseau ici} | ||
+ | |||
+ | Vraiment simple, hein ? | ||
+ | |||
**Moving on. Now we’ll need Peter’s portable NTPTime routine. That’s also in the repository saved as port_ntptime.py. Make sure that’s also on your device. | **Moving on. Now we’ll need Peter’s portable NTPTime routine. That’s also in the repository saved as port_ntptime.py. Make sure that’s also on your device. | ||
Ligne 51: | Ligne 100: | ||
Next we have the settime2 function. It’s very similar to the one defined in the NTPTime module, but this one will return a 0 if, for some reason, it can’t set the time correctly. We get the time and date tuple by sending the number of seconds since our EPOCH (from the network time server) to utime.gmtime and then set the software real time clock with that value. If everything worked correctly we return a True, otherwise return a False (next page, top left).** | Next we have the settime2 function. It’s very similar to the one defined in the NTPTime module, but this one will return a 0 if, for some reason, it can’t set the time correctly. We get the time and date tuple by sending the number of seconds since our EPOCH (from the network time server) to utime.gmtime and then set the software real time clock with that value. If everything worked correctly we return a True, otherwise return a False (next page, top left).** | ||
+ | |||
+ | Passons à autre chose. Nous allons maintenant avoir besoin de la routine portable NTPTime de Peter. Elle se trouve également dans le dépôt, enregistrée sous le nom de port_ntptime.py. Assurez-vous qu' | ||
+ | |||
+ | Maintenant (en haut à droite) nous allons commencer à plonger dans le projet timetestFCM1.py. Tout d' | ||
+ | |||
+ | Ensuite vient la fonction connectNetwork. C'est une fonction assez simple que je réutilise souvent. Tout d' | ||
+ | |||
+ | Ensuite, nous avons la fonction settime2. Elle est très similaire à celle définie dans le module NTPTime, mais celle-ci renvoie un 0 si, pour une raison quelconque, elle ne peut pas régler l' | ||
+ | |||
**Finally, we get to begin to play with the datetime module we went to “so much trouble” to get. | **Finally, we get to begin to play with the datetime module we went to “so much trouble” to get. | ||
Ligne 62: | Ligne 120: | ||
FIrst, I define the two timezone offsets used here where I live. Cst is 6 hours behind GMT (starting in the Fall) and cdt is 5 hours behind (Starting in the Spring). I multiply the number of hours by 3600 (The number of seconds in an hour) and since we are behind GMT, I use a negative number.** | FIrst, I define the two timezone offsets used here where I live. Cst is 6 hours behind GMT (starting in the Fall) and cdt is 5 hours behind (Starting in the Spring). I multiply the number of hours by 3600 (The number of seconds in an hour) and since we are behind GMT, I use a negative number.** | ||
+ | |||
+ | Enfin, nous pouvons commencer à jouer avec le module datetime que nous nous sommes donné « tant de mal » à obtenir. | ||
+ | |||
+ | Tout d' | ||
+ | |||
+ | 2024-04-07 02: | ||
+ | |||
+ | Nous aborderons le formatage le mois prochain. | ||
+ | |||
+ | C'est tout pour nos fonctions de support. Nous avons maintenant la routine principale. Celle-ci appelle les différentes fonctions dont nous avons déjà parlé. | ||
+ | |||
+ | Tout d' | ||
+ | |||
**cst=-6*3600 | **cst=-6*3600 | ||
Ligne 80: | Ligne 151: | ||
if which == False: | if which == False: | ||
sys.exit()** | sys.exit()** | ||
+ | | ||
+ | cst=-6*3600 | ||
+ | cdt=-5*3600 | ||
+ | |||
+ | Ensuite, j' | ||
+ | |||
+ | EPOCH = time.gmtime(0)[0] | ||
+ | print(f« {EPOCH=} ») | ||
+ | rtc=machine.RTC() | ||
+ | dti=rtc.datetime() | ||
+ | print(f« {dti=} ») | ||
+ | |||
+ | Maintenant, j'ai ajouté une vérification rapide pour m' | ||
+ | |||
+ | # Check to see if the Pico has network | ||
+ | which=GetWhichPico() | ||
+ | if which == False: | ||
+ | sys.exit() | ||
+ | | ||
**Now we call the connectNetwork function. | **Now we call the connectNetwork function. | ||
Ligne 96: | Ligne 186: | ||
Until next time, as always; stay safe, healthy, positive and creative!** | Until next time, as always; stay safe, healthy, positive and creative!** | ||
+ | Nous appelons maintenant la fonction connectNetwork. | ||
+ | |||
+ | # Connect to the network | ||
+ | connectNetwork() | ||
+ | |||
+ | Maintenant que nous sommes connectés, nous pouvons essayer d' | ||
+ | |||
+ | À ce stade, nous pouvons supposer que nous avons obtenu l' | ||
+ | |||
+ | Si vous êtes assez âgé pour vous souvenir du passage à l'an 2000, vous serez heureux ou effrayé d' | ||
+ | |||
+ | Le dépôt de ce mois-ci se trouve à l' | ||
+ | |||
+ | Jusqu' | ||
+ | |||
+ | |||
+ | |||
+ | // Encart p 43 en haut à droite, lignes noires // | ||
+ | **Now we can define our internal functions. First the PI function GetWhichPico, | ||
+ | Maintenant, nous pouvons définir nos fonctions internes. D' | ||
+ | |||
+ | // Encart p 45 en bas à gauche, lignes noires // | ||
+ | **Finally, we can enter our forever loop, printing the device’s time every 10 seconds.** | ||
+ | Enfin, nous pouvons entrer dans notre boucle sans fin, avec impression de l' | ||
+ | |||
+ | **That’s it. Here is the REPL output…** | ||
+ | C'est tout. Voici la sortie du REPL : |
issue204/micro-ci_micro-la.1714282969.txt.gz · Dernière modification : 2024/04/28 07:42 de d52fr