Automatisation connexion Bluetooth

Bonjour à tous,

Je rencontre quelques difficultés pour automatiser ma connexion PC-Arduino via Bluetooth.

J'ai connecter mon module Bluetooth ( http://www.robotshop.com/eu/fr/modem-bluetooth-bluesmirf-hid.html ) sur mon arduino.

Le sketch me permet de voir ce que j'envoie et ce que je reçois sur un terminal Putty configuré sur le port Com7 correspondant à ma clé bluetooth de mon Pc :

#include <SoftwareSerial.h>  

int bluetoothTx = 8;  // TX-O pin of bluetooth mate, Arduino D2
int bluetoothRx = 9;  // RX-I pin of bluetooth mate, Arduino D3

SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);

void setup()
{
  Serial.begin(9600);  // Begin the serial monitor at 9600bps

  bluetooth.begin(115200);  // The Bluetooth Mate defaults to 115200bps
  //bluetooth.begin(9600);  // The Bluetooth Mate defaults to 115200bps
  bluetooth.print("$");  // Print three times individually
  bluetooth.print("$");
  bluetooth.print("$");  // Enter command mode
  delay(1000);  // Short delay, wait for the Mate to send back CMD
  
  bluetooth.println("U,9600,N");  // Temporarily Change the baudrate to 9600, no parity
  // 115200 can be too fast at times for NewSoftSerial to relay the data reliably
  bluetooth.begin(9600);  // Start bluetooth serial at 9600
  delay(1000);  // Short delay, wait for the Mate to send back CMD

  // ça :
  bluetooth.println("C"); 
  delay(1000);  // Short delay, wait for the Mate to send back CMD
  // ne marche pas : la connexion ne s'établie pas :-(

  bluetooth.print("-"); 
  bluetooth.print("-"); 
  bluetooth.println("-"); //To return to data mode
}

void loop()
{
  if(bluetooth.available())  // If the bluetooth sent any characters
  {
    // Send any characters the bluetooth prints to the serial monitor
    Serial.print((char)bluetooth.read());  
  }
  if(Serial.available())  // If stuff was typed in the serial monitor
  {
    // Send any characters the Serial monitor prints to the bluetooth
    bluetooth.print((char)Serial.read());
  }
  // and loop forever and ever!
}

Lorsque je tape les commandes manuellement pour entrer en mod CMD et établir la connexion : ça marche :
"$$$" permet de passer en mode CMD, la led rouge clignote rapidement
J'arrive à connaitre la config du module bluetooth :

***Settings***
BTA=000666465362
BTName=FireFly-5362
Baudrt(SW4)=9600
Mode  =Slav   DTR (default)
Authen=1
PinCod=1234
Bonded=0
Rem=0009DD509002
***ADVANCED Settings***
SrvName= SPP
SrvClass=0000
DevClass=1F00
InqWindw=0060
PagWindw=0060
CfgTimer=255
StatuStr=NULL
HidFlags=200
DTRtimer=8
KeySwapr=0

"C" permet d'établir la connexion, elle s'établit lorsque la console Putty est en attente. Le led verte s'allume et la connexion est établie. Je peux dialoguer entre le console Putty et le moniteur série de l'Arduino. J'arrive à prendre la main sur le module Bluetooth depuis la console Putty grace à "$$$" et à stopper la communication avec la commande "K,".

Donc pas de souci matériel ni de branchement

J'aimerai automatiser cette connexion, pour cela j'ai mis dans le setup la ligne

bluetooth.println("C");

Mais elle n'a aucune influence sur l'état de la connexion : le module Bluetooth clignote toujours en rouge lentement.

Si vous avez une idée, elle sera la bien venue.

A+
Olivier

Autre question :

Suite la modification du sketch pour voir d'où viendrait le problème :

#include <SoftwareSerial.h>  

int bluetoothTx = 8;  // TX-O pin of bluetooth mate, Arduino D2
int bluetoothRx = 9;  // RX-I pin of bluetooth mate, Arduino D3

SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);


