issue203: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 | ||
issue203:micro-ci_micro-la [2024/03/30 10:16] – créée auntiee | issue203:micro-ci_micro-la [2024/04/03 12:38] (Version actuelle) – auntiee | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
- | Greetings fellow Beings. I hope you have already read my How-To Python in the Real World #149 in this issue, because that is the actual beginning of this article. Consider this the second half of it. So if not, please jump back to the contents page (usually page #2) and read that first. I’ll wait. | + | **Greetings fellow Beings. I hope you have already read my How-To Python in the Real World #149 in this issue, because that is the actual beginning of this article. Consider this the second half of it. So if not, please jump back to the contents page (usually page #2) and read that first. I’ll wait. |
Ready now? GREAT! We’re talking about Time change from Standard Time to Daylight Saving time and back to Standard time. Why is that important? Well, if you have a Microcontroller that is dealing with time sensitive logging or needs to do something at a particular time, your device needs to know what the actual correct time is at all times. | Ready now? GREAT! We’re talking about Time change from Standard Time to Daylight Saving time and back to Standard time. Why is that important? Well, if you have a Microcontroller that is dealing with time sensitive logging or needs to do something at a particular time, your device needs to know what the actual correct time is at all times. | ||
Ligne 5: | Ligne 5: | ||
You might be thinking to yourself that this isn’t a big deal, especially if you use Thonny as your IDE for programming your device. Have you ever noticed that when you get the current time on your device is correct? That’s because, by default, Thonny sets the time on the device and more times than not, it is also set, again by default, to use the local time from the computer you are running Thonny on. | You might be thinking to yourself that this isn’t a big deal, especially if you use Thonny as your IDE for programming your device. Have you ever noticed that when you get the current time on your device is correct? That’s because, by default, Thonny sets the time on the device and more times than not, it is also set, again by default, to use the local time from the computer you are running Thonny on. | ||
- | 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.** |
- | 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: | + | 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: | ||
How do we keep that from happening? | How do we keep that from happening? | ||
Ligne 13: | Ligne 22: | ||
There are four options that can be turned on or off by clicking in the small squares (Radiobuttons in the Tkinter world). Click on the two center options (‘Synchronize device’s real time clock’ and ‘Use local time in the real time clock’) to turn off the options. Then restart Thonny. | There are four options that can be turned on or off by clicking in the small squares (Radiobuttons in the Tkinter world). Click on the two center options (‘Synchronize device’s real time clock’ and ‘Use local time in the real time clock’) to turn off the options. Then restart Thonny. | ||
- | 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 | ||
+ | |||
+ | The MicroPython firmware has a module called ntptime that, assuming your device supports internet communication, | ||
NTPTIME | NTPTIME | ||
- | The MicroPython | + | Le micrologiciel de MicroPython |
- | 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. | + | |
+ | **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, 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. | ||
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' |
+ | |||
+ | 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 | NOOOOOOOOOO | ||
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' |
+ | |||
+ | 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? | Not something that is known by just everyone! But, ok. How can we handle this better than a try/Except loop? | ||
Ligne 52: | Ligne 98: | ||
The nice thing about Peter’s module, is that not only can you pass a timezone value (again in seconds) it also will return either the proper time value (in seconds) OR a 0 if the NTP Server failed to respond in time. | The nice thing about Peter’s module, is that not only can you pass a timezone value (again in seconds) it also will return either the proper time value (in seconds) OR a 0 if the NTP Server failed to respond in time. | ||
- | There’s a discussion of this at https:// | + | There’s a discussion of this at https:// |
- | Unfortunately, | + | 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, | ||
trials=10 | trials=10 | ||
Ligne 66: | Ligne 125: | ||
Hopefully, this will take the problems away from my code I want to present next month and allow me to get it tested completely without it crashing. | Hopefully, this will take the problems away from my code I want to present next month and allow me to get it tested completely without it crashing. | ||
- | 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.1711790194.txt.gz · Dernière modification : 2024/03/30 10:16 de auntiee