How do you embed Java into a microcontroller? - java

I have a microcontroller that when an event, I want it to send an email. I know how to use email in Java, but how can I put that Java code in the midst of the C code of the microcontroller?
#include <avr/io.h>
#include <util/delay.h>
int main(void)
{
DDRB |= 1 << PINB0; // = 0b00000001
DDRB &= ~(1 << PINB1);
PORTB |= 1 << PINB1;
DDRB |= 1 << PINB2;
while(1)
{
PORTB ^= 1 << PINB0;
if(bit_is_clear(PINB, 1)){
PORTB &= ~(1 << PINB2);
_delay_ms(100); //SLOW
}
else{
_delay_ms(10); //FAST
/////I want the Java code in here/////
PORTB ^= 1 << PINB2;
}
}
}
EDIT
I'm using an Atmel Atmega8A microcontroller. I'm making a simple alarm system, that if the wire at pin B1 is disconnected, I want to get an email or text notification.
And I'm just a high schooler novice so please don't go over my head too much...
Oh and if someone could water down multithreading in C for me that would be nice. I come from a Java background since that's what they teach in high school.

Well your "question" is to embed Java to your AVR... but what you actually want to is send email. There are some implementations to run Java bytecode (or at least I think so), but I believe none of these will be able to send email.
You need to understand that an AVR is very limited in functionality. To send email, your setup will need to connect to the Internet, with TCP/IP. Something so complicated that cannot be accomplished with the ATmega8A itself. It may be possible to find a hardware which you can communicate via UART in order to send email, or you can simply hook it to a PC and run a application to listen to the COM port and send email, or... anyway you need extra hardware.
You may want to look into other microcontrollers instead. I believe there are some with networking support, but they will probably be very expensive. Or, you can just use a Raspberry Pi instead.

Java and C are very, very different langauges.
You'll need to find a compiler for your microcontroller that supports Java.
However, if your controller can get C compiled via GCC, then use of GCJ may work with certain memory constraints on the target platform. You'll need to generate an object file of the correct architecture, and link it using microcontroller-compatible gcc.
This is not worth its time. I recommend implementing it in C instead.
Besides, opening files and sockets may not occur since a microcontroller usually does not provide any kernel routines beyond certain aspects of hardware access. libc will also likely be absent or minimal.
You might also not have networking hardware. If you can tell us which microcontroller, we could help.

