Project tutorial

Build a Windows App to Control Your Arduino! © GPL3+

This tutorial will cover all the details that are required to create your own app for Windows phone to connect with the Arduino.

  • 6,479 views
  • 1 comment
  • 25 respects

Components and supplies

Necessary tools and machines

Windows Phone

Apps and online services

About this project

Today we'll be discussing about a very interesting topic "Communicating an Arduino from a Windows 8 Phone". In my next tutorial, I shall be discussing how to establish communication much easily between a Windows 10 phone and an Arduino. Well, for makers using superior development boards can use the same technology to communicate it with their Edison or Galileo boards (@Intel).

Let's begin with our expedition.

Stuff We'll be Building Today

We'll design a cool mobile application (BETA-version) and see the methodology behind the communication process. The User Interface will be very simple. You can modify the source code accordingly and communicate with as many devices as you need.

Control System

Kindly go through the flow chart below for understanding the basic flow of communication happening within the system.

Need of a Mobile App

The first and foremost reason behind the development of a mobile application is the miniaturization of the interface and ease of controlling large systems by a simple hand-held device, which is inbuilt into your mobile phones!

System

  • Hardware setup
  • Software setup
  • Communication Setup

In this current tutorial we'll try to establish communication using the most economical mode, viz. via Bluetooth. Later, in my future tutorials we'll see amazing capabilities of communication via WiFi.

For this project, we shall use HC-05 module. For makers who have HC-06 module, rest assured this tutorial will be equally beneficial.

Why am I saying so?

Well, you might face certain issues while communicating with your HC-06 module. We'll be be discussing some of them and try to analyse which one is better HC-06 or HC05 at later stages of this tutorial.

Our main course of action is to establish the communication between the two devices. After establishing the connection, the story is the same. The exchange of characters serially will initiate the execution of commands.

Windows Phone App for Control

For controlling the Arduino, we need an app that will first establish the connection and then communicate serially using the Bluetooth protocol. Here I am using Windows Phone 8.0.

Before hitting the code, let's first get an idea about what we are actually up to. Windows Phone 8.0 basically can have one of the following two types of scenarios.

  • App-to-App
  • App-to-Device

In this article, we shall see the working of App-to-Device communication.

First create a new project in Visual Studio under Windows Phone apps development. Name it powerup. You can name anything though. Now as usual before going into the main code, we need to create a user interface.

Our UI will contain the following items:

  • A TextBox named deviceName.
  • A button named connect for connecting to the HC05.
  • A button named on for switching on the LED.
  • A button named off for switching off the LED.

This  basic UI version will serve the purpose. You can edit it by adding button and background colors as per your choice.

Our next course of action is to create click event handlers for our buttons since they will have some specific actions. Just double-click on the buttons and the event handlers will be automatically created. The code  for the grid control where all the controls are placed has been uploaded at the end of this article [Code for designing the UI for POWERUP application].

Since, our UI part is ready, let's move on to MainPage.xaml.cs but before navigating there, we will add the class that would simplify the entire process. Go to the Solution Explorer and right-click on your project name. Then click on add and then on add new item. Now click on class and provide the name ConnectionManager.cs. This will include the main C# code and we will use this class to simplify the communication process. The downloadable file contains the class. Also we need to add this capability to our VMAppManifest.xaml:

  • ID_CAP_PROXIMITY
  • ID_CAP_NETWORKING

Please note that the code for ConnectionManager.cs has been extracted from an article in the Nokia developer group. The website has been deactivated after Nokia's acquisition by Microsoft.

Now let us write the code required for communication using ConnectionManager.cs. As specified earlier, the scenario will be app to device. For our app to work, we need to add our HC05 to the paired device. I won't be going into the in-depth level of Bluetooth communication.Much of the information can be obtained from https://msdn.microsoft.com/en-us/library/windows/apps/jj207007(v=vs.105).aspx.

SendCommand() is a method in the CommentionManager class that is used to send the command. Similarly we will add the events for the other button as well. The code is left for you to do, although the entire project is attached here. As a reference this will be the character for the buttons.

  • On: 1
  • Off: 0

Now let's code for our connect and disconnect button. The connection will be established only and only if we have a name for the Bluetooth device. The name will be obtained from the TextBox named deviceName. In this TextBox, we will write the device name to be paired. By default it is HC06 for a HC06 device.

Now as far as the method for the event of the connect button goes, we will use a string variable to get the current text in the TextBox. After we get the text, we will use the AppToDevice method to connect to the device. The code for the AppToDevice method is separately shown below.

After we are connected, we can use the on and off button to switch on and off a LED. Let us see an overview of the entire process.

  • After creating the project, add ConnectionManager.cs
  • Design the UI
  • Add the capabilities required
  • Now create the event handlers
  • Add the code required for initializing the connection
  • Add code for the on and off button
  • Add the code for connect button.
  • Add the code for AppToDevice method

Ever thought why didn't we have a method for disconnect? The reason lies in the fact that when using the method OnNavigatedFrom we terminated the connection. Well, your Windows Phone app is ready. Now you need to test this app. But it won't work unless we apply some engineering skills to the Arduino part. So let's begin with the Arduino part.

