Mostrando entradas con la etiqueta bluetooth. Mostrar todas las entradas
Mostrando entradas con la etiqueta bluetooth. Mostrar todas las entradas

My bluetooth


Bluetooth con Attiny85

Fuente: http://arduinofy.blogspot.com/2013/05/attiny-bluetooth.html


Tuorial: Attiny84 / 85 and Bluetooth 

Tuorial: Attiny84 / 85 and Bluetooth

What This Tutorial Covers

This tutorial is an expansion / continuation of the previous Bluetooth tutorial. In other words I will assume you are familiar with arduino and simple Bluetooth concepts. Also, I assume you know how to program an Attiny via arduino as an ISP or via a standalone programmer. I'm not going to be going over the code in detail unless it is something I didn't cover in the previous tutorial.

In this tutorial we will learn how to connect a Bluetooth module to an Attiny and how to make them talk! Exciting isint it? This will allow you to control a shrinkified arduino project wirelessly via Bluetooth from another arduino bluetooth enabled device, via a computer or even via a phone. In this tutorial I will cover how to control your Bluetooth enabled Attiny via a computer, but soon I will be doing a tutorial on how to accomplish this via an iPhone.


What You Need

For this tutorial you will need a few things, here is the list:
- Attiny84 or 85
- Bluetooth module
- a bread board and some wires

Identify your Bluetooth

The first thing you have to do is check your hardware and find out which pin means what. This is not difficult and either the manufacturer or the retailer which you have purchased the module from should provide you with a schematic. Here is the module I'm using the HC-05 Bluetooth Transceiver:


You can get it from here from NYPLATFORM on ebay. ($11)

Set Up

Here are two pictures of the setup:




I know its not clear so I will run through it in more detail in text. The Bluetooth module I'am using has 6 pins. I'm using only 4 of them. The TX, RX, 5V, and GROUND. This is all you need to run the Bluetooth and make it work. Some other features are excluded due to this but I will leave it up to you to discover them.

OK, first the simple stuff that you should be familiar with already. In the code notice that I initiate "int led = 4" pin 4 as "led", then I set it up for output and turn it on. We will be using this led to verify connection with our computer via Putty. In other words we will control the led via Bluetooth and make it turn on and off. As mentioned before this led is connected to Attiny pin 4, in my setup I'm using a resistor (should be 330ohm) between the Attiny and the led as I do not want to fry the led.

Now for connecting the Bluetooth with the Attiny. This is simpler that you would have ever thought, all you need is two wires or jumpers that connect the RX pin with pin 1 on the Attiny and connect the TX pn with pin 2 on the Attiny. Now make sure everything has 5V connected to it, the Bluetooth module and the Attiny.

NOTE: some Bluetooth modules could be using a different Voltage!!!

Programming

Hold on there! Before we start programming there is one thing you have to make sure of. In order for this to work properly as I found out you have to burn the 8mhz boot loader for the Attiny! Google is your friend on this one.

Now, since I have gone through most of the code in my previous tutorial I will not be detailed on this. The only part that I will make remarks about is the new code that controls the led.

/*
This code will run the bluetooth as slave
pressing 1 turns on led 4
pressing 0 turns off led 4
*/
 
#include    //Software Serial Port
#define RxD 1
#define TxD 2

#define DEBUG_ENABLED  1
 
SoftwareSerial blueToothSerial(RxD,TxD);

int led = 4;
 
void setup() 
{ 
  pinMode(RxD, INPUT);
  pinMode(TxD, OUTPUT);
  setupBlueToothConnection();
  
  pinMode(led,OUTPUT);
  digitalWrite(led,HIGH);
 
} 
 
void loop() 
{ 
  char recvChar;
  while(1){
    //check if there's any data sent from the remote bluetooth shield
    if(blueToothSerial.available()){
      recvChar = blueToothSerial.read();
      
        if(recvChar == '1')
          digitalWrite(led,HIGH);  
       
        else
          digitalWrite(led,LOW); 
    }
  }
} 
 