A more direct answer to the question was given already by Alvin Wong, but I feel your question is so out of focus that it might be good to help you refocus it.
You should start understanding what is really happening when you send a mail from a Java program, then understand what part of it can actually be done by your microcontroller, and how.
So, what happens when you run your mail-sending Java program?
You wrote the program in the Java language, which got compiled into Java bytecode, which gets interpreted by the Java Virtual Machine, which will call some library provided by the Java runtime,
which will (probably indirectly) start a "network communication", which will take advantage of the Operating System network facilities, ...
which will use the network hardware,
to communicate with a mail server using some mail protocol, and send it some specific sequence of bytes that will make it understand that a mail message is being sent to someone.
The (1) part is mediated by all the layers of the Java platform. (Remember: Java is much more than a language).
The (2) is a layer (or couple of them) under the Java platform, and includes all of your Operative System (Windows, Linux, Mac OS X, whatever).
The (3) is a layer comprised of your hardware, and that includes other sublayers like the firmware in your network card, which is still more software that might manage things like the Ethernet / Wifi protocols' details.
The (4) layer is rather bits and bytes being flipped on and off in some communication channel (Ethernet, WiFi, whatever).
Now, what part of it can be done with your microcontroller?
A microcontroller sits in the lower part of (3). In your case, the Atmega doesn't even have any network hardware support, so ... I don't know how you are planning to connect to the network. Anyway, even if you had it, you are so close to (4) that it would rather be madness to try to go up to (1). (1) is say for elephants, (4) can be done by ants, and a microcontroller is about the size of a ladybug, to refer to Atmel's mascot :P.
So: stop thinking like an elephant. As it is you will have to work to get to your goal, but if you take the elephant's way, you'll be probably doing a ton of unneeded work - if it can be done at all, which I doubt.
(And if anyway I had to take the elephant's way, namely use Java in an AVR, I would start by looking at GCJ or LLVM options. I know that some work was done to port minimalist JVMs to 8 bit microcontrollers more than a decade ago, but no idea how that evolved)

Like others said, sending an e-mail this way is really really far from your micro (I assume you primarily want to send the mail, not to run a Java VM in there). To get the mail going, you need to connect to the Internet using TCP/IP, then using for example the SMTP protocol, you may be able to accomplish this.
The problematic part is connecting to the Internet. For this you need to get your device on a "suitable wire", which is typically Ethernet, but radio based solutions may also exist. Then you would have to implement everything necessary to communicate on that wire (the whole network stack until TCP/IP). This can not reasonably be done all yourself. One way is to use a micro which has some built in Ethernet connectivity and use for example lwip or Contiki depending on your preferences to do the magic. The other way is using extra hardware which does the thing for you such as this Moxa product, but you may not be able to implement SMTP over this.
Then you will "only" need to implement a suitable SMTP (Simple Mail Transfer Protocol) layer to properly send your mail (one or another of the solutions I presented above may already have this for you).

Your microcontroller will need to support the JDK or Java ME (maybe Sun SPOT). http://www.inonit.com/cygwin/jni/invocationApi/c.html has an example of using the JNI to start the JVM and invoke your class.

Microcontrollers very very typically are developed in the C environment since in a microcontroller there are limited resources in terms of hardware power. C is used since it allows the most flexibility and allows fine tuning since in the microcontroller environment real-time computer is very essential. That is not to say though, there is java used in the microcontroller world. Very convoluted though, android runs on some java which in the lowest end possible uses a ARM processor (its up to you to decide if you really want to call that a microcontroller). So all in all, yeah I would recommend that you learn C. If you really want to go the java way, I have not personally run into a lot of resources that allows you to use java on a AVR microcontroller. It might be possible, but expect a hit on performance.
If you have no experience in microcontrollers, I would recommend looking into Arduino (arduino.cc).
Best of luck!

Related

Make a GUI program to communicate with microcontroller via serial link

I want to connect my PIC micro controller to computer via serial link and control it. The MCU will send a series of bits, my program should extract useful information, so basically i should be able to work on individual bytes of received data and send some commands back. So where should i begin to write such a program ?. I have worked on C programs and simple java programs and i know bit of PHP.
It will be much better if i can make it more like a webpage so that i can host a page on my computer which can be accessed by all devices in the network and control the mcu.
Please suggest me some ideas to implement the same
If I were you, I would consider dealing with PIC or other microcontroller, because they are very low level hardware. There are some options like Arduino, RasberyPI, etc. They already have LAN or WiFi modules and cheap as well.
I would do it in a Service oriented way, so communicate via TCP/IP or UDP socket. In this way, you will have a standard interface. No mather what harware/software you have.
One way of going about it:
write software to interface your micro-controller with your computer
the pc side of the interface is most likely written in c, so learn how to interface java and c with something like jni or jna
look into java ee to create a web server
It will be very broad and require a fair amount of work/learning.

How to use multiple network interfaces concurrently in java?

I am developing a software that needs to check the signal strength of different network interfaces like wifi, lan, dongles etc. I want to measure (absolutely or relatively) the signal strength and find out the ipaddress of all the network interfaces having strength greater than some threshold value. How can I do it in JAVA?
http://www.java2s.com/Open-Source/Android/UnTagged/mytracks/com/google/android/apps/mytracks/signalstrength/SignalStrengthListenerCupcake.java.htm has some code for Android. I cannot vet it though as I work more with iOS than Android.
For the desktop, there is no pure Java solution as the JVM has not exposed this network information. If you had to go down that road you have two basic options 1) invoke the command line via java and extract the data from another command or 2) go JNI. Neither approach is portable and they both require a fair amount of effort. A variant of 1 that is available with Linux (http://www.cyberciti.biz/tips/linux-find-out-wireless-network-speed-signal-strength.html) is to parse the file '/proc/net/wireless' from Java.
Sorry for not being able to give you better news. You could try sponsoring a RFC with Oracle, it does strike me as useful information to add to the JVM networking libraries.

How can I implement a good Client-Server approach?

I'm developing a distributed application, and I need to connect a client Java based to a server C++ based. Both of them will need to send information to each other, but I need them to be able to do things while waiting for the information, and they don't know when they are gonna get new information, or send information.
How can I achieve this? Now I'm trying to implement a basic communication with Sockets, but I don't really get to communicate them. I have read that using sockets + threads is usually a good approach for client-server apps.
Could you please recommend me some web or book to read about this, or send me some example code to learn?
Do you think that i should use other approach, better than sockets? maybe a higher level library (i would need it for c++ and java) or a totally different way?
EDIT:
I will add some extra information.
What I would love to achieve is the following:
My C++ program has a main loop, where I would like to have a call like GetUpdatedDataFromRemoteDevice() where I read the new values of some numerical variables that previously got updated from the net (the socket, for example).
Eventually, the C++ program will need to send a message to the remote device, to tell him to send other kind of data, and after that, keep getting the updated values.
From the Java program (remote device) the application running is an interactive touchable screen, that cant get blocked by the network transmissions, because it must keep working for the user, so all the networking should be done in a separated thread.
That thread, should connect to the server, and when a button is pushed, start to send the data (4 changing numerical values) in a loop until another event happens.
It would be nice also to be easily re-connectable to the server.
ICE is a modern and good library for distributed applications:
many languages as C++ and Java
many platforms
GNU GPL
good performance
easy to use
First, you define the messages you want to exchange between server and client.
Then, you implement the C++ and Java source code to handle these messages.
More info at http://zeroc.com/ice.html
Have fun ;-)
EDIT: I have to use ACE in some projects. I can tell ACE is very old, maybe mature, but uses outdated C++ coding rules :-(
Therefore ACE is not as easy to use as STL or BOOST. Moreover, ACE is not really efficient... I prefer ICE ;-)
I don't know what your application is but robust client server socket programming is pretty hairy task to do properly. Hardware byte order, String encoding, Network errors, retries, duplicate messages, acks etc.. require lots of good design and careful programming. You need to get it work well as single-threaded before even thinking using multiple threads.
Unless you need instant notifications from server to client I suggest that you use HTTP as protocol between client and server. Client can poll server occasionally for new messages.
Anyway the problem has been solved multiple times already.
http://activemq.apache.org/
http://www.rabbitmq.com/devtools.html
http://www.cs.wustl.edu/~schmidt/ACE-overview.html
I did something of this sort once. In my case it was easier to connect my C++ app to a local Java app using JNI and then have the two Java apps talk to each other.

