Communication with Pinguino via Bluetooth

February 22, 2010 at 6:12 pm 3 comments

I bought a BlueSMIRF Bluetooth module a few weeks ago, and this weekend I’ve finally had some time to put it in action :-)

Connecting the BlueSMIRF module is really easy. The BlueSMIRF module has 6 pins: CTS-I, VCC, GND, TX-0, RX-1 and RTS-0. You have to connect CTS-I to RTS-0, connect VCC to the supply voltage and GND to ground. The TX-0 pin is connected to the RX pin of the microprocessor, while the RX-1 pin is connected to the TX pin of the microprocessor. On the PIC18F4455 these are pins 26 and 25 (pinguino pins 9 and 8). s

CTS-I  O----------------
VCC    O--5V           |
GND    O--0V (gnd)     |
TX-0   O-- to RX PIC   |
RX-1   O-- to TX PIC   |
RTS-0  O----------------

As explained in a previous post, I am using a Pinguino board, so the code should work on Arduino as well without needing too many modifications. This is the code I used for writing some text:

#define PIC18F4550
char incomingByte = 'K';	// for incoming serial data

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

void loop() {
	// send data only when you receive a C character
	if (Serial.available() > 0) {
		// read the incoming byte:
		incomingByte = Serial.read();
		if (incomingByte == 'C') {
			Serial.print("I received: ");
			Serial.print(incomingByte, DEC);
		}
	}
}
In the setup routine I connect to the serial port at 115200 bauds. The bluetooth module is just used as a regular serial cable, so no fancy tricks are necessary.
On my Ubuntu laptop, I had to do the following to get things running:
  • first connect your Bluetooth dongle to the BlueSMIRF (mine appeared as FireFLY-719A)
  • sudo hcitool scan #so we know the MAC address of the FireFLY
  • rfcomm connect 0 00:06:66:03:72:9A #substitute with your BlueSMIRF’s MAC address
  • sudo gtkterm -p /dev/rfcomm0 -s 115200 #start gtkterm to read/write to the serial port

Of course we don’t want to interact through gtkterm with our microcontroller. I wanted to employ Processing, but this gave some additional difficulties in my setup. In Processing, Serial.list() is used to list all available serial ports. The Bluetooth serial port /dev/rfcomm0 does not appear in the list though. A dirty hack to solve the issue is symlinking /dev/ttyS0 to /dev/rfcomm0:

sudo rm /dev/ttyS0
sudo ln -s /dev/rfcomm0 /dev/ttyS0

If anyone knows a better way to solve the issue, please mention so in the comments! This is the Processing code:

import processing.serial.*;

Serial myPort;                // Create object from Serial class

void setup()
{
  size(200, 200);
  myPort = new Serial(this, Serial.list()[0], 115200);  //the first port in the list is /dev/ttyS0 which we symlinked to /dev/rfcomm0
}

void draw()
{
  while (myPort.available() > 0) {                      //if something was received via serial port
    String inBuffer = myPort.readString();
    print(inBuffer);
  }
}

void keyPressed() {
  print(key);
  myPort.write(key);
}

void stop() {
  myPort.stop();
}
About these ads

Entry filed under: Electronics, PIC18F, Uncategorized. Tags: , , , , , , .

Synergy rocks Putting Bluetooth into a flea market toy car

3 Comments Add your own

  • 1. butters  |  July 1, 2010 at 3:20 am

    Hello,

    I have a similar RC Car project going, but I’m having trouble getting serial data on these terminal programs on Ubuntu.

    One difference is that I’m using Arduino Duemilanove but I have the same wire connection. I tried both 9600 and 115200 baud rates, but don’t see anything on serial comm via GTKTerm or Minicom.

    I enable serial via Blueman software instead of hcitool.

    Here’s the simple code I wrote:
    char val; // variable to receive data from the serial port
    int ledpin = 13; // LED connected to pin 48 (on-board LED)

    void setup() {
    pinMode(ledpin, OUTPUT); // pin 48 (on-board LED) as OUTPUT
    // Serial.begin(115200); // start serial communication at 9600bps
    Serial.begin(9600); // start serial communication at 9600bps
    }

    void loop() {
    if( Serial.available() ) // if data is available to read
    {
    digitalWrite(ledpin, HIGH); // turn ON the LED
    val = Serial.read(); // read it and store it in ‘val’
    }

    if( val == ‘H’ ) // if ‘H’ was received
    {
    Serial.print(“Print YES\n”);
    }

    else {
    Serial.print(“\rPrint no\n”);
    }
    delay(1000); // wait 100ms for next reading
    }

    Any tips?

    Thank you

    Reply
    • 2. Karel  |  August 16, 2010 at 9:59 pm

      are you also using the bluesmirf chip? Then tje baudrate is 11520b. Have you connected the tx from your chip to the rx from you bluesmirf and vice versa?
      What port do you connect to in ubuntu? /dev/rfcomm0 ?

      Reply
  • 3. Nuke  |  October 15, 2012 at 12:37 pm

    The symbolic link trick works great. Thanks. I don’t know why we can’t do like

    new Serial(this, “/dev/rfcomm0″, 115200);

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Trackback this post  |  Subscribe to the comments via RSS Feed


Feeds

Articles to be written…

Twitter – kr3l

my del.icio.us

RSS Google Reader Shared Stuff

  • An error has occurred; the feed is probably down. Try again later.

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: