Main Site Documentation

HC-06 Bluetooth module


#1

Has someone try to use a BT module (HC-06) with TinyClr ?
I have difficulties to talk to module.

using System;
using System.Diagnostics;
using System.Text;
using System.Threading;
using GHIElectronics.TinyCLR.BrainPad;
using GHIElectronics.TinyCLR.Devices.SerialCommunication;
using GHIElectronics.TinyCLR.Storage.Streams;

namespace testBTBrainpad
{
    class Program
    {
        private DataReader _reader;
        private DataWriter _writer;

        private SerialDevice ser;

        public void BrainPadSetup()
        {
            //Put your setup code here. It runs once when the BrainPad starts up.

            BrainPad.Display.DrawTextAndShowOnScreen(0, 0, "Setup BT");
            ser = SerialDevice.FromId(GHIElectronics.TinyCLR.Pins.BrainPad.Expansion.UartPort.Usart1);
            ser.BaudRate = 9600;

            ser.ErrorReceived += Ser_ErrorReceived;
            _reader = new DataReader(ser.InputStream);
            _writer = new DataWriter(ser.OutputStream);
            BrainPad.Buttons.WhenLeftButtonPressed += Buttons_WhenLeftButtonPressed;
            Thread.Sleep(1000);
            BrainPad.Display.DrawTextAndShowOnScreen(0, 0, "Running...");
        }

        private void Buttons_WhenLeftButtonPressed()
        {
            _writer.WriteString("AT");
            _writer.Store();
        }

        private void Ser_ErrorReceived(SerialDevice sender, ErrorReceivedEventArgs e)
        {
            BrainPad.Display.DrawTextAndShowOnScreen(0, 0, "Error !!!");
            Debug.WriteLine(sender + ":" + e.Error);
        }

        public void BrainPadLoop()
        {
            //Put your program code here. It runs repeatedly after the BrainPad starts up.

            if (_reader.UnconsumedBufferLength > 0)
            {
                BrainPad.Display.DrawTextAndShowOnScreen(0, 0, "Data...");
                Debug.WriteLine("Data...");
            }

            Thread.Sleep(500);
            string str = "Hello";
            byte[] b = Encoding.UTF8.GetBytes(str);
            _writer.WriteBytes(b);
            _writer.Store();
        }
    }
}

#2

I haven’t used the HC-06, but I have used an HC-05 (Not with TinyCLR, but I could run a test later this evening even though HC-05, and HC-06 are a bit different).

Do you know if the HC-06 is configured for 9600 baud? I believe that’s the default, but JIC… You may want to set this so that you know for sure.

Forgive me if you know all this already :slight_smile:


#3

You are forgiven: it’s always useful to say things again. It’s often some little mistakes (things we know but we don’t verify which cause some troubles !) :wink:

To came back to HC-06, I have find some mistakes indeed:

  • the way I try to consume data in loop,
  • the way Windows creates port: 2 ports are created, but only one is useful (I supposed wrongly, one is input and the other is output),
  • baud speed was incorrect: after some test I try other speed, and forget to come back 9600 !
  • and last but not least, Tx should connect to Rx, and Rx to Tx !!!

Now I will try HC-05, and after Grove bluetooth !