Bluetooth modules

Modules, cases, buttons...

Bluetooth modules

Postby Myndale » Sun Feb 01, 2015 4:36 am

I got my hands on a few HC-05 bluetooth modules last night, so of course the first thing I've done is try them out with my favourite little handheld! :)

bt1.jpg
bt1.jpg (183.09 KiB) Viewed 7622 times


Unfortunately they don't support I2C but given that they're currently selling on Ebay for US$1 each (inc intl shipping) they're an incredibly cheap way to add wireless/internet connectivity. That link is for one with a breakout board, I bought it without and simply glued/soldered it to a 4-way female header:

bt2.jpg
bt2.jpg (158.52 KiB) Viewed 7622 times


They're very simple to use, you just create an instance of SoftwareSerial and set it to use the CLK/SDA pins on the IC2 ports. Since it's a raw serial stream you need something on the other end to communicate with, I whipped up a simple console app that sits in the background listening for incoming characters and firing off media keyboard events, thus turning Gamebuino into a wireless media controller:

bt3.jpg
bt3.jpg (154.8 KiB) Viewed 7622 times


One interesting thing I did manage to do was get the module to pair with my Android phone, which instantly opens the door to giving Gamebuino an internet connection anywhere you go! I have too many other things on my Gamebuino plate at the moment but an Android app that provides socket support would be very cool indeed.

Range is surprisingly good, I had no problems getting a straight-line signal at 30 meters (the longest open stretch in my house), it also managed to make it through 3 brick walls. Power consumption is a little high at 40mA, for comparison Gamebuino itself is usually around 10-15mA depending on what you're doing. There's probably a way to cut power consumption in AT mode but that would require the use of another pin.

Gamebuino sketch is here:

Code: Select all
#include <SPI.h>
#include <Gamebuino.h>
Gamebuino gb;

#define TX A4    // DA
#define RX A5    // CK

#include <SoftwareSerial.h>
SoftwareSerial bluetooth(RX, TX);

extern const byte font5x7[];

void setup(){
  gb.begin();
  gb.display.setFont(font5x7);
  bluetooth.begin(9600);
  pinMode(RX, INPUT);
  pinMode(TX, OUTPUT);
}

void loop(){
  if(gb.update()){
    gb.display.println(F("Commands:"));
    gb.display.print(F(" \25  Play/pause"));
    gb.display.println(F(" \26  Stop"));
    gb.display.println(F(" \27  Mute"));
    gb.display.println(F(" \36\37 Volume +-"));
    gb.display.println(F(" \21\20 Prev/next"));
   
    if (gb.buttons.pressed(BTN_A))
      bluetooth.print("A");
    if (gb.buttons.pressed(BTN_B))
      bluetooth.print("B");
    if (gb.buttons.pressed(BTN_C))
      bluetooth.print("C");
    if (gb.buttons.pressed(BTN_LEFT))
      bluetooth.print("<");
    if (gb.buttons.pressed(BTN_RIGHT))
      bluetooth.print(">");
    if (gb.buttons.pressed(BTN_UP) || gb.buttons.held(BTN_UP, 1))
      bluetooth.print("^");
    if (gb.buttons.pressed(BTN_DOWN) || gb.buttons.held(BTN_DOWN, 1))
      bluetooth.print("v");
  }
}


And here's the code for the Visual Studio console app that decodes the serial stream (it uses 32feet.net for the bluetooth support which you can find in nuget):

Code: Select all
using InTheHand.Net;
using InTheHand.Net.Sockets;
using System;
using System.Linq;
using System.Runtime.InteropServices;


namespace BluetoothTest
{
   class Program
   {
      [DllImport("user32.dll")]
      public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

      const int KEYEVENTF_EXTENDEDKEY = 0x0001; //Key down flag
      const int KEYEVENTF_KEYUP = 0x0002; //Key up flag
      const int VK_PLAY            = 0xFA;
      const int VK_MEDIA_STOP         = 0xB2;
      const int VK_VOLUME_MUTE      = 0xAD;
      const int VK_MEDIA_NEXT_TRACK   = 0xB0;
      const int VK_MEDIA_PLAY_PAUSE   = 0xB3;
      const int VK_MEDIA_PREV_TRACK   = 0xB1;
      const int VK_VOLUME_DOWN      = 0xAE;
      const int VK_VOLUME_UP         = 0xAF;
      const int MediaNext = 11;
      const int MediaPrevious = 12;

