3. Arduino blue tooth



Arduino bluetooth 

voorbeeldcode:

De code gebruiken in combinatie met bluetooth controller app.
Deze app is geschikt voor het aansturen van meerdere objecten:
https://play.google.com/store/apps/details?id=dev.merahkemarun.btcontrollamp&hl=en
1. Installeer de app op je (Android) telefoon.
2. Upload onderstaande code in de Arduino IDE
3. Sluit de bedrading aan zoals op het aansluitschema staat aangegeven.
4. Probeer het programma uit.

  

/*
Lamp 1 connected to PinOut 2 Arduino
Lamp 2 connected to PinOut 3 Arduino
Lamp 3 connected to PinOut 4 Arduino
Lamp 4 connected to PinOut 5 Arduino
Lamp 5 connected to PinOut 6 Arduino
Lamp 6 connected to PinOut 7 Arduino
Lamp 7 connected to PinOut 8 Arduino
Lamp 8 connected to PinOut 9 Arduino
--->you can connected to relay modul 8 channel

Serial data sending from Bluetooth Controll Lamp.apk
data '1'-'8' to on is lamp 1-8
data 'A'-'H' to off is lamp 1-8
data '9' to on ALL is lamp
data 'I' to off ALL is lamp
======================================================================================*/


#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); //Pin10 RX , Pin 11 TX connected to--> Bluetooth TX,RX

#define Lamp1 2
#define Lamp2 3
#define Lamp3 4
#define Lamp4 5
#define Lamp5 6
#define Lamp6 7
#define Lamp7 8
#define Lamp8 9

//If Out to active Low change ON 0 and OFF 1
//If Out to active High change ON 1 and OFF 0
#define ON 1
#define OFF 0
char val;
String statusLamp1,statusLamp2,statusLamp3,statusLamp4,statusLamp5,statusLamp6,statusLamp7,statusLamp8;

void setup() {
  pinMode(Lamp1,OUTPUT);digitalWrite (Lamp1,OFF);
  pinMode(Lamp2,OUTPUT);digitalWrite (Lamp2,OFF);
  pinMode(Lamp3,OUTPUT);digitalWrite (Lamp3,OFF);
  pinMode(Lamp4,OUTPUT);digitalWrite (Lamp4,OFF);
  pinMode(Lamp5,OUTPUT);digitalWrite (Lamp5,OFF);
  pinMode(Lamp6,OUTPUT);digitalWrite (Lamp6,OFF);
  pinMode(Lamp7,OUTPUT);digitalWrite (Lamp7,OFF);
  pinMode(Lamp8,OUTPUT);digitalWrite (Lamp8,OFF);
  mySerial.begin(9600);
  Serial.begin(9600);
}

void loop() {
//cek data serial from bluetooth android App
if( mySerial.available() >0 ) {
    val = mySerial.read();
    Serial.println(val); 
}
//Lamp is on
  if( val == '1' ) {
    digitalWrite(Lamp1,ON); statusLamp1="1"; }
  else if( val == '2' ) {
    digitalWrite(Lamp2,ON); statusLamp2="2"; }
  else if( val == '3' ) {
    digitalWrite(Lamp3,ON); statusLamp3="3"; }
  else if( val == '4' ) {
    digitalWrite(Lamp4,ON); statusLamp4="4"; }
  else if( val == '5' ) {
    digitalWrite(Lamp5,ON); statusLamp5="5"; }
  else if( val == '6' ) {
    digitalWrite(Lamp6,ON); statusLamp6="6"; }
  else if( val == '7' ) {
    digitalWrite(Lamp7,ON); statusLamp7="7"; }
  else if( val == '8' ) {
    digitalWrite(Lamp8,ON); statusLamp8="8"; }
  else if( val == '9' ) {
    digitalWrite(Lamp1,ON); statusLamp1="1"; 
    digitalWrite(Lamp2,ON); statusLamp2="2"; 
    digitalWrite(Lamp3,ON); statusLamp3="3"; 
    digitalWrite(Lamp4,ON); statusLamp4="4"; 
    digitalWrite(Lamp5,ON); statusLamp5="5"; 
    digitalWrite(Lamp6,ON); statusLamp6="6"; 
    digitalWrite(Lamp7,ON); statusLamp7="7"; 
    digitalWrite(Lamp8,ON); statusLamp8="8"; 
 }
 //Lamp is off
  else if( val == 'A' ) {
    digitalWrite(Lamp1,OFF); statusLamp1="A"; }
  else if( val == 'B' ) {
    digitalWrite(Lamp2,OFF); statusLamp2="B"; }
  else if( val == 'C' ) {
    digitalWrite(Lamp3,OFF); statusLamp3="C"; }
  else if( val == 'D' ) {
    digitalWrite(Lamp4,OFF); statusLamp4="D"; }
  else if( val == 'E' ) {
    digitalWrite(Lamp5,OFF); statusLamp5="E"; }
  else if( val == 'F' ) {
    digitalWrite(Lamp6,OFF); statusLamp6="F"; }
  else if( val == 'G' ) {
    digitalWrite(Lamp7,OFF); statusLamp7="G"; }
  else if( val == 'H' ) {
    digitalWrite(Lamp8,OFF); statusLamp8="H"; }
  else if( val == 'I' ) {
    digitalWrite(Lamp1,OFF); statusLamp1="A";
    digitalWrite(Lamp2,OFF); statusLamp2="B";
    digitalWrite(Lamp3,OFF); statusLamp3="C";
    digitalWrite(Lamp4,OFF); statusLamp4="D";
    digitalWrite(Lamp5,OFF); statusLamp5="E";
    digitalWrite(Lamp6,OFF); statusLamp6="F";
    digitalWrite(Lamp7,OFF); statusLamp7="G";
    digitalWrite(Lamp8,OFF); statusLamp8="H";
 }
  //synchronize Arduino to APK
 else if( val == 'S' ) {
    //send data to android apk
    delay(500);    
    mySerial.println(statusLamp1+statusLamp2+statusLamp3+statusLamp4+statusLamp5+statusLamp6+statusLamp7+statusLamp8+"J"); //delay(500);
    val=' ';
 }
}


