Ceci est une ancienne révision du document !
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 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 solution 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.00$, 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 détecteur. 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 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 }
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”.
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.
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)