Outils pour utilisateurs

Outils du site


issue120:python

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. 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.

D'abord, je voudrais dire « Bon anniversaire, Full Circle Magazine ». C'est fantastique pour un magazine gratuit sur Internet d'atteindre les 10 ans.

Le mois dernier, je terminais mon article en disant que la solution de Firmata ne fonctionnera qu'aussi longtemps que nous aurons une connexion série directe avec la carte Arduino, et que nous avions besoin d'une sorte de connexion sans fil pour que la solution de la communication série soit une option viable pour nos future projets.

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'être nivelés. Le code et la disposition de la plaque d'essai devraient fonctionner pour les deux.

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. 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'humidité et de température DHT22 comme capteur. Nous avons déjà joué avec la série de modules DHT dans des articles précédents.

Sur le diagramme Fritzing de la plaque d'essai, vous pouvez noter que j'utilise la forme du module bluetooth HC-05, car il n'y en a pas pour le HC-06. Les deux pattes extérieures ne sont pas utilisées.

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 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: switch (value) { case test1: statements break; case test2: statements break; … default: break }

Code de test du DHT

J'ai utilisé un exemple d'Adafruit pour créer un programme de test du capteur DHT22. Je l'ai aussi modifié pour utiliser la déclaration Switch disponible en langage C pour rendre les choses un peu plus lisibles. Notez que dans la routine loop nous appelons simplement une autre routine nommée « handleSerial ». Celle-ci prend soin de toutes nos communications série avec le monde extérieur.

La déclaration Switch fonctionne comme un paquet de déclarations if… elif… elif… else. Cela ressemble à quelque chose comme ceci :

switch (value) {

   case test1:
        statements
        break;
   case test2:
        statements
        break;
    …
    default:
        break

}

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. 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”, “t”, “H” and “h”. 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”.

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'à ce qu'elle atteigne la déclaration break. Vous pouvez voir aussi les « case » multiples qui sont dans la même logique, comme nous le verrons plus bas.

Aussi, voici le code de test du capteur DHT (voir à droite). N'importe laquelle de ces lignes peut être enlevée, si vous le voulez.

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. 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.

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'Arduino. Pour la phase de test, j'ai utilisé une appli appelée « Bluetooth Terminal HC-05 » sur mon téléphone Android, disponible gratuitement sur le Google Play Store.

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: 105.32”) and .println(“Humidity: 100.0”). This was just to make sure that there were no problems in sending multiple characters to the bluetooth host. 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. Next time, we’ll combine the two programs and get the RPi set up to act as host. I’ve uploaded the two sketches to pastebin: https://pastebin.com/vnZ4ZJfT (DHT22 test sketch) https://pastebin.com/ZcWGLWXa (Bluetooth test sketch)

Ici dans la fonction loop (voir page 22), les seuls changements que j'ai fait au code d'origine a été de changer les lignes d'impression par bluetooth de .print (“1”) and .print (“2”) en .println(“Temp: 105.32”) et .println(“Humidity: 100.0”). C'était juste pour m'assurer qu'il n'y aura pas de problèmes en envoyant plusieurs caractères à l'hôte bluetooth.

Pour le tester, une fois que vous avez appairé et connecté votre projet au téléphone en bluetooth, vous devriez être capable d'envoyer un « 1 », « 0 » ou « b » et voir le résultat sur votre téléphone.

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://pastebin.com/vnZ4ZJfT (Maquette de test du DHT22).

https://pastebin.com/ZcWGLWXa (Maquette de test du Bluetooth).

issue120/python.txt · Dernière modification : 2017/05/16 10:00 de andre_domenech