1

I try to make an application that runs a script on my raspberrypi to turn on a LED. This should be managed with an android app. I used jsch to create a ssh tunnel to execute the script. But if I press the button nothing happens and logcat doesnt do anything.

I tried to check if its even possible to create a ssh connection to my pi from the emulator and downloaded JuiceSSH and everthing went fine...

public class HomeActivity extends AppCompatActivity {



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        Button btUnlock = findViewById(R.id.btUnlock);
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();

        StrictMode.setThreadPolicy(policy);

        btUnlock.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                  {
                   new ScriptTask().execute();
            }
            }
        });

    }
}
    class ShellExecuter {
        private static String username="pi";
        private static String hostname="XXX";
        private static String password="XXX";
        private static int port=22;


        public void executeScript(String scriptname){
            try{
                JSch jsch= new JSch();
                Session session= jsch.getSession(username, hostname, port);
                session.setConfig("StrictHostKeyChecking", "no");
                session.setPassword("XXXX");
                session.connect();

                ChannelExec channelExec=(ChannelExec)session.openChannel("exec");
                InputStream in= channelExec.getInputStream();
                channelExec.setCommand("sh "+scriptname);
                channelExec.connect();
                channelExec.disconnect();
                session.disconnect();
            }
            catch (Exception e){
                System.err.println("Error: " + e);
            }
        }


}
    class ScriptTask extends AsyncTask<String,Void,Void>{


        @Override
        protected Void doInBackground(String... strings) {
            ShellExecuter shellExecuter=new ShellExecuter();
            shellExecuter.executeScript("on.sh");

            return null;

        }
    }

Unfortunately I dont have any error message. It just happens nothing...

Butiri Dan
  • 1,759
  • 5
  • 12
  • 18
  • 1
    *"nothing happens"* is not really a helpful problem description. We cannot debug your application for you. Did you debugging the application? Did you try adding more logging? Anyway, you do not even wait for the command to complete - see https://stackoverflow.com/q/22734134/850848. – Martin Prikryl Aug 19 '19 at 06:03
  • Thank you for your answer! Unfortunately it didnt fix it to add it to my code. I tried it with other commands and for example "shutdown -h now " worked pretty well. Could it be that my command is not correct. If I connect to my raspberrypi through Putty and enter "sh on.sh" it works. I am confused tbh ... – Tarik Puplic Aug 19 '19 at 10:29
  • 1
    Try to read the command (error) output. It's also worth trying `./on.sh` or `/full/path/on.sh`, as it's very likely you have this problem: [Certain Unix commands fail with “… not found”, when executed through Java using JSch](https://stackoverflow.com/q/40021860/850848). – Martin Prikryl Aug 19 '19 at 10:30
  • I found the problem. I just added the Thread.sleep(1000) blank, without while loop. Thank you for your help! – Tarik Puplic Aug 19 '19 at 10:44
  • That's a hack, not a solution. And if that helped, my original correct solution with waiting for the command to complete must have helped too, had you did it right. – Martin Prikryl Aug 19 '19 at 14:28

0 Answers0