Connection bluetooth commande AT

a marqué ce sujet comme résolu.

Voici notre programme on utilise un module bluetooth grove BLE dual model HM-13. On cherche a comprendre pourquoi lorsque l’on lance le serial on reçoit la commande AT+LSTE ce qui déconnecte notre téléphone du bluetooth. On a remarqué aussi qu’une partie du programme ne se faisait pas ce qui nous empêche d’afficher les messages reçue (nous avons pourtant déjà réussi).Notre objectif afficher le RSSI entre notre téléphone et le module bluetooth or rien ne ce fait .

#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!!");
  while (setupBlueToothConnection() != 0);  //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("OK1");
        Serial.println("connected\r\n");
        break;
      }
    }
    delay(2000);
  }
}

void loop()
{
#if MASTER  //central role
  //in master mode, the bluetooth send message periodically.
  delay(400);
  Serial.println("send: hi");
  blueToothSerial.print("je suis opérationel");
  delay(2000);

  //get any message to print
  if (recvMsg(1000) == 0)
  {
    Serial.print("recv: ");
    Serial.print((char *)recv_str);
    Serial.println("");

  }
#else   //peripheral role
  delay(200);
  Serial.println("OK2");
  //the slave role only send message when received one.
  if (recvMsg(1000) == 0)
  {
    Serial.println("OK3");
    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(2000);
    blueToothSerial.begin(115200);
    delay(2000);
    Serial.print("try 115200\r\n");
    if (sendBlueToothCommand("AT") == 0)
      break;
  }
  blueToothSerial.begin(9600);
#if MASTER
  sendBlueToothCommand("AT+ROLB0");//set to master mode
#else
  sendBlueToothCommand("AT+ROLB1");
  sendBlueToothCommand("AT+MODE1");
#endif
  delay(3500);//wait for module restart
  sendBlueToothCommand ("AT+AUTH?");
  delay(2000);
  if (sendBlueToothCommand("AT") != 0) return -1; //detect if the module exists
  Serial.print("Setup complete\r\n\r\n");
  sendBlueToothCommand ("AT+RSSB?"); // interroger RSSI
  delay(2000);
  sendBlueToothCommand ("AT+RSSE?");
  delay(2000);
  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 < 9000))
  {
    recv_str[i] = char(blueToothSerial.read());
    i++;
  }
  recv_str[i] = '\0';

  return 0;
}
+0 -0
Connectez-vous pour pouvoir poster un message.
Connexion

Pas encore membre ?

Créez un compte en une minute pour profiter pleinement de toutes les fonctionnalités de Zeste de Savoir. Ici, tout est gratuit et sans publicité.
Créer un compte