Lilypad + bluetooth + C# communication problem

Hi,
I’m trying to make a communication between Arduino Lilypad and my pc through a Bluetooth connection.
To make it, I bought a Bluetooth Silver Mate from Sparkfun and connected it to the Lilypad. I made a program in C# using 32Feet.com library like this:

class Program
{
///


/// The bluetooth component used to discover devices.
///

private static BluetoothComponent component = new BluetoothComponent();

///


/// The bluetooth client.
///

private static BluetoothClient bluetoothClient = new BluetoothClient();

///


/// The stream to communicate with the device.
///

private static NetworkStream stream;

///


/// The stream writer used to communicate with the device.
///

private static StreamWriter streamWriter;

///


/// the stream reader used to receive messages from the device.
///

private static StreamReader streamReader;

///


/// The serial port.
///

private static SerialPort serialPort;

static void Main(string args)
{
try
{
var com9 = from port in SerialPort.GetPortNames() where port == “COM9” select port;
if (com9.Count() == 0)
{
Console.WriteLine(“Discovering…”);
var devices = bluetoothClient.DiscoverDevices();
Console.WriteLine(“Discovery finished.”);
devices[0].SetServiceState(BluetoothService.SerialPort, true);
}

serialPort = new SerialPort(“COM9”, 9600);
serialPort.ErrorReceived += new SerialErrorReceivedEventHandler(serialPort_ErrorReceived);
serialPort.DataReceived += new SerialDataReceivedEventHandler(sp_DataReceived);
serialPort.Open();
serialPort.WriteLine(“L”);
Thread.Sleep(1000);
serialPort.WriteLine(“H”);
Thread.Sleep(1000);
}
finally
{
serialPort.Close();
}
}

private static void serialPort_ErrorReceived(object sender, SerialErrorReceivedEventArgs e)
{
throw new NotImplementedException();
}

private static void sp_DataReceived(object sender, SerialDataReceivedEventArgs args)
{
byte data = new byte[1];
serialPort.Read(data, 0, 1);
Console.WriteLine(new string(ToCharArray(data)));
}

private static char ToCharArray(byte array)
{
char result = new char[array.Length];
for (int i = 0; i < array.Length; i++)
{
result = (char)array*;*
* }*
* return result;*
* }*
* }*
and using the following firmware for the Lilypad. It receives a data from bluetooth device and controls a led depending on the received value. If it is H the led is turned on, if L the led is turned off else the led starts blinking. The received value is also sent back to the sender.
int val; // variable to recv data from the serial port
int ledpin = 5; // LED connected to pin 5
int initialized = 0;
int H = 72;
int L = 76;
int debugEnabled = 1;
void setup() {
* pinMode(ledpin, OUTPUT); *
* Serial.begin(9600); // start serial communication at 9600bps*
}
void Blink(int value)
*{ *
* int i;*
* delay(3000);*

* if(value == 0)*
* {*
* delay(5000);*
* }*
* for(i = 0; i < value; i++)*
* {*
* digitalWrite(ledpin, 1);*
* delay(500);*
* digitalWrite(ledpin, 0);*
* delay(500);*
* }*

* delay(3000);*
}
void loop() {
* if(initialized == 0)*
* {*
* val = H; *
* initialized = 1;*
* }*
* if( Serial.available() ) // if data is available to read*
* {*
* val = Serial.read(); // read it and store it in ‘val’ *
* }*
* if(val == H) // if ‘H’ was received*
* {*
* digitalWrite(ledpin, 1); // turn ON the LED*
* }*
* else if(val == L)*
* {*
* digitalWrite(ledpin, 0); // otherwise turn it OFF*
* }*
* else*
* {*
* if(debugEnabled)*
* {*
* Blink(val);*
* }*
* }*
* Serial.println(val);*
* if(val != L && val != H)*
* {*
* val = H; // Resetting…*
* }*
* delay(100); // wait 100ms for next reading*
}
Initially the C# program seems to work: the connection to the Bluetooth Silver Mate is done correctly and data is received from the Lilypad but the received data are always 254 or 255, not depending if I send ‘L’ or ‘H’. The same ‘L’ value is received from the device like 254 or 255. Furthermore no “acknowledgement” data is received from my pc.
I also connected a LCD display to display the received value and the behavior is confirmed.
I also tried to make a Java client using BlueCove library but it seems it’s the same thing.
Can anyone help me to understand the problem?
Thank you in advance.
Matteo

Sounds to me like a wrong baud rate or parity error. Can you check that all your baud settings are similar for the bluetooth and arduino?

Hi Jeremyvnc,
thank you for your answer.
I set the baud rate on the Arduino firmware to 9600 (coherently to the C# program), is it enough? How can I check the baud rate on the Bluetooth device?

Matteo

Thank you Jeremyvnc! I solved my problem. By default, the baud rate on the Bluetooth Silver Mate is 115200 (not working on Lilypad). So, I tried to change the baud rate on Bluetooth Silver Mate (http://electronics.stackexchange.com/questions/15027/how-do-i-change-baud-rate-for-bluesmirf-without-connecting-to-pc, this is for BlueSmirf but works perfectly on Bluetooth Silver Mate too) to 9600 and all works correctly. I also tried to set the baud rate to 115200 using an Arduino Uno instead of a Lilypad with the Silver Mate connected and it all works (updating C# program and firmware).

Thank you very much!

Matteo