issue109:python
Différences
Ci-dessous, les différences entre deux révisions de la page.
Prochaine révision | Révision précédente | ||
issue109:python [2016/05/29 17:56] – créée auntiee | issue109:python [2016/06/15 14:45] (Version actuelle) – auntiee | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
- | Last month, I suggested you get a number of parts and if you were able to get them, I hope that it didn’t cost you too much. If you haven’t gotten them, then follow along as best you can, and if there is a particular project you want to try, then get those components that are needed. I’m trying to do this on as little cash outlay for either you or me as possible. Frequently, you can recycle many of the items from older electronic items; many can be found at a local thrift store for pence on a pound. (Hopefully I got that one right. We say pennies on a dollar here, so at least give me an “F” for effort… ok?) | + | **Last month, I suggested you get a number of parts and if you were able to get them, I hope that it didn’t cost you too much. If you haven’t gotten them, then follow along as best you can, and if there is a particular project you want to try, then get those components that are needed. I’m trying to do this on as little cash outlay for either you or me as possible. Frequently, you can recycle many of the items from older electronic items; many can be found at a local thrift store for pence on a pound. (Hopefully I got that one right. We say pennies on a dollar here, so at least give me an “F” for effort… ok?) |
- | As I was laying last week, waiting for some surgery, I was thinking that if someone were to come up to me and ask directly why I’m doing this, what my answer would be. Before the wonderful chemicals they pumped into my body to make the process less horrible, I realized that the REAL reason is multi-part. First, is to create excitement in “non-programmers” when doing things that seemingly could not be done without a ton of training. Secondly, is to show that the newer technology, like the Raspberry Pi and the Arduino, is not beyond the ken of the “general joe” out there, but that anyone can do things that have real world applications (hence the title of our series). That having been said, making LEDs blink is only the same kind of project for the hardware world as the “Hello World” program is in the programming realm. You have to take small steps before you move to the big race. Believe me, we will be doing some amazing things with all those little parts, whatsits and thingamabobs. | + | As I was laying last week, waiting for some surgery, I was thinking that if someone were to come up to me and ask directly why I’m doing this, what my answer would be. Before the wonderful chemicals they pumped into my body to make the process less horrible, I realized that the REAL reason is multi-part. First, is to create excitement in “non-programmers” when doing things that seemingly could not be done without a ton of training. Secondly, is to show that the newer technology, like the Raspberry Pi and the Arduino, is not beyond the ken of the “general joe” out there, but that anyone can do things that have real world applications (hence the title of our series). That having been said, making LEDs blink is only the same kind of project for the hardware world as the “Hello World” program is in the programming realm. You have to take small steps before you move to the big race. Believe me, we will be doing some amazing things with all those little parts, whatsits and thingamabobs.** |
- | This month, we will be using the DHT11 Basic Temperature/ | + | Le mois dernier, je vous suggérais de vous procurer un certain nombre de pièces et si vous avez été capable de les avoir, j' |
- | Enough about the future. Let’s start with this month's project. | + | Alors que j' |
- | The DHT11 is the least expensive sibling of a series of temperature and humidity sensor sets. The DHT11 has a temperature range from 0⁰ to 50⁰ C with ±2⁰ C accuracy (32⁰ to 122⁰ F, ±3.6⁰F) and a humidity range from 20-90%RH ±5%. You can see that it’s not the most accurate sensor on the market; there is a DHT22 that is more accurate and has a wider range (-40⁰ to 80⁰ C temp range) but about twice as expensive. | + | **This month, we will be using the DHT11 Basic Temperature/ |
+ | |||
+ | Enough about the future. Let’s start with this month' | ||
+ | |||
+ | Ce mois-ci, nous utiliserons le capteur de température/ | ||
+ | |||
+ | C'est assez pour le futur. Commençons notre projet du mois. | ||
+ | |||
+ | **The DHT11 is the least expensive sibling of a series of temperature and humidity sensor sets. The DHT11 has a temperature range from 0⁰ to 50⁰ C with ±2⁰ C accuracy (32⁰ to 122⁰ F, ±3.6⁰F) and a humidity range from 20-90%RH ±5%. You can see that it’s not the most accurate sensor on the market; there is a DHT22 that is more accurate and has a wider range (-40⁰ to 80⁰ C temp range) but about twice as expensive. | ||
It’s a bit of a funny looking thing. A blue rectangular plastic box with holes in it and something shiny inside it. It might come just as a single sensor with 4 pins, or already on a mini-circuit board with 3 or 4 pins. Either way, they are basically the same. For now, we’ll use the discrete component (the one without the circuit board) for the sake of the discussion, and I’ll address the differences as we go along. | It’s a bit of a funny looking thing. A blue rectangular plastic box with holes in it and something shiny inside it. It might come just as a single sensor with 4 pins, or already on a mini-circuit board with 3 or 4 pins. Either way, they are basically the same. For now, we’ll use the discrete component (the one without the circuit board) for the sake of the discussion, and I’ll address the differences as we go along. | ||
- | Whenever you want to work with a new sensor, you should get a spec sheet (data sheet). A simple web search should turn up a number of results. Try to get something directly from the manufacturer if at all possible. For the DHT11, a good place to get one of the various data sheets available is http:// | + | Whenever you want to work with a new sensor, you should get a spec sheet (data sheet). A simple web search should turn up a number of results. Try to get something directly from the manufacturer if at all possible. For the DHT11, a good place to get one of the various data sheets available is http:// |
+ | |||
+ | Le DHT11 est l' | ||
+ | |||
+ | C'est une drôle de pièce. Un boîtier plastique rectangulaire bleu avec des trous et quelque chose de brillant à l' | ||
+ | |||
+ | Chaque fois que vous voulez travailler avec un nouveau capteur, vous devrez prendre la feuille de spécifications (la « data sheet »). Une simple recherche sur le Web devrait retourner un bon nom de résultats. Essayez de trouver quelque chose venant directement du fabricant si c'est possible. Pour le DHT11, un bon endroit pour se procurer une des nombreuses data sheets disponibles est http:// | ||
+ | |||
+ | **You might already be asking, why do I need this? There’s a bunch of information that, unless you have a PhD in Physics or something, you’ll never need. Well, that is true, but there is a lot of information that IS relevant and can potentially keep you from blowing up either the sensor, the controller, or your work bench. In this case, we find that the DC operating voltage is between 3 to 5 volts and it pulls about 0.5mA during “normal” conditions (section 6). We also find that this is a rather slow device and that we should not try to pull data more than once per second. Basically we’ll keep it around once every five seconds in our testing program, which is way more than we’ll need in reality. Another thing: if the cable that sends the data from the sensor to the microcontroller (our RPi) is less than 20 meters, we should have about a 5K ohm resistor between the data line and the local power supply (at the sensor) as a pull-up. One last thing (I’m going to stop here, but there’s much more): Pin 1 is positive voltage, Pin 2 is the data pin, and Pin 4 is the ground pin. This gives us pretty much everything we need to know to safely connect this to our RPi. Below is the wiring diagram for a “raw” DHT11 sensor WITHOUT a breakout board. If you have a sensor with a breakout board, see my discussion below the diagram.** | ||
- | You might already be asking, why do I need this? There’s a bunch of information that, unless you have a PhD in Physics or something, you’ll never need. Well, that is true, but there is a lot of information that IS relevant and can potentially keep you from blowing up either the sensor, the controller, or your work bench. In this case, we find that the DC operating voltage is between | + | Vous devrez déjà vous demander : pourquoi en ai-je besoin |
- | Notice that I said earlier that a 5K resistor was needed as a pull-up. If you are going to use 3.3 VDC as a power source (RPi pin 1), then a 5K resistor works pretty well. If, however, you are going to use 5 VDC as shown in the diagram, use a 10K resistor. | + | **Notice that I said earlier that a 5K resistor was needed as a pull-up. If you are going to use 3.3 VDC as a power source (RPi pin 1), then a 5K resistor works pretty well. If, however, you are going to use 5 VDC as shown in the diagram, use a 10K resistor. |
You can see that it’s fairly simple, just three wires and a resistor. For our simple project, don’t try to make the wiring the entire 20 meters though. | You can see that it’s fairly simple, just three wires and a resistor. For our simple project, don’t try to make the wiring the entire 20 meters though. | ||
- | If you have a DHT11 on a breakout board, you will likely have only 3 output pins on it. I have two sensors from different vendors, and (go figure) both have a different pinout. One is laid out [Data] [Positive Voltage] [Ground] and is marked “S -”. The other is [Ground] [Data] [Positive Voltage] and is marked as such. Hopefully, yours has some sort of pinout definition printed on it. If not, you can use a multimeter to trace the ground pin and voltage pin directly from the sensor to the breakout pin. You can usually guess that if there are three output pins on the breakout board and you know ground and positive voltage, then the other SHOULD be the data pin. | + | If you have a DHT11 on a breakout board, you will likely have only 3 output pins on it. I have two sensors from different vendors, and (go figure) both have a different pinout. One is laid out [Data] [Positive Voltage] [Ground] and is marked “S -”. The other is [Ground] [Data] [Positive Voltage] and is marked as such. Hopefully, yours has some sort of pinout definition printed on it. If not, you can use a multimeter to trace the ground pin and voltage pin directly from the sensor to the breakout pin. You can usually guess that if there are three output pins on the breakout board and you know ground and positive voltage, then the other SHOULD be the data pin.** |
- | Now our program code. | + | Notez que j'ai dit avant qu'il y avait besoin d'une résistance de tirage de 5kΩ. Si vous utilisez une alimentation de 3,3 V DC (le picot 1 du RPi), alors la résistance de 5kΩ marche bien. Cependant, si vous choisissez 5 V DC, comme sur le schéma, utilisez une résistance de 10kΩ. |
+ | |||
+ | Vous pouvez voir que c'est plutôt simple, juste trois fils et une résistance. Pour notre projet simpliste, cependant, n' | ||
+ | |||
+ | Si votre DHT11 est sur circuit, vous devriez avoir au moins 3 picots. J'ai deux capteurs de marchands différents et (allez savoir pourquoi) ils ont une disposition des picots différente. Sur l'un je trouve [Data] (données) [Positive Voltage] (alim +) [Ground] (alim -) et c'est marqué « S - ». Pour l' | ||
+ | |||
+ | **Now our program code. | ||
For the sake of getting things up and running quickly, we will be using some code provided by the kind people at Adafruit.com – they provide the library for working with the DHT11. (They found that trying to run straight Python code for the library causes some timing issues, so the library is actually written in ‘C’.) There are a number of steps involved, so follow the instructions carefully. I’ve paraphrased them so if something doesn’t work, you can also find the instructions at the Adafruit website at https:// | For the sake of getting things up and running quickly, we will be using some code provided by the kind people at Adafruit.com – they provide the library for working with the DHT11. (They found that trying to run straight Python code for the library causes some timing issues, so the library is actually written in ‘C’.) There are a number of steps involved, so follow the instructions carefully. I’ve paraphrased them so if something doesn’t work, you can also find the instructions at the Adafruit website at https:// | ||
Ligne 35: | Ligne 57: | ||
sudo apt-get install build-essential python-dev python-openssl | sudo apt-get install build-essential python-dev python-openssl | ||
- | Ignore any errors that state a package is already installed. | + | Ignore any errors that state a package is already installed.** |
- | Next, install the library by running: | + | Maintenant, notre code de programme. |
+ | |||
+ | Pour une mise en route rapide, nous utiliserons du code fourni par des gens sur Adafruit.com - ils fournissent une bibliothèque fonctionnant avec le DHT11. (Ils trouvent que d' | ||
+ | |||
+ | Dans votre répertoire « /home/pi », lancez les commandes suivantes : | ||
+ | |||
+ | git clone https:// | ||
+ | |||
+ | cd Adafruit_Python_DHT | ||
+ | |||
+ | sudo apt-get update | ||
+ | |||
+ | sudo apt-get install build-essential python-dev python-openssl | ||
+ | |||
+ | Ignorez toutes les erreurs qui font état d'un paquet déjà installé. | ||
+ | |||
+ | **Next, install the library by running: | ||
sudo python setup.py install | sudo python setup.py install | ||
Ligne 50: | Ligne 88: | ||
sleep(3) | sleep(3) | ||
- | Here we define that the sensor is connected to GPIO pin 4 and then we wait 3 seconds for things to settle and be ready to work. | + | Here we define that the sensor is connected to GPIO pin 4 and then we wait 3 seconds for things to settle and be ready to work.** |
- | We use a simple loop (next page, top right) to grab the values for humidity and temp over and over. I never got the knack of relating Celsius to “real” temperatures, | + | Ensuite, installez la bibliothèque en lançant : |
+ | |||
+ | sudo python setup.py install | ||
+ | |||
+ | Une fois que tout est fait, vous pouvez passer à notre code exemple. | ||
+ | |||
+ | Ci-dessus, voici mon échantillon de code modifié, « emprunté » au code exemple d' | ||
+ | |||
+ | Tout ce qui est au-dessus pourrait être réduit à trois lignes de code. Les deux déclarations d' | ||
+ | |||
+ | pin = 4 | ||
+ | sleep(3) | ||
+ | |||
+ | Ici, nous définissons que le capteur est connecté au picot 4 du GPIO et que nous attendons 3 secondes pour que tout s' | ||
+ | |||
+ | **We use a simple loop (next page, top right) to grab the values for humidity and temp over and over. I never got the knack of relating Celsius to “real” temperatures, | ||
Now (next page, bottom right) we check to see if we got realistic values for both humidity and temperature, | Now (next page, bottom right) we check to see if we got realistic values for both humidity and temperature, | ||
Ligne 60: | Ligne 113: | ||
Well, that’s it for this month. Remember, we’ll be using the Dallas temp sensor next time, so be ready. | Well, that’s it for this month. Remember, we’ll be using the Dallas temp sensor next time, so be ready. | ||
- | Have a good time and I’ll see you next month. | + | Have a good time and I’ll see you next month.** |
+ | |||
+ | Nous utilisons une simple boucle (page suivante, en haut à droite) pour récupérer les valeurs d' | ||
+ | |||
+ | Maintenant (page suivante, en bas à droite), nous vérifions que les valeurs de température et d' | ||
+ | |||
+ | Je dois admettre que, quand je lance le programme avec un certain capteur, j' | ||
+ | |||
+ | Bon, c'est tout pour ce mois-ci. Souvenez-vous que, la prochaine fois, nous utiliserons le capteur de température de Dallas ; soyez prêt. | ||
+ | |||
+ | Amusez-vous bien et au mois prochain. | ||
issue109/python.1464537373.txt.gz · Dernière modification : 2016/05/29 17:56 de auntiee