Connecting Processing to Arduino Yún / Yun

edited October 2013 in Share Your Work

Not being well versed with Java libraries this took me a bit of time to figure out, so hopefully it will save others the trouble.

You will need the jsch package which provides an SSH client.

This example code makes an SSH connection to an Arduino Yun (which is assumed to be on the local network and have its default host name) and sets up an InputStream and OutputStream which can be substituted for the serial I/O stream normally used to communicate with Arduinos via USB.

For anything at all interesting to happen when you run this example, you should at a minimum be running the Console example on the Arduino, available at

// jsch is an open source SSH client for Java. 
// get it from
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;

String user = "root";
String password = "<ARDUINO PASSWORD>";
String host = "arduino.local";
int port=22;

  JSch jsch = new JSch();
  Session session = jsch.getSession(user, host, port);
  session.setConfig("StrictHostKeyChecking", "no");  // less than maximally secure
  System.out.println("Establishing Connection...");
  System.out.println("Connection established.");
  Channel channel = session.openChannel("exec");

  // this stream will be used to send data to the Yun
  DataOutputStream dataOut = new DataOutputStream(channel.getOutputStream());

  // this jsch member class provides remote execution of a shell command
  ((ChannelExec) channel).setCommand("telnet localhost 6571");
  // see for why this command

  InputStream in=channel.getInputStream();

  // after configuring all channel parameters, we connect, causing the
  // command to be executed. Results and further input will be handled
  // by the streams

  byte[] tmp=new byte[1024];

  // poll input stream forever
  while (true) {
    while (in.available ()>0) {
      int, 0, 1024);
      if (i<0)break;
      System.out.print(new String(tmp, 0, i));
    if (channel.isClosed()) {
      System.out.println("exit-status: "+channel.getExitStatus());
    try {
      // if used with the Console example code, this will blink the LED
      // in time with polling events
    catch(Exception ee) {
catch(Exception e) {

This is my first and a hasty forum posting, feedback on anything is welcome.



  • Works like a charm... Exactly what I needed. Tnx!

  • Thank you for your code. I'm currently working on this, but i get an error when i'm trying to load the sketch: "The method setConfig(Properties) in the type Session is not applicable for the arguments (String,String)" Am I missing something with the installation of the library Jsch?

  • Hmm, not sure. Have you tried commenting out that command to see if the rest compiles? Things should work at least until the ssh connection is attempted in that case.

  • Yes, I've tried, and the compiler stops few lines after, telling me that he can't find anything named Channel. Seems to be bad links to the library.

    Where should I put library folder once i've downloaded it? I'm sorry but my knoledge in Java programming and libraries setup is very poor!

    Thank you!

  • /libraries/jsch/library/jsch-0.1.50.jar is how mine is set up.

    But I see a copy of the jar in a code/ folder inside the folder where my .pde sits. I'm not sure whether I put that there or Processing did.

    Are you not getting an error on the very first use of Jsch, where the new object instance is created?

  • I think I've made some steps further... Simply "Sketch -> Add File" on the menu and added the "jsch-0.1.50.jar" file to the sketch. I'm currently out of home, but the sketch compiles correctly and establish the connection with another SSH server on a Raspberry. I'll verify the connection with the Yùn as soon as I'll arrive at home!

    Thank you! =)

  • Great - yes I think doing that will have created the code/ folder with a copy of the jar within your sketch. Glad it's working!

  • Thx a lot. Saved me a lot of work !

  • Thanks for this. I've got a few questions:

    1. Is it possible to connect to multiple Yuns at the same time? I need to get datas from all the yuns connected to my network, to use it in the same program.

    2. My Yuns are programmed to execute only once, after a specific command (the loop is stopped until i run that command again). I added the command on your example but the Yun goes in loop, just as if the command was given endless times. I added "dataOut.writeChars("s");" immediately after the while(true) line. (s is the command needed). Where am i going wrong?

    1. I don't know why not, you should be able to create multiple instances of the JSch / Session objects and connect each to a Yun. You can then cycle through a communication with each of them.

    2. Since while(true) creates an infinite loop, it's going to repeat that command over and over. You can change while(true) to a conditional, remove the while(true) so it only goes through one communication cycle (but then your Java program is finished, probably not what you want), or you can add code to pause the loop until some condition is met. Hope that helps!

  • Of course it helps, thanks! i've tried with if(true) but it doesn't seem to work, i guess i'll have to find the right condition to stop the loop after the first data recieving is complete. thanks again!

  • Good luck! if(true) is a meaningless statement because whatever comes after it is completely unaffected by the if statement. It sounds like you may want to read up on program flow commands, either in Arduino reference docs or general C programming tutorials.

  • Hi, Thanks for this code. I´m kind of newbie merging Java and Processing code. How should I create processing objects and OSC with this code? Thanks in advance if someone can give some tips to learn it.


  • edited May 2016

    How to send and get data from user interface? need to create void draw and setup

Sign In or Register to comment.