Java networking?

First off, before I ask, i would like to point out that this question is for education. I want to know to expand my understanding of Java and network security (what little there is).
How could you use Java for network security and counter attacks? I have been using server/sockets for a while now (for non system security stuffs), but I don't quite understand what I'm doing. Naturally, I should learn up on networking, but where to start? There is a protocol for everything, heck there are protocols to have protocols. To further expand, how could you use Java to say, port sniff, catch packets or kill/open a port remotely?
I guess to phrase the question more adequately; does anyone know of any good sources that I could look at to get a more in depth look/study of how Java handles network security and counter hacking and malware containment?
I think the best thing to do would be to learn concepts, then worry about using Java to implement the concepts later on. There are some gaps in your understanding (for example, I don't even know what "open a port remotely" might mean) and the best thing to do would be to solidify your understanding of how networks work first.
I don't really have a list of network security texts I can recommend -- probably someone else will! -- but IMHO it might not hurt to start with a classic like Steven's "UNIX Network Programming" to shore up the fundamentals, if you can find a copy.
how could you use java to say, port
sniff, catch packets or kill/open a
port remotely?
You can't use Java to sniff ports.
You can't use Java to catch packets.
You can't use Java to kill/open a port remotely.
how Java handles network security and
counter hacking and malware
containment?
Java doesn't handle network security other than internally for its own applications via the security sandbox.
Java doesn't handle counter hacking.
Java doesn't handle malware containment other than internally for its own applications via the security sandbox and bytecode verifier.
One of those things above can be done via an add-on to Java, but basically Java isn't the correct tool for this job.

How do I configure and communicate with a serial port? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I need to send and receive data over serial connections (RS-232 and RS-422).
How do I set up and communicate with such a connection? How do I figure out what the configuration settings (e.g. baud rate) should be and how do I set them?
In particular I am looking to do this in Java, C/C++, or one of the major Unix shells but I also have some interest in serial programming using Windows/Hyperterminal.
Build a time machine and go back to 1987? Ho ho.
Ok, no more snarky comments.
How do I figure out what the configuration settings (e.g. baud rate) should be...
Read the datasheet? Ok, ok. Seriously, last one. If you don't know the baud rate of the device you are trying to communicate with, you have two choices. Start guessing, or possibly bust out an o-scope. If you need a good starting point, let me suggest 9600-8-N-1. My suspicion is you can get there with brute force relatively quickly. There's a third option of having an old-school ninja who can tell just by the LOOK of the garbled characters at some standard baud rate what actual baud rate is. An impressive party trick to be sure.
Hopefully though you have access to this information. In unix/linux, you can get ahold of minicom to play with the serial port directly. This should make it fairly quick to get the configuration figured out.
one of the major Unix shells
In Unix the serial port(s) is/are file-mapped into the /dev/ subdir. ttyS0, for example. If you setup the correct baud rate and whatnot using minicom, you can even cat stuff to that file to send stuff out there.
On to the meat of the question, you can access it programmatically through the POSIX headers. termios.h is the big one.
See: http://www.easysw.com/~mike/serial/serial.html#3_1
(NOT AVAILABLE ANYMORE)
but I also have some interest in serial programming using Windows/Hyperterminal.
Hyperterminal and minicom are basically the same program. As for how Windows let's you get access to the serial port, I'll leave that question for someone else. I haven't done that in Windows since the Win95 days.
If you want to code in Java I really recommend SerialIOs SerialPort. It is very easy to use and saves you days of work. I've never found an open source library as good as SerialIO, REALLY!
My advice: do not use Sun's serial IO framework! It is from 1998 and full of bugs. You can use rxtx but serialio is better!
For C/C++ on Windows you have (at least) two choices:
Use the SerialPort class provided by .NET.
Use the Win32 API. There is an extensive MSDN article dating back to 1995, and many free libraries and examples on the web to get you started.
The .NET option will be much easier.
If it needs to be cross platfrom, I would suggest looking at Boost Asio.
Awhile back I wrote a decent sized application to route connections from a farm of modems through to a TCP/IP network address.
Initially I looked for an unencumbered (free) Serial IO library. I tried Sun's, IBM's and RxTx. They were fine for developing the application, and in initial testing, but in production they each proved unstable.
Finally I paid for SerialIO's SerialPort. Converting over was literally an exercise in changing imports, and the library has been absolutely rock solid - I cannot recommend it enough. My application has been running in the field 24/7 for a couple of years now, with not a single problem encountered by multiple customers.
If you start development using SerialPort, they have a better API and I would use it.
If you need cross platform support, Java with SerialPort was the best choice I could find.
Lastly, their licensing is pretty darn reasonable as long as you are not preinstalling software on the equipment for your customer(s).
From the other side, if you want to do it using C#, which will run on both Windows and Linux--with some limitations (EDIT: which may be out of date. I have no way to test it.). Just create a SerialPort object, set its baudrate, port and any other odd settings, call open on it, and write out your byte[]s. After all the setup, the SerialPort object acts very similar to any networked stream, so it should be easy enough to figure out.
And as ibrandy states, you need to know all these settings, like baud rate, before you even start attempting to communicate to any serial device.
At work we use teraterm and realterm for checking serial data is correctly formatted. Also we have a hardware splitter with a switch so we can monitor traffic to our application via a cable back to another port.
Windows allows you access to the serial port via CreateFile. That gives you a handle and from there you can configure access.
Depending on the device You are trying to communicate with, there may be more parameters than the baud rate, number of data bits, type of parity checking and number of stop bits to consider. If I recall correctly, modems use nine lines of the RS-232C interface. Some devices like, for example cash registers, may use hardware handshaking on RTS/CTS lines or on DTR/STR lines.
In general it's good to know how the interface works. You can't communicate if the baud rate doesn't match, but wrong setting of other parameters might kind of work. For example You can easily send data to the device expecting 1 stop bit with 2 stop bits set. Problems start when You try to receive data in such case. You can also use appropriately set parity bit as one of stop bits, etc.
If you are not forced to use a particular compiler I suggest to use Qt and in the new 5.3 version you will find a class dedicated to serial ports:
http://qt-project.org/doc/qt-5/qserialport.html
The code you will write will run on all supprited Qt platforms, at least those that have serial ports.
I have been using purejavacomm:
It is an implementation of javax.comm written in pure java + JNA
Unlike rxtx, you don't need to install a dll. It is written in pure Java + JNA, which solved the problem of portability between Windows and Linux for me. It should be easy to port to other OS-es that JNA supports, such as Solaris and FreeBSD, but I haven't tried it.
You might expect a pure java library to lag behind a native implementation such as rxtx in performance, but with modern CPU's, the bottleneck is very likely to be the bitrate of your serial port, not CPU cycles. Also, it's much easier to debug than a mixed Java/Native library or pure compiled native code.

Categories

Resources