Friday, May 14, 2010

BASIC Stamp, Bluetooth, BlueSMiRF

About a year ago, I bought a BlueSMiRF Silver bluetooth modem from SparkFun Electronics so I could equip my robot, Pokey, with the ability to send telemetry data for troubleshooting.

Over the past year, the bluetooth module has been sitting on the shelf, collecting dust, and judging me harshly for not using it.  I couldn't take the disapproval any longer--and there's the firefighting competition coming up--so I decided to get the thing working.

The next few articles will detail all the baby steps to getting this going.  Along the way I'll provide a smorgasbord of information -- plus code -- for getting this working on various platforms: MCUs, PC, and Mac.

Step 1: Establish Communications With MacBook

After soldering on a 6 pin header, I plugged it into my Basic STAMP 2 prototyping board (that came with the book, 123 Robotics Experiments for the Evil Genius) and powered it up with 5V to test connectivity with my MacBook's built in bluetooth transceiver. Et voila, it worked.

BlueSMiRF Silver on BS2 protoboard

The BS2 is a great tool for rapidly testing and prototyping microcontroller projects, like this one.  Many tasks become ridiculously simple, like serial communication. But, I had no joy trying to get the BS2 to communicate with the BlueSMiRF. I decided to come back to this part later. 

Step 2: Communicate with BlueSMiRF Using PC

To talk to the BlueSMiRF, connect to it over COM1, using the BS2's serial DB9 cable and using HyperTerminal on the PC (I later found out that I like Termite a lot better). But first, one needs an RS-232 to TTL level shifter, like the one Sparkfun sells. Being cheap and impatient, I decided to make my own.  Once I had this impromptu device built and working, the PC was communicating with the BlueSMiRF.  Yay!

Eureka! The PC can talk to the BlueSMiRF!

The next trick was to use the BlueSMiRF to enable my PC to talk back and forth with my MacBook.  Using ZTerm on the Mac and HyperTerminal on the PC talking through the level shifter to the BlueSMiRF, the two could communicate. Shazam!

Step 3: Communicate Between BS2 Microcontroller and MacBook

After getting some basic bluetooth communication working, next up was coercing the BS2 to communicate with my MacBook using the BlueSMiRF Silver.  It took a few code tweaks but eventually I could use Zterm on the Mac to successfully send data to the BS2.  The key was selecting the correct communications mode (9600 bps inverted vs. non-inverted).  Here's the code:

mytext VAR Byte

loop: SERIN 14,84,[mytext]
      DEBUG mytext
      GOTO loop

Sending data from the BS2 to the MacBook went very well, too.

loop: SEROUT 14,84,["hello world",10,13]
      GOTO loop

I was having a hard time getting the BS2 to force the bluetooth modem into config mode (via escape sequence "+++"), recognize it was in that mode, configure the modem, and return to data mode. This turned out to simply be a question of sending the correct line termination (carriage return, ASCII 13 / 0x0D) at the end of AT commands.

To find out what was going on, I set up the RS-232-to-TTL level shifter as a serial sniffer. To see what the BlueSMiRF was receiving, I connected its receive (RX) pin to the level shifter/PC RX pin with a 22K resistor.  Switch the resistor to see the other side of the conversation.  To see both at once, at TTL levels, ... well, I'm not sure. I'll have to get back to you on the best way to do that.

I finally got the line termination and wait commands (to wait for specific text) worked out. Here's the code.


DEBUG "trying to enter config mode",13

'baudmode 84 is 9600,8,N,1 inverted
SEROUT 1,84,200,["+++"]

'wait for "OK" response from modem"
SERIN 0,84,[WAIT("OK",13)] 

'13 = CR = Carriage Return, \r, 0x10
DEBUG "config mode",13     

'get modem version info
SEROUT 1,84,["ATI",13]     
SERIN 0,84,[WAIT("OK",13)]
DEBUG "done",13
'server mode, channel 1
SEROUT 1,84,["AT+BTSRV=1",13]
DEBUG "server mode",13

I took the serial communications a baby step farther, programming the BS2 to receive and echo a whole string of text at once, terminated by a carriage return. I played around with timeouts, and use of the SERIN WAIT modifier to wait for specific text strings before continuing.

' {$STAMP BS2}
' {$PORT COM1}
string VAR Byte(16)

      ' stop reading at 15 chars or CR (0x10); 3sec timeout
loop: SERIN 0,84,3000,idle,[STR string\15\CR] 
      ' echo back the entire string
      SEROUT 1,84,[STR string,CR]             
      ' send string to debug window on PC
      DEBUG STR string,CR                    
      GOTO loop                             
      ' timeout means no strings waiting
idle: DEBUG "."                              
      GOTO loop

Step 4: Two-Way Communication with BS2

Prototyping two way communication was next, in anticipation of needing a query/response protocol for exchanging data and commands with the robot.  I wrote a simple AppleScript application to do line-by-line two way communications (I'll write about that in the next article). Once completed, the application successfully sent data to the BS2 over bluetooth and displayed the echo response from the BS2 on the screen.

Line by line echo from BS2

To be continued...


  1. hello there., im trying to attach BlueSMiRF to my basic Stamp II on the ARobot. could you help out how to deal with it? i have changed the baud rate but not working. is there any suggestion?

  2. What have you tried? "not working" is a very vague description of what's happening. :) Try using the contact me link in the upper right of the blog and give me a lot more detail and I will try to help.


Note: Only a member of this blog may post a comment.