void setupBlueToothConnection()
{
  blueToothSerial.begin(9600); //Set BluetoothBee BaudRate to default baud rate 38400
  blueToothSerial.print("\r\n+STWMOD=0\r\n"); //set the bluetooth work in slave mode
  blueToothSerial.print("\r\n+STNA=HC-05\r\n"); //set the bluetooth name as "HC-05"
  blueToothSerial.print("\r\n+STOAUT=1\r\n"); // Permit Paired device to connect me
  blueToothSerial.print("\r\n+STAUTO=0\r\n"); // Auto-connection should be forbidden here
  
  delay(2000); // This delay is required.
  //blueToothSerial.print("\r\n+INQ=1\r\n"); //make the slave bluetooth inquirable 
  blueToothSerial.print("bluetooth connected!\n");
  
  delay(2000); // This delay is required.
  blueToothSerial.flush();
}

The only new code that you should see is the following:
void loop() 
{ 
  char recvChar;
  while(1){
    //check if there's any data sent from the remote bluetooth shield
    if(blueToothSerial.available()){
      recvChar = blueToothSerial.read();
      
        if(recvChar == '1')
          digitalWrite(led,HIGH);  
       
        else
          digitalWrite(led,LOW); 
    }
  }
} 

This is our loop. We do this 8mhz per second. Lets take a look at it and what it does. It is not very complicated and just looking at it you should be able to figure it out. Skipping to the first if statement, we are checking if there is any data that has been transmitted from the master device. Then, if there was we write it into a char. Now the char should be holding whatever value you have passed to it. In this example I choose "1" and any. "1" will turn on the led and any other will turn it off. Now in the second if statement we are comparing the char to "1" and if they match then we turn the led on. If they don't match we turn it off. So, as long as the connection is working, you should be able to turn off the led that is turned on at initialization.

Getting it All Connected

Now, power everything on. If you are using the same module as I am, you should see a red blinking led. This is good, your Bluetooth device is ready for pairing. Open up your computers Bluetooth software, find HC-05 and connect to it. This device uses a 4 digit pairing code. The default is "1234", type it in and sync them together. Open up device manager in control panel note the COM port that the Bluetooth is using, if there is more than one you will have to try them all if your unlucky. Open up putty select COM connection type in the COM# and leave the bound rate at 9600, this is what we used in the code and this is what worked best for this device.
If putty opens up a new session (black window) with the text displaying "Bluetooth Connected" you are in business. Press any key and the led should turn off, press "1" and the led should turn on. Congratulations!! You have established a bluetooth connection using an Attiny.

Comments and Questions welcome!
Thanks for reading.

Control remoto con Android y módulo bluetooth de Arduino

Los íconos los conseguí en: http://www.iconsdb.com/soylent-red-icons/ Códigos TV SONY Bravia POWER - 2704 1 - 16 2 - 2064 3 - 1040 4 - 3088 5 - 528 6 - 2574 7 - 1552 8 - 3600 9 - 272 0 - 2320 VOL+ - 1168 VOL- - 3216 CH+ - 144 CH- - 2192 MUTING - 656 JUMP - 3536 Codigo 1 - Simple para prender la TV Sony

#include 

IRsend irsend;

int codeType = -1; // The type of code
unsigned long codeValue; // The code value if not raw
unsigned int rawCodes[RAWBUF]; // The durations if raw
int codeLen; // The length of the code
int toggle = 0; // The RC5/6 toggle state

void setup()
{
  Serial.begin(9600);
}

void sendCode(int repeat) {
  if (codeType == NEC) {
    if (repeat) {
      irsend.sendNEC(REPEAT, codeLen);
      Serial.println("Sent NEC repeat");
    }
    else {
      irsend.sendNEC(codeValue, codeLen);
      Serial.print("Sent NEC ");
      Serial.println(codeValue, HEX);
    }
  }
  else if (codeType == SONY) {
    irsend.sendSony(codeValue, codeLen);
    Serial.print("Sent Sony ");
    Serial.println(codeValue, HEX);
  }
  else if (codeType == RC5 || codeType == RC6) {
    if (!repeat) {
      // Flip the toggle bit for a new button press
      toggle = 1 - toggle;
    }
    // Put the toggle bit into the code to send
    codeValue = codeValue & ~(1 << (codeLen - 1));
    codeValue = codeValue | (toggle << (codeLen - 1));
    if (codeType == RC5) {
      Serial.print("Sent RC5 ");
      Serial.println(codeValue, HEX);
      irsend.sendRC5(codeValue, codeLen);
    }
    else {
      irsend.sendRC6(codeValue, codeLen);
      Serial.print("Sent RC6 ");
      Serial.println(codeValue, HEX);
    }
  }
  else if (codeType == UNKNOWN /* i.e. raw */) {
    // Assume 38 KHz
    irsend.sendRaw(rawCodes, codeLen, 38);
    Serial.println("Sent raw");
  }
}

