issue110:python
Différences
Ci-dessous, les différences entre deux révisions de la page.
Prochaine révision | Révision précédente | ||
issue110:python [2016/06/30 11:31] – créée andre_domenech | issue110:python [2016/07/10 17:59] (Version actuelle) – auntiee | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
- | This month, we will be using my current favorite temperature sensor; the Dallas Semiconductor DS18B20 One Wire sensor. It looks like a ‘normal’ transistor, but is a very accurate sensor, much more so than the DHT11 that we used last month. It doesn’t do humidity, but for temperature readings, it’s a very good and inexpensive device. All data requests and output are sent on a single pin. It has an operating range from -55°C to 125°C ( -67°F to 257°F) and should be able to run about 3 metres (9.8 feet). It also has a parasitic mode that allows power to be derived from the data line. | + | **This month, we will be using my current favorite temperature sensor; the Dallas Semiconductor DS18B20 One Wire sensor. It looks like a ‘normal’ transistor, but is a very accurate sensor, much more so than the DHT11 that we used last month. It doesn’t do humidity, but for temperature readings, it’s a very good and inexpensive device. All data requests and output are sent on a single pin. It has an operating range from -55°C to 125°C ( -67°F to 257°F) and should be able to run about 3 metres (9.8 feet). It also has a parasitic mode that allows power to be derived from the data line. |
The data sheet can be found at https:// | The data sheet can be found at https:// | ||
- | Wiring a single sensor is very easy. Shown right is the diagram. | + | Wiring a single sensor is very easy. Shown right is the diagram.** |
- | There are only three connections to the RPi. Ground (sensor pin 1) to RPi pin 6, 3.3v (sensor pin 3) to RPi pin 1, and data (sensor pin 2) to RPI pin 7 (GPIO 4). You need to put a 4.7k resistor between sensor pins 2 and 3 (data and +Voltage). That’s it. If you wish to add more sensors to the project, simply connect them ground to ground, +voltage to +voltage and pin 2 to pin 2 of the “main” sensor. No additional resistors should be needed for a reasonable line length. Next page, right-hand side, is an example of a three sensor project. | + | Ce mois-ci, nous utiliserons mon capteur de température favori actuellement : le capteur Dallas Semiconductor DS18B20 One Wire. Il ressemble à un transistor « ordinaire », mais c'est un capteur très précis, beaucoup plus que le DHT11 utilisé le mois dernier. Il ne fait pas l' |
+ | |||
+ | La feuille de spécifications peut être trouvée sur : https:// | ||
+ | |||
+ | Le câblage d'un seul capteur est facile. Voir le dessin à droite. | ||
+ | |||
+ | **There are only three connections to the RPi. Ground (sensor pin 1) to RPi pin 6, 3.3v (sensor pin 3) to RPi pin 1, and data (sensor pin 2) to RPI pin 7 (GPIO 4). You need to put a 4.7k resistor between sensor pins 2 and 3 (data and +Voltage). That’s it. If you wish to add more sensors to the project, simply connect them ground to ground, +voltage to +voltage and pin 2 to pin 2 of the “main” sensor. No additional resistors should be needed for a reasonable line length. Next page, right-hand side, is an example of a three sensor project. | ||
The Code | The Code | ||
- | One thing you have to do is tell the operating system you want to use kernel support for one-wire devices. If you are using Raspbian Jessie, this is done in raspi-config. If you are using another OS, then you must add the following line to the / | + | One thing you have to do is tell the operating system you want to use kernel support for one-wire devices. If you are using Raspbian Jessie, this is done in raspi-config. If you are using another OS, then you must add the following line to the / |
- | dtoverlay=w1-gpio | + | Il n'y a que trois connexions au RPi. La masse (picot 1 du capteur) vers le picot 6 du RPi, 3,3 V (picot 3 du capteur) vers le picot 1 du RPi, et les données (picot 2 du capteur) vers le picot 7 du RPi (GPIO 4). Vous avez besoin d'une résistance de 4,7 k entre les picots 2 et 3 du capteur (données et alim +). C'est tout. Si vous souhaitez ajouter d' |
+ | |||
+ | Le code | ||
+ | |||
+ | Vous devez dire au système d' | ||
+ | |||
+ | **dtoverlay=w1-gpio | ||
The following two commands load the 1-Wire and thermometer drivers on GPIO 4. | The following two commands load the 1-Wire and thermometer drivers on GPIO 4. | ||
Ligne 25: | Ligne 37: | ||
ls | ls | ||
- | In the device drivers, your sensor should be listed as a series of numbers and letters. In this case, the device is registered as 28-000005e2fdc3. You then need to access the sensor with the cd command, replacing our serial number with your own. | + | In the device drivers, your sensor should be listed as a series of numbers and letters. In this case, the device is registered as 28-000005e2fdc3. You then need to access the sensor with the cd command, replacing our serial number with your own.** |
- | cd 28-000005e2fdc3 | + | dtoverlay=w1-gpio |
+ | |||
+ | Les deux commandes suivantes chargent les pilotes du 1-fil et du thermomètre sur GPIO 4. | ||
+ | |||
+ | sudo modprobe w1-gpio | ||
+ | |||
+ | sudo modprobe w1-therm | ||
+ | |||
+ | Puis vous devez modifier le répertoire (cd) pour le répertoire du composant 1-fil et lister (ls) les appareils pour vérifier que votre thermomètre est chargé correctement. | ||
+ | |||
+ | cd / | ||
+ | |||
+ | ls | ||
+ | |||
+ | Dans les pilotes des matériels, votre capteur doit être listé comme une série de chiffres et de lettres. Dans mon cas, le composant est enregistré comme 28-000005e2fdc3. Vous devez ensuite accéder au capteur avec la commande cd, en remplaçant mon numéro de série par le vôtre. | ||
+ | |||
+ | **cd 28-000005e2fdc3 | ||
The sensor periodically writes to the w1_slave file, so we simply use the cat command to read it. | The sensor periodically writes to the w1_slave file, so we simply use the cat command to read it. | ||
Ligne 39: | Ligne 67: | ||
72 01 4b 46 7f ff 0e 10 57 t=23125 | 72 01 4b 46 7f ff 0e 10 57 t=23125 | ||
- | Next page, top right, is how we had to do it in the “old days”, the library that we will actually use. | + | Next page, top right, is how we had to do it in the “old days”, the library that we will actually use.** |
- | Timo Furrer has provided a wonderful library for us to use on the RPi written in pure Python. You can get it at https:// | + | cd 28-000005e2fdc3 |
+ | |||
+ | Le capteur écrit périodiquement dans le fichier w1_slave ; aussi, nous utilisons simplement la commande cat pour le lire. | ||
+ | |||
+ | cat w1_slave | ||
+ | |||
+ | Ceci nous amène aux deux lignes de texte suivantes, avec la sortie t= montrant la température en degrés Celsius. Un point décimal devrait être placé avant les trois derniers digits ; par exemple, la température que nous avons reçue se lit 23.125 degrés Celsius. | ||
+ | |||
+ | 72 01 4b 46 7f ff 0e 10 57 : crc=57 YES | ||
+ | |||
+ | 72 01 4b 46 7f ff 0e 10 57 t=23125 | ||
+ | |||
+ | À la page suivante, en haut à droite, vous pouvez voir comment il aurait fallu faire « jadis », la bibliothèque que nous allons utiliser réellement. | ||
+ | |||
+ | **Timo Furrer has provided a wonderful library for us to use on the RPi written in pure Python. You can get it at https:// | ||
The beauty of this library is that it takes almost all the work out of dealing with the sensors and allows you to just concentrate on your code. | The beauty of this library is that it takes almost all the work out of dealing with the sensors and allows you to just concentrate on your code. | ||
Ligne 49: | Ligne 91: | ||
There are actually only 7 lines of code needed here. Those lines that are commented out are so you can see the other ways to get and print the data in various temperature units (celsius and kelvin). | There are actually only 7 lines of code needed here. Those lines that are commented out are so you can see the other ways to get and print the data in various temperature units (celsius and kelvin). | ||
- | As I mentioned above, you can have more than one sensor on the same data line. So here is the code to make a single call to get the temp readings from all sensors in the system… | + | As I mentioned above, you can have more than one sensor on the same data line. So here is the code to make a single call to get the temp readings from all sensors in the system…** |
- | from w1thermsensor import W1ThermSensor | + | Timo Furrer nous a fourni une bibliothèque fantastique, |
+ | |||
+ | La beauté de cette bibliothèque vient de ce qu' | ||
+ | |||
+ | Voyez le code « d' | ||
+ | |||
+ | Seulement 7 lignes de code sont nécessaires ici. Les lignes qui sont commentées vous permettent de voir les autres façons d' | ||
+ | |||
+ | Comme indiqué plus haut, vous pouvez avoir plus d'un capteur sur la même ligne de données. Aussi, voici le code pour un appel unique qui récupère les valeurs de température de tous les capteurs du système. | ||
+ | |||
+ | **from w1thermsensor import W1ThermSensor | ||
for sensor in W1ThermSensor.get_available_sensors(): | for sensor in W1ThermSensor.get_available_sensors(): | ||
Ligne 60: | Ligne 112: | ||
If you would like to make a call to a particular sensor, you can use this code as a baseline. | If you would like to make a call to a particular sensor, you can use this code as a baseline. | ||
+ | |||
+ | from w1thermsensor import W1ThermSensor | ||
+ | |||
+ | sensor = W1ThermSensor(W1ThermSensor.THERM_SENSOR_DS18B20, | ||
+ | |||
+ | temperature_in_celsius = sensor.get_temperature()** | ||
+ | |||
+ | from w1thermsensor import W1ThermSensor | ||
+ | |||
+ | for sensor in W1ThermSensor.get_available_sensors(): | ||
+ | |||
+ | print(" | ||
+ | | ||
+ | Bien sûr, vous voudrez faire plus d'un appel de données ; modifiez donc le code ci-dessus de la façon que vous souhaitez. | ||
+ | |||
+ | Si vous voulez faire appel à un capteur en particulier, | ||
from w1thermsensor import W1ThermSensor | from w1thermsensor import W1ThermSensor | ||
Ligne 67: | Ligne 135: | ||
temperature_in_celsius = sensor.get_temperature() | temperature_in_celsius = sensor.get_temperature() | ||
- | So, you can see, by using Timo‘s library, we can basically go from 27 lines of code to 3 (for a single call). That’s wonderful. | + | **So, you can see, by using Timo‘s library, we can basically go from 27 lines of code to 3 (for a single call). That’s wonderful. |
I wanted to show you how to use a 16x2 LCD display with this, but I think I’ll leave room for the other authors and we’ll push that part out to next month. Don’t lose your project hardware, we’ll use it next month. | I wanted to show you how to use a 16x2 LCD display with this, but I think I’ll leave room for the other authors and we’ll push that part out to next month. Don’t lose your project hardware, we’ll use it next month. | ||
- | Until then, enjoy checking out the temperatures around your office/ | + | Until then, enjoy checking out the temperatures around your office/ |
+ | |||
+ | Ainsi, vous pouvez voir qu'en utilisant la bibliothèque de Timo Furrer vous réduisez votre code de 27 lignes à 3 (pour un seul appel). C'est merveilleux ! | ||
+ | |||
+ | Je voulais vous montrer comment utiliser l' | ||
+ | |||
+ | En attendant, amusez-vous à vérifier la température de votre bureau/ |
issue110/python.1467279095.txt.gz · Dernière modification : 2016/06/30 11:31 de andre_domenech