Arduino Communications Using the netcat(nc) Utility
Welcome to Combustory
Any questions or comments:
- Send them to - email@example.com
Why reinvent the wheel. I have seen very complicated methods for communicating with the Arduino, Including some I have made on my own. What I have recently found out is that the easiest way to communicate to the Arduino is using the existing tool netcat. It is soooooo simple and super powerful. You can send the output to a Network port and access the communications from literally any place in the world.
One thing that you have to realize is that there are more versions of netcat than there are versions of linux. Ok maybe that is a stretch, but it seems that every system that I use has a different version with different options or worse same option that acts differently. This is generally not an issue if you use the main versions of linux as they have come to use the same version. However, if you are using embedded linux with busybox, then make sure you look at the help to get the options. In general I recommend always looking at the help of nc(netcat) just to make sure, because I am constantly getting confused on which options to use on this command.
There is also some windows versions that have with or without the -e option that is considered a gaping security hole, which is really quite over blown considering the other utilites that can do the same thing. But the claim on the securlty issue is quite ridiculas considering that you can still accomplish the same task using fifo's on linux. Windows is not as easy considering that you do not have the ability to create a fifo so easily from the command line. If you plan on using Windows, make sure you get the version with the -e aka - Gaping Security Hole.
- You have basic knowledge of command line usage
- You know how to load a program to the Arduino
Here is the one line linux command that will send the serial terminal /dev/ttyUSB0 to a network TCP port:
mkfifo arduino_fifo; nc -lk 2111 <arduino_fifo | plink -serial /dev/ttyUSB0 -sercfg 115200,8,n,1,n >arduino_fifo &
Before you start asking, plink is a command that is downloaded with the putty pacakge. Since I use windows and linux quite often, I decided to stick with putty as a multi-use package. It is easy to download and you can get it from any major linux distribution with the package manager that meets your linux distribution. You could just as easily replace putty with minicom or screen. I will leave it to the user to figure out which serial communication software to use.
To explain the basis of this command we are redirecting inputs and outputs to get the information to the TCP port by looping back on a fifo (named pipe). So after you run this command you will able to connect to the port and communicate back and forth with the arduino. From another computer on your network, here is how you would connect to the port:
nc 192.168.30.208 2111
You would put the ip of the computer you have the Arduino connected to and port 2111 is the port we used on the nc command to send the data through that port. When you press enter on that command you will have direct access to that arduino and it's serial port through the network. Now you can access that port from wherever you are on the internet if you have access your network. I encourage you to use VPN's or ssh to securly access this port and do not just put it out on the web.
Mini2440 or Embedded Systems
Embedded systems like the Mini2440, use Busy Box as a small utility package that includes a version of nc that makes communications to Arduino a breeze. All you have to do is go to a terminal and use these two commands after you plug in the Arduino.
stty -F /dev/ttyUSB0 cs8 115200 ignbrk -brkint -icrnl -imaxbel -opost -onlcr -isig -icanon -iexten -echo -echoe -echok -echoctl -echoke noflsh -ixon -crtscts nc -f /dev/ttyUSB0
You should now have a terminal session that is communicating with the Arduino. Of course you should have a program that responds to serial communication on the Arduino, or nothing will be happening. How this works is the nc command has redirected the input and output streams of the /dev/ttyUSB0, which is the Arduino terminal and the stty command sets the serial communication parameters. To exit out of the program, just use ^C (ctrl-c)
Now we expand on this idea and we send the Arduino serial port to a network port with this command.
nc -ll -p 2111 -e nc -f /dev/ttyUSB0
This is where it gets interesting. Now you can access your Arduino from anywhere on the planet, or space if you still have a good satellite connection `,~) Use this command from any other computer that has netcat.
nc 192.168.30.206 2111
Use the ip of your Mini2440 of course.