void setup()
{
  Serial.begin(9600);  // Begin the serial monitor at 9600bps

  bluetooth.begin(115200);  // The Bluetooth Mate defaults to 115200bps

  Serial.println("Passe en mode CMD :");
  bluetooth.print("$");  // Print three times individually
  bluetooth.print("$");
  bluetooth.print("$");  // Enter command mode
  delay(1000);  // Short delay, wait for the Mate to send back CMD
  Serial.println("devrait afficher CMD :");
  loop_BT();
  Serial.println("<>");

  //bluetooth.println("SM,3");  // Mode Auto-connect Master
  //delay(1000);  // Short delay, wait for the Mate to send back CMD

  Serial.println("Change le Baudrate à 9600 :");
  bluetooth.println("U,9600,N");  // Temporarily Change the baudrate to 9600, no parity
  // 115200 can be too fast at times for NewSoftSerial to relay the data reliably
  Serial.println("devrait afficher AOK :");
  loop_BT();
  Serial.println("<>");

  bluetooth.begin(9600);  // Start bluetooth serial at 9600
  delay(1000);  // Short delay, wait for the Mate to send back CMD

  Serial.println("Passe en mode CMD :");
  bluetooth.print("$");  // Print three times individually
  bluetooth.print("$");
  bluetooth.print("$");  // Enter command mode
  Serial.println("devrait afficher CMD :");
  loop_BT();
  Serial.println("<>");

  Serial.println("Etablie la connexion :");
  bluetooth.println("C"); 
  delay(1000);  // Short delay, wait for the Mate to send back CMD
  Serial.println("devrait afficher TRYING :");
  loop_BT();
  Serial.println("<>");

  Serial.println("Sort du mode CMD :");
  bluetooth.print("-"); 
  bluetooth.print("-"); 
  bluetooth.print("-"); //To return to data mode
  Serial.println("<>");
}

void loop(){
  loop_BT();
  loop_RS();
  // and loop forever and ever!
}



void loop_BT(){
  while(bluetooth.available()){  // If the bluetooth sent any characters
    // Send any characters the bluetooth prints to the serial monitor
    Serial.print((char)bluetooth.read()); 
    if (bluetooth.available()== 0 ){  // If no more characters
      delay(2) ; // 2 millisec pour attendre un eventuel autre char
    }
  }
}


void loop_RS(){
  while (Serial.available()){  // If stuff was typed in the serial monitor
    // Send any characters the Serial monitor prints to the bluetooth
    bluetooth.print((char)Serial.read());
    if (Serial.available() == 0){ // If no more characters
      delay(2) ; // 2 millisec pour attendre un eventuel autre char
    }
  }
}

Je n'ai nulle trace d'une réponse du module Bluetooth lorsque les commandes sont lancée par le setup. Celles-ci sont pourtant exécutées (sauf la connexion) :confused:

Vos idées sont les bien venues
Olivier

Bonjour à tous,

Je sais que je déterre un vieux topic, mais j'ai le même problème.
Je pense qu'il ne sert à rien d'avoir plusieurs pages sur le forum traitant du même sujet.

Donc si quelqu'un a une piste ...

Merci par avance

Puisque j'ai trouvé la réponse en forçant un peu sur les méninges, j'en fait profiter qui en aurait besoin ...

Voici le bon code :

#include <SoftwareSerial.h>  

int bluetoothTx = 2;
int bluetoothRx = 3;

SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);

void setup()
{
  Serial.begin(9600);  // Begin the serial monitor at 9600bps

  bluetooth.begin(115200);  // The Bluetooth Mate defaults to 115200bps
  bluetooth.print("$");  // Print three times individually
  bluetooth.print("$");
  bluetooth.print("$");  // Enter command mode
  delay(100);  // Short delay, wait for the Mate to send back CMD
  bluetooth.println("U,9600,N");  // Temporarily Change the baudrate to 9600, no parity
  // 115200 can be too fast at times for NewSoftSerial to relay the data reliably

  bluetooth.begin(9600);  // Start bluetooth serial at 9600
  delay(1000);

  bluetooth.print("$");  // Print three times individually
  bluetooth.print("$");
  bluetooth.print("$");  // Enter command mode
  delay(300);  // Short delay, wait for the Mate to send back CMD
  bluetooth.println("C");
  delay(1500);

Quelques explications :

Après l'instruction ci-dessous, la communication est réinitialisée.:

bluetooth.begin(9600);

Il faut donc rentrer à nouveau dans le mode de commande avant d'envoyer des instructions !

bluetooth.print("$");  // Print three times individually
bluetooth.print("$");
bluetooth.print("$");

Ensuite on peut envoyer à nouveau des commandes AT:

  bluetooth.println("C");

C'est logique, mais il fallait y penser !

Je ne sais pas si les pauses que je mets dans le programme sont indispensables.
A chacun de tester ...

Si ça peut servir ...