Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentes Révision précédente Prochaine révision | Révision précédente | ||
wiki:tutoriels:arduino:module-bleutooth [2022/05/10 11:20] damien.muti [Dialogue entre le smartphone et la carte Arduino] |
wiki:tutoriels:arduino:module-bleutooth [2022/05/16 17:39] (Version actuelle) damien.muti [Dialogue entre le smartphone et la carte Arduino] |
||
---|---|---|---|
Ligne 18: | Ligne 18: | ||
Vous trouverez un lien sur un tutoriel sur la manière d' | Vous trouverez un lien sur un tutoriel sur la manière d' | ||
- | * [[https:// | + | * [[https:// |
* [[https:// | * [[https:// | ||
* [[https:// | * [[https:// | ||
Ligne 42: | Ligne 42: | ||
#include < | #include < | ||
- | SoftwareSerial | + | SoftwareSerial |
void setup(){ | void setup(){ | ||
Ligne 49: | Ligne 49: | ||
Serial.println(" | Serial.println(" | ||
// | // | ||
- | | + | |
} | } | ||
void loop(){ | void loop(){ | ||
//Write data from HC06 to Serial Monitor | //Write data from HC06 to Serial Monitor | ||
- | if (hc06.available()){ | + | if (Module_BT.available()){ |
- | Serial.write(hc06.read()); | + | Serial.write(Module_BT.read()); |
} | } | ||
| | ||
//Write from Serial Monitor to HC06 | //Write from Serial Monitor to HC06 | ||
if (Serial.available()){ | if (Serial.available()){ | ||
- | | + | |
} | } | ||
} | } | ||
Ligne 66: | Ligne 66: | ||
Le code Arduino est téléchargeable ici : {{ : | Le code Arduino est téléchargeable ici : {{ : | ||
+ | |||
+ | === Problème de connexion === | ||
+ | |||
+ | Dans une classe où il y a plusieurs modules BT, il est parfois difficile de trouver le module BT que vous avez connecté à votre carte. Sur les applications de communication Série en Bluetooth (Bluetooth Serial), il apparaît seulement plusieurs " | ||
+ | |||
+ | |||
+ | La solution est disponible sur cet Article : [[https:// | ||
+ | |||
+ | La liste de toutes les commandes AT pour le module BT en mode " | ||
+ | |||
Ligne 84: | Ligne 94: | ||
* [[https:// | * [[https:// | ||
- | == Programme == | + | == Programme |
+ | |||
+ | Ce Programme permet d' | ||
- | Il faut ensuite téléverser le code suivant sur la carte Arduino : | ||
< | < | ||
+ | |||
#include < | #include < | ||
SoftwareSerial hc06(2, 3); | SoftwareSerial hc06(2, 3); | ||
Ligne 94: | Ligne 106: | ||
float sensor_val = 0; | float sensor_val = 0; | ||
// faire clignoter clignoter la led | // faire clignoter clignoter la led | ||
- | byte led = 13; | + | byte led = 8; |
int Tblink = 100; | int Tblink = 100; | ||
float isBlink = false; // drapeau pour lancer le clignotement... ou non | float isBlink = false; // drapeau pour lancer le clignotement... ou non | ||
Ligne 105: | Ligne 117: | ||
Serial.begin(9600); | Serial.begin(9600); | ||
// | // | ||
- | hc06.begin(9600); | + | hc06.begin(115200); /// Tester aussi avec 9600 si vous rencontrez des difficultés de communication avec le moule BT |
} | } | ||
void loop() { | void loop() { | ||
Ligne 119: | Ligne 131: | ||
if (cmd == " | if (cmd == " | ||
Serial.println(" | Serial.println(" | ||
- | digitalWrite(13, HIGH); // alumer la led | + | digitalWrite(led, HIGH); // alumer la led |
isBlink = false; // arrêter le clignotement | isBlink = false; // arrêter le clignotement | ||
} else if (cmd == " | } else if (cmd == " | ||
Serial.println(" | Serial.println(" | ||
- | digitalWrite(13, LOW); // éteindre la led | + | digitalWrite(led, LOW); // éteindre la led |
isBlink = false; // arrêter le clignotement | isBlink = false; // arrêter le clignotement | ||
} else { | } else { | ||
Ligne 150: | Ligne 162: | ||
hc06.print(sensor_val); | hc06.print(sensor_val); | ||
delay(100); | delay(100); | ||
+ | } | ||
+ | |||
+ | void clignoterLed(int led, int Ta, int Te) { // led : patte connécéet à la led, Ta: temps d' | ||
+ | digitalWrite(led, | ||
+ | delay(Ta); | ||
+ | digitalWrite(led, | ||
+ | delay(Te); | ||
} | } | ||
</ | </ | ||
+ | |||
+ | |||
+ | |||
Le code Arduino est téléchargeable ici : | Le code Arduino est téléchargeable ici : | ||
Ligne 313: | Ligne 335: | ||
{{ : | {{ : | ||
+ | |||
+ | ===== Mode Commende : initialisation du module - Facultatif ===== | ||
+ | voir tutoriel : [[https:// | ||
+ | |||
+ | ==== Câblage | ||
+ | {{ : | ||
+ | |||
+ | ==== Programme ==== | ||
+ | < | ||
+ | #include < | ||
+ | |||
+ | SoftwareSerial mySerial(10, | ||
+ | |||
+ | void setup() { | ||
+ | Serial.begin(9600); | ||
+ | pinMode(9, | ||
+ | Serial.println(" | ||
+ | mySerial.begin(38400); | ||
+ | } | ||
+ | |||
+ | void loop() | ||
+ | { | ||
+ | if (mySerial.available()) | ||
+ | Serial.write(mySerial.read()); | ||
+ | | ||
+ | if (Serial.available()) | ||
+ | mySerial.write(Serial.read()); | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ==== Les commandes AT ==== | ||
+ | AT : Vérifier la connexion avec le module | ||
+ | |||
+ | AT+NAME : Voir le nom du module | ||
+ | |||
+ | AT+ADDR : Voir l’adresse du module | ||
+ | |||
+ | AT+VERSION : Connaitre la version | ||
+ | |||
+ | AT+UART : Connaitre la vitesse de connexion | ||
+ | |||
+ | AT+ROLE: Voir le rôle du module (1=master/ 0=slave/ 2=esclave boucle) | ||
+ | |||
+ | AT+RESET : Redemarrage dy module et sortir du mode AT | ||
+ | |||
+ | AT+ORGL : Restorer le module d’usine | ||
+ | |||
+ | AT+PSWD: Consulter le mot de passe | ||
+ | |||
+ | AT+BIND=adresse, | ||
+ | |||
===== Bibliographie ===== | ===== Bibliographie ===== | ||
Data sheet : | Data sheet : | ||
Ligne 334: | Ligne 407: | ||
{{ : | {{ : | ||
+ | |||
+ | Le tutoriel d' | ||
+ | ]] | ||
+ | |||
+ | |||
+ | Le code permettant de connaître les caractéristiques du modules BT Grove-BLE est accessible ici : [[https:// | ||
+ | |||
+ | ===== Etalonage du module ===== | ||
+ | |||
+ | Le code permettant de connaître les caractéristiques du module BT Grove-BLE est le suivant : [[https:// | ||
+ | |||
+ | |||
+ | < | ||
+ | |||
+ | |||
+ | /* | ||
+ | Bluetooth HM13 Demo Code | ||
+ | 2014 Copyright (c) Seeed Technology Inc. All right reserved. | ||
+ | Author: Jacky Zhang | ||
+ | This demo code is free software; you can redistribute it and/or | ||
+ | modify it under the terms of the GNU Lesser General Public | ||
+ | License as published by the Free Software Foundation; either | ||
+ | version 2.1 of the License, or (at your option) any later version. | ||
+ | This library is distributed in the hope that it will be useful, | ||
+ | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
+ | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
+ | Lesser General Public License for more details. | ||
+ | You should have received a copy of the GNU Lesser General Public | ||
+ | License along with this library; if not, write to the Free Software | ||
+ | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 | ||
+ | For more details about the product please check http:// | ||
+ | */ | ||
+ | |||
+ | /* Upload this sketch into Arduino Uno and press reset*/ | ||
+ | |||
+ | #include < | ||
+ | #define RxD 2 | ||
+ | #define TxD 3 | ||
+ | |||
+ | #define MASTER 1 //change this macro to define the Bluetooth as Master or not | ||
+ | |||
+ | SoftwareSerial blueToothSerial(RxD, | ||
+ | |||
+ | char recv_str[100]; | ||
+ | |||
+ | void setup() | ||
+ | { | ||
+ | Serial.begin(115200); | ||
+ | pinMode(RxD, | ||
+ | pinMode(TxD, | ||
+ | Serial.println(" | ||
+ | if(setupBlueToothConnection() != 0) while(1); | ||
+ | //this block is waiting for connection was established. | ||
+ | while(1) | ||
+ | { | ||
+ | if(recvMsg(1000) == 0) | ||
+ | { | ||
+ | if(strcmp((char *)recv_str, (char *)" | ||
+ | { | ||
+ | Serial.println(" | ||
+ | break; | ||
+ | } | ||
+ | } | ||
+ | delay(200); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | void loop() | ||
+ | { | ||
+ | #if MASTER | ||
+ | //in master mode, the bluetooth send message periodically. | ||
+ | delay(400); | ||
+ | Serial.println(" | ||
+ | blueToothSerial.print(" | ||
+ | delay(100); | ||
+ | //get any message to print | ||
+ | if(recvMsg(1000) == 0) | ||
+ | { | ||
+ | Serial.print(" | ||
+ | Serial.print((char *)recv_str); | ||
+ | Serial.println("" | ||
+ | } | ||
+ | #else // | ||
+ | delay(200); | ||
+ | //the slave role only send message when received one. | ||
+ | if(recvMsg(1000) == 0) | ||
+ | { | ||
+ | Serial.print(" | ||
+ | Serial.print((char *)recv_str); | ||
+ | Serial.println("" | ||
+ | Serial.println(" | ||
+ | blueToothSerial.print(" | ||
+ | } | ||
+ | #endif | ||
+ | } | ||
+ | |||
+ | //used for compare two string, return 0 if one equals to each other | ||
+ | int strcmp(char *a, char *b) | ||
+ | { | ||
+ | unsigned int ptr = 0; | ||
+ | while(a[ptr] != ' | ||
+ | { | ||
+ | if(a[ptr] != b[ptr]) return -1; | ||
+ | ptr++; | ||
+ | } | ||
+ | return 0; | ||
+ | } | ||
+ | |||
+ | //configure the Bluetooth through AT commands | ||
+ | int setupBlueToothConnection() | ||
+ | { | ||
+ | #if MASTER | ||
+ | Serial.println(" | ||
+ | #else | ||
+ | Serial.println(" | ||
+ | #endif | ||
+ | |||
+ | Serial.print(" | ||
+ | delay(3500);// | ||
+ | |||
+ | //send command to module in different baud rate | ||
+ | while(1) | ||
+ | { | ||
+ | delay(500); | ||
+ | blueToothSerial.begin(9600); | ||
+ | delay(500); | ||
+ | Serial.print(" | ||
+ | if(sendBlueToothCommand(" | ||
+ | break; | ||
+ | delay(500); | ||
+ | blueToothSerial.begin(115200); | ||
+ | delay(500); | ||
+ | Serial.print(" | ||
+ | if(sendBlueToothCommand(" | ||
+ | break; | ||
+ | } | ||
+ | | ||
+ | //we have to set the baud rate to 9600, since the soft serial is not stable at 115200 | ||
+ | sendBlueToothCommand(" | ||
+ | sendBlueToothCommand(" | ||
+ | sendBlueToothCommand(" | ||
+ | sendBlueToothCommand(" | ||
+ | blueToothSerial.begin(9600);// | ||
+ | delay(3500);// | ||
+ | //configure parameters of the module | ||
+ | sendBlueToothCommand(" | ||
+ | sendBlueToothCommand(" | ||
+ | sendBlueToothCommand(" | ||
+ | sendBlueToothCommand(" | ||
+ | sendBlueToothCommand(" | ||
+ | sendBlueToothCommand(" | ||
+ | sendBlueToothCommand(" | ||
+ | sendBlueToothCommand(" | ||
+ | sendBlueToothCommand(" | ||
+ | // | ||
+ | sendBlueToothCommand(" | ||
+ | #if MASTER | ||
+ | sendBlueToothCommand(" | ||
+ | #else | ||
+ | sendBlueToothCommand(" | ||
+ | #endif | ||
+ | sendBlueToothCommand(" | ||
+ | delay(3500);// | ||
+ | if(sendBlueToothCommand(" | ||
+ | Serial.print(" | ||
+ | return 0; | ||
+ | } | ||
+ | |||
+ | //send command to Bluetooth and return if there is a response | ||
+ | int sendBlueToothCommand(char command[]) | ||
+ | { | ||
+ | Serial.print(" | ||
+ | Serial.print(command); | ||
+ | Serial.println("" | ||
+ | |||
+ | blueToothSerial.print(command); | ||
+ | delay(200); | ||
+ | |||
+ | if(recvMsg(200) != 0) return -1; | ||
+ | |||
+ | Serial.print(" | ||
+ | Serial.print(recv_str); | ||
+ | Serial.println("" | ||
+ | return 0; | ||
+ | } | ||
+ | |||
+ | //receive message from Bluetooth with time out | ||
+ | int recvMsg(unsigned int timeout) | ||
+ | { | ||
+ | //wait for feedback | ||
+ | unsigned int time = 0; | ||
+ | unsigned char num; | ||
+ | unsigned char i; | ||
+ | | ||
+ | //waiting for the first character with time out | ||
+ | i = 0; | ||
+ | while(1) | ||
+ | { | ||
+ | delay(50); | ||
+ | if(blueToothSerial.available()) | ||
+ | { | ||
+ | recv_str[i] = char(blueToothSerial.read()); | ||
+ | i++; | ||
+ | break; | ||
+ | } | ||
+ | time++; | ||
+ | if(time > (timeout / 50)) return -1; | ||
+ | } | ||
+ | |||
+ | //read other characters from uart buffer to string | ||
+ | while(blueToothSerial.available() && (i < 100)) | ||
+ | { | ||
+ | recv_str[i] = char(blueToothSerial.read()); | ||
+ | i++; | ||
+ | } | ||
+ | recv_str[i] = ' | ||
+ | |||
+ | return 0; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | |||
+ | ===== Programme de test du module ===== | ||
+ | |||
+ | Ce Programme permet d' | ||
+ | |||
+ | |||
+ | < | ||
+ | |||
+ | #include < | ||
+ | SoftwareSerial hc06(2, 3); | ||
+ | String cmd = ""; | ||
+ | float sensor_val = 0; | ||
+ | // faire clignoter clignoter la led | ||
+ | byte led = 8; | ||
+ | int Tblink = 100; | ||
+ | float isBlink = false; // drapeau pour lancer le clignotement... ou non | ||
+ | |||
+ | void setup() { | ||
+ | // pin 13 OUTPUT | ||
+ | pinMode(led, | ||
+ | |||
+ | // | ||
+ | Serial.begin(9600); | ||
+ | // | ||
+ | hc06.begin(115200); | ||
+ | } | ||
+ | void loop() { | ||
+ | //Read data from HC06 | ||
+ | while (hc06.available() > 0) { | ||
+ | cmd += (char)hc06.read(); | ||
+ | } | ||
+ | //Select function with cmd | ||
+ | if (cmd != "" | ||
+ | Serial.print(" | ||
+ | Serial.println(cmd); | ||
+ | // We expect ON or OFF from bluetooth | ||
+ | if (cmd == " | ||
+ | Serial.println(" | ||
+ | digitalWrite(led, | ||
+ | isBlink = false; // arrêter le clignotement | ||
+ | } else if (cmd == " | ||
+ | Serial.println(" | ||
+ | digitalWrite(led, | ||
+ | isBlink = false; // arrêter le clignotement | ||
+ | } else { | ||
+ | // convertir la comande en un entier | ||
+ | int x = cmd.toInt(); | ||
+ | if (x >= 0 && x <= 10000) { // si la commande est un nombre entre 0 et 10000 (soit 10s) | ||
+ | Tblink = x; | ||
+ | clignoterLed(13, | ||
+ | isBlink=true; | ||
+ | } | ||
+ | else { | ||
+ | Serial.println(" | ||
+ | } | ||
+ | } | ||
+ | cmd = ""; | ||
+ | } | ||
+ | // gestion du clignotement | ||
+ | if (isBlink) { | ||
+ | clignoterLed(led, | ||
+ | } | ||
+ | |||
+ | // Simulate sensor measurement | ||
+ | sensor_val = (float)random(256); | ||
+ | |||
+ | //Write sensor data to HC06 | ||
+ | hc06.print(sensor_val); | ||
+ | delay(100); | ||
+ | } | ||
+ | |||
+ | void clignoterLed(int led, int Ta, int Te) { // led : patte connécéet à la led, Ta: temps d' | ||
+ | digitalWrite(led, | ||
+ | delay(Ta); | ||
+ | digitalWrite(led, | ||
+ | delay(Te); | ||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | |||
====== Carte Arduino Nano BLE ====== | ====== Carte Arduino Nano BLE ====== |