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 TodayWe'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 SystemKindly go through the flow chart below for understanding the basic flow of communication happening within the system.
Need of a Mobile AppThe 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 ControlFor 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 ProgrammingUntil 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.
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.
Comments