Fez Cerberus - Comm between PC and Bluetooth module

Hi to all in the community,

I have a question concerning the use of the Bluetooth Module and the Fez Cerberus MainBoard.

I am attempting to establish communication between my PC (which uses a standard USB dongle) and the GHI Bluetooth module based on the useful tutorial provided by Marco (Controlling a Gadgeteer device using Bluetooth Module and 32feet.NET library | Integral Design)

I don’t have the Seeed oledDisplay so I commented out that part of the code. My first goal was just to get a message from the PC that a Bluetooth device was attempting to connect.

However, when I run my code I get the following error:
The type or namespace name ‘Serial’ does not exist in the namespace ‘Gadgeteer.Interfaces’ (are you missing an assembly reference?)

The error would seem to imply that I am missing one of my ‘libraries? I am currently running the GHI NETMF v4.2 and Gadgeteer Package July 23rd and I have installed the 32feet.NET [b]3.5.

I have in[/b]cluded my code below. Thank you to all for taking a look at my issue.

using Syst

em;
using System.Collections;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Media;
using Microsoft.SPOT.Touch;


using Gadgeteer.Networking;
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
using Gadgeteer.Modules.GHIElectronics;

using Gadgeteer.Modules.Velloso;


namespace GadgeteerBluetooth
{

    public partial class Program
    {
        private Bluetooth bluetooth;
        private Bluetooth.Client client;


        // This method is run when the mainboard is powered up or reset.     
        void ProgramStarted()
        {
           // oledDisplay.SimpleGraphics.Clear();

            bluetooth = new Bluetooth(2);
            client = bluetooth.ClientMode;

            bluetooth.SetDeviceName("Gadgeteer");
            bluetooth.SetPinCode("1234");
            bluetooth.DataReceived += new Bluetooth.DataReceivedHandler(bluetooth_DataReceived);
            client.EnterPairingMode();


            // Use Debug.Print to show messages in Visual Studio's "Output" window during debugging.  

            Debug.Print("Program Started");
        }

        private void bluetooth_DataReceived(Bluetooth sender, string data)
        {
            /*
            oledDisplay.SimpleGraphics.ClearNoRedraw();
            oledDisplay.SimpleGraphics.DisplayText(data, Resources.GetFont(Resources.FontResources.NinaB), GT.Color.Yellow, 0, 60);
            */
        }
    }
}



I suspect that Bluetooth module driver needs to be recompiled for 4.2. See if you can do it yourself if not then leave a comment at the original blog post for Marco to help you out.

