12 Mayıs 2015 Salı

Led On-Off via bluetooth and android

In this modification, we built bluetooth connection with android mobile device. On android side, there is a android code and simple user interface. Arduino code has led on-off code and bluetooth code.
Arduino code includes "SoftwareSerial.h" library. This library is in arduino automatically. We should define as include this library at first. Bluetooth communication is done with Serial Port of the Arduino IDE adn bluetooth device.
Bluetooth device is 4.0 and mobile device must has 4.0 bluetooth for matching.

On the figure, you can see connection of the equipment :





Android code link : https://github.com/olimpias/BluetoothConnection

Arduino code :

#include <SoftwareSerial.h>  

int bluetoothTx = 11;  // TX-O pin of bluetooth mate, Arduino D2
int bluetoothRx = 10;  // RX-I pin of bluetooth mate, Arduino D3
int ledPin = 13;
SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);
int lightStatus = 'c';
void setup()
{
  Serial.begin(9600);  // Begin the serial monitor at 9600bps
  pinMode(ledPin,OUTPUT);
  // 115200 can be too fast at times for NewSoftSerial to relay the data reliably
  //Setup Bluetooth serial connection to android
  bluetooth.begin(115200);
  bluetooth.print("$$$");
  delay(100);
  bluetooth.println("U,9600,N");
  bluetooth.begin(9600);
}

void loop()
{
  if (Serial.available() > 0) {
    // read the oldest byte in the serial buffer:
    lightStatus = Serial.read();
    bluetooth.println(lightStatus);
    // if it's a capital R, reset the counter
  }
  
  if(bluetooth.available() > 0){
    lightStatus = bluetooth.read();
    if ( lightStatus == 'c'){
      digitalWrite(ledPin,LOW);
    }
    if (lightStatus == 'o'){
      digitalWrite(ledPin,HIGH);
    }
  }
  // and loop forever and ever!
}



Hiç yorum yok:

Yorum Gönder