Arduino, C#, Hardware, Windows 8, WinRT

Bluetooth communication between Arduino and Windows 8.1

Introduction

Recently, after being inspired by this video of Arduino Bluetooth communication with Windows Phone 8 by Marcos Pereira, I got myself some new devices to play with:

  • Arduino Uno R3 in a starter kit (link)
  • JY-MCU Bluetooth module (link)
  • and a few extra cables

The thing is: I don’t have a Windows Phone 8 yet and the Windows Phone 7.8 APIs do not support this kind of Bluetooth communication.

But: I have a Surface RT and with Windows 8.1 the RFCOMM API can be used to establish a serial link to the Arduino.

Arduino and Visual Studio

It happens that my developing skills are quite limited to C# and when I had to develop some Kinect software with Processing in early 2012, I almost freaked out. Arduino code is written in C++  and the standard Arduino IDE is derived from the Processing IDE. Fortunately there’s the Visual Studio extension VisualMicro which brings a very intuitive Arduino support for VS2010-VS2013. (not available for Express versions)

You can get VisualMicro for free at http://visualmicro.codeplex.com/. They also offer a pro-version (>$20) with support for Breakpoint-Debugging.

The Arduino IDE should be installed as well and you have to tell the VisualMicro extension where to find it.

Setting up the Arduino board

For the setup I’ll show here, I set up two LEDs, a potentiometer and of course the Bluetooth module (Fig.1).

Arduino UNO R3 with 2 LEDs, a potentiometer and a Bluetooth module
Fig.1: Arduino UNO R3 with 2 LEDs, a potentiometer and a Bluetooth module

Preparing the Code

The Arduino has to be loaded with some code to send and receive Bluetooth messages. I created a Visual Studio solution with a blank Windows 8.1 app and a new Arduino sketch and added a new header file “SoftwareSerial.h” (Fig.2). The code of SoftwareSerial.h can be found here.

A blank VS 2013 solution was created to which a blank Windows Store 8.1 application and an Arduino sketch were added.
Fig.2: A blank VS 2013 solution was created to which a blank Windows Store 8.1 application and an Arduino sketch were added.

The actual Arduino code can be studied in the sample application: http://code.msdn.microsoft.com/Bluetooth-communication-7130c260

For the Windows 8.1 app we build a class, BluetoothConnectionManager to simplify enumerating, connecting, send & receive and state management. The code of the BluetoothConnectionManager and the actual application can also be studied in the sample application.

For the Windows 8.1 app it’s very important to declare the right Bluetooth capabilites. The following snippet should be included in Package.appxmanifest:

<Capabilities>
    <m2:DeviceCapability Name="bluetooth.rfcomm">
      <m2:Device Id="any">
        <m2:Function Type="name:serialPort" />
      </m2:Device>
    </m2:DeviceCapability>
  </Capabilities>

As soon as you have everything set up, the Arduino code can be deployed via DebugStart new instance and the Windows 8.1 app can be launched.

In the sample application I implemented broadcast of the potentiometer value and switching the LEDs on/off. Additionally the red LED automatically turns on/off depending on the analog value.

One problem that this implementation brings, which I did not yet solve, is that simultaneous sending of a message by either party causes the other one to crash or get an exception. But due to my lack of experience, I do not know how to lock the streams to prevent this from happening..

Anyways, have fun =)

12 comments

  1. Marcos Fábio

    Really nice work, congratulations! I’m really happy that my video inspired you :).

    Do you already have a http://www.forumnokia.com account? Nokia is going to release a tablet this year and your work would be appreciated there.

    Regards,

    1. thecake

      And thanks to you for posting this great tutorial!
      This was my first Arduino project and I’m still not that sure how some of this code works.. For example I observed that occasionally two methods can write to the stream at the same time, which results in severe errors on the other side of the connection. I tried to lock() the Stream in the Windows 8.1 App and to use a bool to get it synchronized on the Arduino, but it had no effect. Maybe you can reproduce this with your Arduino/WP8 combination? (set up periodic messages with responses, increase the frequency, then try to send other messages as well)

      I’ll be making myself an account in the Nokia developer wiki as well^^

      best wishes,
      michael

      1. Marcos Pereira

        I didn’t try this combination yet, but i’ll do it. It’s a interesting problem to investigate. A lot of fun is coming 🙂

        Right now, I’m making a small remote control car, using the ATTiny85 (http://www.atmel.com/devices/attiny85.aspx). I don’t know if you know it, but it can be seen as a small Arduino. It have VCC, GND and I/O ports.

        You can use the Arduino UNO to write code into it and the JY-MCU Works like a charm with it. In fact, i’ve just tested the 3-colors example using the ATTiny85.

        Regards,
        marcos

  2. rik

    Error 2 Error: DEP0700 : No se puede registrar la aplicación. error 0x80070003: no se puede registrar el paquete 1b2dd8e5-77f9-4ae9-8683-c4e46a492b0f_1.0.0.0_x86__eq9h3e6grdrp4 porque se detectó el siguiente error al crear el archivo de dependencias del paquete C:\Users\Ricardo\Documents\Visual Studio 2013\Projects\Bluetooth communication between Arduino and Windows 8.1\C#\BluetoothCommunicationSampleController\bin\x86\Debug\AppX\microsoft.system.package.metadata\S-1-5-21-1862554326-2498594583-4198777847-1001.pckgdep: El sistema no puede encontrar la ruta especificada.
    (0x80073cf6) BluetoothCommunicationSampleController

    1. kuchenzeit Post author

      Try to “clean” and “rebuild” (right click the project).

      1. rik

        Thanks I solved the previous error and I can see the list of devices connected via bluetooth
        but it has appeared to me a new error which is:

        “No mapping for the Unicode character exists in the target multi-byte code page.”

        The error is in this line:

        string message = reader.ReadString(actualMessageLength);

        inside the method “ListenForMessagesAsync()” from class “BluetoothConnectionManager.cs”

        Any thought?

        Regards

        1. kuchenzeit Post author

          This looks like some kind of communication error. You can either try/catch errors and debug it or do your own code because that’s where incoming messages arrive.

          1. rik

            Hi, In fact is my own code based in yours. The only diferrence between your code and mine is that I launch the arduino code from the arduino ide.

            It will be great if you could look into my code.

            I send you two links to download my code the first one with the code of visual studio and the second one with the code of arduino.

            https://mega.co.nz/#!bdMFjLiD!OUmm7b3WLRx8kChWoRJV6HLu-SJ8JqlgwvvK8PkbwzI

            https://mega.co.nz/#!2N9E1JyJ!–gTNxRAjPXvEC_qHhNFBx81CrkwTFBdDwDyaF_rw7Y

            Regards

          2. kuchenzeit Post author

            Then you can try to use simple code on the Arduino and just btSerial.println(“myteststring”)
            On the receiving side you can then use breakpoints and look at the values directly. I did this stuff only once and it is sometimes very confusing..

      2. rik

        Thanks I solved the issue. The problem were the bluetooth device to solved I reset to default settings the device.

  3. hung.msp

    are you sure that the pin TX and RX of the bluetooth module cnnected to to the right pins of the board, because pin 10 and 11 of arduino board is PWM, and they cannot transmit or receive the signal.

    1. kuchenzeit Post author

      yes, that’s the nice thing about SoftwareSerial. Maybe you can program the Arduino to send a simple string every 10 seconds and turn on the LED of pin 13 at the same time. Than you can correlate that moment with the moment when the Debugger in Visual Studio breaks at the line with the exception.

Leave a Reply

Your email address will not be published. Required fields are marked *