Quantcast
Channel: MSDN Blogs
Viewing all articles
Browse latest Browse all 12366

Windows 10 remote Arduino

$
0
0

In IoT world, we need access electronics platform for listening or processing some actions in real world. Arduino is a kind of popular electronics platform with open source hardware and software for making interactive projects.

Summary

Windows 10 devices, including PCs, mobile devices (such as phones) and IoT devices, can access Arduino by Windows Remote Arduino library which is an open source Windows runtime component library to allow Makers to control Arduino with following functions though a connection of USB, Bluetooth, Ethernet, Wi-Fi, etc.

  • GPIO, including digital and analog I/O, and listen events when pin values are reported or changed.
  • Send and receive data between devices over I2C.
  • Enable customized commands via Firmata SysEx.

We can use any of the WinRT languages to develop, e.g. C#, C++, JavaScript.

To add reference of the library to our project, we just need add its NuGet package. And you can also go to its GitHub site to get source code.

Windows Remote Arduino build a communication architecture like a three layers cake.

  • Top layer is surface API which is for business logic. It allows for remote control from our code.
  • Middle layer is about protocol for meaningful message encoding and decoding.
  • Bottom layer is about physical communication to exchange raw data.

Windows Remote Arduino provides a RemoteDevice class for us to access Arduino and I will introduce it in later section.

Set up Arduino device

Firstly, we need upload a Firmata for Arduino to facilitate communication as protocol layer. We will use StandardFirmata sketch because it contains some advanced behaviors, such as SPI transactions. It comes pre-packaged with the Arduino IDE. And it is also used by Windows Remote Arduino.

So we need download Arduino IDE to install in dev machine. It is named Arduino.

Connect the Arduino to dev PC via USB after installation. Here I will use a Uno like board for demo.

Open the IDE.

Please check Board and Port in Tools menu to confirm if the model and name are correct.

Navigate to File > Examples > Firmata > StandardFirmata in menu. It will open a new window with codes of StandardFirmata sketch. Go to setup function, you will find Firmate.begin(57600) code which is to connect to the device in 57600 bauds per second. You can change the baud as the value in the manual of your Arduino device.

Click or tap the Upload button which is a round button with a right arrow. The StandardFirmata sketch will deploy to the Arduino device. Then the Arduino device will run this sketch forever unless reprogrammed with a different one.

Get Arduino device information

Right click Start button at left bottom corner. Select Device Manager in the context menu. Under Ports (COM & LPT) group, find your Arduino device. Open its property window, go to Details tab. Select Hardware Ids in Property dropbox. You will get its VID (vender ID) and PID (product ID) which are used to identify it via USB.

You may need to save this information if you want to use USB to connect it to remote.

But this is not the only way to connect it. Following are some of others.

  • Bluetooth: ID or name.
  • Wi-Fi / Ethernet: IP address and port.

We also can list all devices available so that we do not need an identifier to connect the device.

Create a project in Visual Studio

Open Visual Studio 2015 Update 2 (or higher) to add a Blank App (Universal Windows) project which is used to create UWP.

You can select any of languages supported in UWP and I will use C# to introduce here.

Then install Windows-Remote-Arduino NuGet package for the project. You can execute following command to do so.

Install-Package Windows-Remote-Arduino

Or you can also use GUI by right click the project and click Manage NuGet packages in context menu to search Windows-Remote-Arduino. Then choose the one in the list to install.

Now, the project has added the references of the libraries.

Then we need add some permissions for the UWP to access hardware or network. Open Package.appxmanifest file in the project and add following code to add USB access capability.

<DeviceCapability Name="serialcommunication">
  <Device Id="any">
    <Function Type="name:serialPort"/>
  </Device>
</DeviceCapability>

And network access permission.

<Capability Name="privateNetworkClientServer"/>
<Capability Name="internetClientServer"/>

You can add following if you want to add capability for Bluetooth access.

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

Then we can begin to develop how to communicate with Arduino device from a Windows 10 device.

Connection and communication

Open MainPage.xaml.cs file and add following namespace using.

using Microsoft.Maker.RemoteWiring;
using Microsoft.Maker.Serial;

Insert following code to the constructor. This will create a serial stream instance for a USB device. The arguments in the constructor are the VID and PID of the device. We want to use this to target the Arduino device.

var usb = new USBSerial("VID_2341", "PID_0043");

For Bluetooth device, we can use following code to create the serial stream instance.

var bluetooth = new BluetoothSerial("ArduinoUno-D01");

The argument is the name or ID.

In the same way, the NetworkSerial class is used for ethernet and Wi-Fi, and DfRobotBleSerial is used for Bluetooth Low Energy (Bluetooth Smart).

Add a property to the MainPage class for RemoteDevice instance.

private RemoteDevice _arduino;

Create a new instance of the RemoteDevice class and pass the serial instance to provide a way to access surface API for Arduino. And then begin to set up the connection. So insert following code to the constructor.

_arduino = new RemoteDevice(usb);
usb.begin(57600); // Need pass baud and serial configuration settings.

The RemoteDevice class has following member methods.

  • // Reads mode or state of a specific digital pin.
    public PinMode getPinMode(byte pin_)
    public PinState digitalRead(byte pin_)
  • // Writes mode or state of a specific digital pin.
    public void pinMode(byte pin_, PinMode mode_)
    public void digitalWrite(byte pin_, PinState state_)
  • // Gets mode or state of a specific analog pin.
    public PinMode getPinMode(string analog_pin_)
    public ushort analogRead(string analog_pin_)
  • // Writes mode or state of a specific analog pin.
    public void pinMode(string analog_pin_, PinMode mode_)
    public void analogWrite(byte pin_, ushort value_)
  • // Disposes the instance.
    public void Dispose()  

And following events.

  • public event RemoteDeviceConnectionCallback DeviceReady
  • public event RemoteDeviceConnectionCallbackWithMessage DeviceConnectionFailed
  • public event RemoteDeviceConnectionCallbackWithMessage DeviceConnectionLost
  • public event DigitalPinUpdatedCallback DigitalPinUpdated
  • public event StringMessageReceivedCallback StringMessageReceived
  • public event SysexMessageReceivedCallback SysexMessageReceived
  • public event AnalogPinUpdatedCallback AnalogPinUpdated

And following properties.

  • public HardwareProfile DeviceHardwareProfile { get; }
  • public TwoWire I2c { get; }

So we can use these to access the Arduino device.

Turn on LED

In Uno, there is a green LED L on the board which is connected with digital pin 13. So we can set this pin to OUTPUT mode and HIGH state to turn on.

public void Setup()
{
    _arduino.pinMode(13, PinMode.OUTPUT);
    _arduino.digitalWrite(13, PinState.HIGH);
}

And insert following code in the constructor of the MainPage class to register this delegate to the event for device is read.

_arduino.DeviceReady += Setup;

The LED will be on when the app runs. Let's have a test.

It works as what we expect.

And you can also connect further things through a breadboard by wires in a current circuit, such as to use wires connect following one by one.

  1. Power pin 3.3V on Uno.
  2. A LED on breadboard.
  3. A resistor on breadboard.
  4. Digital pin 6 on Uno.
So we can control the LED on/off on the breadboard.

Update the Setup() method as following to update digital pin 6.

public void Setup()
{
    _arduino.pinMode(6, PinMode.OUTPUT);
    _Arduino.digitalWrite(6, PinState.HIGH);
}

Then the LED is on now.

Enjoy!


Viewing all articles
Browse latest Browse all 12366

Trending Articles