Windows Remote Arduino for Windows 10 and Windows 10 IoT core を用いてLチカしてみた。

2016.03.01

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

はじめに

好物はインフラとフロントエンドのかじわらゆたかです。
前回のブログでこっそり書いていたWindows Remote Arduino for Windows 10 and Windows 10 IoT coreを
用いてLチカをやってみました。

Firmata

前回の記事で導入したのFirmataを用います。
Intel Edison Kit for Arduinoに対してFirmataを導入した。 | Developers.IO
WindowsでArduino IDEを用いる場合はWindows用のEdison Driverのインストールが必要です。
シリアルポートの設定もMacとWindowsでは異なります。
Windows環境でのドライバの導入・設定・Arduino IDEの設定等は以下のサイトが参考になりました。
インテル® Edison キット For Arduinoをはじめよう(windows) | Edison Lab (エジソン ラボ)

Windows Remote Arduino for Windows 10 and Windows 10 IoT core

Microsoftが公開しているUniversal Windows App(ユニバーサルアプリ)にて、Arduino(Firmata)を扱うライブラリです。
ストアアプリとして、公開されているWindows Remote Arduino Experienceでも用いられており、
Windows Remote Arduino Experienceを先にインストールすることで挙動を確認することが可能です。
また、Windows Remote Arduino ExperienceはGithubでコードも公開されているため、
実装するにあたりソースを確認することができます。
GitHub - ms-iot/remote-wiring-experience: Test application for the Remote Wiring project

Lチカ実装

ライブラリのインストール

Visual Studioでユニバーサルアプリを新規作成し、NuGetにてWindows-Remote-Arduinoと入力しライブラリを導入します。
20160215_01

Serial接続の設定

ライブラリのサンプルはBluetooth Serial接続ですが、今回は手軽にUSBSerial 接続でArduino(Firmata)側と通信を行います。
そのためのUSBSerial接続の確立を行う必要があります。
サンプルやドキュメントは見当たらなかったのですが、Windows Remote Arduino Experience内にてUSB Serial接続を実施しているので、
そのソースコードを元に実装しました。
Windows Remote Arduino Experience内ではリストを出力し、その中からユーザーの選択したSerialに対して接続を実施していますが、 今回の例ではEdison決め打ちで接続を実施しています。

private void connectArduinoEdison()
{
    var task = UsbSerial.listAvailableDevicesAsync().AsTask<DeviceInformationCollection>();
    //store the returned DeviceInformation items when the task completes
    task.ContinueWith(listTask =>
    {
        //store the result and populate the device list on the UI thread
        var action = Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, new Windows.UI.Core.DispatchedHandler(() =>
        {
            var result = listTask.Result;
            foreach (DeviceInformation device in result)
            {
                if (device.Name == "Edison")
                {
                    connection = new UsbSerial(device);
                    arduino = new RemoteDevice(connection);

                    //add a callback method (delegate) to be invoked when the device is ready, refer to the Events section for more info
                    arduino.DeviceReady += Setup;

                    //always begin your IStream
                    connection.begin(57600, SerialConfig.SERIAL_8N1);
                }
            }
        }));
    });
}
//treat this function like "setup()" in an Arduino sketch.
public void Setup()
{
    //set digital pin 13 to OUTPUT
    arduino.pinMode(13, PinMode.OUTPUT);
}

上記で接続が完了した際には、13番PinはOutputができるように設定されます。
13番Pinに対してOn/OffすることでL チカが実装できることになります。

13番PinへのOn/Off

ボタン押下時に13番Pinに対して、PinStateを変更することでLEDの状態が変更されることになります。

private void button_Click_1(object sender, RoutedEventArgs e)
{
    arduino.digitalWrite(13, PinState.HIGH);
}

private void button1_Click(object sender, RoutedEventArgs e)
{
    arduino.digitalWrite(13, PinState.LOW);
}

20160215_02

まとめ

Windows Remote Arduino for Windows 10 and Windows 10 IoT coreを用いて
Firmataを導入したArduinoに対して、Lチカを実施することができました。
FirmataをArduino側にいれることで様々な言語から容易にArduino側の値にアクセスできることがわかりました。
次の目的としては、Analogセンサーの値の読み取りや読み取った値をAWS Kinesissへの投入を試してみようかと思います。