      static void Main(string[] args)
      {
         try
         {
            Console.WriteLine("Searching for HC-05...");
            var cli = new BluetoothClient();
            var device = cli.DiscoverDevices().FirstOrDefault(p => p.DeviceName=="HC-05");
            if (device==null)
               throw new Exception("Device not found!");
            Console.WriteLine("Device found! Trying to connect...");
            try {cli.Connect(new BluetoothEndPoint(device.DeviceAddress, device.InstalledServices.First()));}
            catch (Exception e) {throw new Exception("Couldn't connect: " + e.Message);}
            try
            {
               Console.WriteLine("Connected! Press any key to stop service.");
               var stream = cli.GetStream();
               while (stream.DataAvailable)
                  stream.ReadByte(); // flush pending data
               while (true)
               {
                  if (Console.KeyAvailable && Console.ReadKey(true).Key==ConsoleKey.Escape)
                     break;
                  if (stream.DataAvailable)
                  {
                     var b = (char)stream.ReadByte();
                     switch (b)
                     {
                        // play/pause
                        case 'A':
                           keybd_event(VK_MEDIA_PLAY_PAUSE, 0x45, 0, 0);
                           keybd_event(VK_MEDIA_PLAY_PAUSE, 0x45, KEYEVENTF_KEYUP, 0);
                           break;

                        // stop
                        case 'B':
                           keybd_event(VK_MEDIA_STOP, 0x45, 0, 0);
                           keybd_event(VK_MEDIA_STOP, 0x45, KEYEVENTF_KEYUP, 0);
                           break;

                        // mute
                        case 'C':
                           keybd_event(VK_VOLUME_MUTE, 0x45, KEYEVENTF_EXTENDEDKEY, 0);
                           keybd_event(VK_VOLUME_MUTE, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
                           break;

                        // prev track
                        case '<':
                           keybd_event(VK_MEDIA_PREV_TRACK, 0x45, KEYEVENTF_EXTENDEDKEY, 0);
                           keybd_event(VK_MEDIA_PREV_TRACK, 0x45, KEYEVENTF_EXTENDEDKEY|KEYEVENTF_KEYUP, 0);
                           break;

                        // next track
                        case '>':
                           keybd_event(VK_MEDIA_NEXT_TRACK, 0x45, KEYEVENTF_EXTENDEDKEY, 0);
                           keybd_event(VK_MEDIA_NEXT_TRACK, 0x45, KEYEVENTF_EXTENDEDKEY|KEYEVENTF_KEYUP, 0);
                           break;

                        // volume up
                        case '^':
                           keybd_event(VK_VOLUME_UP, 0x45, KEYEVENTF_EXTENDEDKEY, 0);
                           keybd_event(VK_VOLUME_UP, 0x45, KEYEVENTF_EXTENDEDKEY|KEYEVENTF_KEYUP, 0);
                           break;

                        // volume down
                        case 'v':
                           keybd_event(VK_VOLUME_DOWN, 0x45, 0, 0);
                           keybd_event(VK_VOLUME_DOWN, 0x45, KEYEVENTF_KEYUP, 0);
                           break;
                     }
                  }
               }
            }
            finally
            {
               if (cli.Connected)
                  cli.Close();
            }
         }
         catch (Exception e)
         {
            Console.WriteLine(e.Message);
         }
      }
   }

}
Myndale
 
Posts: 507
Joined: Sat Mar 01, 2014 1:25 am

Re: Bluetooth modules

Postby erico » Sun Feb 01, 2015 5:17 am

that is genious! :)
I wonder what the connection could bring up in future.
User avatar
erico
 
Posts: 671
Joined: Thu Mar 27, 2014 9:29 pm
Location: Brazil

Re: Bluetooth modules

Postby Skyrunner65 » Sun Feb 01, 2015 4:38 pm

Hooray for crazy ideas!
My idea for Buinonet can come to fruition!
(I dunno, it could be anything internet related really)

Also, this would be a great idea for a Gamebuino Module, cutting the need for I2C cables.
(example: I have a GBA with 2 wireless play, instead of a cable, and its a LOT better)

Another idea for use (I can't do this, I don't know how to make the GB communicate with a Computer):
Joystick!
Buttons are a big problem, but it would work with Sega Master System and Atari 2600 emulators (both have like 1-2 buttons).
User avatar
Skyrunner65
 
