3

I want to open a ssh session to my Raspberry Pi and run simple command echo 0=+10 > /dev/servoblaster at what time I want ( means not using system("ssh pi@192.168.1.5 echo 0=+10 > /dev/servoblaster") because it takes time to run ssh again). What is the easiest way in C++?

Nhan Ly
  • 95
  • 1
  • 4
  • 12
  • 1
    Why [tag:c++]? In fact why [tag:ssh]? What's the underlying problem that you are trying to solve here? – johnsyweb Apr 06 '14 at 09:29
  • 1
    I'm writing a C++ program to control servo motor by Raspberry Pi with command above, and how can I access and run command to Raspberry Pi without ssh ? – Nhan Ly Apr 06 '14 at 09:32
  • 1
    Why are you not sending control commands over serial port? That would be the most intuitive. – László Papp Apr 06 '14 at 09:38

4 Answers4

1

Assuming you only need one-way communication, open the ssh connection with FILE *ssh = popen("ssh pi@192.168.1.5", "w") instead of system. That will give you a handle to write to, e.g. fprintf(ssh, "echo 0=%#d > /dev/servoblaster", 10);. The ssh connection is then avilable until you pclose(ssh); at some later point.

If you need to read back, you will need to open both sides of a pipe, which requires a "proper fork jobbie". You could perhaps start with this example, in that case.

fork() and pipes() in c

Community
  • 1
  • 1
Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
  • Your method and Sysem() method always take a time to make a ssh connection at beginning and then runs echo command (and this don't meet my demand), I want to keep a ssh connection and then run echo command via it. – Nhan Ly Apr 06 '14 at 11:51
  • Yes, so using `popen` will allow you to keep the `ssh` conection open for a longer period and send multiple commands. That's the point. I have edited the answer to make it clearer. – Mats Petersson Apr 06 '14 at 12:07
0

For sending simple control commands, ssh is an overkill to say the least.

People usually use the simple serial port for that. This can be done in several ways, using libraries like QtSerialPort or just go through the manual termios settings.

You could use "gserial" for getting serial port operation over the USB connection and use it as a regular serial port.

László Papp
  • 51,870
  • 39
  • 111
  • 135
0

I have not used it but it looks promising.

http://api.libssh.org/master/libssh_tutor_guided_tour.html

dhruvvyas90
  • 1,135
  • 1
  • 15
  • 31
0

I'm writing a C++ program to control servo motor by Raspberry Pi with command above, and how can I access and run command to Raspberry Pi without ssh ?

You can access and run commands to control servo motor by Raspberry Pi, using REST API provided by WebIOPi.