You may not need to recompile, if you’re using the bluetooth.cs file from the codeplex site (or from my demo project http://www.tinyclr.com/codeshare/entry/446 )

In fact, it might be worth just grabbing my CS file and add it to your project - if there are any unresolved references before you start you should be able to unravel that reasonably simply.

One thing I did note was that the module takes a long time to stabilise and be ready to initiate pairing, so I had to move the client.enterpairingmode() function out of ProgramStarted and I decided to put it in a joystick button pressed handler. Once the module stopped spitting out comms after it initialised, I press the button and then I entered pairing mode (blue/red LED flashes), and then I could connect to it from my PC.

In 4.2, SerialPort was moved to Microsoft.SPOT.Hardware.SerialPort. Make sure you have that reference. I have a 4.2 program working just fine based on Michael’s example. It’s still in progress but the basic communications are working and you can check it out here:

I believe the author name is Marco.

I have looked closer at the 4.2 Gadgeteer code and it looks like the Serial class (which wraps NetMF SerialPort internally ) is completely gone from Gadgeteer 4.2.
That Serial class is used by Bluetooth.cs.

If you copy it from Codeplex as is and add it to your solution (preserve the name space), then you should be fine.

Here is the link:

http://gadgeteer.codeplex.com/SourceControl/changeset/view/21495#200003

Oops, yea Marco is the author on Michael’s blog :slight_smile:

If you use the latest version of Gadgeteer.Serial it should work fine w/o pulling anything from Codeplex.

Yeah, found it. It is moved into its own assembly.

@ naveej

That means you need to add reference for Gadgeteer.Serial.dll.

Dear Architect and ianlee74 and Brett,

Thank you for both taking so much time to share your experience and offer guidance to help me find a solution to my problem with the Bluetooth.

I must admit that the world of coding has intimidated me for some time, I’m more of a tactile person but you can only get so much satisfaction from building an artefact before you want to breathe life into it (give it a brain and a heart).

I have implemented the last suggestion by Architect and it has worked. Thank you for also explaining to me why it was necessary to change the software reference and the change in the SDK that caused it.

I am currently building and debugging the code. So far I have not been able to detect the module (I have verified that my PC’s dongle is working by connecting to other devices). The information shown in the Output window is included below:

Gentlemen did any of you encounter a similar output from your system during development?

The thread ‘’ (0x2) has exited with code 0 (0x0).
Using mainboard GHI Electronics FEZCerberus version 1.1
Reader Thread
Client Mode
Enter Pairing Mode
Program Started

WORK:SLAVER

+BTSTATE:0

+STWMOD=0

+STNA=Gadgeteer

+STPIN=1234

+INQ=1

OK

OK

OK

OK

+BTSTATE:1

+BTSTATE:3

WORK:SLAVER

+BTSTATE:0

+BTSTATE:1

+BTSTATE:3

CONNECT:FAIL

+BTSTATE:1

The thread ‘’ (0x3) has exited with code 0 (0x0).
The program ‘[2] Micro Framework application: Managed’ has exited with code 0 (0x0).

@ naveej

Glad you have sorted it out. Did you see Brett’s suggestion on moving pairing code to a button press instead of trying to do it right away. See if this helps. If it does then may be put a delay and let bluetooth module initialize fully before trying to pair.

Hi Architect

I am taking a look at Brett’s code at the moment.

It was built using .Net 4.1 so I am currently building a new project using .Net 4.2 and using his approach.

There are some syntactical errors on my end which I am working through. I will let you guys know when I get her to start talking.

Thanks again

naveej

If you look at my sample app that I linked to previously you will see that it uses Brett’s pairing technique and is already running on 4.2. Any issues you run into have probably already been resolved there.

Dear Ianlee74,

My apologies for not responding earlier, my wireless was down and I am only now seeing your post.

Thank you for pointing the resource out to me, I should have reread the previous post with greater care!

I have managed to get Brett’s modified code to build and I am currently debugging (the present code is included below). From the Microsoft Visual Studio’s (MSVS) Output window I am seeing that the module has entered ‘Pairing Mode’.

Now, from what I have read (http://support.verizonwireless.com/faqs/Bluetooth/faq.html) the Pairing Mode is the state the BT device enters when it is ready to be discovered and establish a connection. However, I am unable to ‘see’ the BT module using either my PC or cellphone. Does anyone have any suggestions as to what I can try to further troubleshoot?

Output window:
Using mainboard GHI Electronics FEZCerberus version 1.1
Program Started
Reader Thread
Client Mode
Enter Pairing Mode
The thread ‘’ (0x3) has exited with code 0 (0x0).


usingSystem;
using System.Collections;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Media;
using Microsoft.SPOT.Touch;

using Gadgeteer.Networking;
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
using Gadgeteer.Modules.GHIElectronics;

using Gadgeteer.Modules.Velloso;

namespace Bluetooth_32feet_V2
{
    public partial class Program
    {
        private Bluetooth bluetooth;
        private Bluetooth.Client client;

        // This method is run when the mainboard is powered up or reset.   
        void ProgramStarted()
        {
            // Use Debug.Print to show messages in Visual Studio's "Output" window during debugging.
            Debug.Print("Program Started");

            // Joystick press event handler
            joystick.JoystickPressed += new Joystick.JoystickEventHandler(joystick_JoystickPressed);

            // set up a timer to fire every second
            GT.Timer timer = new GT.Timer(1000);
            timer.Tick += new GT.Timer.TickEventHandler(timer_Tick);
            timer.Start();

            // define a bluetooth module running at 460800 baud on socket 6
            bluetooth = new Bluetooth(6, 460800);
            // define a client mode connection to the bluetooth module
            client = bluetooth.ClientMode;

            // set up bluetooth module connection parameters
            bluetooth.SetDeviceName("FezCerberus");    // change this to whatever name you want
            bluetooth.SetPinCode("9999");           //likewise, set whatever PIN you want.

            // need a handler for state changes and data recieved.
            bluetooth.BluetoothStateChanged += new Bluetooth.BluetoothStateChangedHandler(bluetooth_BluetoothStateChanged);
            bluetooth.DataReceived += new Bluetooth.DataReceivedHandler(bluetooth_DataReceived);

            // put the device in pairing mode as part of a normal execution
            client.EnterPairingMode();

        }

        void joystick_JoystickPressed(Joystick sender, Joystick.JoystickState state)
        {
            // check to see that the bluetooth module is connected, and if so send the joystick pressed notification
            if (bluetooth.IsConnected)
            {
                client.Send("You pressed the Joystick\r\n");
            }
            // otherwise, just go into first-time pairing mode.
            else
            {
                client.EnterPairingMode();
            }
        }

        void timer_Tick(GT.Timer timer)
        {
            // check to see that the bluetooth module is connected, and if so get then send the joystick position over the connection
            if (bluetooth.IsConnected)
            {
                GTM.GHIElectronics.Joystick.Position joyPosn = joystick.GetJoystickPosition();
                client.Send("Joystick in position X=" + joyPosn.X + " Y=" + joyPosn.Y + "\r\n");
            }
        }

        private void bluetooth_DataReceived(Bluetooth sender, string data)
        {
            // For sample purposes, we'll just debug print what we get
            Debug.Print("Recieved: " + data);
        }

        void bluetooth_BluetoothStateChanged(Bluetooth sender, Bluetooth.BluetoothState btState)
        {
            // here the bluetooth module's state has changed. First, just debug print the value so we know what is happening
            Debug.Print("New state:" + btState.ToString());

            // If the state is now "connected", we can do stuff over the link.
            if (btState == Bluetooth.BluetoothState.Connected)
            {
                Debug.Print("Connected");
                Thread.Sleep(2000);      // do this to wait for BT module to connect; 2000 may be too long, but a quick trial and error seemed to show this was ok.
                // if we don't have this pause, then the BT module will take the data we send it and loop it back as input.
                client.Send("Connected to Fez\r\n");
            }
            // if the state is now "disconnected", you might need to stop other processes but for this example we'll just confirm that in the debug output window
            if (btState == Bluetooth.BluetoothState.Disconnected)
            {
                Debug.Print("Disconnected");
            }
        }

    }

}

You’re working out of order.

Make sure you’re set to WORK:SLAVE and have a BTSTATE:1 before sending +INQ=1

You’ll also need to pair your PC w/ the device. It WILL timeout and fail the first time while drivers are being installed and serial ports setup. After that connections should run smoothly as long as you keep the module in slave mode.

Try to remove EnterPairingMode from ProgramStarted. Start your application, wait few seconds and the press the joystick.

Dear Architect

Commented out the client.EnterPairingMode(); in ProgramStarted and reran the code. The outpur generated was as follows:

The thread ‘’ (0x2) has exited with code 0 (0x0).
Using mainboard GHI Electronics FEZCerberus version 1.1
Program Started
Reader Thread
Client Mode
The thread ‘’ (0x3) has exited with code 0 (0x0).

Waited for just over a minute then pressed the joystick the following addtional output was generated:

Enter Pairing Mode

Attempted to connect to device, but still unsuccessful.

Architect, do you think that I am making some fundamental error? Several of the guys on the forum have been able to get this to work, I’m really wondering what I’m doing wrong :s

Dear Skewworks,

Please forgive I am not totally versed in the area of programming. The settings which you described in your email are wrt my PC or changes that I need to implement to the syntax of my code?

Regards
naveej

@ naveej

What Skewworks is telling about is a side effect of calling EnterPairingMode too early. You have removed it from ProgramStarted which is good and should help. If you look at your bluetooth trace you should press joystick right after you see +BTSTATE:1

Can you please show all the bluetooth trace messages as well?

Another thing I’ve just noiticed is that try using 38400 baud rate instead of 460800.

I GOT BLUE AND RED BLINKING LEDS!!!

I’m connected, I can see the BT Module!

Architect the change in Baud rate did the trick!!! How did you know this would work?

Sorry for the exuberance … but man this is exciting!

Thank you everybody. Thank you Architect

I’m going to keep building on this now… Coffee, lots of coffee.

Cheers Everyone!
naveej

The thread ‘’ (0x2) has exited with code 0 (0x0).
Using mainboard GHI Electronics FEZCerberus version 1.1
Program Started
Reader Thread

WORK:SLAVER

+BTSTATE:0

+BTSTATE:1

+BTSTATE:3

Client Mode

Recieved:

OK

Recieved:
OK

OK

Recieved:
OK

OK

Recieved:
OK

WORK:SLAVER

Recieved:
WORK:SLAVER

+BTSTATE:0

New state:0
Recieved:
+BTSTATE:0

+BTSTATE:1

New state:1
Recieved:
+BTSTATE:1

+BTSTATE:3

New state:3
Recieved:
+BTSTATE:3

CONNECT:FAIL

Recieved:
CONNECT:FAIL

+BTSTATE:1

New state:1
Recieved:
+BTSTATE:1

The thread ‘’ (0x3) has exited with code 0 (0x0).
Enter Pairing Mode

Recieved:

+INQ=1

Recieved:
+INQ=1

OK

Recieved:
OK

+BTSTATE:2

New state:2
Recieved:
+BTSTATE:2

Enter Pairing Mode

Recieved:

+INQ=1

Recieved:
+INQ=1

OK

Recieved:
OK

+BTSTATE:2

New state:2
Recieved:
+BTSTATE:2

Enter Pairing Mode

Recieved:

+INQ=1

Recieved:
+INQ=1

OK

Recieved:
OK

+BTSTATE:2

New state:2
Recieved:
+BTSTATE:2