I have written a program in Arduino to detect intrusion using an ultrasonic sensor. And when it detects intrusion, a camera must be triggered. I have written the triggering program in Java in the NetBeans IDE. But I don't know how to link the Arduino program with Java. I browsed many sites and came across the blog post Arduino and Java, but I got an error while running the program.
The error is at the line Preference.init() How can I fix this problem?
Your Java application running on some computer or board can communicate with your Arduino application via serial communication link. This can be RS232 for shorter distances (up to 30m if 9600bps is used), or RS485 for up to 1200m. You need touse some known protocol, or make your own. This link can also be wireless if needed. More complex would be to put bluetooth on both sides, if that is more convinient for some reason. Alternatively, if you have an ethernet shield for Arduino you can implement UDP or TCP messaging to notify Java application about Arduino events. Instead of sending an event only when it happens, you can also think about periodical polling from Java application.
Related
I'm new into arduino programming and robotics, and I want to use Java to send data to the arduino sketch, to notify it when to use the robots motors. In particular, I have already written a working A* algorithm in Java and would really like to apply this to my arduino robot, so in my java code, when the best state is found, I want to then send data to arduino sketch to notify it what direction to go. Anyone have and ideas as to the best way? I've looked online but haven't anything that really answers this specific question.
Also, I realize I'll need to have the arduino connected via USB to use Java, but for now I'm fine with it
You are going to need JAVA USB API in order to send the data to the USB port where your Arduino is connected. From there you just read the input in your Arduino sketch and you are done.
You can also use a Java Serial Port library (like jssc).
Personally I use Processing (which uses Java) and it's quite easy to do Serial communication with the serial library it ships with.
Check out the SimpleWrite example (which includes example Processing-java code and Arduino code)
Let me go straight to the problem.
Let say, I have a Arduino Uno (COM4) and Arduino Mega (COM5), both is communicating with my OS through serial port. How can I detect that COM4 is really an Arduino Uno so that I don't mistakenly connecting my program to COM5 port? The process of connecting to the serial port is done in runtime by my program.
The code that I saw online here in another SO question and from my experiment with that code earlier, it only listed out all serial ports while it don't reveal out what is that device is (so the program don't know what is really that device, either can be Arduino Uno or Arduino Mega or something else).
So my question is, how can I achieve this purpose? Cross-platform is preferred but if such things can't be achieve, then please answer on how to do it inside Windows (as I currently developing this program for computer that's using Windows OS)
You can ask a device to identify itself right after connecting. For that you want to support device identification in the device's firmware.
Add something like following logic into loop method defenition of the arduino sketch:
int inputData = Serial->read();
if (inputData == IDENTIFICATION_REQUEST) {
// respond with information that identify the device
}
Then you can check which device the programm is connected to sending IDENTIFICATION_REQUEST.
Today i came up with an idea to controle a arduino board or any other micro controller wirelessly using a java program. Im somewhat of an intermediate java programmer, meaning i can make basic applications. I know next to nothing about arduino, or the programming of microcontrollers in general, and i dont want to start to learn until i know i can link java with arduino, meaning running a program on the computer that, lets say, turn on a led. Ive done some research, and thats where i got the bluetooth from, but it seemed like the only videos were in spanish, and not much documentation about it, as far as i can see. I was just wondering how easy is it to link these two, is it possible, any links to tutorials of sorts, thanks. P.S. it doesn't have to be arduino, im just familier with that name, im just interested in controlling something physical through a java app, thanks.
Yes you can,
If you can program in java you will find arduino fairly easy but here is a arduino based tutorial with all the code your need:
http://playground.arduino.cc/Learning/Tutorial01#.UxClcvl_tFs
A majority of bluetooth modules will spit out serial data and this simply interacts with the RX/Tx (receive/transmit) inputs of the arduino. They are usually configured to standard board rates mainly 9600 to start.
All you have to do is spit a string out via bluetooth, an java based bluetooth example is below but you can also find bluetooth modules for PC that interact and install as a CDC (serial port).
Sending a string via bluetooth from a PC as client to a mobile as server.
Serial ports are a little easyier :)
http://www.java2s.com/Code/Java/Development-Class/COM-Port.htm
Cheers,
Chris
I am looking for a very fast method of sending commands from my Android device to my computer.
There are 3 methods in question, Bluetooth, ADB w/ USB, and USB. A server socket connection is too slow for what I need.
All 3 seem to give me a headache when trying to implement them.
I know a straight USB connection would be faster than anything else, but I'm finding it especially difficult to implement.
Many users suggested using adb to communicate over USB.
The main method in question is using the ADB server/client implementation, where I would forward the ADB ports and use a socket connection to host a server on my Android device (Somehow the USB is involved).
More information is available here:
http://www.anothem.net/archives/2010/02/15/communicating-over-the-usb-cable/
Would this method be any faster than using a standard server/client socket connection (since there is a USB involved in this method).
I'm a little hazy about how this all works, since I don't think it's a very common request. If anyone could explain this method a bit more I would greatly appreciate it.
Thanks!
I made an application which I started with Bluetooth, after some testing I decided to go with normal TCP sockets with the WIFI interface. The application consists of one PC which handles up to 20 tablets for a commercial command entry in restaurants, discos, etc. Every time a tablet connects the server send about 100 small JPG pictures which represent the different items in the menu. It's done very fast, less than a couple of minutes, and in normal data entry I've seen no problems so far.
I don't know how fast you need it to be but WIFI sockets are as least as fast as USB (2.0) and Bluetooth. Also with sockets the tablet and the PC can be hundreds of meters away from each other, depends on your WIFI configuration which can be expanded easily. Other thing to consider is when the device goes to sleep, when it wakes up it may take some seconds before getting the WIFI up and running and, in some few strange cases you need to restart the socket connection.
Whichever you decide to use, all of this should be handled by a well define protocol of course.
I installed the Java serial RXTX libraries and got my Arduino set up right. I even have the sample code up and running, but I can't seem to understand how to access the different Arduino pins in order to turn a LED for example. How can I access the pins with the OutputStream or any other way?
Do I need to send something special to the stream in order to tell it about a specific pin?
Out of the box the Arduino doesn't provide any way to control the I/O ports from the serial link.
You will need to obtain or write a program which runs on the Arduino and which listens for commands from the serial port to control the I/O pins. Because the Arduino only has a small microcontroller on it, you probably cannot write this program in Java.
Here is an example program which runs on the Arduino and reads numbers spelt out in decimal ASCII characters over the serial line, then uses those to control an RGB LED. You should be able to use this example to understand how to approach the Arduino side of the problem, and your Java RXTX code can send the integers to control this once it's working.
http://arduino.cc/en/Tutorial/ReadASCIIString
I am not sure how readable my code is, or how easily you can install it, but I wrote a small project to talk to the Arduino via RXTX. You can see my code here:
https://github.com/p90puma/SerialToIR
It should give you some ideas.
These three files should give you everything you need:
RXTX Code
Interface
Arduino Sketch
No, the Arduino itself accesses its own pins. You use Java to send serial data to the Arduino, and it then decides which pin(s)/code to use.
So in your Arduino code you start with:
Serial.begin(9600);
...
...
And to make a serial connection from Java, you can use the SerialPortEventListener interface.
All of the information in the prior answers is correct. The Arduino does not by itself, talk to a computer running Java and do anything. You need to put code onto the Arduino that will talk to your computer with some kind of protocol.
It turns out that a standard set of code for this exists. It is called Firmata. See http://firmata.org/wiki/Main_Page for the Firmata home page. See http://playground.arduino.cc/Interfacing/Processing for a discussion of Firmata and Processing.
Processing is a PC/Mac/Linux programming environment based on Java. The Processing and Arduino projects are closely related.
Try the various examples. Put Processing and the Arduino IDE on your computer. Upload Firmata onto your Arduino and the use the Processing / Firmata examples to try everything out.