Let's Make Robots!

Programming Arduino via bluetooth

Allows me to program my Arduino via bluetooth, USB not needed any more

I finally figured out how to program my Arduino controlled "Start Here Robot" clone without leaving my chair using bluetooth. Unfortunately it still only works when the Arduino IDE is running on a Linux box.

I ordered an other $6.49 bluetooth module on ebay, this time I payed attention on getting the right type, HC-05 to be precise. See my other bluetooth post to understand why I'm emphasize this.. (http://letsmakerobots.com/node/30345) The HC-05 is well documented, i easily found several PDF's in the Internet, which described the module in great detail. 

The module is quite small, 13x27mm which is a little more than 1/2x1 inch. And it features 34 terminals around. I mounted the module on a piece veroboard with a double sided sticky foam thingy. Then soldered thin wires between the 8 terminals needed and the veroboard. Also on the veroboard i have level shifters, 3.3v regulator and a reset circuit to trigger the Arduino bootloader. All in all I have build this:

Now I put a jumper on the mode pins to enable the onboard command processor, and then I connected the transmit and receive pins to my level shifter cable described here http://letsmakerobots.com/node/29207 and  fired up putty on my linux. Initially the module communicates at 38400 baud

Connected to the port where I put the cable and typed AT. The module went berserk, spraying ERROR all over my terminal. It seems that my putty is sending carriage return when I hit enter nut the module wants a line feed as well. A little reconfigure in putty fixed this.

Ok, now that I have established communication to the command processor on the bluetooth module it's time for the initial configuration. I sent the following list of commands to the module:

AT
AT+RESET
AT+ORGL
AT+ROLE=0
AT+POLAR=1,0
AT+UART=57600,0,0
AT+INIT
AT+INQ

Some of these needs a little explanation. ORGL is a factory reset, ROLE is enter slave mode, POLAR configures pin32 to shift from high to low when link is established, UART sets the line speed to 57600 BAUD (but first when module is reset next time), INIT and INQ makes the module listen for incoming connections.

Now I reset the module and reconfigured putty to 57600 BAUD and checkedthat AT still responded with an OK. Leave the putty online, connected to the module via the serial link.

Now scan the ether for your module. On linux its hcitool scan which responds with the address of the module. My module has the address 00:11:11:28:06:25 which is the address you will need for the connection. The command sudo rfcomm bind 3 00:11:11:28:06:25 makes the linux "prepare" the connection. At this point the kernel prepares the device file /dev/rfcomm3, but no connection is established yet. Now fire up another putty and this time make a serial connection to the /dev/rcfomm3. If everything goes well your bluetooth manager will pop-up and request you a pin code. for the HC-05 it defaults to 1234. After you have entered the pin, the two puttys are connected, and I were able to send data from the one to the other.

Now, disconnect the serial cable and connect the bluetooth interface to your Arduino. Pin numbers are on the schematic. It helps if you have a program on the Arduino which slowly counts and outputs the result on the serial link. Something like this:

void setup()
{
  Serial.begin(57600);
  Serial.println("READY");
}

void loop()
{
  static int i;
  Serial.println(i++);
  delay(1000);
}

Now you should see the output of your program in the putty connected to /dev/rfcomm3. And when you close the putty and open it again it should trigger a reset, making the counter starting over.

Now its time for tweaking the Arduino IDE to program through the bluetooth link. This requires a little hack to the IDE, which I were not able to get working on the windows side, which is why this guide is considered linux only.Btw. I'm working in version 0022 of the IDE. I still don't like the new version 1 thing!

A little background info may be needed here. Then the OS, linux or windows, opens the bluetooth interface, the lower stuff in the system establishes the actual link. This is an automatic process. This also happens if the Arduino IDE opens the interface. Sounds good, but its not. Problem is that the IDE scans all serial ports on the system, and it does this by opening them. Just a fast open-close, but this is enough to trigger the automatic establish bluetooth link process, which takes a second or two. And since the scan is way faster than this it often clutters up the link.

On windows the bluetooth interface gets a serial port name, eg. COM21. and this gets detected, and smashed by the IDE. On linux the devices (thats what we call interfaces there) gets named /dev/rfcomm3, which the Arduino IDE DOES not find and hence wont smash! Perfect! Ok the IDE cannot select the port, how does I then actually program my Arduino though a port thats not visible in the IDE?? Hey its linux, it hackable!

I located the version of avrdude shipped with my Arduino IDE, it's under $ARDUINO_HOME/hardware/tools/avrdude. here I renamed the avrdude program to to avrdude-real and then put a little perl program there instead:

#!/usr/bin/perl
use strict;
use warnings;

