Jump to content


Photo

Code for Bluetooth Shields


  • Please log in to reply
4 replies to this topic

#1 chain

chain

    Advanced Member

  • Members
  • PipPipPip
  • 756 posts

Posted 08 October 2014 - 02:11 AM

Can you please help.

I have set up 2 Bluetooth Shields on 2 Arduino UNO One Master One Slave -I have the RED lights flashing.

Can you please send me an " Complete " Arduino Code showing me the setup (LED,s or return commands) and hardware to send a command via a PC using a Serial Monitor so I can prove/test the Bluetooth Shields are working, then I can look at the structure of the Arduino Code to add two servos.



#2 chain

chain

    Advanced Member

  • Members
  • PipPipPip
  • 756 posts

Posted 08 October 2014 - 02:21 AM

You said your shields' RED lights flashing. Can your Bluetooth shields send and receive data each other?

 

In the Quick Test , you can see these steps: http://linksprite.co...uino#Quick_Test



#3 Guest_Jeff Harris_*

Guest_Jeff Harris_*
  • Guests

Posted 08 October 2014 - 07:10 AM

 

I have tried Serial Communication in X-CTU Host and Slave as photos Bluetooth Shield_host_slave_8 and 9.jpg

It does not talk "Hello Master" " Hello Slave" are the Bluetooth Shields Talking can you send me an Arduion Code say Blink or Swipe so I can test my are Talking Thanks Jeff Harris :)



#4 chain

chain

    Advanced Member

  • Members
  • PipPipPip
  • 756 posts

Posted 09 October 2014 - 05:10 AM

Please try out below code. You need connect a LED on both bluetooth shields' D8 .
 
 
Bluetooth Slave code:
 
#include <SoftwareSerial.h>  
#define RxD 7
#define TxD 6
SoftwareSerial BlueToothSerial(RxD,TxD);
char flag=1;
const int ledPin = 8;
 
void Test_BlueTooth()
{
   unsigned char t=0;
   String RXD = "";
   Serial.println("Test BlueTooth ...");
   BlueToothSerial.print("AT\r\n");  
   delay(100);
   while(BlueToothSerial.available()>0)
   {    
      RXD +=  char( BlueToothSerial.read() );
      delay(1);
   } 
   do{
     t++;
     delay(400);
     Serial.println("Test Failed ! Retrying ...");
   }while( ((RXD[0]!='O')&&(RXD[1]!='K'))&&(t<5));  
   if(t<5) Serial.println("Test Successful !\r\n");
   else { Serial.println("Retry Failed !"); while(1); }
}
 
void sendBlueToothCommand(char *Command)
{
  BlueToothSerial.print(Command);
  Serial.print(Command); 
  delay(100);
  while(BlueToothSerial.available())
  {    
     Serial.print(char(BlueToothSerial.read())); 
  }
}
 
void setupBlueTooth()
{
  Serial.println("Bluetooth Initialization ...");      
  sendBlueToothCommand("AT+NAME=Slave\r\n");
  sendBlueToothCommand("AT+ROLE=0\r\n");
  sendBlueToothCommand("AT+CMODE=1\r\n");
  sendBlueToothCommand("AT+PSWD=1234\r\n");
  sendBlueToothCommand("AT+UART=38400,0,0\r\n");
  delay(500);
  Serial.println("Bluetooth Initialized Successfully !\r\n");
  do{
    if(Serial.available())
    {
      if( Serial.read() == 'S')
      {
        sendBlueToothCommand("AT+RESET\r\n");
        flag = 0;
      }
    }
  }while(flag);
}
 
void setup()
{
   Serial.begin(38400);     
   BlueToothSerial.begin(38400); 
   delay(500);
   Test_BlueTooth();  
   setupBlueTooth();
   pinMode (ledPin, OUTPUT);
}
 
void loop()
{
    if(BlueToothSerial.available())
    {
       char btRecive = BlueToothSerial.read();
       Serial.print(btRecive);
       if (btRecive == 'o'){
         digitalWrite (ledPin, HIGH);
       }
       else {
         digitalWrite (ledPin, LOW);
       }
    }
    if(Serial.available())
    {
      BlueToothSerial.print(char(Serial.read()));
    }    
}
 
 
Bluetooth Master code:
 
#include <SoftwareSerial.h>  
#define RxD 7
#define TxD 6
SoftwareSerial BlueToothSerial(RxD,TxD);
const int ledPin = 8;
 
void Test_BlueTooth()
{
   unsigned char t=0;
   String RXD = "";
   Serial.println("Test BlueTooth ...");
   BlueToothSerial.print("AT\r\n");  
   delay(100);
   while(BlueToothSerial.available()>0)
   {    
      RXD +=  char( BlueToothSerial.read() );
      delay(1);
   } 
   do{
     t++;
     delay(400);
     Serial.println("Test Failed ! Retrying ...");
   }while( ((RXD[0]!='O')&&(RXD[1]!='K'))&&(t<5));  
   if(t<5) Serial.println("Test Successful !\r\n");
   else { Serial.println("Retry Failed !"); while(1); }
}
 
void sendBlueToothCommand(char *Command)
{
  BlueToothSerial.print(Command);
  Serial.print(Command); 
  delay(100);
  while(BlueToothSerial.available())
  {    
     Serial.print(char(BlueToothSerial.read())); 
  }
}
 
void setupBlueTooth()
{
  Serial.println("Bluetooth Initialization ...");      
  sendBlueToothCommand("AT+NAME=Master\r\n");
  sendBlueToothCommand("AT+ROLE=1\r\n");// 1:Master   0:Slave
  sendBlueToothCommand("AT+PSWD=1234\r\n");
  sendBlueToothCommand("AT+UART=38400,0,0\r\n");
  sendBlueToothCommand("AT+CMODE=1\r\n");
  sendBlueToothCommand("AT+INIT\r\n");
  sendBlueToothCommand("AT+PAIR=2014,3,251687,10\r\n");//slave address 
  for(int i=0;i<5;i++) delay(1000);
  BlueToothSerial.flush();
  Serial.println("Bluetooth Initialized Successfully !\r\n");
}
 
void setup()
{
   Serial.begin(38400);     
   BlueToothSerial.begin(38400); 
   delay(500);
   Test_BlueTooth();  
   setupBlueTooth();
   pinMode (ledPin, OUTPUT);
}
 
void loop()
{
  if(BlueToothSerial.available())
  {
     char btRecive = BlueToothSerial.read();
       Serial.print(btRecive);
       if (btRecive == 'p'){
         digitalWrite (ledPin, HIGH);
       }
       else {
         digitalWrite (ledPin, LOW);
       }
  }
  if(Serial.available())
  {
     BlueToothSerial.print(char(Serial.read()));
  }      
}


#5 satbirkira

satbirkira

    Newbie

  • Members
  • Pip
  • 2 posts

Posted 06 February 2017 - 06:11 PM

Nevermind, I found the correct tutorial 

 

http://learn.linkspr...-ble4-0-shield/






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users