Différences entre versions de « RASP-SENSE-HAT-ASTRO-PI-Temperature »

De MCHobby - Wiki
Sauter à la navigation Sauter à la recherche
Ligne 89 : Ligne 89 :
 
== Afficher la température sur la matrice LED ==
 
== Afficher la température sur la matrice LED ==
  
Think about how you could show the temperature information on the LED matrix in some way (see the LED Matrix guide for more information). The obvious choice would be to use the show_message function, but, while this would work, there are probably better ways to do it. For example, you could:
+
Think about how you could show the temperature information on the LED matrix in some way (voyez [[RASP-SENSE-HAT-ASTRO-PI-Matrice|le guide consacré à la Matrice LED pour plus d'information]]). The obvious choice would be to use the show_message function, but, while this would work, there are probably better ways to do it. For example, you could:
  
 
* Use the {{fname|clear}} function to display some predefined colours based on ranges that the temperature falls in. For example 0 to 5 degrees could be blue?
 
* Use the {{fname|clear}} function to display some predefined colours based on ranges that the temperature falls in. For example 0 to 5 degrees could be blue?
Ligne 114 : Ligne 114 :
 
           for y in range(temp, 8):
 
           for y in range(temp, 8):
 
               sense.set_pixel(x, y, 0, 0, 0)</nowiki>
 
               sense.set_pixel(x, y, 0, 0, 0)</nowiki>
 +
 +
It works by subtracting the minimum value from the measured value which should give a number between 0 and 8. We then use two nested {{fname|for}} loops. The outer loop is for the {{fname|x}} axis and the two inner loops are for the {{fname|y}} axis. We use two loops here because we want to turn all the LEDs below the measurement red with {{fname|set_pixel}} and those above it off. That way the bar will appear to move up and down the y axis following the measured temperature.
 +
 +
Remember that you can use {{fname|sense.set_rotation(n)}} (where n is 0, 90, 180 or 270) at the start of the program just after {{fname|sense.clear()}} if you want to change the orientation of the bar.
  
 
{{RASP-SENSE-HAT-ASTRO-PI-TRAILER}}
 
{{RASP-SENSE-HAT-ASTRO-PI-TRAILER}}

Version du 14 septembre 2015 à 08:08


MCHobby investit du temps et de l'argent dans la réalisation de traduction et/ou documentation. C'est un travail long et fastidieux réalisé dans l'esprit Open-Source... donc gratuit et librement accessible.
SI vous aimez nos traductions et documentations ALORS aidez nous à en produire plus en achetant vos produits chez MCHobby.

Senseur de température

Le Sense HAT met en oeuvre un certain nombre de senseurs... dont un senseur de température.

Le source anglaise de cette traduction se trouve ici www.raspberrypi.org/learning/astro-pi-guide/sensors/temperature.md

RASP-SENSE-HAT-ASTRO-PI-Temperature-00.jpg

L'image ci-dessus présente un thermomètre clinique. Il sert à prendre votre température (par exemple dans la bouche) afin de savoir si vous êtes malade. Notez que les nombres commencent 35, ce thermomètre ne peut donc être utilisé que pour mesurer la température humaine. Le senseur de température du Sense HAT peut mesurer des températures aussi basse que -40 degrés Celsius jusqu'a +120 degrés Celsius, ce senseur est nettement plus flexible que le thermomètre clinique.

Le Sense HAT dispose de deux senseurs de températures. L'un d'entre eux est incorporé au senseur d'humidité et l'autre est incorporé au senseur de pression. Vous pouvez choisir lequel des deux vous voulez utiliser pour relever la température ou vous pouvez utiliser les deux pour faire une moyenne de la température relevée.

Qu'est ce que la température?

1. Ouvrez Python 3 depuis un terminal en tapant la commande suivante:

sudo idle3 &

2. Saisissez le code suivant dans la nouvelle fenêtre:

from sense_hat import SenseHat

sense = SenseHat()
sense.clear()

temp = sense.get_temperature()
print(temp)

3. Selectionnez File > Save (Fichier > Sauver) et choisissez un nom pour votre programme.

4. Selectionnez Run > Run module Executer > Exécuter module.

5. Si vous voyez l'erreur Humidity Init Failed, alors exécutez le programme en tant que root en utilisant sudo (voyez la dernière ligne en rouge du message), cela signifie que vous n'avez pas scrupuleusement suivit les instructions ci-dessus. Fermer tout er revenez au point 1.

6. Vous devriez voir quelque-chose comme ceci:

Humidity sensor Init Succeeded
28.6293258667

La première ligne "Humidity sensor Init Succeeded" signifie Senseur d'humidité initialisé avec succès. Il se fait que le senseur de température est embarqué sur le senseur d'humidité et c'est via le senseur d'humidité que la température est relevée (un autre senseur de température est embarqué sur le senseur de pression).

7. Juste avant la ligne contenant le print(temp), saisissez la ligne suivante:

temp = round(temp, 1)

8. Vous devriez maintenant voir quelque chose similaire à ceci (il n'y a plus toutes les décimales après le point):

Humidity sensor Init Succeeded
28.6

9. Vous pouvez également essayer les fonctions suivantes à la place de get_temperature.

  • get_temperature_from_humidity (utilise le senseur d'humidité, get_temperature est un raccourcis vers cette fonction)
  • get_temperature_from_pressure (utilise le senseur de pression pour relever la température)

Par exemple:

from sense_hat import SenseHat

sense = SenseHat()
sense.clear()

temp = sense.get_temperature_from_pressure()
temp = round(temp, 1)
print(temp)

Votre code prend une mesure puis sort du programme.

Surveiller la température

1. It would be good to monitor the temperature as it changes, so let's put our code into a while loop and run it again.

while True:
  temp = sense.get_temperature()
  temp = round(temp, 1)
  print(temp)

When you run the code the temperature values will scroll up the screen with the latest ones at the bottom.

2. Put your thumb over the sensor and hold it there. You should see the measurement start to rise.

3. Blow on it (or give the sensors a short blast from an air duster, if available). The measurement should fall.

4. Pressez Ctrl + c pour arrêter le programme.

Afficher la température sur la matrice LED

Think about how you could show the temperature information on the LED matrix in some way (voyez le guide consacré à la Matrice LED pour plus d'information). The obvious choice would be to use the show_message function, but, while this would work, there are probably better ways to do it. For example, you could:

  • Use the clear function to display some predefined colours based on ranges that the temperature falls in. For example 0 to 5 degrees could be blue?
  • Use the clear function to display a single colour but change the brightness of red (0 to 255) based on the measured temperature?
  • Use the set_pixel function to display a bar that moves up and down similar to a thermometer.

Below is some starter code for the final suggestion above. This code will display a bar that has a range of 8 degrees Celsius (one degree per horizontal row of LEDs). The maximum it can display is 31 (hard coded; feel free to edit this) and so the minimum is 31 - 8 which is 23. If the measured temperature goes outside of that range then errors can occur. You can add code to clamp the measured temperature to prevent these errors if you like.

  from sense_hat import SenseHat

  sense = SenseHat()
  sense.clear()

  tmax = 31
  tmin = tmax - 8

  while True:
      temp = sense.get_temperature()
      print(temp)
      temp = int(temp) - tmin
      for x in range(0, 8):
          for y in range(0, temp):
              sense.set_pixel(x, y, 255, 0, 0)
          for y in range(temp, 8):
              sense.set_pixel(x, y, 0, 0, 0)

It works by subtracting the minimum value from the measured value which should give a number between 0 and 8. We then use two nested for loops. The outer loop is for the x axis and the two inner loops are for the y axis. We use two loops here because we want to turn all the LEDs below the measurement red with set_pixel and those above it off. That way the bar will appear to move up and down the y axis following the measured temperature.

Remember that you can use sense.set_rotation(n) (where n is 0, 90, 180 or 270) at the start of the program just after sense.clear() if you want to change the orientation of the bar.


Source: Getting Started with Astro PI et Astro-Pi Guide proposé par Raspberry Pi Learning Resource (www.raspberrypi.org)

Licence Creative Commons - CC-BY-SA
The learning resource is provided for free by the Raspberry Pi Foundation under a Creative Commons licence.
Find more at raspberrypi.org/resources and github.com/raspberrypilearning.

Traduction réalisée par Meurisse. D pour shop.MCHobby.be - Licence CC-BY-SA.
Crédit de traduction: Toute référence, mention ou extrait de cette traduction doit également être explicitement accompagné du crédit de traduction suivant : «  Traduction par MCHobby (shop.MCHobby.be) » avec un lien vers la source (donc cette page) et ce quelque soit le média utilisé.