Arduino Programming

Until now we have seen how to develop the Windows Phone app. This section will cover the hardware perspective of your Arduino project and also show the code for the Arduino written in the Arduino processor language.The Bluetooth communication can be accomplished using the hardware serial, but what will happen when it is used by another device such as a LCD driver? In that case, we need another communication point. For Arduino Mega, we have more than 1 hardware serials but we don't have that facility for Arduino Leonardo. That's the reason to try to this using the software serial of the Arduino.

The SoftwareSerial library has been developed to allow serial communication using the digital pins.

But here again we have some limitations.

  • If using multiple software serial ports, only one can receive data at a time.
  • Not all pins on the Mega and Mega 2560 support change interrupts, so only the following can be used for RX: 10, 11, 12, 13, 14, 15, 50, 51, 52, 53, A8 (62), A9 (63), A10 (64), A11 (65), A12 (66), A13 (67), A14 (68), A15 (69).
  • Not all pins on the Leonardo and Micro support change interrupts, so only the following can be used for RX: 8, 9, 10, 11, 14 (MISO), 15 (SCK), 16 (MOSI).

Let's first gather the required components:

  • Arduino Leonardo
  • LED
  • HC05 or HC06
  • Jumper wires

The circuit diagram has been attached herewith to help you with the connections. I have connected a LED that will turn on when we press the on button on the Windows Phone app. The LED would stop glowing when we press the off button. The connections for the HC06 are to be noted. The HC06 has the following 4 pins:

  • Vcc to be connected to the 3.3V pin of Arduino
  • Gnd to be connected to the Gnd pin of the Arduino
  • Rx pin to be connected to pin 11 of the Arduino
  • Tx pin to be connected to pin 10 of the Arduino
  • LED to be connected to the pin 13 and Gnd.

The question may develop, what is the difference between HC05 and HC06?

The major difference between the two is that HC05 can send as well as receive signals, that is it can operate both in master as well as in slave mode. The module HC06 doesn't have the facility for the operation in master mode. That is, it can only receive signals. By sending and receiving signals I am specifically referring to the transfer via Bluetooth, not from the device to the Arduino.

That's it. Everything is done. Now, the following are some problems that may develop.

  • The Windows Phone app crashes: There may develop certain connection problems in which the Windows Phone app may crash. The reason is mainly due to the fact the connection may appear to be establish but it has somehow got disconnected. In that case the best possible course of action is to restart the app. When developing the app, a try catch block may be useful.
  • The LED may not switch off or may not switch on: This may develop due to some faulty connections or problems in the Bluetooth module. A proper inspection to the circuit and the module may be useful.
  • Pairing problems: This is one of the most common problems. Remember the fact that you may need to add the device first into the paired list and then tap it to connect from the settings menu. Then you are required to open your app, enter the name and connect it. When pairing a pin may be asked. By default it is 0000 or 1234. You can change the name and the pin from the AT command menu.

How to Run this System

Let's see the procedure involved to run this system:

  • Switch on the Arduino.
  • Add the device to your paired list first (important).
  • Now open the app and write the name of the device and click on the connect button.
  • You may include a status TextBlock that will display connected when the connection is established. Alternatively you can enable the on and off buttons the instance when connection is established.
  • Press the on and off button to see the respective events on the Arduino.

Congratulations! Your entire Bluetooth-controlled Arduino by Windows Phone is ready.

Code

Code for designing the UI for BLUE ARDUINO applicationXML
Copy this XAML code onto your Visual Studio editor.
 <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
            <TextBlock Text="Blue Arduino" Style="{StaticResource PhoneTextNormalStyle}" Margin="12,0" FontSize="64" FontFamily="Segoe WP Black" Foreground="#FF1C23C7"/>
        </StackPanel>

        <!--ContentPanel - place additional content here-->
        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <Grid.Background>
                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                    <GradientStop Color="Black" Offset="0"/>
                    <GradientStop Color="#FF3DB8DC" Offset="1"/>
                </LinearGradientBrush>
            </Grid.Background>
            <TextBox x:Name="DeviceName" HorizontalAlignment="Left" Height="72" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="217" Margin="39,437,0,0">
                <TextBox.Background>
                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                        <GradientStop Color="#FFF8F9FB" Offset="0"/>
                        <GradientStop Color="#FFC1C1D1" Offset="1"/>
                    </LinearGradientBrush>
                </TextBox.Background>
            </TextBox>
            <Button x:Name="on" Content="ON" HorizontalAlignment="Left" Margin="116,254,0,0" VerticalAlignment="Top" Click="on_Click" Height="100" Width="101" RenderTransformOrigin="0.5,0.5" Foreground="Black" BorderBrush="White">
                <Button.Background>
                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                        <GradientStop Color="#FF3FD613" Offset="0"/>
                        <GradientStop Color="#FFF0EEF1" Offset="1"/>
                    </LinearGradientBrush>
                </Button.Background>
            </Button>
            <Button x:Name="off" Content="OFF" HorizontalAlignment="Left" Margin="246,254,0,0" VerticalAlignment="Top" Click="off_Click" Height="100" Width="100" RenderTransformOrigin="0.5,0.5" Foreground="#FF040404" BorderBrush="White">
                <Button.Background>
                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                        <GradientStop Color="#FFEC391D" Offset="0.377"/>
                        <GradientStop Color="#FFF0EEF1" Offset="1"/>
                    </LinearGradientBrush>
                </Button.Background>
            </Button>
            <Button x:Name="connect" Content="CONNECT" HorizontalAlignment="Left" Margin="261,437,0,0" VerticalAlignment="Top" Width="161" Click="connect_Click" Foreground="Black" Height="72" BorderBrush="White">
                <Button.Background>
                    <LinearGradientBrush EndPoint="0.5,1" MappingMode="RelativeToBoundingBox" StartPoint="0.5,0">
                        <GradientStop Color="#FFE4E5F1" Offset="0"/>
                        <GradientStop Color="#FFC0C2E8" Offset="1"/>
                    </LinearGradientBrush>
                </Button.Background>
            </Button>
            <TextBlock Style="{StaticResource PhoneTextNormalStyle}" Margin="10,23,2,528" FontSize="64" FontFamily="Segoe WP Black" Foreground="White">
            	<Run FontSize="17.777" Text="               Simplest Windows Bluetooth Application"/>
            </TextBlock>
        </Grid>