Posts: 371
Joined: Thu Mar 20, 2014 5:37 pm
Location: NC,USA

Re: Bluetooth modules

Postby Jolly » Sun Feb 01, 2015 11:42 pm

For a second I was screaming like a little girl, because I have two of these flying around.
But then I noticed, that I have the HC-06, which only works as slave...
And the guy on ebay doesn't ship to Germany.

But now I want to have the HC-05 so bad. :D
Jolly
 
Posts: 40
Joined: Fri Jul 25, 2014 9:07 pm
Location: Germany

Re: Bluetooth modules

Postby Skyrunner65 » Mon Feb 02, 2015 12:05 am

Slave, huh?
Well, you could still SEND stuff to the Gamebuino.
User avatar
Skyrunner65
 
Posts: 371
Joined: Thu Mar 20, 2014 5:37 pm
Location: NC,USA

Re: Bluetooth modules

Postby Myndale » Mon Feb 02, 2015 6:43 am

Jolly wrote:But then I noticed, that I have the HC-06, which only works as slave...


You wouldn't be able to connect them to each other directly but you could still connect them both to a PC app that simply gateways msgs between them.

Or just fork out $1 for an HC-05 :D
Myndale
 
Posts: 507
Joined: Sat Mar 01, 2014 1:25 am

Re: Bluetooth modules

Postby Jolly » Mon Feb 02, 2015 9:19 pm

Now I'm a little confused.
Can I directly connect the HC-05 to one another one or do I still need a PC in between?
Jolly
 
Posts: 40
Joined: Fri Jul 25, 2014 9:07 pm
Location: Germany

Re: Bluetooth modules

Postby Myndale » Tue Feb 03, 2015 3:02 am

Jolly wrote:Can I directly connect the HC-05 to one another one or do I still need a PC in between?


As far as I'm aware they can be connected together but you have to configure one of them to be a slave and the other a master (they're master by default). The HC-06's only work in slave mode, hence the need for a master they can both connect to.
Myndale
 
Posts: 507
Joined: Sat Mar 01, 2014 1:25 am

Re: Bluetooth modules

Postby Jolly » Tue Feb 10, 2015 11:33 pm

I finally got the HC-05's and managed to configure one as master.
A few minutes later I could chat with myself (even if that sounds a little sad). :P

smaller.jpg
smaller.jpg (152.85 KiB) Viewed 7511 times


Code: Select all
#include <SPI.h>
#include <Gamebuino.h>
Gamebuino gb;

#define TX A5    // DA
#define RX A4    // CK

#define TEXT_LENGTH 13

#include <SoftwareSerial.h>
SoftwareSerial bluetooth(RX, TX);

extern const byte font5x7[];

char text[TEXT_LENGTH];

bool sendText = false;
String received_text = "";


void setup()
{
  gb.begin();
  gb.display.setFont(font5x7);
  bluetooth.begin(9600);
  pinMode(RX, INPUT);
  pinMode(TX, OUTPUT);
}

void loop()
{
  if(gb.update())
  {
    if(gb.buttons.pressed(BTN_B))
    {
        for(byte i=0; i < TEXT_LENGTH; i++)
        {
            text[i] = ' ';
        }
        gb.keyboard(text, TEXT_LENGTH);
        sendText = true;
    }
       
    // make sure the last char is a terminating 0
    text[TEXT_LENGTH-1] = '\0';

    gb.display.println(F("You wrote:"));
    gb.display.println(text);
    gb.display.println();

    if(sendText)
    {
        byte i=0;
        while(text[i] != '\0') i++;
        bluetooth.write(text, i);
        sendText = false;
    }
   
    while (bluetooth.available() > 0)
    {
        received_text = bluetooth.readStringUntil('\0');
    }
    gb.display.println("You received:");
    gb.display.println(received_text);
  }
 
}
Jolly
 
Posts: 40
Joined: Fri Jul 25, 2014 9:07 pm
Location: Germany

Re: Bluetooth modules

Postby rodot » Fri Feb 13, 2015 6:48 am

Haha awesome, I just ordered a pair of HC-05 :)
User avatar
rodot
Site Admin
 
Posts: 1290
Joined: Mon Nov 19, 2012 11:54 pm
Location: France

Next

Return to Hardware Development

Who is online

Users browsing this forum: No registered users and 41 guests

cron