Modifications

Sauter à la navigation Sauter à la recherche
3 687 octets ajoutés ,  19 mai 2015 à 13:33
aucun résumé de modification
Ligne 1 : Ligne 1 :  
{{Rasp-Hack-MCP9808-NAV}}
 
{{Rasp-Hack-MCP9808-NAV}}
 +
 +
{{traduction}}
    
== Intro ==
 
== Intro ==
Ligne 8 : Ligne 10 :  
Vous avez également besoin d'être accoutumé avec la connexion terminal sur un [[PI-SSH|Raspberry Pi]] ou BeagleBone Black avec SSH.}}
 
Vous avez également besoin d'être accoutumé avec la connexion terminal sur un [[PI-SSH|Raspberry Pi]] ou BeagleBone Black avec SSH.}}
    +
== Dépendances ==
 +
 +
First install dependencies by executing in a terminal:
 +
 +
<nowiki>sudo apt-get update
 +
sudo apt-get install build-essential python-dev python-pip python-smbus git</nowiki>
 +
 +
You can ignore warnings about dependencies which are already installed.
 +
 +
== 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:
 +
 +
<nowiki>cd examples
 +
sudo python simpletest.py</nowiki>
 +
 +
If everything goes well you should see the sensor's temperature displayed every second:
 +
 +
<nowiki>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
 +
...</nowiki>
 +
 +
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.
 +
 +
<nowiki># 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)</nowiki>
 +
 +
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.
 +
 +
<nowiki># Initialize communication with the sensor.
 +
sensor.begin()</nowiki>
 +
 +
After creating the MCP9808 class instance the '''begin()''' function is called to initialize communication with the device. 
 +
 +
<nowiki># 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)</nowiki>
 +
 +
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, [https://github.com/adafruit/Adafruit_Python_MCP9808 vous pouvez également suivre les évolutions de la bibliothèque sur la page Github].
    
{{Rasp-Hack-MCP9808-TRAILER}}
 
{{Rasp-Hack-MCP9808-TRAILER}}
29 918

modifications

Menu de navigation