void loop() {
  codeType = SONY;
  codeValue = 2704;
  codeLen = 12;

  for (int i=0; i < 5; i++)
  {
    sendCode(false);
    delay(50); // Wait a bit between retransmissions
  }

  for(;;);
}

Codigo 2



Este código es el que viene de ejemplo, pero modificado para que reciba un código y lo reenvíe a luego de 8 segundos.

/*
 * IRrecord: record and play back IR signals as a minimal
 * An IR detector/demodulator must be connected to the input RECV_PIN.
 * An IR LED must be connected to the output PWM pin 3.
 * A button must be connected to the input BUTTON_PIN; this is the
 * send button.
 * A visible LED can be connected to STATUS_PIN to provide status.
 *
 * The logic is:
 * If the button is pressed, send the IR code.
 * If an IR code is received, record it.
 *
 * Version 0.11 September, 2009
 * Copyright 2009 Ken Shirriff
 * http://arcfn.com
 */

#include 

int RECV_PIN = 11;
int BUTTON_PIN = 12;
int STATUS_PIN = 13;

IRrecv irrecv(RECV_PIN);
IRsend irsend;

decode_results results;

void setup()
{
  Serial.begin(9600);
  irrecv.enableIRIn(); // Start the receiver
  pinMode(BUTTON_PIN, INPUT);
  pinMode(STATUS_PIN, OUTPUT);
}

// Storage for the recorded code
int codeType = -1; // The type of code
unsigned long codeValue; // The code value if not raw
unsigned int rawCodes[RAWBUF]; // The durations if raw
int codeLen; // The length of the code
int toggle = 0; // The RC5/6 toggle state

// Stores the code for later playback
// Most of this code is just logging
void storeCode(decode_results *results) {
  codeType = results->decode_type;
  int count = results->rawlen;
  if (codeType == UNKNOWN) {
    Serial.println("Received unknown code, saving as raw");
    codeLen = results->rawlen - 1;
    // To store raw codes:
    // Drop first value (gap)
    // Convert from ticks to microseconds
    // Tweak marks shorter, and spaces longer to cancel out IR receiver distortion
    for (int i = 1; i <= codeLen; i++) {
      if (i % 2) {
        // Mark
        rawCodes[i - 1] = results->rawbuf[i]*USECPERTICK - MARK_EXCESS;
        Serial.print(" m");
      }
      else {
        // Space
        rawCodes[i - 1] = results->rawbuf[i]*USECPERTICK + MARK_EXCESS;
        Serial.print(" s");
      }
      Serial.print(rawCodes[i - 1], DEC);
    }
    Serial.println("");
  }
  else {
    if (codeType == NEC) {
      Serial.print("Received NEC: ");
      if (results->value == REPEAT) {
        // Don't record a NEC repeat value as that's useless.
        Serial.println("repeat; ignoring.");
        return;
      }
    }
    else if (codeType == SONY) {
      Serial.print("Received SONY: ");
    }
    else if (codeType == RC5) {
      Serial.print("Received RC5: ");
    }
    else if (codeType == RC6) {
      Serial.print("Received RC6: ");
    }
    else {
      Serial.print("Unexpected codeType ");
      Serial.print(codeType, DEC);
      Serial.println("");
    }
    Serial.println(results->value, HEX);
    codeValue = results->value;
    codeLen = results->bits;
  }
}

