Introduction
Few days back, the Windows IOT challenge dropped in my twitter feed. While the challenge was very interesting, after poking around the web, I quickly realized that there is no Windows IoT Core for Arduino. Hopefully, sometime soon, Microsoft and Arduino will give us one.
Until then we will have to make do with Windows Remote Arduino which is essentially a Firmata client library wrapped in a Windows Runtime Component. I had both an Arduino Uno and an Arduino Yun and i wanted to check out the Windows Remote Arduino interfacing.
So when I started looking around i noticed that while there are lot of articles on using the Arduino Uno and Bluetooth there is not much starter material on using the Arduino Yun over the Wifi. And since i didn't want to buy a Bluetooth or Ethernet shields, i set out out to make it work using Arduino Yun WiFi.
Arduino Yun Serial Port Primer
I am assuming you are familiar with your Arduino Yun. If not, please refer to this.
Basically its comprised of two sections: The ATMega32u4 Arduino environment and the Linino AR9331 Linux (OpenWrt) environment that interfaces the WiFi.
These two are connected through hardware serial. Accessed through Serial1 when writing Arduino Sketch for Yun and available on the Linux environment as "/dev/ttyATH0"
There is also the USB programming chip that we normally use to connect the Arduino to the PC. Accessed through "Serial" when writing Arduino code and available on the Windows as COM port.
To enable Windows Remote Arduino to control the Yun over WiFi, we need to do the following.
1. Expose the Yun Serial Port (/dev/ttyATH0) over TCP
2. Load modified StandardFirmata.ino to Yun that uses Serial1.
3. Connect to Yun using NetworkSerial class in WRA library
Expose the Yun Serial Port over TCP
If you were familiar with connecting to Yun over Putty go to step 4.
1. Install WinSCP and Putty. Both are available here. You will need this to access the Linux terminal on Yun from your Windows 10
2. Optional: Expand the disc space on Yun if you have not already done so. (I would recommend expanding)
3. Run Putty and connect to the Yun using the IP address of the Yun using SSH. (You can see the IP Address on "Tools> Ports" in the Arduino IDE under Network ports)
4. Update OPKG package manager and install "ser2net". Make sure update happens first as this will allow the package manager to load the list of available packages in to Yun's memory. Ser2net is a Serial Port to TCP port proxy.
~# opkg update
~# opkg install ser2net
5. Configure Ser2net by editing the config file at : /etc/ser2net.conf
5055:raw:0:/dev/ttyATH0:115200
5055 is the TCP port. You can change it to whatever un-allocated port number you fancy.
raw -- represents that it should be raw data
0 -- timeout disabled
/dev/ttyATH0 -- The tty serial port on the Linux side that we are proxying over TCP
115200 -- The baud rate. 115200 is the highest baud rate that Yun could reliably work on.
6. You can always run "ser2net" from the console to make it use the config file and proxy the serial port. If you need the Ser2net is always started when the Yun is powered on, then "ser2net" should be added to /etc/rc.local
file..
Open the file and add "ser2net" line before the exit 0 line.
7. By Default Arduino Yun attaches a serial console to this port. We need to disable this serial console so that it doesn't interfere whenever Windows Remote Arduino network serial connection is made. Note that this will disable Yun Bridge Library. Who cares? We have WRA now. Any way you can bring it back whenever you want.
/etc/inittab and comment out the following line by adding "#" in front.
ttyATH0::askfirst:/bin/ash --login
With all this done, reboot the Arduino Yun. To verify if everything is good, you can enable telnet client on the Windows 10 and connect to the ip address of Yun and the port that was configured. Example :
telnet 192.168.1.192 5055
If the connection is successfully made, then the yun is now ready for next step. Close telnet.
Load modified StandardFirmata.ino to Yun
We need to make 2 modifications to ensure that the Yun can communicate over WiFi.
The following update is in the setup() method of the StandardFirmata.ino..
// to use a port other than Serial, such as Serial1 on an Arduino Leonardo or Mega,
// Call begin(baud) on the alternate serial port and pass it to Firmata to begin like this:
Serial1.begin(115200);
//this code is to delay the firmata begin process to avoid interference with the boot process of Yun Linux.
do {
while (Serial1.available() > 0) {
Serial1.read();
}
delay(1000);
} while (Serial1.available()>0);
Firmata.begin(Serial1);
// then comment out or remove lines 701 - 704 below
//Firmata.begin(57600);
// while (!Serial) {
// ; // wait for serial port to connect. Only needed for ATmega32u4-based boards (Leonardo, etc).
// }
Testing with Windows Remote Arduino Experience
2. Get the Windows Remote Arduino Experience Universal Windows App from the App Store. Launch it and choose the Connection as "Network", Baud rate of 115200, provide ip address of the yun and the port number (5055 in this sample). Click Connect. If everything is good, you should be connected and see a list of available Digital Pins. Scroll down to Pin 13. This PIN is connected on the Yun to onboard LED. (red). Make sure its on output and toggle the value. You should see the LED toggling on the arduino. Below are the screenshots and the video.
Code for connecting to the Yun over WiFi through Universal Windows App
With this setup in place, when you create a Universal Windows App, you can interface it with the Yun by using the "NetworkSerial" connection.
install-package 'windows-remote-arduino'
<Capability Name="internetClientServer" />
public MainPage()
{
this.InitializeComponent();
this.InitArduino(); //Init Arduino connection
}
Microsoft.Maker.RemoteWiring. RemoteDevice arduino;
Microsoft.Maker.Serial. NetworkSerial netWorkSerial;
public void InitArduino()
{
//Establish a network serial connection. change it to the right IP address and port
netWorkSerial = new Microsoft.Maker.Serial. NetworkSerial(new Windows.Networking.HostName ("192.168.1.192" ), 5055);
//Create Arduino Device
arduino = new Microsoft.Maker.RemoteWiring.RemoteDevice (netWorkSerial);
//Attach event handlers
netWorkSerial.ConnectionEstablished += NetWorkSerial_ConnectionEstablished;
netWorkSerial.ConnectionFailed += NetWorkSerial_ConnectionFailed;
//Begin connection
netWorkSerial.begin(115200, Microsoft.Maker.Serial.SerialConfig .SERIAL_8N1);
}
private void NetWorkSerial_ConnectionEstablished()
{
arduino.pinMode(13, Microsoft.Maker.RemoteWiring. PinMode.OUTPUT); //Set the pin to output
//turn it to High. The RED LED on Arduino Yun should light up
arduino.digitalWrite(13, Microsoft.Maker.RemoteWiring. PinState.HIGH);
}
private void NetWorkSerial_ConnectionFailed( string message)
{
System.Diagnostics. Debug.WriteLine( "Arduino Connection Failed: " + message);
}
5. Run and check.
Comments