RASP-MCP9808-LOGICIEL

De MCHobby - Wiki
Sauter à la navigation Sauter à la recherche


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.

Intro

Suivez les étapes suivantes pour installer et utiliser la bibliothèque Python du MCP9808.

Avant de démarrer, assurez-vous que votre carte soit connectée sur Internet (via la prise Ethernet ou WiFi) de façon à télécharger les dépendances.

Vous avez également besoin d'être accoutumé avec la connexion terminal sur un Raspberry Pi ou BeagleBone Black avec SSH.

Dépendances

Commençons par installer les dépendances en exécutant les commandes suivantes dans un terminal:

sudo apt-get update
sudo apt-get install build-essential python-dev python-pip python-smbus git

Vous pouvez ignorer les "warnings" à propos des dépendances qui sont déjà installées.

Raspberry Pi

On a Raspberry Pi execute the following to make sure the RPi.GPIO library is installed:

sudo pip install RPi.GPIO

BeagleBone Black

On a BeagleBone Black execute the following to make sure the Adafruit_BBIO library is installed:

sudo pip install Adafruit_BBIO

Installer la bibliothèque

Next download the MCP9808 Python library to your home directory and install it by executing:

</nowiki>cd ~

git clone https://github.com/adafruit/Adafruit_Python_MCP9808.git cd Adafruit_Python_MCP9808 sudo python setup.py install</nowiki>

That's all you need to do to install the Python library!

Utilisation

To learn how to use the MCP9808 Python library you can run and review an example program included with the library. To run the example navigate to the examples directory and run the simpletest.py script by executing:

cd examples
sudo python simpletest.py

If everything goes well you should see the sensor's temperature displayed every second:

Press Ctrl-C to quit.
Temperature: 23.750*C / 74.750*F
Temperature: 25.688*C / 78.237*F
Temperature: 27.000*C / 80.600*F
...

If you see an error make sure you're running the program as root with the sudo command, and that the dependencies & library were installed successfully.

To understand the usage, open simpletest.py in a text editor and follow along with the description of the code below.

import Adafruit_MCP9808.MCP9808 as MCP9808

First the MCP9808 module is imported with a Python import statement.

# Default constructor will use the default I2C address (0x18) and pick a default I2C bus.
#
# For the Raspberry Pi this means you should hook up to the only exposed I2C bus
# from the main GPIO header and the library will figure out the bus number based
# on the Pi's revision.
#
# For the Beaglebone Black the library will assume bus 1 by default, which is
# exposed with SCL = P9_19 and SDA = P9_20.
sensor = MCP9808.MCP9808()
 
# Optionally you can override the address and/or bus number:
#sensor = MCP9808.MCP9808(address=0x20, busnum=2)

Next an instance of the MCP9808 class is created.

Notice that if nothing is passed in to the initializer function the library will pick a default I2C device address (0x18) and bus number. If you need to specify the I2C address or bus number you can do so by specifying them as optional parameters in the MCP9808 initializer.

# Initialize communication with the sensor.
sensor.begin()

After creating the MCP9808 class instance the begin() function is called to initialize communication with the device.

# Loop printing measurements every second.
print 'Press Ctrl-C to quit.'
while True:
    temp = sensor.readTempC()
    print 'Temperature: {0:0.3F}*C / {1:0.3F}*F'.format(temp, c_to_f(temp))
    time.sleep(1.0)

Finally the example enters a loop where it reads the sensor's temperature and prints it out every second. The important thing to see is the temperature measurement function readTempC(). The readTempC() function will read the sensor temperature and return its value in Celsius.

That's all there is to use the MCP9808 Python library! If you run into issues or wish to contribute, vous pouvez également suivre les évolutions de la bibliothèque sur la page Github.


Source: Use a MCP9808 precision temperature sensor on a BeagleBone Black or Raspberry Pi!. Créé par Tony Dicola pour AdaFruit Industries.

Traduction et corrections réalisée par Meurisse D pour MCHobby.be.

Toute référence, mention ou extrait de cette traduction doit être explicitement accompagné du texte suivant : «  Traduction par MCHobby (www.MCHobby.be) - Vente de kit et composants » avec un lien vers la source (donc cette page) et ce quelque soit le média utilisé.

L'utilisation commercial de la traduction (texte) et/ou réalisation, même partielle, pourrait être soumis à redevance. Dans tous les cas de figures, vous devez également obtenir l'accord du(des) détenteur initial des droits. Celui de MC Hobby s'arrêtant au travail de traduction proprement dit.

Traduit avec l'autorisation d'AdaFruit Industries - Translated with the permission from Adafruit Industries - www.adafruit.com