void sendCode(int repeat) {
  if (codeType == NEC) {
    if (repeat) {
      irsend.sendNEC(REPEAT, codeLen);
      Serial.println("Sent NEC repeat");
    }
    else {
      irsend.sendNEC(codeValue, codeLen);
      Serial.print("Sent NEC ");
      Serial.println(codeValue, HEX);
    }
  }
  else if (codeType == SONY) {
    irsend.sendSony(codeValue, codeLen);
    Serial.print("Sent Sony ");
    Serial.println(codeValue, HEX);
  }
  else if (codeType == RC5 || codeType == RC6) {
    if (!repeat) {
      // Flip the toggle bit for a new button press
      toggle = 1 - toggle;
    }
    // Put the toggle bit into the code to send
    codeValue = codeValue & ~(1 << (codeLen - 1));
    codeValue = codeValue | (toggle << (codeLen - 1));
    if (codeType == RC5) {
      Serial.print("Sent RC5 ");
      Serial.println(codeValue, HEX);
      irsend.sendRC5(codeValue, codeLen);
    }
    else {
      irsend.sendRC6(codeValue, codeLen);
      Serial.print("Sent RC6 ");
      Serial.println(codeValue, HEX);
    }
  }
  else if (codeType == UNKNOWN /* i.e. raw */) {
    // Assume 38 KHz
    irsend.sendRaw(rawCodes, codeLen, 38);
    Serial.println("Sent raw");
  }
}

int lastButtonState;

void loop() {
  // If button pressed, send the code.
/*  int buttonState = digitalRead(BUTTON_PIN);
  if (lastButtonState == HIGH && buttonState == LOW) {
    Serial.println("Released");
    irrecv.enableIRIn(); // Re-enable receiver
  }
*/

  if (Serial.available())
  {
     char c = Serial.read();
   
    if (c == 's') {
      Serial.println("Pressed, sending");
      digitalWrite(STATUS_PIN, HIGH);
      sendCode(false);
      digitalWrite(STATUS_PIN, LOW);
      delay(50); // Wait a bit between retransmissions
   
      irrecv.enableIRIn(); // Re-enable receiver  
   
   
   
    }
  } else if (irrecv.decode(&results)) {
      Serial.println("recibiendo...");
   
      digitalWrite(STATUS_PIN, HIGH);
      storeCode(&results);
      irrecv.resume(); // resume receiver
      digitalWrite(STATUS_PIN, LOW);
   
      delay(8000);

for (int i=0; i<5; i++)
{
      digitalWrite(STATUS_PIN, HIGH);
      sendCode(false);
      digitalWrite(STATUS_PIN, LOW);
      delay(50); // Wait a bit between retransmissions
}

      irrecv.enableIRIn();
  }
}

Receptor bluetooth con Arduino y android

Ejemplo de door lock (con codigo C# y para Android)

http://www.instructables.com/id/Easy-Bluetooth-Enabled-Door-Lock-With-Arduino-An/



Para conocer la MAC address del dispositivo bluetooth, acceder con alguna aplicación para Android (ej, Bluetooth SPP)
Ej: 
linvor (rssi:-55dBM)
00:12:08:24:01:61
Class:1f00 Bind:Bonded


Instrucciones:

Features:
1) Wireless serial bluetooth port.
2) With free power adapter bottom board come with well power regulator.
User can connect 3.3 to 5VDC and connect TX and RX to your control IO (general 3.3 to 5V digital input output of MCU or arduino IO is ok, or general TLL IO)
3) Easy to connect this module with PC, just search and key "1234" passcode.
4) With white SMD LED on the adapter board, can see the Bluetooth connection status.
 
Step to connect:
1) Connect the wiring, power up, while the device is not connected, the bluetooth module board has a white LED flashing
2) At PC side, search bluetooth device.
3) Found name called "LINVOR" device
4) Connect it, and passcode is "1234"
5) While connection is ok, you can see the LED become always on.
6) Enjoy
 
Basically, user can only use 2 to 5 pin in most of the application
PINNAMEFUNCTION
1KEYFor configuration use (normally not connected)
2VCC3.3 to 5V input
3GNDGround
4TXDTX transmit pin
5RXDRX receive pin
6STATE Connection status
   
Industrial serial port bluetooth, Drop-in replacement for wired serial connections, transparent usage. You can use it
simply for a serial port replacement to establish connection between MCU and GPS, PC to your embedded project and etc.