Arduino Yun Serial Port over TCP

Arduino Yun Serial Port Bridges

Introduction

This article contains instructions on how to expose the Arduino Yun Serial Port over TCP.

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. It 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.

I noticed that while there are lot of articles on interfacing with the Arduino Uno through Bluetooth, there was not much 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 by exposing the Arduino Yun Serial Port over TCP.

Arduino Yun Serial Port Primer

I am assuming you are familiar with your Arduino Yun. If not, please refer to this. Here is a diagram of the Arduino Yun Bridge with the serial ports annotated.

Arduino Yun Serial Port Bridges
Arduino Yun Serial Port Bridges

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 Arduino Yun Serial Port over TCP (/dev/ttyATH0)
  2. Load modified StandardFirmata.ino to Yun that uses Serial1.
  3. Connect to Yun using NetworkSerial class in WRA library

Expose the Arduino 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)

Connect through Putty
Connect through Putty

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</code>
~# opkg install ser2net

5. Configure Ser2net by editing the config file at : /etc/ser2net.conf
You can comment out everything in the ser2net.conf file and add this line in the end:

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.

You can edit it either by using vi editor if you are familiar with Linux or by using WinSCP to retrieve the file to windows and edit/save from there. Following screenshots are for editing the file on windows using Winscp.

Winscp Connection
Winscp Connection
Winscp ser2net.conf
Winscp ser2net.conf
Winscp ser2net.conf edit
Arduino Yun Serial Port over TCP configuration

6. You can always run “ser2net” from the console to make it use the config file and proxy the serial port. But if you need the ser2net to be always started when the Yun is powered on, then “ser2net” can be added to /etc/rc.local file.

Open the file using winscp and add “ser2net” line before the exit 0 line.

rc.local file on Arduino Yun
rc.local file on Arduino Yun

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.

For disabling serial console, edit the this file: /etc/inittab and comment out the following line by adding "#" in front.
ttyATH0::askfirst:/bin/ash --login
inittab -- disable yun console
inittab — disable yun console

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 Arduino Yun Serial Port over TCP configuration is complete.

Load modified StandardFirmata.ino to Yun

We need to make 2 modifications to ensure that the Yun can communicate over WiFi.

1. The standardfirmata.ino on Yun uses “Serial” by default. We need to change it to use the “Serial1” stream.
2. Since the Yun is comprised of two distinct sections, when the Yun is started, the sketch on the Arduino part will start and run even before linux side is fully booted. During the linux boot processes if it receives any unexpected signals over the Serial1, the linux boot process will be interrupted. (tell tale sign of this issue: no arduino yun ip address showing up on the ports list in Arduino IDE.) To avoid this, we need to ensure that our sketch waits until the process is complete and the Serial1 becomes available without any issue. You can read all about this in this Arduino forum thread.

The following change 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 when arduino linux side is restarted.
    do {
     while (Serial1.available() > 0) {
        Serial1.read();
        }
    delay(1000);
  } while (Serial1.available()>0);
  
  Firmata.begin(Serial1);
A modified StandardFirmatYun.ino is available at Github with these two changes. (This file will come in handy if you want to run nodejs scripts right on the Yun to interface with the arduino side.)
Load the sketch to Yun and we are ready to test if everything is good.

Testing with Windows Remote Arduino Experience

1. If you want to check out it truly works over WiFi, run the Arduino yun from a wall wart or battery without connecting it to the PC.

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.

Screenshots of the Windows Remote Arduino Experience app:

Windows Remote Arduino Experience App Connection screen.
Windows Remote Arduino Experience App Connection screen.

Toggle GPIO - WRA Experience app
Toggle GPIO – WRA Experience app

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.

1. Create a blank Windows Universal App project in VS 2015 ( I am assuming you have a Windows 10 with all the necessary software needed to write a Windows Universal App…Click here for more details..)
2. use Nuget package manager to add WRA to your project.

install-package 'windows-remote-arduino'

or Package manager UI.
Windows Remote Arduino Nuget Package Install
Windows Remote Arduino Nuget Package Install
3. Ensure Internet client server capability is enabled by editing the Package.appxmanifest file.

<Capability Name="internetClientServer" />

4. In the mainpage.xaml.cs add the following code for creating a NetworkSerial Connection and interacting with the Arduino GPIO using Windows Remote Arduino library.
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.

Now that my Yun is ready for Windows Remote Arduino over WiFi, I will explore more on what’s possible with the combination of Windows 10, Windows Remote Arduino and Yun.

If you would like to purchase an Arduino Yun, Check out the following links.

3 thoughts on “Arduino Yun Serial Port over TCP”

Leave a Reply

Your email address will not be published. Required fields are marked *