Introduction: Computer to Arduino Communication.

Chapter 7:

Hello again, this is another chapter of our domotics series. From now on we'r going to start dealing with very interesting stuff. Today we will see how to connect you computer to the arduino board. I think is pretty obvious the cool things that could came of this relationship, but I guess is never to 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, infra red and other), to other arduinos and every device you can think of. If your computer are plugged to 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 yous Java language to build a bridge between what ever we use in 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 things. May be not...not sure... but I do know this: Java has very strong points, one of them being the capability of run the same code in several operating systems.

Also we will use a simple sketch to test the communication. So lets 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 with 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 another places.

Step 3: Java Libraries.

This 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 the java program is running. And this is very important since we need to select the precise native library. And also to find out with port are connected to the Arduino board.

And also you will find the RXTXcomm.jar librarie. This library will work among the native rxtx libs. I wil 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 mention first that you need to install first of all the java virtual machine. This is the base program that let us run java code and has to be installed in your computer. Here you will find information on how to do that.

Sources: zip file.

java: jar file.

Create a folder an 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 in a console (of what ever system your running in, 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 (what ever you want to call it) for quite a wile, and I have found lots of issues. For example some times things don't work, the computer do not recognize the board or the board do 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 opinions works pretty nice.

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 a impressive piece of machinery, without counting its low price) things become a little bit messy. This board do not have the same on-board USB controller that 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 problems 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 for has been assigned a name, COM in case of windows architectures, or something like /dev/ttyUSB0 in Linux architectures. There is a possibility that script wont 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 issue 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

Comments

About This Instructable

1,132views

23favorites

Bio: I am a programmer and I will be presenting my work in a form of chapters related to DOMOTICS.
More by PabloPaparini:Computer to Arduino Communication. Compressing Radio SignalsNormalize and Compress IR Signals.
Add instructable to: