Netduino and Bluetooth (RN-42)
Sunday, April 24th, 2011After many a hour playing (and swearing) I think I have now managed to get the Netduino talking to a Bluetooth module in command mode. There were many teething problems along the way but I think the source code which is attached along with the notes should allow the reconfiguration/control of the Bluetooth module from the Netduino.
Objective
There were two objectives for this project, namely
- Allow a PC to send data to the Netduino over a Bluetooth device
- Provide a mechanism for the Netduino to reconfigure a Bluetooth device
This is a mainly software project as the connections are straightforward.
The Hardware
The Bluetooth module chosen was a Roving Networks RN-42. This device can be found on a breakout board on several hardware sites (Proto-Pic or Sparkfun) and provides a simple COM connection to the Netduino.
Wiring up the module was simple, only six connections to worry about 🙂
- CTS and RTS – connect these together.
- Vcc – connect to 3.3V
- GND – connect to ground (make sure this is connected to the ground on the Netduino as well)
- TX – connect to RX on the Netduino (I was using COM1 so connect to RX on the Netduino – Digital pin 1)
- RX – connect to TX on the Netduino (I was using COM1 so connect to TX on the Netduino – Digital pin 0)
There were a few teething problems along the way. The major issue was that at the time I was not able to get access to the correct documentation for the module. A little persistence and I finally had it.
So the first task was to get the module talking to my PC. Powering on the module and then getting my PC to search for Bluetooth device was simple. The PC found the device and paired without much fuss. I opened PuTTY and set the COM port parameters and I was talking to the module.
This is where it started to get interesting (i.e. frustrating) as I had missed a crucial part of the documentation. In order to enter command mode you must enter the command string within 60 seconds of the module being powered up. Once I had this figured it was a doddle. PuTTY allowed me to enter commands and displayed the responses.
So at this point I knew I had a working Bluetooth module and it could connect to the the PC in both data and command mode.
Software
The next problem was to bring the module under the control of the Netduino. This required the a few design decisions. The module can return both single and multi-line responses to commands.
The simplest problem was the single line response. The system simply looped until a line of data was returned.
For the multi-line response it was necessary to allow the system some processing time. The approach taken was to allow the user to specify a wait time. This would allow the Bluetooth module to fill the buffer with response to the command. This puts the Netduino to sleep far a while. Again, this was considered acceptable as the system would be configuring the Bluetooth module – i.e. a startup overhead rather than a run-time overhead.
Usage
The library is pretty simply to use with the constructor taking the parameters needed to create a new instance of the class and connect it to the Bluetooth module and the few remaining public methods allowing the user to talk to the module.
Constructor
The public constructor takes five parameters which essentially allow the class to configure the serial port used to communicate with the module.
public RovingNetworks(string port, BaudRate baudRate, Parity parity, int dataBits, StopBits stopBits)
EnterCommandMode
This method switches the module into command mode. It must be called within 60 seconds (if using default configuration) of the module being turned on.
ExitCommandMode
This method returns the module to data mode.
This methods has two variants. The first is used for simple commands and returns a single string which is the one line response from the module. The second is for more verbose commands where more than one line is returned from the module.
For the majority of commands only one line of test is returned and so the simple one line method can be used. This will loop until a response has been received by the system. This response is then returned to the user.
public string SendCommand(string command)
{
string result;
ClearBuffer();
_bluetoothModule.Write(Encoding.UTF8.GetBytes(command), 0, command.Length);
result = ReadLine();
while (result == "")
{
result = ReadLine();
}
return (result);
}
The second method assumes a multi-line response from the command. In order to collect these responses together the system pauses a while. It is important that this pause is long enough for the module to process the command and generate the response but not too long as this will block the thread for the specified timeout period.
public ArrayList SendCommand(string command, int timeout)
{
ArrayList result;
string line;
ClearBuffer();
result = new ArrayList();
_bluetoothModule.Write(Encoding.UTF8.GetBytes(command), 0, command.Length);
Thread.Sleep(timeout);
line = ReadLine();
while (line != "")
{
result.Add(line);
line = ReadLine();
}
return (result);
}
Example
The following small program shows the execution of a single command which returns some configuration information for the module.
RovingNetworks rn42;
rn42 = new RovingNetworks(Serial.COM1, BaudRate.Baudrate115200, Parity.None, 8, StopBits.One);
if (rn42.EnterCommandMode())
{
Debug.Print("Success");
ArrayList result = rn42.SendCommand("Drn", 500);
foreach (string str in result)
{
Debug.Print(str);
}
Thread.Sleep(Timeout.Infinite);
}
else
{
Debug.Print("Oh dear...");
}
The code is available RN 42 Bluetooth Source Code There’s not a lot of it and it’s reasonably well commented so we’ll call it a day.
Edit (25th April 2011): Implemented IDispose and updated the source code.