issue204:micro-ci_micro-la
Différences
Ci-dessous, les différences entre deux révisions de la page.
Prochaine révision | Révision précédente | ||
issue204:micro-ci_micro-la [2024/04/28 07:22] – créée d52fr | issue204:micro-ci_micro-la [2024/05/01 11:41] (Version actuelle) – andre_domenech | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
- | More fun with dates and time | + | **More fun with dates and time |
Greetings fellow Beings. Yet again, I come to you from an undisclosed location somewhere in time and space. Yes, I am a fan of all things Doctor Who, Star Trek, Star Wars, and other things. | Greetings fellow Beings. Yet again, I come to you from an undisclosed location somewhere in time and space. Yes, I am a fan of all things Doctor Who, Star Trek, Star Wars, and other things. | ||
Ligne 9: | Ligne 9: | ||
I also talked about the fact that there are two functions that provide the current time (once it’s set) named gmtime and localtime, and the fact that they both give GMT time. There is no timezone information handled by either of these functions. | I also talked about the fact that there are two functions that provide the current time (once it’s set) named gmtime and localtime, and the fact that they both give GMT time. There is no timezone information handled by either of these functions. | ||
- | 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.** |
- | 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). | + | 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). | ||
By using the micropython-lib datetime module in our programs, we can, with a few modifications, | By using the micropython-lib datetime module in our programs, we can, with a few modifications, | ||
- | 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.) |
The other way, which is a bit easier but only provides you the compressed mpy file, is to use mip. Here is a link to using mip: https:// | The other way, which is a bit easier but only provides you the compressed mpy file, is to use mip. Here is a link to using mip: https:// | ||
Ligne 29: | Ligne 49: | ||
Of course, you need to import the library before you can call it, but we’ll see that in this month’s first project. | Of course, you need to import the library before you can call it, but we’ll see that in this month’s first project. | ||
- | 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.** |
- | 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. | + | 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. | ||
The Code | The Code | ||
Ligne 42: | Ligne 77: | ||
PASSWORD = {Your Network Password Here} | PASSWORD = {Your Network Password Here} | ||
- | Really simple huh? | + | Really simple huh?** |
- | 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. | + | 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. | ||
Now (top right) we’ll start digging into the timetestFCM1.py project. First the imports. It will seem to be a REALLY big number of imports, but it’s not much. | Now (top right) we’ll start digging into the timetestFCM1.py project. First the imports. It will seem to be a REALLY big number of imports, but it’s not much. | ||
Ligne 50: | Ligne 99: | ||
Next comes the connectNetwork function. This is a fairly straight forward function that I reuse often. First, I set up the onboard LED of the Pico (name it “led”), then make sure it’s off. Next I start to set up the network modes, send in the SSID and Password to the wlan object and loop until we get connected. Once we are connected to our local network, we turn the onboard LED on (next page, top right). | Next comes the connectNetwork function. This is a fairly straight forward function that I reuse often. First, I set up the onboard LED of the Pico (name it “led”), then make sure it’s off. Next I start to set up the network modes, send in the SSID and Password to the wlan object and loop until we get connected. Once we are connected to our local network, we turn the onboard LED on (next page, top right). | ||
- | 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. |
First we need to set a value for our timezone, which in my case is -5 hours from GMT. Once we go through the time change again, this will be a -6 until the next time change. Then we take that and use it to seed the datetime.datetime.now() function, which returns a time string like this… | First we need to set a value for our timezone, which in my case is -5 hours from GMT. Once we go through the time change again, this will be a -6 until the next time change. Then we take that and use it to seed the datetime.datetime.now() function, which returns a time string like this… | ||
Ligne 61: | Ligne 119: | ||
That’s it for all of our support functions. Now we have the main routine. This makes calls to the various functions that we’ve already discussed. | That’s it for all of our support functions. Now we have the main routine. This makes calls to the various functions that we’ve already discussed. | ||
- | 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.** |
- | cst=-6*3600 | + | 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 | ||
cdt=-5*3600 | cdt=-5*3600 | ||
Ligne 75: | Ligne 146: | ||
Now, I threw in a quick check to make sure this program was running on a Pico-W. If not, well, there is no need to continue on, since we can’t connect to a network. We simply call sys.exit() if it isn’t a Pico-W. | Now, I threw in a quick check to make sure this program was running on a Pico-W. If not, well, there is no need to continue on, since we can’t connect to a network. We simply call sys.exit() if it isn’t a Pico-W. | ||
+ | |||
+ | # Check to see if the Pico has network | ||
+ | which=GetWhichPico() | ||
+ | if which == False: | ||
+ | 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 | # Check to see if the Pico has network | ||
Ligne 80: | Ligne 169: | ||
if which == False: | if which == False: | ||
sys.exit() | sys.exit() | ||
+ | | ||
- | Now we call the connectNetwork function. | + | **Now we call the connectNetwork function. |
# Connect to the network | # Connect to the network | ||
Ligne 94: | Ligne 184: | ||
The repository for this month is located at https:// | The repository for this month is located at https:// | ||
- | 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.1714281752.txt.gz · Dernière modification : 2024/04/28 07:22 de d52fr