/*Arduino project created by: pujar
//www.mutekla.com
//Apk Android remote controll suport this project, download on Playstore:
//Bluetooth Controll Lamp.apk
https://play.google.com/store/apps/details?id=dev.merahkemarun.btcontrolllamp
*/





*************************************************************************************************************************************


code bluetooth shield:
 

/*
BluetoothShield Demo Code Master.pde.This sketch could be used with
Slave.pde to establish connection between two Arduino.
2011 Copyright (c) Seeed Technology Inc.  All right reserved.
 
Author: Steve Chang
 
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 Seeeduino and press reset*/
#include <SoftwareSerial.h>   //Software Serial Port

#define RxD 6
#define TxD 7
 
#define DEBUG_ENABLED  1

String retSymb = "+RTINQ=";//start symble when there's any return
String slaveName = ";SeeedBTSlave";// caution that ';'must be included, and make sure the slave name is right.
int nameIndex = 0;
int addrIndex = 0;

String recvBuf;
String slaveAddr;

String connectCmd = "\r\n+CONN=";

SoftwareSerial blueToothSerial(RxD,TxD);
 
void setup()
{
  Serial.begin(9600);
  pinMode(RxD, INPUT);
  pinMode(TxD, OUTPUT);
  setupBlueToothConnection();
  //wait 1s and flush the serial buffer
  delay(1000);
  Serial.flush();
  blueToothSerial.flush();
}
 
void loop()
{
  char recvChar;
  while(1){
    if(blueToothSerial.available()){//check if there's any data sent from the remote bluetooth shield
      recvChar = blueToothSerial.read();
      Serial.print(recvChar);
    }
    if(Serial.available()){//check if there's any data sent from the local serial terminal, you can add the other applications here
      recvChar  = Serial.read();
      blueToothSerial.print(recvChar);
    } 
 }
}
 
void setupBlueToothConnection()
{
  blueToothSerial.begin(38400); //Set BluetoothBee BaudRate to default baud rate 38400
  blueToothSerial.print("\r\n+STWMOD=1\r\n");//set the bluetooth work in master mode
  blueToothSerial.print("\r\n+STNA=SeeedBTMaster\r\n");//set the bluetooth name as "SeeedBTMaster"
  blueToothSerial.print("\r\n+STAUTO=0\r\n");// Auto-connection is forbidden here
  delay(2000); // This delay is required.
  blueToothSerial.flush();
  blueToothSerial.print("\r\n+INQ=1\r\n");//make the master inquire
  Serial.println("Master is inquiring!");
  delay(2000); // This delay is required.
   
  //find the target slave
  char recvChar;
  while(1){
    if(blueToothSerial.available()){
      recvChar = blueToothSerial.read();
      recvBuf += recvChar;
      nameIndex = recvBuf.indexOf(slaveName);//get the position of slave name
      //nameIndex -= 1;//decrease the ';' in front of the slave name, to get the position of the end of the slave address
      if ( nameIndex != -1 ){
        //Serial.print(recvBuf);
  addrIndex = (recvBuf.indexOf(retSymb,(nameIndex - retSymb.length()- 18) ) + retSymb.length());//get the start position of slave address    
  slaveAddr = recvBuf.substring(addrIndex, nameIndex);//get the string of slave address    
  break;
      }
    }
  }
  //form the full connection command
  connectCmd += slaveAddr;
  connectCmd += "\r\n";
  int connectOK = 0;
  Serial.print("Connecting to slave:");
  Serial.print(slaveAddr);
  Serial.println(slaveName);
  //connecting the slave till they are connected
  do{
    blueToothSerial.print(connectCmd);//send connection command
    recvBuf = "";
    while(1){
      if(blueToothSerial.available()){
        recvChar = blueToothSerial.read();
  recvBuf += recvChar;
  if(recvBuf.indexOf("CONNECT:OK") != -1){
          connectOK = 1;
    Serial.println("Connected!");
    blueToothSerial.print("Connected!");
    break;
  }else if(recvBuf.indexOf("CONNECT:FAIL") != -1){
    Serial.println("Connect again!");
    break;
  }
      }
    }
  }while(0 == connectOK);
}