I have added a HC-05 bluetooth module to my "mega" polargraph and have a couple of experiences that will definately benefit anyone else who is attempting to undertake the same. The good thing is that now my laptop has a BT connection to the machine, and the module handles all the messing about for me.
First the basics: HC-05 (and most bluetooth modules) are 3.3v. You will need to work out how to supply voltage to it, and also convert the logic levels too. You can buy an adapter to do this for you, but I had a logic level converter lying around. I opted to create a zener voltage regulator rather than muck about trying to access the 3.3v line that was blocked by the adafruit motorshield.
Step 1: Solder a tail on TX, RX, Gnd, and 3.3v obviously. Also, put one on the Key (PIO11 / Pin 34), and if you want, you can put one on the LED status (PIO9 / Pin 32 will give you pairing status, PIO8 / Pin 31 will give you module status). Use a 470ohm resistor for the LED.
Step 2: Connect Gnd, 3.3v, TX and RX (remember TX on BT goes to RX on Arduino and vice versa) to RX1 and TX1. Connect the Key to a 3.3v source - this will bring the module into AT command mode.
Step 3: Configure the BT module. We want to do the following: Set a nice name, set the baud rate to 57600, and ensure the device is in slave mode. Upload the following program to your arduino:
void setup() {
// initialize both serial ports:
Serial.begin(9600);
Serial1.begin(38400); //AT mode is always 38400 baud
}
void loop() {
int inByte = 0;
if (Serial1.available()) {
inByte = Serial1.read();
Serial.write(inByte);
}
if (Serial.available()) {
inByte = Serial.read();
if( inByte == 65 )
Serial1.write( "AT\r\n" );
if( inByte == 66 )
Serial1.write( "AT+NAME=Polargraph1\r\n" ); // Change to whatever you want, within reason.
if( inByte == 67 )
Serial1.write( "AT+ROLE=0\r\n" );
if( inByte == 68 )
Serial1.write( "AT+UART=57600,1,0\r\n" );
}
delay(50);
}
Execute and open a terminal session on your USB Serial port with baud 9600. Use the following capital keys: A = check AT mode (ensure the device reponds OK). B = Set a nice name on your BT module. C = Set Slave mode. D = Set baud to 57600 (N81).
You should get an OK after running each command. When satisfied, power down. Ensure you disconnect the Key from 3.3v and you can leave it to float, or ground it.
Step 4a: You can move the RX1 and TX1 pins to your arduino's RX and TX. It should just work at this point. However I was lazy to access those pins under my motorshield, so I chose to leave on RX1 and TX1 and modify the Polargraph firmware instead.
Step 4b: To modify the firmware, you can either replace every Serial.xxxx with Serial1.xxxx and USB will be replaced. Or, you could replicate output to both Serial.xxx and Serial1.xxx and duplicate the serial.read section to read from both Serial and Serial1. This will give you both USB and Bluetooth. I ended up writting wrapper functions and some macros to perform this for me. Don't forget to include Serial1.begin(57600); somewhere in your init code.
Here is my Serial read code:
char inString[INLENGTH+1];
int inCount = 0;
if( Serial.available() > 0 )
{
while (Serial.available() > 0)
{
char ch = Serial.read(); // get it
delay(15);
inString[inCount] = ch;
if (ch == INTERMINATOR)
{
Serial.flush();
break;
}
inCount++;
}
}
else if( Serial1.available() > 0)
{
while (Serial1.available() > 0)
{
char ch = Serial1.read(); // get it
delay(15);
inString[inCount] = ch;
if (ch == INTERMINATOR)
{
Serial1.flush();
break;
}
inCount++;
}
}
inString[inCount] = 0; // Terminate string
String inS= inString;
Step 5: You must replace the rxtxSerial.dll file included with the Polargraph with an updated version. There is a bug in it that has been hanging around in Processing for a long time. The good news is that you can just drop in the replacement. Detailes here: http://code.google.com/p/processing/issues/detail?id=1336
You can also replace the version included in the Arduino IDE with the same update. The original library will crash the HC-05 module (as well as most other Bluetooth modules).
Step 6: Pair the module to your PC. The pairing code is 1234 (You can use an AT command to change the password). You will get 2 serial ports, but the one you want is the one with the lowest number (i.e. the outgoing port).
You should now be able to test with the polargraph controller, or with a putty session.
|