Communication bluetooth

Fonctionnement en maître/esclave dans des picoréseaux de 7 clients (255 en mode parked) pour un maître (et 10 picoréseaux dans la même zone).

Par défaut, sur un pc (et tél. portable ?), il n'est pas possible de chercher un périphérique maître. Il n'est donc pas possible d'apairer le pc et le module en mode master ? Voir par ex. : http://www.elechouse.com/elechouse/index.php?main_page=product_info&cPath=90_91&products_id=602

Déroulement de la connexion

Configuration et commandes du modem bluetooth

1. Set working MODE

\r\n+STWMOD=0\r\n Set device working mode as client (slave). Save and Rest.
\r\n+STWMOD=1\r\n Set device working mode as server (master). Save and Rest.

Note: \r\n is necessary for operation and the value of are 0x0D 0x0A in Hex. \r and \n represent carriage-return and line-feed (or next line).

2.Set BAUDRATE

\r\n+STBD=115200\r\n Set baudrate 115200. Save and Rest.

Supported baudrate: 9600, 19200,38400,57600,115200,230400,460800.

3. Set Device NAME

\r\n+STNA=abcdefg\r\n Set device name as “abcdefg”. Save and Rest.

4. Auto-connect the last paired device on power

\r\n+STAUTO=0\r\n Auto-connect forbidden. Save and Rest.
\r\n+STAUTO=1\r\n Permit Auto-connect. Save and Rest.

5. Permit Paired device to connect me

\r\n+STOAUT=0\r\n Forbidden. Save and Rest.
\r\n+STOAUT=1\r\n Permit. Save and Rest.

6. Set PINCODE

\r\n +STPIN=2222\r\n Set pincode “2222”, Save and Rest.

7. Delete PINCODE(input PINCODE by MCU)

\r\n+DLPIN\r\n Delete pincode. Save and Rest.

8. Read local ADDRESS CODE

\r\n+RTADDR\r\n Return address of the device.

9. Auto-reconnecting when master device is beyond the valid range (slave device will auto-reconnect in 30 min when it is beyond the valid range)

\r\n+LOSSRECONN=0\r\n Forbid auto-reconnecting.
\r\n+LOSSRECONN=1\r\n Permit auto-reconnecting.

Commands for Normal Operation

1. Inquire

a) Master \r\n+INQ=0\r\n Stop Inquiring
\r\n+INQ=1\r\n Begin/Restart Inquiring
b) Slave
\r\n+INQ=0\r\n Disable been inquired
\r\n+INQ=1\r\n Enable been inquired


When +INQ=1 command is successful, the red and green LEDS blink alternatively.

2. Bluetooth module returns inquiring result

\r\n+RTINQ=aa,bb,cc,dd,ee,ff;name\r\n Serial Bluetooth device with the address “aa,bb,cc,dd,ee,ff” and the name “name” is inquired

3. Connect device

\r\n+CONN=aa,bb,cc,dd,ee,ff\r\n Connect to a device with address of "aa,bb,cc,dd,ee,ff”

4. Bluetooth module requests inputting PINCODE

\r\n+INPIN\r\n

5. Input PINCODE

\r\n+RTPIN=code\r\n
Example: RTPIN=0000 Input PINCODE which is four zero

6. Disconnect device Pulling PIO0 high will disconnect current working Bluetooth device.

7. Return status \r\n+BTSTA:xx\r\n

xx status:

0 - Initializing
1 - Ready
2 - Inquiring
3 - Connecting
4 - Connected

(Note: This is not a command, but the information returned from the module after every command)

Avec arduino

Pour communiquer avec le module bluetooth, la librairie SoftwareSerial permet d'initialiser le module, de définir les pins pour l'émission (Tx) et la réception (Rx) et de lire et écrire des données.

Exemple

#include <SoftwareSerial.h> #define RxD 2 #define TxD 3 SoftwareSerial blueToothSerial(RxD,TxD); void setup() { pinMode(RxD, INPUT); pinMode(TxD, OUTPUT); Serial.begin(38400); //Set BluetoothBee BaudRate to default baud rate 38400 blueToothSerial.begin(38400); //Set BluetoothBee BaudRate to default baud rate 38400 delay(1000); sendBlueToothCommand("\r\n+STBD=38400\r\n"); sendBlueToothCommand("\r\n+STWMOD=1\r\n"); sendBlueToothCommand("\r\n+STNA=BaseBT\r\n"); sendBlueToothCommand("\r\n +STPIN=0000\r\n"); delay(2000); // This delay is required. sendBlueToothCommand("\r\n+INQ=1\r\n"); delay(2000); // This delay is required. }

Envoyer des commandes au module bluetooth

Pour communiquer avec un module bluetooth, il faut transmettre les codes pour du retour à la ligne (carriage-return) et de nouvelle ligne (line-feed) suivit de la commande. La séquence est terminée en envoyant à nouveau \r\n.

Le module renvoit des informations sur son status ainsi que le texte "OK" une fois la commande acceptée et interprétée. Il est nécessaire (?) d'attendre que le modem termine de traiter une commande avant de passer à la suivante. Pour cette raison, la solution consiste à attendre la réception du "OK" après l'envoie d'une nouvelle commande.

Algorithme :
Boucler sans fin
Attendre des données sur le canal bluetooth
Lire le caractère reçu
Si le caractère est égal à la lettre 'O', on valide la première étape du test
Si le caractère est égal à la lettre 'K' ET que la première étape du test est validée, on sort de la boucle sans fin
Si le caractère n'est ni 'O' ni 'K', on réinitialise la variable de test

Références

http://www.commentcamarche.net/contents/bluetooth/bluetooth-fonctionnement.php3

http://www.seeedstudio.com/wiki/Bluetooth_Bee

© Clic4.org 2012