Code for AppToDevice method [BLUE ARDUINO]C#
No need to download/copy this code separately. Entire code has been uploaded separately in POWERUP.ZIP.
Refer to this code for reference purposes.

For any kind of assistance regarding the hardware or software setup of this project comment below.
private void connect_Click(object sender, RoutedEventArgs e)    
{    
    //Saving the status    
    s = DeviceName.Text;    
    if (DeviceName.Text == "")    
    {    
        MessageBox.Show("Enter Device Name");    
        connect.Content = "Click to Connect";    
    }    
    else    
    {    
        AppToDevice();    
    }    
}    
private async void AppToDevice()    
{    
    connect.Content = "Connecting...Please wait";    
    PeerFinder.AlternateIdentities["Bluetooth:Paired"] = "";    
    var pairedDevices = await PeerFinder.FindAllPeersAsync();    
  
    if (pairedDevices.Count == 0)    
    {    
        MessageBox.Show("Compatible devices not found.Try again");    
    }    
    else    
    {    
        foreach (var pairedDevice in pairedDevices)    
        {    
            if (pairedDevice.DisplayName == DeviceName.Text)    
            {    
                connectionManager.Connect(pairedDevice.HostName);    
                connect.Content = "Connection Successful";      
                continue;    
            }    
        }    
    }    
}    
Arduino CodeArduino
Copy this code to your ArduinoIDE
#include <SoftwareSerial.h>  
SoftwareSerial BTSerial(10, 11); // We configure PIN 10 as RX and PIN 11 as TX
void setup()  
{  
  pinMode(13,OUTPUT); //Led is connected here  
  BTSerial.begin(9600); //Begin the serial communication using bluetooth  
}  
void loop()  
{  
  char c;  
  if(BTSerial.available())  
  {  
    c=BTSerial.read();  
    if(c=='1')  // Simplest way of detecting HIGH
    {  
      digitalWrite(13,HIGH); //Set pin 13 to high "LED GLOWS" 
    }  
    else if(c=='0') // Simplest way of detecting LOW 
    {  
      digitalWrite(13,LOW); //Set pin 13 to low "LEDS STOPS GLOWING" 
    }  
   }  
  }  
}  
POWERUP App
The complete app can be downloaded and the user is granted permission to modify the application according to the needs of the project. For any kind of assistance regarding the hardware or software setup of this project comment below.

Schematics

Circuit diagram for the Bluetooth system
Connect the hardware components as per the wiring diagram shown here.
Bluetooth

Comments

Author

12804838 977761582298192 2413374391461584639 n
Aritro Mukherjee
  • 6 projects
  • 202 followers

Additional contributors

  • Developer of "connectionmanager.cs" code ,which i have used as a part of this project. by Marcos Pereira
  • For developing the basic structure of the ui by Avirup171

Published on

April 19, 2016

Members who respect this project

AritroRp logoRajib1AbhiDefaultDefaultDefaultLe me

and 17 others

See similar projects
you might like

Similar projects you might like

Control your LED with Cortana

Project tutorial by Mohamed Emam

  • 8,956 views
  • 7 comments
  • 21 respects

Control Arduino Robot Arm with Android App

Project tutorial by Slant Concepts

  • 9,013 views
  • 7 comments
  • 26 respects

Control RGB LED by Dragging – Arduino 101 & App Inventor

Project tutorial by DFRobot and CAVEDU Education

  • 4,446 views
  • 0 comments
  • 13 respects

Control Arduino Using Smartphone via USB with Blynk App

Project tutorial by Smart Technology

  • 1,528 views
  • 0 comments
  • 3 respects
Add projectSign up / Login