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)
Related
I have a new project about plc (siemens), but I dont know how they work.
What I have to do, is by given some data from the plc I have to handle the data and to display it on a simple GUI.
The PLC (I dont know if there are different types) that I have to use is a "proximity sensor" which only capture if something is infront of it, then it hasto send some data to my program in Java and then I have to display it on the GUI (Basically some red or green circles)
I dont have to handle the behavior of the PLC, I just have to do display a red or green circle by given the data from the PLC
Questions:
I thought of Java because I know that is a powerful language, but is it the best for what I have to do?
Second if Java is the best choice, are there any librarys for working with PLC?
Third... I started to read something about some languages like:
Functional Block Diagram (FBD)
What are they for?
"The PLC(...) that I have to use is a "proximity sensor" which only capture something in front of it."
Do you mean to state here that you are connecting a proximity sensor to a PLC, reading the data from the PLC and back to your UI program OR do you want to read the proximity sensor directly back to your UI program?
If it’s the latter connect your sensor to a microcontroller and then send the data serially to your computer.
If you are using a PLC you will need some interface card (i.e. digital or analogue). Connect your sensor to the applicable card. If you want to read an S7 PLC you will need a library - libnodave is a good one.
If you are using a library like libnodave you will need to use ones of its supported languages - it includes Java but you can also use others e.g. C. You could also use Snap7.
FBD is one of the IEC61131-3 programming languages - see:
http://www.automation.com/pdf_articles/IEC_Programming_Thayer_L.pdf
The PLC should have an RS232/485 interface. All communications happen via serial communications. Java does have the Java Communications API, But before you go down that path, What is the computer that would be interfacing with the PLC device - A low power ARM SBC or a x86 PC ? The downsides of using Java is the footprint related to the JDK. If it were me, I'd stick to C/ Rust or GoLang based on the constraints of the platform.
To communicate with a Siemens plc you could use a few different options based on the exact type of PLC that you use.
A few communication protocols supported by Siemens:
opc: the industry standard for communication between PLC and other devices
libary like Snap7 http://snap7.sourceforge.net/ which has lots of supported programming languages
for the programming language you can use Java but as alternative you could use C# or vb.net in cooperation with Visual Studio for creating a simple GUI.
If you need any further information please let me know.
i had similar problem and this is how i sorted it out:
Communication protocol that most PLCs are using are either modbus, bacnet, lonworks and milion others. In your case , i assume that Siemens uses modbus.
Firstly you neet the Rs485 to usb adapter (arround 20Eur on Amazon)
you have to read registers from modbus - please use easyModbus api
This approach works fine but it is not happening in the real time as the modbus communication could run in separate threads.
This depends on the application that you want to design, if the application is based on the web, it is better to use a easymodbus TCP that sends data through TCP connection, and if you want a desktop application, it is better to use Easy Modbus RTU that Using RS485, this library it's the best.
http://easymodbustcp.net/en/
I Recommended to use this library it's also for future (Smart Phone, Desktop and Web Application using Spring Boot MVC).
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 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.
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.
I'm thinking about making a physical controller (device?) with knobs, buttons, and LEDs. I'd like to interact with it using Java (respond to the knobs, light up LEDs, etc). The reason I mention Java is two-fold: first, I know Java well1. Second, I've written the rest of the program I need to interface with in Java (though there are ways to talk to the Java program from another language).
I would like the device to connect via USB and be (computer-)platform independent.
I haven't the slightest idea of where to start, except to start reading the Arduino website. Is this my best/only option? Is there something better suited for communicating with Java?
Note: I know that Arduino has something to do with Java (not sure what), but it seems like code must be written in a subset of C.
How would I get moving on this topic?
1 - No laughter, please.
The Arduino development environment is written in Java.
But the standard language you write a program for the Arduino platform is effectively C++.
The Arduino platform is based on an Atmel AVR chip. There is at least one Java VM for AVR chips. There are other languages available for the AVR such as Forth and BASIC (although I could only find commercial versions, so I'll if you want to find them, search for "AVR BASIC").
The Arduino uses a virtual COM port to communicate between the host computer and it. A virtual COM port emulates an old style serial line but is done with USB. You can use the Java communication API to then have a Java program running on the host computer communicate with your physical device.
For some encoders and buttons, you probably want to implement a USB HID device. If you're going to produce more than a couple of them, you'll want to do a custom board. Check out V-USB, an open-source library for making USB HID devices using Atmel microcontrollers. They have a bunch of examples of projects that use this library.
You could probably make this look like a HID joystick, using the encoders to produce X/Y axis information and having the buttons act like buttons. They you could use standard Java gaming APIs to read the joystick values.
Can you expand on your need for a custom device? It seems to me that designing hardware has a pretty high barrier to entry and that most applications I can think of would be better resolved by repurposing an existing piece of game controller hardware. If you really need new hardware, then i suggest you start by googling 'USB development kit' or
'USB development board' which will get you links like this, this and this.
As for working with USB hardware from Java, I've played around with the JUSB library a bit and it looks promising, but appears to be limited to Linux only because the Windows version of the native library half of the library hasn't been written. Alternatives can by found by googling 'HID java'.
Ok, computer-platform independant? What platforms are you targetting? That would depend on the driver and support for that said platform, what does the usb device do? Is it a mass storage device...You may have to look around and see if you can find a device driver that can talk to the device...
Hope this helps,
Best regards,
Tom.
I know for the serial port there were libraries that existed for interacting with it (rs232 library). Googling for java and USB returned several answers (the first was called jUSB). That would be the first type of thing I would be looking for.
sample for java usb connection to freescale microcontroller:
http://javausbapi.blogspot.com/