In the previous article, issues with and how to connect a Windows 10 system to an Arduino device using the Classic (Generic) Bluetooth Serial (SPP) connectivity  was covered in detail. This is so that a UWP app running on a Windows 10 device can act as the UI for an app running on a remote device such as an Arduino or IOT-Core device. The UI a dynamically generated array of buttons with multiline text that when pressed send a code representing the button to the remote app for interpretation. There is also a list box in the UI that can be used to display messages from the remote app. The UI specified in Json is passed via the serial medium from the remote app to the Windows system and used to generate the app’s UI dynamically in XAMl at runtime. This article covers the code to receive the json text from the remote device and how to interpret it within the  UWP app as well as how to pass the button code and get the remote app’s text messages.

 

Links

Index of Articles

Next: Bluetooth Connectivity-UWP Software 3/4

Prev: Bluetooth Connectivity-Connectivity 1/4

Code Repository: (Github) djaus2/SurfPad

     

The Arduino Remote App

Whilst this app was initially developed using the Arduino IDE, the decision was made to install the Arduino addin available for Visual Studio 2017 which besides supporting debugging, also enables its Sketch to be included the SufPad project and thus to be maintained in the GitHub repository., I tried to use a much earlier version of this plugin without success. I did though find that the current version works well. 

 

Visual Micro

The addin is from Visual Micro: arduino for visual studio.

  • Visual Micro is an extension to Visual Studio.
  • Installation requires C++ options to be installed with VS.
  • Run Visual Studio 2017.
  • Go to Extensions and Updates and search Online for arduino
  • Download Arduino for Visual Studio
  • Close Visual Studio
  • Install Visual Micro by doubleclicking on the "vsix" icon of the downloaded file.

Install the Arduino IDE (see link above) if not already installed *.

  • Note the installation location which is typically C:\Program Files (x86)\Arduino
  • Run Visual Studio 2017 and you will get the following dialog  (*This dialog also gives you an option to first install the Arduino Ide)

 

  • image
  • Enter the location of the Arduino IDE, typically as above. Leave the other two fields blank.

 

  • To create a new Sketch (Arduino project) File>New Project> Visual C++>Visual Micro->Arduino Project

image

 

And the created project .ino file is as to be expected:

image

Plug the the Arduino board to the development desktop.

On the Arduino Toolbar select the Arduino board and the USB COM port that the Arduino device is connect to the desktop via.

image

 

To test build press image on the toolbar

To build, download and run the app press image on the toolbar

When you first run a sketch you get an option to trial or subscribe.

When the Sketch is running press image to  disconnect. But of course the sketch will continue to run on the device.

These buttons are located on the left of the toolbar:

image

Note that a Serial Terminal appears as normal used for Arduino Serial.print() commands

 

As per normal VS debugging you can set Breakpoints, traces etc for debugging.

image

  • You can have breakpoints
  • You can have conditional breakpoints
  • You can disable breakpoints
  • You can set the action at a breakpoint
        - Log a message to the Output Window
  •    - Continue execution immediately, or not

Logging a message is useful as if you stop at the breakpoint the current message (eg a loop counter) shows in the Expression window.
These messages also get written to the Output-Debug window so if continued execution is chosen you see a trace in that window.

To display an expression/variable/property value you encase it in braces in the Log Message string eg Loop No: {i}

More: See Debugging in  http://www.visualmicro.com/page/User-Guide.aspx?doc=index:

 

The Arduino Sketch

The State Diagram:

image
The Sketch State Diagram
	// Read BT Serial char by char
	// At start read messages to sync
	// Expect '0'
	// Send '1' as ack
	// Expect '2'
	// Send '3' as ack
	// Expect '4'
	// Send '5 as ack
	// If char = '!' 
	//   Send back '/' as ack ready to send json
	//   Expect '/'
	//   Send first json string (Config)
	//   Expect '~'
	//   Send back single line json string (MainMenu)
	// Default: Just echo the character back
Received character processing a pseudo code

 

Outline of Code:

#include <SoftwareSerial.h>
SoftwareSerial bt(2, 3); // RX, TX Pins
char thisByte;
enum Mode { ACK0, ACK1, ACK2,ACK4, Running, GetJson, GetString };
Mode mode = ACK0;

void setup() {
    bt.begin(9600);
    Serial.begin(9600);
    Serial.print("Started");
    mode = ACK0;
}

void loop() {
    bt.listen();
    thisByte = bt.read();
 if (thisByte != -1)
    { //Each char is interpreted as byte representing a keypress
        //The byte is the id of button pressed + ' ' (so are printable switch (thisByte)
        {
        }
    }
}

 

The loop( ) Code:

void loop() {
    bt.listen();
    thisByte = bt.read();
    if (thisByte != -1)
    {
        //Each char is interpreted as byte representing a keypress
        //The byte is the id of button pressed + ' ' (so are printable
        switch (thisByte)
        {
        case '0':
            bt.print('1');
            mode = ACK1;
            break;
        case '2':
            bt.print('3');
            mode = ACK2;
            break;
        case '4':
            bt.print('5');
            mode = ACK4;
            break;
        case '!': //Get Exclamation mark as indicator of request for Json
            bt.print('/'); //Send back / meaning json text will follow
            mode = GetJson;
            break;
        default:
            if (mode==Running)
                bt.print(thisByte);
            else if (mode = GetJson)
            {
                switch (thisByte)
                {
//Send Jason strings

} }
break; } } }

 

Send Json strings within default case:

      default:
            if (mode==Running)
                 bt.print(thisByte);
            else if (mode = GetJson)
            {
                switch (thisByte)
                {
                    case '/':  //App sends this back
                        bt.print("{\"Config\":   }~");
                        break;
                    case '~':  //Then when it gets above then sends this back as confirmation
                               //bt.print("Hello World~");
                        bt.print("{\"MainMenu\": [ \"Setup BT\", \"Setup Serial\", \"Redo\", \"Show full list\", \"The quick brown fox jumps over the lazy dog\" ],[ \"First\", \"Back\", \"Next\", \"Last\", \"Show All\" ]  }~");
                        mode = Running;
                        break;
                    default:
                        mode = Running;
                }
            }
            break;

 

Note that the Json strings are a single line. They are wrapped here for display purposes.

 

The Json strings are concatenated in the C# code into one string, as an array of two objects.

 

This Sketch code can be sourced form the project on GitHub at: https://github.com/djaus2/SurfPad


Next: The UWP C# BT Serial Code Coming)
Also (later) Windows 10 IOT-Core version of this.