Using a Cheap Bluetooth Shield on the Netduino

I found this $19 Bluetooth Shield on DX.com from Elec Freaks.  They claim it is Arduino compatible but the Wiki and spec sheet both say 3.3v only, in bold,  which means it doesn't work with a standard 5V Arduino. Netduino boards use Arduino shields and have 3.3V signal levels.

The shield comes with an HC-05 Bluetooth module which can act as either master or slave. You can set the mode by sending it AT style commands after configuring the two switches to set it to command mode.   It is a little simpler than the "you have x seconds from bootup to configure" that some other devices have. My board arrived in Slave Mode which works with a PC acting as Master.

The module has an on-board voltage regulator and a set of daughter card connectors to add additional shields. They used offset top and bottom connectors rather than a single pass through so the card extends off one side.  I'd probably stack this board on top of any other shields I was using.



A basic Netduino comes with two hardware COM ports.  The COM port attached to the USB connector is used for programming and debugging, leaving one available for any purpose.  That means you can't power your Netduino and communicate with it in your program "out of the box" unless you give up debug capability and console output.  The Netduino team does support swapping the two COM ports but it seems like a hassle on the forums. It would be nicer if they supported multiple channels on the USB connector like the TI Launchpad does.  My particular project is unaffected because I intend to power the device over the DC jack and communicate with it over Bluetooth. The USB port will only be used for development.
  • COM1 D0/D1 connected to the USB connector and used for debugging and programming.  Using this in a program means you have no way to control or debug the device.
  • COM2 D2/D3 usually available for an FTDI or other serial breakout board. I jumper'd the Bluetooth shield to COM2.
The unit was simple to pair with my Dell laptop using the pin 1234 as specified on the Wiki.  The Windows 7 Bluetooth stack installed two virtual COM ports, one Outgoing and one Incoming.  I was able to use putty.exe, under windows, to communicate with the device using the Outgoing port.  The Wiki says the Bluetooth shield defaults to 38400.  Mine defaulted to 9600 bps.

I lightly modified a Netduino program provided in this blog that shows you how to do serial communication with a Netduino.  My changes were mainly to use the 2nd COM port and to flash the LED to show that the program is running.

The program sets up communication on COM2 (D2/D3) at 9600 bits per second. It then prints out a welcome message and echos anything you send to the Bluetooth module from your computer over the virtual serial port.

using System;
using System.Text;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.Netduino;
using System.IO.Ports;

namespace Netduino_COM2_Echo
{
    public class Program
    {
        static SerialPort serial;
 
        public static void Main()
        {
            string greeting = "Everything you type in this window will be echoed\n\r";
            byte[] greetingBytes = Encoding.UTF8.GetBytes(greeting);
            // initialize the serial port for COM2 (using D2 & D3)
            serial = new SerialPort(SerialPorts.COM2, 9600, Parity.None, 8, StopBits.One);
            // open the serial-port, so we can send & receive data
            serial.Open();
            // add an event-handler for handling incoming data
            serial.DataReceived += new SerialDataReceivedEventHandler(serial_DataReceived);

            // Wait a little bet to let the bluetooth connection setup 
            // -- ok this magic worked so I'm keeping it!
            Thread.Sleep(250);
            serial.Write(greetingBytes, 0, greetingBytes.Length);

            // we can toggle the LED including Sleep() because we use a multi threaded event handler
            OutputPort led = new OutputPort(Pins.ONBOARD_LED, false);
            while (true)
            {
                led.Write(true);
                Thread.Sleep(250);
                led.Write(false);
                Thread.Sleep(250);
            }

            // original example code said wait forever...
            // Thread.Sleep(Timeout.Infinite);
        }
 
        static void serial_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            // create a single byte array
            byte[] bytes = new byte[1];
 
            // as long as there is data waiting to be read
            while (serial.BytesToRead > 0)
            {
                // read a single byte
                serial.Read(bytes, 0, bytes.Length);
                // send the same byte back
                serial.Write(bytes, 0, bytes.Length);
            }
        } 

    }
}

Thanks for reading...

Comments

Popular posts from this blog

Understanding your WSL2 RAM and swap - Changing the default 50%-25%

Installing the RNDIS driver on Windows 11 to use USB Raspberry Pi as network attached

DNS for Azure Point to Site (P2S) VPN - getting the internal IPs