Testing a network connection or troubleshooting a data flow is a common job for the system admin or developer. One tool they can take advantage of: the Netcat command line program, which provides a quick way to read and write data between two devices over a TCP/IP network. Wi-Fi TestingI recently used several flavors of Netcat to help me develop a servo control program between the new Arduino Yun and a simple companion slider application, written in Processing, on my Linux notebook. As you probably know, the Yun is an Arduino Leonardo (ATmega32u4 micro-controller) bolted to a Atheros AR9331 chip, along with a Wi-Fi radio, Ethernet, a mini-USB port and 20 I/O pins. Click here to find system administrator jobs. Netcat is available for Linux, Android, Windows and Mac OS X. You may have to experiment with it a bit, because different versions sport different options. In its most basic application, Netcat takes keyboard input from one machine and displays the text on a remote machine. The remote machine can also input characters that display on the original machine's screen. You can also use Netcat to send text to an existing server. In addition, you can pipe and redirect data from other programs, scripts and files into and out of Netcat. Setting up a basic Netcat session between two machines is fairly straightforward. You'll need to know your IP address, so you can use ifconfig to display and note your address on the listening (server) notebook machine. Start Netcat with the “-l” option and port number on your Linux notebook.

linux-notebook% netcat -l 6666

In a terminal on another Linux machine, or on your Wi-Fi-connected smartphone, start Netcat (note: Netcat is called “nc” on Android version 4.1.2) with the notebook's IP address and the same port number.

rob-android% nc 6666

After hitting Return, you can type on the Android's terminal. The text will show up on the Linux notebook terminal. Likewise, typing on the Linux notebook displays in the Android terminal app.

Other Approaches

You can also use Netcat in a one-sided way. Such was the case of data-flow troubleshooting between my Arduino Yun and my Linux notebook. I had uploaded an Arduino script to the Yun to read in characters and interpret them as commands, thereby controlling two servos and an LED. The program was looking for a number from 0 to 180 followed by a single letter. An “A” activated the pan servo, a “B” activated the tilt servo, and an “l” toggled the LED. I also put together a Processing script on the Linux notebook, displaying two sliders to control the servos and a button to turn the LED on and off. Neither the connection to the Yun nor data flowing from the sliders worked properly. Servo movement was jerky as I moved a slider and it certainly didn't work in real time. As a matter of fact, there was a 10- or 20-second delay. To get a handle on what was happening, I decided to send just individual commands to the Yun and see how the servo performed. To accomplish this, I ran Netcat as a client, connecting to the Arduino servo (server) script running on the Yun.

linux-notebook% netcat 6666

Upon connecting to the Yun server, I could then send commands like “1l”, “90A”, “180B”, and so on. “1l” turned on the LED, while the other two moved the respective servos to new positions, thus proving that my connection worked properly. After a little while, I noticed that the servos were behind by one command. In other words, the first command, after connecting, would not move the servo. The second command would move the servo to the previously sent command position. Everything was out of step by one command. This led to my re-ordering a case statement in the Arduino script to eliminate the lag. Near real-time control is tough when you’re a step behind on each command. Getting the servos to respond immediately using discrete commands also showed that I was flooding the Yun with unnecessary instructions, leading to the time delays. The Processing slider program was changed to send a command only when a slider was moved or the lamp button was pushed. No movement or button push, no data sent. Another horrendous lag problem solved. After making these changes, the servos moved in sync with the sliders over a W-iFi connection. Pretty cool. Netcat is a quick little program that should be in every admin's or developer's tool kit. While I only covered the basics here, there are a ton of options you’ll want to explore. One that’s particularly interesting is the ability to add network communication capabilities to applications that don't have them natively by simply piping data through Netcat. Give Netcat a try. You'll find it very useful.

Related Links

Image: 123rf