Outils pour utilisateurs

Outils du site


wiki:tutoriels:arduino:module-bleutooth

Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

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'utiliser un module BlueTooth avec une carte Arduino: Vous trouverez un lien sur un tutoriel sur la manière d'utiliser un module BlueTooth avec une carte Arduino:
  
-  * [[https://eskimon.fr/tuto-arduino-907-utiliser-un-module-bluetooth-hc-05-avec-arduino|module-bluetooth-hc-05]]+  * [[https://eskimon.fr/tuto-arduino-907-utiliser-un-module-bluetooth-hc-05-avec-arduino|Eskimon - module-bluetooth-hc-05]]
   * [[https://www.aranacorp.com/fr/arduino-et-le-module-bluetooth-hc-06/|module-bluetooth-hc-06]]   * [[https://www.aranacorp.com/fr/arduino-et-le-module-bluetooth-hc-06/|module-bluetooth-hc-06]]
   * [[https://knowledge.parcours-performance.com/arduino-bluetooth-hc-05-hc-06/|caractéristiques techniques module-bluetooth-hc-05-06]]   * [[https://knowledge.parcours-performance.com/arduino-bluetooth-hc-05-hc-06/|caractéristiques techniques module-bluetooth-hc-05-06]]
Ligne 42: Ligne 42:
 #include <SoftwareSerial.h> #include <SoftwareSerial.h>
  
-SoftwareSerial hc06(2,3);+SoftwareSerial Module_BT(2,3); // RX, TX
  
 void setup(){ void setup(){
Ligne 49: Ligne 49:
   Serial.println("ENTER AT Commands:");   Serial.println("ENTER AT Commands:");
   //Initialize Bluetooth Serial Port   //Initialize Bluetooth Serial Port
-  hc06.begin(9600);+  Module_BT.begin(9600);
 } }
  
 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()){
-    hc06.write(Serial.read());+    Module_BT.write(Serial.read());
   }     }  
 } }
Ligne 66: Ligne 66:
  
 Le code Arduino est téléchargeable ici : {{ :wiki:tutoriels:arduino:init_hc-06.zip |}} Le code Arduino est téléchargeable ici : {{ :wiki:tutoriels:arduino:init_hc-06.zip |}}
 +
 +=== 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 "HC-05"... Difficile de choisir. On peut s'en sortir si on connait l'adresse MAC (identifiant unique du matériel) du type : 7D:2C:55:C0:2E:83.
 +
 +
 +La solution est disponible sur cet Article : [[https://forum.arduino.cc/t/hc-05-bluetooth-mac-adress/571920/2|Forum Arduino]]
 +
 +La liste de toutes les commandes AT pour le module BT en mode "commande" : [[https://eskimon.fr/commandes_AT_HC05.pdf|Embedded Bluetooth Serial Communication Module - AT command set]]
 +
  
  
Ligne 84: Ligne 94:
   * [[https://play.google.com/store/apps/details?id=Qwerty.BluetoothTerminal&hl=en|Bluetooth Terminal]]   * [[https://play.google.com/store/apps/details?id=Qwerty.BluetoothTerminal&hl=en|Bluetooth Terminal]]
  
-== Programme ==+== Programme de test du module == 
 + 
 +Ce Programme permet d'allumer une LED en envoyant ON via le terminal BT du smartphone. Si on envoie OFF, la LED s'éteind. Si on envoie un nombre "T" compris entre 0 et 10000, la LED va clignoter avec une période T. 
  
-Il faut ensuite téléverser le code suivant sur la carte Arduino : 
  
 <code> <code>
 +
 #include <SoftwareSerial.h> #include <SoftwareSerial.h>
 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);
   //Initialize Bluetooth Serial Port   //Initialize Bluetooth Serial Port
-  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 == "ON") {     if (cmd == "ON") {
       Serial.println("Function is on");       Serial.println("Function is on");
-      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 == "OFF") {     } else if (cmd == "OFF") {
       Serial.println("Function is off");       Serial.println("Function is off");
-      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'allumage, Te: temps led éteinte
 +  digitalWrite(led, HIGH);
 +  delay(Ta);
 +  digitalWrite(led, LOW);
 +  delay(Te);
 } }
  
 </code> </code>
 +
 +
 +
  
 Le code Arduino est téléchargeable ici : Le code Arduino est téléchargeable ici :
Ligne 313: Ligne 335:
  
 {{ :wiki:tutoriels:arduino:ori-module-bluetooth-hc05-26097.jpeg?400 |}} {{ :wiki:tutoriels:arduino:ori-module-bluetooth-hc05-26097.jpeg?400 |}}
 +
 +===== Mode Commende : initialisation du module - Facultatif =====
 +voir tutoriel : [[https://retroetgeek.com/arduino/configuration-du-module-hc-05-pour-arduino/|Retrogeek]]
 +
 +==== Câblage  ====
 +{{ :wiki:tutoriels:arduino:hc-05configuration-circuit_bb.jpg?600 |}}
 +
 +==== Programme ====
 +<code>
 +#include <SoftwareSerial.h>
 +
 +SoftwareSerial mySerial(10, 11); // RX, TX
 +
 +void setup() {
 +  Serial.begin(9600);
 +  pinMode(9,OUTPUT); digitalWrite(9,HIGH);
 +  Serial.println("Enter AT commands:");
 +  mySerial.begin(38400);
 +}
 +
 +void loop()
 +{
 +  if (mySerial.available())  
 +  Serial.write(mySerial.read());
 +  
 +  if (Serial.available())  
 +  mySerial.write(Serial.read());
 +}
 +</code>
 +
 +==== 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,du,slave   (on a remplacé les : par des , ) , permet de connecter un module master à un slave
 +
 ===== Bibliographie ===== ===== Bibliographie =====
 Data sheet : Data sheet :
Ligne 334: Ligne 407:
  
 {{ :wiki:tutoriels:arduino:grove-ble-dual_model-v1.0.jpeg?400 |}} {{ :wiki:tutoriels:arduino:grove-ble-dual_model-v1.0.jpeg?400 |}}
 +
 +Le tutoriel d'utilisation est le suivant : [[https://wiki.seeedstudio.com/Grove-BLE-dual_model-v1.0/|Wiki Seeeduino -Grove-BLE-dual_model-v1.0
 +]]
 +
 +
 +Le code permettant de connaître les caractéristiques du modules BT Grove-BLE est accessible ici : [[https://github.com/Seeed-Studio/HM_13_SW|Github]]
 +
 +===== Etalonage du module =====
 +
 +Le code permettant de connaître les caractéristiques du module BT Grove-BLE est le suivant : [[https://github.com/Seeed-Studio/HM_13_SW/blob/master/HM_13_SW.ino|Programme Arduino Sur Github]]
 +
 +
 +<code>
 +
 +
 +/*
 +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.  See the GNU
 +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  USA
 +For more details about the product please check http://www.seeedstudio.com/depot/
 +*/
 +
 +/* Upload this sketch into Arduino Uno and press reset*/
 +
 +#include <SoftwareSerial.h>   //Software Serial Port
 +#define RxD 2
 +#define TxD 3
 +
 +#define MASTER 1    //change this macro to define the Bluetooth as Master or not 
 +
 +SoftwareSerial blueToothSerial(RxD,TxD);//the software serial port 
 +
 +char recv_str[100];
 +
 +void setup() 
 +
 +    Serial.begin(115200);   //Serial port for debugging
 +    pinMode(RxD, INPUT);    //UART pin for Bluetooth
 +    pinMode(TxD, OUTPUT);   //UART pin for Bluetooth
 +    Serial.println("\r\nPower on!!");
 +    if(setupBlueToothConnection() != 0) while(1);   //initialize Bluetooth
 +    //this block is waiting for connection was established.
 +    while(1)
 +    {
 +        if(recvMsg(1000) == 0)
 +        {
 +            if(strcmp((char *)recv_str, (char *)"OK+CONB") == 0)
 +            {
 +                Serial.println("connected\r\n");
 +                break;
 +            }
 +        }
 +        delay(200);
 +    }
 +
 +
 +void loop() 
 +
 +    #if MASTER  //central role
 +    //in master mode, the bluetooth send message periodically. 
 +    delay(400);
 +    Serial.println("send: hi");
 +    blueToothSerial.print("hi");
 +    delay(100);
 +    //get any message to print
 +    if(recvMsg(1000) == 0)
 +    {
 +        Serial.print("recv: ");
 +        Serial.print((char *)recv_str);
 +        Serial.println("");
 +    }
 +    #else   //peripheral role
 +    delay(200);
 +    //the slave role only send message when received one.
 +    if(recvMsg(1000) == 0)
 +    {
 +        Serial.print("recv: ");
 +        Serial.print((char *)recv_str);
 +        Serial.println("");
 +        Serial.println("send: hello");
 +        blueToothSerial.print("hello");//return back message
 +    }
 +    #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] != '\0')
 +    {
 +        if(a[ptr] != b[ptr]) return -1;
 +        ptr++;
 +    }
 +    return 0;
 +}
 +
 +//configure the Bluetooth through AT commands
 +int setupBlueToothConnection()
 +{
 +    #if MASTER
 +    Serial.println("this is MASTER\r\n");
 +    #else
 +    Serial.println("this is SLAVE\r\n");
 +    #endif
 +
 +    Serial.print("Setting up Bluetooth link\r\n");
 +    delay(3500);//wait for module restart
 +
 +    //send command to module in different baud rate
 +    while(1)
 +    {
 +        delay(500);
 +        blueToothSerial.begin(9600);
 +        delay(500);
 +        Serial.print("try 9600\r\n");
 +        if(sendBlueToothCommand("AT") == 0)
 +            break;
 +        delay(500);
 +        blueToothSerial.begin(115200);
 +        delay(500);
 +        Serial.print("try 115200\r\n");
 +        if(sendBlueToothCommand("AT") == 0)
 +            break;
 +    }
 +    
 +    //we have to set the baud rate to 9600, since the soft serial is not stable at 115200
 +    sendBlueToothCommand("AT+RENEW");//restore factory configurations
 +    sendBlueToothCommand("AT+BAUD2");//reset the module's baud rate
 +    sendBlueToothCommand("AT+AUTH1");//enable authentication
 +    sendBlueToothCommand("AT+RESET");//restart module to take effect
 +    blueToothSerial.begin(9600);//reset the Arduino's baud rate
 +    delay(3500);//wait for module restart
 +    //configure parameters of the module
 +    sendBlueToothCommand("AT+VERS?");//get firmware version
 +    sendBlueToothCommand("AT+ADDE?");//get EDR MAC
 +    sendBlueToothCommand("AT+ADDB?");//get BLE MAC
 +    sendBlueToothCommand("AT+NAMEHM-13-EDR");//set EDR name
 +    sendBlueToothCommand("AT+NAMBHM-13-BLE");//set BLE name
 +    sendBlueToothCommand("AT+PINE123451");//set EDR password
 +    sendBlueToothCommand("AT+PINB123451");//set BLE password
 +    sendBlueToothCommand("AT+SCAN0");//set module visible
 +    sendBlueToothCommand("AT+NOTI1");//enable connect notifications
 +    //sendBlueToothCommand("AT+NOTP1");//enable address notifications
 +    sendBlueToothCommand("AT+PIO01");//enable key function
 +    #if MASTER
 +    sendBlueToothCommand("AT+ROLB1");//set to master mode
 +    #else
 +    sendBlueToothCommand("AT+ROLB0");//set to slave mode
 +    #endif
 +    sendBlueToothCommand("AT+RESET");//restart module to take effect
 +    delay(3500);//wait for module restart
 +    if(sendBlueToothCommand("AT") != 0) return -1;//detect if the module exists
 +    Serial.print("Setup complete\r\n\r\n");
 +    return 0;
 +}
 +
 +//send command to Bluetooth and return if there is a response
 +int sendBlueToothCommand(char command[])
 +{
 +    Serial.print("send: ");
 +    Serial.print(command);
 +    Serial.println("");
 +
 +    blueToothSerial.print(command);
 +    delay(200);
 +
 +    if(recvMsg(200) != 0) return -1;
 +
 +    Serial.print("recv: ");
 +    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] = '\0';
 +
 +    return 0;
 +}
 +</code>
 +
 +
 +===== Programme de test du module =====
 +
 +Ce Programme permet d'allumer une LED en envoyant ON via le terminal BT du smartphone. Si on envoie OFF, la LED s'éteind. Si on envoie un nombre "T" compris entre 0 et 10000, la LED va clignoter avec une période T. 
 +
 +
 +<code>
 +
 +#include <SoftwareSerial.h>
 +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, OUTPUT);
 +
 +  //Initialize Serial Monitor
 +  Serial.begin(9600);
 +  //Initialize Bluetooth Serial Port
 +  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("Command recieved : ");
 +    Serial.println(cmd);
 +    // We expect ON or OFF from bluetooth
 +    if (cmd == "ON") {
 +      Serial.println("Function is on");
 +      digitalWrite(led, HIGH); // alumer la led
 +      isBlink = false; // arrêter le clignotement
 +    } else if (cmd == "OFF") {
 +      Serial.println("Function is off");
 +      digitalWrite(led, LOW); // éteindre la led
 +      isBlink = false; // arrêter le clignotement
 +    } else {
 +      // convertir la comande en un entier
 +      int x = cmd.toInt(); // conversion de la chaine de caractère en un entier
 +      if (x >= 0 && x <= 10000) { // si la commande est un nombre entre 0 et 10000 (soit 10s)
 +        Tblink = x;
 +        clignoterLed(13, x, x);
 +        isBlink=true;
 +      }
 +      else {
 +        Serial.println("Function is off by default");
 +      }
 +    }
 +    cmd = ""; //reset cmd
 +  }
 +  // gestion du clignotement
 +  if (isBlink) {
 +    clignoterLed(led,Tblink,Tblink);
 +  }
 +
 +  // Simulate sensor measurement
 +  sensor_val = (float)random(256); // random number between 0 and 255
 +
 +  //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'allumage, Te: temps led éteinte
 +  digitalWrite(led, HIGH);
 +  delay(Ta);
 +  digitalWrite(led, LOW);
 +  delay(Te);
 +}
 +
 +</code>
 +
 +
  
 ====== Carte Arduino Nano BLE ====== ====== Carte Arduino Nano BLE ======
wiki/tutoriels/arduino/module-bleutooth.1652174420.txt.gz · Dernière modification: 2022/05/10 11:20 de damien.muti