foreach (@ARGV)
{
  s|/dev/ttyS0|/dev/rfcomm3|;
}

my @cmd = ("$0-real", @ARGV);

#exec @cmd;
system @cmd;
printf "Programming %s\n", $? ? 'FAILED' : 'SUCCESS'

Pay attention the the regexp substitute there. I replace the device /dev/ttyS0 with my bluetooth devicde. Now I simply tell my IDE to program an Arduino on /dev/ttyS0, the my wrapper program translates this to /dev/rfcomm3 and avrdude programs away.

Now what about the automatic reset triggering? Yes it woks. When avrdude opens the device, it gets blocked until the link is established, typically 1.2 second. And when link is ready, the bluetooth module lowers pin32, which through the cap triggers the reset pin on the Arduino. with 1uF, the timing seems to be just right!

Ok, the using a different port to keep the IDE from breakign everything is an ugly hack, but I have not been able to locate a better solution, and nor make it work in windows.

I see a few options here

  1. Fixing the IDE to not probing everything but taking the port to be used as a configuration parameter. Requires a JAVA hacker, which it NOT me!
  2. Using an other IDE than the official Arduino one. It have been done, but its not beginner friendly.
  3. Using Linux. Oh yes, thats the way to go anyway. ( yes, I'm a fanboy, I admit it!)

 

 

 

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Hi

I am using bluetooth device RN-41 and i want to pair my PC with it so i power   up the device and search for bluetooth device, i found and it wanted pass key, i wrote 1234(default key) and  it wont connect. Can someone pls help me i am using Windows XP and i have installed extern bluetooth usb.

I am using HC-05 bluetooth,i can able to communicate through at commands. It responding but if i put "at+init or at+cmode=0 or at+cmode=1" its not replying anything. how can make the device to discoverable by another bluetooth

 

Thanks in advance

So I tested my circuit with an HC-05 module similar to yours(I say similar because it came with a poorly designed breakout board), but it doesn't seem like my method will work for this module. The reason for this is that the status pin on this module pulses when there is not a connection and goes solid when there is. On my module it is simply the status pin is HIGH when connected and LOW when not connected so I am easily able to use it as a reset indicator. I noticed that there are multiple status pins for this module, do you happen to know if there is one that is just a high/low indicator of the connection status?

I simply don't follow you. You said

  • On my module it is simply the status pin is HIGH when connected and LOW when not connected

and

  • do you happen to know if there is one that is just a high/low indicator of the connection status

isn't it the same?

Haha, yes they are the same. What I meant was there is a high/low indicator of the connection status on the eb500 module. I was wondering if one was present on the HC-05 module?

Though I have recently realized it may be possible to use an attiny to interpret the status indicator on the HC-05. Then it would be quite simple to replicate my method using the HC-05.

Speaking of which I am planning to make a post here on how I made the eb500 into a wireless programmer once I have time. In the meantime I have been using it to program my robot for a competition. It works decently. I occasionally have problems where it does not want to upload correctly but I think this is more due to issues with the com ports then the actual programming method.

Yes, pin32. and AT+POLAR=1,0 reverses its way of working, making it suitable for use as a reset trigger.

can someone reply to my comment i need help people thanks

As your bluetooth module is different you will have to read your datasheet and work it out for yourself. To begin with you will need to change your baudrate. you will need to find a suitable pin (preferably a DTR) to reset your Arduino (via a 100nF capacitor) and you will also need to do some voltage level shifting as Mogul did.

Do you know if windows wake-up the connection when the IDE tries to access the link? What happens with my modules is that the IDE first probes really fast, which triggers a connection setup. This takes a few seconds. And before the link is ready IDE tears down the link again. At this point the bluetooth layer is locked up, and avrdude cannot do anything.

Its exactly the same on linux, but here i were able to hack my way around it.

I wonder if I can solve the problem by changing some of the woodoo parameters in my bluetooth module, maybe i can make it act slower or something.

Btw, which version of windows, and is it an embedded bluetooth interface or en external one? (USB?)

From what I can google, your eb500 works quite different than my HC-05. Even though the look the same, they seems to have different firmware.

I am using a usb bluetooth dongle to connect to my eb500. I don't use avrdude so I don't know how to solve anything with that. All I had to do is select the bluetooth module port as my serial port on the ide. When I told it to upload, the ide connects to the module without any problems. This may be due to a difference between modules. I am not overly familiar with the dynamics of bluetooth modules. I plan to test my method with a different(cheaper) module so that it will be a more viable solution to others. I may find out then that my solution is exclusive to my bluetooth module but I guess I'll just have to wait and see.