.Net Gadgeteer; Adding Bluetooth

.Net Gadgeteer is great because there are so many modules available already. But what about hardware modules not specifically built for Gadgeteer? Well, I want to show you how easy it is to connect a Bluetooth module to .Net Gadgeteer. Here I will be using Sparkfun’s Bluesmirf modules.

First you need to connect the pins correctly like this:

Extender with Bluetooth pin mapping

Next create a class in your .Net Gadgeteer project to encapsulate the Bluetooth functionality.

using System;
using Microsoft.SPOT;

using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
using GTI = Gadgeteer.Interfaces;
using System.Threading;
using System.Text;

namespace Gadgeteer.Modules.Custom
{
    ///
 /// A bluetooth module for Microsoft .NET Gadgeteer
 ///
    public class Bluetooth : GTM.Module
    {
        public GTI.Serial serial;
        public Bluetooth(int socketNumber)
        {
            Socket socket = Socket.GetSocket(socketNumber, true, this, null);
            serial = new GTI.Serial(socket, 57600, GTI.Serial.SerialParity.None, GTI.Serial.SerialStopBits.One, 8, GTI.Serial.HardwareFlowControl.NotRequired, this);
            byte tx = 0; ;
            byte rx = 0; ;
            serial.ReadTimeout = 50;

            serial.Open();
            byte[] rx_byte = new byte[1];
            byte i = 50;

            while (serial.IsOpen)
            {

                Thread.Sleep(50);
                int read_count = 0;
                read_count = serial.Read(rx_byte, 0, 1);
                if (read_count > 0)
                {
                    char[] chars = Encoding.UTF8.GetChars(rx_byte);
                    string s = new string(chars);
                    Debug.Print(s);
                    Thread.Sleep(10);
                    serial.Write(rx_byte);
                }
            }
        }
    }
}

Now you can create an instance similar to how you would any other module except you will need to pass in the socket number you are using. Here I am using socket 8. Any of the sockets with a “u” should work.

Gadgeteer.Modules.Mine.Bluetooth dm = new GTM.Mine.Bluetooth(8);

And that is all there is to it. The class above will just echo the input and is meant to just show how to use the serial send and receive commands and how to wire up a bluetooth module. Enjoy.

Update: There is now a .Net Gadgeteer bluetooth module available. http://www.ghielectronics.com/catalog/product/312

Advertisements

4 thoughts on “.Net Gadgeteer; Adding Bluetooth

    • I’ll explain, yes, and yes. The thing about the extender is that its a “pinout” module meaning it gives you direct access to the pins. When you do this you are going off the Gadgeteer field and into regular .Net Micro.
      If you go to the GHI site and look at the schematics( http://www.ghielectronics.com/downloads/Gadgeteer/Mainboard/Spider/FEZ%20Spider%20Schematics.pdf)

      …you can see how the pins map to the hardware. Since we are using socket 8 here look for the X8 in the diagram. There you will see that pin 4 and 5 are TXD2 and RXD2 respectively. I knew from the “U” next to the socket on the Spider board that socket 8 supported UART. And the only pins with TX and RX are these two. Let me know if that helps.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s