issue203: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 | ||
issue203:micro-ci_micro-la [2024/04/02 08:09] – d52fr | issue203:micro-ci_micro-la [2024/04/03 12:38] (Version actuelle) – auntiee | ||
---|---|---|---|
Ligne 6: | Ligne 6: | ||
That is a good thing, isn’t it? Well, yes. Most of the time.** | That is a good thing, isn’t it? Well, yes. Most of the time.** | ||
+ | |||
+ | Salutations, | ||
+ | |||
+ | Vous êtes prêt ? C'EST GÉNIAL ! Nous parlons du passage de l' | ||
+ | |||
+ | Vous vous dites peut-être que ce n'est pas très grave, surtout si vous utilisez Thonny comme IDE pour programmer votre appareil. Avez-vous déjà remarqué que lorsque vous obtenez l' | ||
+ | |||
+ | C'est une bonne chose, n' | ||
+ | |||
**Let’s say that you have a program that you are going to test that requires the time and date to be set. You test it under Thonny and it runs just fine. Now, you save the program to the device as main.py so that it will run when the device is powered on. Now you close Thonny and unplug then replug in the device. Assuming the device and your program have some sort of display showing the time, the time will display “00: | **Let’s say that you have a program that you are going to test that requires the time and date to be set. You test it under Thonny and it runs just fine. Now, you save the program to the device as main.py so that it will run when the device is powered on. Now you close Thonny and unplug then replug in the device. Assuming the device and your program have some sort of display showing the time, the time will display “00: | ||
Ligne 14: | Ligne 23: | ||
Originally, I had planned on a full project including code for this article. However, there have been many stumbling blocks keeping me from doing this. Not the least of which is a problem in the ntptime function of MicroPython. It seems that this is across all the types of Microcontrollers, | Originally, I had planned on a full project including code for this article. However, there have been many stumbling blocks keeping me from doing this. Not the least of which is a problem in the ntptime function of MicroPython. It seems that this is across all the types of Microcontrollers, | ||
+ | |||
+ | Supposons que vous souhaitiez tester un programme qui nécessite le réglage de l' | ||
+ | |||
+ | Comment éviter cela ? | ||
+ | |||
+ | Il y a quatre options qui peuvent être activées ou désactivées en cliquant sur les petits carrés (Radiobuttons dans le monde Tkinter). Cliquez sur les deux options centrales (« Synchroniser l' | ||
+ | |||
+ | A l' | ||
+ | |||
**NTPTIME | **NTPTIME | ||
- | The MicroPython firmware has a module called ntptime that, assuming your device supports internet communication, | + | The MicroPython firmware has a module called ntptime that, assuming your device supports internet communication, |
- | To make matters a bit crazier, there are two versions of gmtime and localtime. There is one set in the time module and one in the utime module. Currently they all point to the same values.** | + | NTPTIME |
- | **So, if you are interested in getting the correct time for wherever your Microcontroller is located at that moment, you have to play a bunch of games. | + | Le micrologiciel de MicroPython dispose d'un module appelé ntptime qui, en supposant que votre appareil supporte la communication Internet, se rendra à une adresse Internet spéciale et demandera l' |
+ | |||
+ | |||
+ | **To make matters a bit crazier, there are two versions of gmtime and localtime. There is one set in the time module and one in the utime module. Currently they all point to the same values. | ||
+ | |||
+ | So, if you are interested in getting the correct time for wherever your Microcontroller is located at that moment, you have to play a bunch of games. | ||
So, since there is no consensus on this subject, let’s look at the games we can play to get the values that we want/need, at least for now. | So, since there is no consensus on this subject, let’s look at the games we can play to get the values that we want/need, at least for now. | ||
Ligne 29: | Ligne 52: | ||
User MrEbbinghaus posted a quick solution on August 31, 2023 at https:// | User MrEbbinghaus posted a quick solution on August 31, 2023 at https:// | ||
- | I copied the small function (since I already have time imported in my code) and named it “localtime2”. That way, there is no naming conflict. | + | I copied the small function (since I already have time imported in my code) and named it “localtime2”. That way, there is no naming conflict.** |
+ | Pour compliquer les choses, il existe deux versions de gmtime et localtime. L'une est définie dans le module time et l' | ||
- | Now, you would think that the TZ_OFFSET value should be simply the hour offset, either positive or negative based on your offset. Here where I live, “Standard” time is 6 hours behind UTC and “Daylight Saving Time” is 5 hours behind UTC. So I should use -6 as the TZ_OFFSET during “Standard” time and -5 as the offset during “Daylight Saving Time”, right?** | + | Ainsi, si vous souhaitez obtenir l' |
- | **NOOOOOOOOOO | + | Puisqu' |
+ | |||
+ | La solution la plus simple à ce stade est d' | ||
+ | |||
+ | L' | ||
+ | |||
+ | J'ai copié la petite fonction (puisque j'ai déjà importé le temps dans mon code) et je l'ai nommée « localtime2 ». De cette façon, il n'y a pas de conflit de nom. | ||
+ | |||
+ | |||
+ | **Now, you would think that the TZ_OFFSET value should be simply the hour offset, either positive or negative based on your offset. Here where I live, “Standard” time is 6 hours behind UTC and “Daylight Saving Time” is 5 hours behind UTC. So I should use -6 as the TZ_OFFSET during “Standard” time and -5 as the offset during “Daylight Saving Time”, right? | ||
+ | |||
+ | NOOOOOOOOOO | ||
You have to convert that hour offset to seconds first. WHAT? | You have to convert that hour offset to seconds first. WHAT? | ||
Ligne 40: | Ligne 75: | ||
Yep. Now, we all know that there are 60 seconds in a minute and 60 minutes in an hour, so that would be 60*60*-6 (-21600) or 60*60*-5 (-18000). THOSE are the numbers you need to send into the “replacement” localtime function . | Yep. Now, we all know that there are 60 seconds in a minute and 60 minutes in an hour, so that would be 60*60*-6 (-21600) or 60*60*-5 (-18000). THOSE are the numbers you need to send into the “replacement” localtime function . | ||
- | To make matters worse, the ntptime module is NOT very user friendly. Nor is the NTP Timeserver. Many times, the ntptime server doesn’t return anything at all and sometimes it returns a value that will crash your program. When that happens, the “current thinking” is that the NTP server is sending you a KoD or “Kiss-’o-Death” packet that basically says you have made too many calls in too short a time. | + | To make matters worse, the ntptime module is NOT very user friendly. Nor is the NTP Timeserver. Many times, the ntptime server doesn’t return anything at all and sometimes it returns a value that will crash your program. When that happens, the “current thinking” is that the NTP server is sending you a KoD or “Kiss-’o-Death” packet that basically says you have made too many calls in too short a time.** |
- | According to the RFC4430: Best Practices document for the NTP servers: “A client MUST NOT under any conditions use a poll interval less than 15 seconds.”** | + | On pourrait penser que la valeur TZ_OFFSET est simplement le décalage horaire, positif ou négatif en fonction de votre décalage. Là où je vis, l' |
- | **Not something that is known by just everyone! But, ok. How can we handle this better than a try/Except loop? | + | NOOOOOONNNN |
+ | |||
+ | Il faut d' | ||
+ | |||
+ | Oui. Nous savons tous qu'il y a 60 secondes dans une minute et 60 minutes dans une heure, ce qui donne 60*60*-6 (-21600) ou 60*60*-5 (-18000). Ce sont ces nombres que vous devez envoyer à la fonction de « remplacement » de l' | ||
+ | |||
+ | Pour ne rien arranger, le module ntptime n'est PAS très convivial. Il en va de même pour le serveur de temps NTP. Souvent, le serveur ntptime ne renvoie rien du tout et parfois il renvoie une valeur qui fera planter votre programme. Lorsque cela se produit, la « pensée actuelle » est que le serveur NTP vous envoie un paquet KoD ou « Kiss-' | ||
+ | |||
+ | |||
+ | **According to the RFC4430: Best Practices document for the NTP servers: “A client MUST NOT under any conditions use a poll interval less than 15 seconds.” | ||
+ | |||
+ | Not something that is known by just everyone! But, ok. How can we handle this better than a try/Except loop? | ||
It seems that Peter Hinch (who I’ve mentioned more than once in my MTMT articles) has created a ‘portable’ NTP module of his own. You can find it at https:// | It seems that Peter Hinch (who I’ve mentioned more than once in my MTMT articles) has created a ‘portable’ NTP module of his own. You can find it at https:// | ||
Ligne 53: | Ligne 99: | ||
There’s a discussion of this at https:// | There’s a discussion of this at https:// | ||
+ | |||
+ | Selon le document RFC4430 : Best Practices document for the NTP servers (Document des bonnes pratiques pour les serveurs NTP) « Un client NE DOIT EN AUCUN CAS utiliser un intervalle d' | ||
+ | |||
+ | Ce n'est pas quelque chose qui est connu de tout le monde ! Mais d' | ||
+ | |||
+ | Il semble que Peter Hinch (que j'ai mentionné plus d'une fois dans mes articles sur MCML) ait créé son propre module NTP « portable ». Vous pouvez le trouver à l' | ||
+ | |||
+ | Je copierais le code et le placerais dans un module à part entière avec un nom comme « port_ntptime ». | ||
+ | |||
+ | Ce qui est intéressant avec le module de Peter, c'est qu'il permet non seulement de passer une valeur de fuseau horaire (toujours en secondes), mais qu'il renvoie également soit la bonne valeur de temps (en secondes), soit un 0 si le serveur NTP n'a pas répondu à temps. | ||
+ | |||
+ | Il y a une discussion à ce sujet à https:// | ||
+ | |||
**Unfortunately, | **Unfortunately, | ||
Ligne 67: | Ligne 126: | ||
Until next time, as always; stay safe, healthy, positive and creative!** | Until next time, as always; stay safe, healthy, positive and creative!** | ||
+ | |||
+ | Malheureusement, | ||
+ | |||
+ | trials=10 | ||
+ | while trials > 0 : | ||
+ | | ||
+ | |||
+ | lt est la valeur de retour. Vérifiez si elle est égale à 0 ou non. Si c'est le cas, appelez la fonction ntptime.settime(). Si ce n'est pas le cas, attendez 15 secondes, puis réessayez (en haut à droite). | ||
+ | |||
+ | La fonction ntptime.settime() forcera le temps corrigé dans utime.gmtime. Une fois de plus, le problème est que gmtime et localtime sont identiques ; vous devez donc vous rappeler que lorsque vous voulez utiliser localtime, vous devez utiliser la fonction localtime2. | ||
+ | |||
+ | J' | ||
+ | |||
+ | Jusqu' | ||
+ |
issue203/micro-ci_micro-la.1712038148.txt.gz · Dernière modification : 2024/04/02 08:09 de d52fr