issue120:python
Différences
Ci-dessous, les différences entre deux révisions de la page.
Prochaine révision | Révision précédente | ||
issue120:python [2017/04/29 12:49] – créée auntiee | issue120:python [2017/05/16 10:00] (Version actuelle) – andre_domenech | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
- | First, I’d like to say “HAPPY BIRTHDAY” to Full Circle Magazine. It’s fantastic for a free Internet magazine to make 10 years. | + | **First, I’d like to say “HAPPY BIRTHDAY” to Full Circle Magazine. It’s fantastic for a free Internet magazine to make 10 years. |
Last month, I ended the article by saying that the Firmata solution will work only as long as we have a direct serial connection to the Arduino board and that we needed something in a wireless connection to really make serial communications a viable option for our future projects. | Last month, I ended the article by saying that the Firmata solution will work only as long as we have a direct serial connection to the Arduino board and that we needed something in a wireless connection to really make serial communications a viable option for our future projects. | ||
Ligne 5: | Ligne 5: | ||
This month, I will begin to show you how to use an inexpensive bluetooth module to make this a reality. | This month, I will begin to show you how to use an inexpensive bluetooth module to make this a reality. | ||
- | We’ll use the HC-06 Bluetooth module, which you can find on the web for around $3.00 US or less. While this isn’t the best module to use, it will do the job for our projects. When I purchased mine, the HC-06 was much cheaper than the more capable HC-05, but the prices have pretty much evened out. The code and breadboard layout should work for either of them. | + | We’ll use the HC-06 Bluetooth module, which you can find on the web for around $3.00 US or less. While this isn’t the best module to use, it will do the job for our projects. When I purchased mine, the HC-06 was much cheaper than the more capable HC-05, but the prices have pretty much evened out. The code and breadboard layout should work for either of them.** |
- | We’ll also be using a DHT22 DIgital Humidity and Temperature module as our sensor. We’ve played with the DHT series of modules before in earlier articles. | + | D' |
+ | |||
+ | Le mois dernier, je terminais mon article en disant que la solution de Firmata ne fonctionnera qu' | ||
+ | |||
+ | Ce mois-ci, je commencerai par vous montrer comment utiliser un module bluetooth bon marché pour en faire une réalité. | ||
+ | |||
+ | Nous utiliserons le module Bluetooth HC-06, que vous pouvez trouver sur le Web pour environ 3 $, voire moins. Alors que ce n'est pas le meilleur module à utiliser, il conviendra pour nos projets. Quand j'ai acheté le mien, le HC-06 était beaucoup moins cher que le HC-05 plus puissant, mais les prix peuvent s' | ||
+ | |||
+ | **We’ll also be using a DHT22 DIgital Humidity and Temperature module as our sensor. We’ve played with the DHT series of modules before in earlier articles. | ||
In the Fritzing breadboard diagram, you should notice that I’m using the HC-05 bluetooth module part, since there isn’t one for the HC-06. The two outer pins are not used. | In the Fritzing breadboard diagram, you should notice that I’m using the HC-05 bluetooth module part, since there isn’t one for the HC-06. The two outer pins are not used. | ||
- | Also, the HC-06 Module needs to be powered at 3.3VDC, NOT 5VDC. If you don’t, the bluetooth module will probably go up in smoke. | + | Also, the HC-06 Module needs to be powered at 3.3VDC, NOT 5VDC. If you don’t, the bluetooth module will probably go up in smoke.** |
+ | |||
+ | Nous utiliserons aussi un module numérique d' | ||
+ | |||
+ | Sur le diagramme Fritzing de la plaque d' | ||
+ | |||
+ | De plus, le module HC-06 doit être alimenté en 3 V DC. Pas en 5 V DC. Si vous n'y prenez pas garde, le module bluetooth risque probablement de partir en fumée. | ||
- | DHT Test Code | + | **DHT Test Code |
I’ve used an example from Adafruit to create a test program for the DHT22 sensor. I also modified it to use the Switch statement available in the C language to make things a bit more readable. Notice in the loop routine, we simply call another routine named ‘handleSerial’. This takes care of all of our serial communications with the outside world. | I’ve used an example from Adafruit to create a test program for the DHT22 sensor. I also modified it to use the Switch statement available in the C language to make things a bit more readable. Notice in the loop routine, we simply call another routine named ‘handleSerial’. This takes care of all of our serial communications with the outside world. | ||
The Switch statement works like a bunch of if...elif...elif...else statements. It looks something like this: | The Switch statement works like a bunch of if...elif...elif...else statements. It looks something like this: | ||
+ | |||
+ | switch (value) { | ||
+ | case test1: | ||
+ | statements | ||
+ | break; | ||
+ | case test2: | ||
+ | statements | ||
+ | break; | ||
+ | … | ||
+ | default: | ||
+ | break | ||
+ | }** | ||
+ | |||
+ | Code de test du DHT | ||
+ | |||
+ | J'ai utilisé un exemple d' | ||
+ | |||
+ | La déclaration Switch fonctionne comme un paquet de déclarations if… elif… elif… else. Cela ressemble à quelque chose comme ceci : | ||
switch (value) { | switch (value) { | ||
Ligne 31: | Ligne 63: | ||
} | } | ||
- | The statement tests the value with each case in the list, and, if it matches, it runs the statements in that segment until it hits the break statement. You can also have multiple “cases” within the same logic set, as you’ll see below. | + | **The statement tests the value with each case in the list, and, if it matches, it runs the statements in that segment until it hits the break statement. You can also have multiple “cases” within the same logic set, as you’ll see below. |
So, here (shown right) is the DHT sensor test code. Any of the commented lines can be left out if you want. | So, here (shown right) is the DHT sensor test code. Any of the commented lines can be left out if you want. | ||
Ligne 37: | Ligne 69: | ||
Here (next page) we see the handleSerial routine with the switch case statements. In this example, we will be looking for four possible characters…”T”, | Here (next page) we see the handleSerial routine with the switch case statements. In this example, we will be looking for four possible characters…”T”, | ||
- | Since we are using standard serial to test the DHT sensor, you can use the serial monitor built into the Arduino IDE. Simply send an “H”, “h”, “T” or a “t”. | + | Since we are using standard serial to test the DHT sensor, you can use the serial monitor built into the Arduino IDE. Simply send an “H”, “h”, “T” or a “t”.** |
- | Bluetooth test code | + | La déclaration teste la valeur dans chaque cas de la liste et, si ça correspond, elle lance les déclarations de ce segment jusqu' |
+ | |||
+ | Aussi, voici le code de test du capteur DHT (voir à droite). N' | ||
+ | |||
+ | Sur la page suivante, nous pouvons voir la routine handleSerial avec les déclarations des switch case. Dans cet exemple, nous chercherons quatre lettres possibles… « T », « t », « H » et « h ». | ||
+ | |||
+ | Comme nous utilisons le lien série standard pour tester le capteur DHT, vous pouvez utiliser le moniteur série intégré à l'IDE Arduino. Simplement, envoyez un « T », « t », « H » ou « h ». | ||
+ | |||
+ | **Bluetooth test code | ||
In order for you to be able to use the ultimate project, you’ll need to have a bluetooth device to send and receive data to the Arduino. For the testing phase, I used an app called “Bluetooth Terminal HC-05” on my Android phone, available for free from the Google Play store. | In order for you to be able to use the ultimate project, you’ll need to have a bluetooth device to send and receive data to the Arduino. For the testing phase, I used an app called “Bluetooth Terminal HC-05” on my Android phone, available for free from the Google Play store. | ||
+ | |||
This sketch is one that I was able to get from the Internet. Notice, that I didn’t change the logic from if statements to switch case statements in this case. We will be doing that in the combined project. | This sketch is one that I was able to get from the Internet. Notice, that I didn’t change the logic from if statements to switch case statements in this case. We will be doing that in the combined project. | ||
- | In the setup function (next page), we use bluetooth.print and bluetooth.println pretty much like we do when we print to serial. | + | In the setup function (next page), we use bluetooth.print and bluetooth.println pretty much like we do when we print to serial.** |
- | Here in the loop function (see page 22), the only changes I made from the original code was to change the bluetooth printout lines from .print (“1”) and .print (“2”) to .println(“Temp: | + | Code de test du Bluetooth |
+ | |||
+ | De façon à ce que vous puissiez utiliser le projet final, vous devrez avoir un dispositif bluetooth pour envoyer et recevoir des données vers/de l' | ||
+ | |||
+ | Cette maquette est une de celles que j'ai pu trouver sur Internet. Notez que je n'ai pas changé la logique des déclarations if en déclarations switch case dans ce cas. Nous le ferons dans un projet combiné. | ||
+ | |||
+ | Dans la fonction de paramétrage (page suivante), nous utilisons bluetooth.print et bluetooth.println d'une façon assez proche de ce que nous faisons quand nous imprimons par le lien série. | ||
+ | |||
+ | **Here in the loop function (see page 22), the only changes I made from the original code was to change the bluetooth printout lines from .print (“1”) and .print (“2”) to .println(“Temp: | ||
To test this, once you have paired and connected your project with the bluetooth phone, you should be able to send a “1”, “0” or “b” and see the result on the phone. | To test this, once you have paired and connected your project with the bluetooth phone, you should be able to send a “1”, “0” or “b” and see the result on the phone. | ||
Ligne 56: | Ligne 105: | ||
https:// | https:// | ||
- | https:// | + | https:// |
+ | |||
+ | Ici dans la fonction loop (voir page 22), les seuls changements que j'ai fait au code d' | ||
+ | |||
+ | Pour le tester, une fois que vous avez appairé et connecté votre projet au téléphone en bluetooth, vous devriez être capable d' | ||
+ | |||
+ | La prochaine fois, nous combinerons les deux programmes et préparerons le paramétrage du RPi pour agir comme hôte. | ||
+ | |||
+ | J'ai téléchargé les deux maquettes sur pastebin : | ||
+ | |||
+ | https:// | ||
+ | |||
+ | https:// |
issue120/python.1493462943.txt.gz · Dernière modification : 2017/04/29 12:49 de auntiee