Introduction: Computer to Arduino Communication.

Chapter 7:

Hello again, this is another chapter of our Domotics series. From now on we're going to start dealing with very interesting stuff. Today we will see how to connect your computer to the Arduino board. I think is pretty obvious the cool things that could come of this relationship, but I guess is never too much to mention it again. Once you have control over your board from your computer, you can control everything that the board controls from your computer. For example, sending messages (radio, infrared and other), to other Arduino and every device you can think of. If your computer is plugged into the internet the possibilities are endless. Basically, you can control your house from the other side of the world in a very affordable and cheap way.

First of all, we are going to your Java language to build a bridge between whatever we use on our computers to the boards. Yes... I know... I heard here and there that Java is not the best language to do this kind of thing. Maybe not...not sure... but I do know this: Java has very strong points, one of them being the capability to run the same code in several operating systems.

Also, we will use a simple sketch to test the communication. So let's briefly start with this:

Step 1: The Testing Sketch.

This is a very simple sketch. Basically what will do is to listen to incoming information from the serial, and if it matches certain sting tern of or on digital outputs. In this case, we will use 11 and 2. Remember to plug among a led the proper resistance. But bear in mind that is not extremely necessary to connect the LEDs, just the code will do find, since the code itself answers back when the correct instruction comes. Example

written instruction: blue on

Sent: blue on

board answer: Blue is on

First, you can try it with an Arduino IDE to check if everything is ok.

Step 2: Native Libraries.

As you might know, the Arduino board can be connected to the computer through a USB port. This is the way you upload the sketches and make them run by the Arduino IDE. But also is a way of communication once your sketch is already running. So we can send information to the board in order to do fun stuff. But depending on the operating system we will need different libraries to speak to the USB port. So here you have the libraries required for Linux, Windows, Solaris, Mac, and ARM systems (Raspberry). Downloaded from here or from here and other places.

Step 3: Java Libraries.

These are the Java libraries you will need in order to make Java work. Here you will find a description of the lib and its function. I use it basically to detect what is the operation system the Java program is running. And this is very important since we need to select the precise native library. And also to find out which ports are connected to the Arduino board.

And also you will find the RXTXcomm.jar library. This library will work among the native RXTX libs. I will show you how in the next step.

Step 4: Java

In this step, I will provide the Java source code that you can modify and change as you like with an Eclipse IDE. And also with a jar file with the compiled classes of this project ready to run out of the box. And of course an explanation of the functionality.

I should have mentioned first that you need to install first of all the Java virtual machine. This is the base program that lets us run Java code and has to be installed on your computer. Here you will find information on how to do that.

Sources: zip file.

java: jar file.

Create a folder and place our folders (native and lib) and the jar file as shown in the photo above.

To run the program you will have to execute it in a console (of whatever system you running in, all would be the same) java -jar arduCom.jar. You can check if everything is good with your Java installation running java -version That will print out something like this:

java version "1.8.0_131"

Java(TM) SE Runtime Environment (build 1.8.0_131-b11) Java HotSpot(TM) 64-Bit Server VM (build 25.131-b11, mixed mode)

if everything goes OK, you should see this output:

PATH: /home/pablo/Documents/arducomic/lib

Arduino Linux 64 Stable Library


Native lib Version = RXTX-2.2pre2

Java lib Version = RXTX-2.1-7

true Started

And then you can hit "blue on", "blue off", "red on" and "red off"

Step 5: Testing and Issues.

I have been testing this... thing (whatever you want to call it) for quite a while, and I have found lots of issues. For example, sometimes things don't work, the computer does not recognize the board or the board does not respond and you have to unplug and plug the USB several times. Most of all in Windows environments. Bear in mind that this technology is kind of new. Nevertheless, in my opinion, works pretty nicely.

Did not test this with a Mac, do not have one in hand. But tested it dough with Windows 7 (64/32), Ubuntu (several versions - last ones) (also 64/32), and most interesting of all (maybe for its price), on a Raspberry PI (raspbian) which is anARM architecture, and it works pretty fast. All this using Arduino Uno. This board works pretty well and works everywhere. But with Arduino Nano (which by the way it is also an impressive piece of machinery, without counting its low price) things become a little bit messy. This board does not have the same onboard USB controller as the one in Arduino Uno. And order to connect it you have to install extra libraries.

  • In the Raspian you will need to run apt-get install sudo apt-get install librxtx-java
  • In Windows (64/32) you will have to download a driver.
  • Don't remember that kind of problem in Ubuntu, I think that always worked for me there (nano)

There is a class called ActivePorts. The purpose of this class is to find the USB that has been plagued and there has been assigned a name, COM in case of Windows architectures, or something like /dev/ttyUSB0 in Linux architectures. There is a possibility that the script won't work specially in old Windows systems. But you can hardcode this line in ArduController class

/** The port we're normally going to use. */

private static final String PORT_NAMES[] = ap.getPortsAsArray();

like this:

private static final String PORT_NAMES[] = {"yourportname"};

Please let me know if have any issues I will be more than happy to help.

This is the end of Chapter 7, please check on my previous chapters:

1, 2, 3, 4,5, 6