Poll
Question: arduino max msp help help
max msp, codeing - 0 (0%)
max msp led light code - 0 (0%)
Total Voters: 0

Pages: [1] 2 3   Go Down
Author Topic: Make Arduino execute commands on computer?  (Read 4301 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 42
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello,
I was wondering if there is some way that I can have my Arduino Duemilanove execute commands on my computer (which is running Ubuntu 8.04.1 amd64).

I think I've got down the parts dealing with how to handle input, so I don't think I need help there. I was just wondering if there is some way to have the Arduino, for example, run a command when a button attached to it is pressed, but have the command execute on the computer.
Logged

Bristol, UK
Offline Offline
Edison Member
*
Karma: 0
Posts: 1197
Exhibitor at UK Maker Faire
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

The Arduino's processor cannot directly invoke commands on the host (whether it's Linux, Windows or MacOS).  But, a program running on the host can accept characters over the serial connection (via USB), which can then trigger actions on the host such as, running commands.

This kind of program on the host is sometimes called a proxy.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 42
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for the response. I looked around after I saw your post, and I can't seem to find any clear tutorials for this.

I downloaded the proxy software for Linux from the Arduino website, but I don't know how to use it to execute commands on my computer. If I could even get enough information to run one command when a button connected to the Arduino is pressed, I think I'd be set.

Any links to good tutorials for this?


Thanks,
computer_freak_8
Logged

0
Offline Offline
Faraday Member
**
Karma: 7
Posts: 2526
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

A quick and very dirty method would be to redirect bash's input from the Arduino's serial port, e.g.

bash$ bash -v  </dev/tty.USB0

the -v option will make bash echo the commands being executed.

-j

Logged

England
Offline Offline
Newbie
*
Karma: 0
Posts: 44
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I was wondering if there is some way that I can have my Arduino Duemilanove execute commands on my computer (which is running Ubuntu 8.04.1 amd64).
Can't help much on the Linux specifics, but one preliminary is to sort out exactly what commands you want to issue. That is independent of getting that command triggered somehow.
If you can't do what you want from the Ubuntu command line, then I can't see how you can get it done from an external trigger.

Then you need a program running on your Ubuntu machine that can
a - get stuff from the serial port (ie the Arduino)
b - if needed, interpret that stuff
and c - issue the appropriate command
This means a bit of programming in a some sort of 'proper' language.
For all I know, you might even be able to achieve what you want entirely with a shell script...
But it seems PERL might be a better choice than PHP -
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1208451736/6#6


The Mac has AppleScript, which allows messages to be passed to running programs -- as well as starting or quitting programs.
Massimo Banzi himself has released a miniature Mac app that takes individual characters from a serial port (ie an Arduino) and uses them to trigger whichever of the AppleScripts written by the user
http://tinker.it/now/2007/04/26/control-your-mac-from-arduino-the-easy-way/

More generally for Mac, SerialPort X makes serial ports (like the one hooked up to the Arduino) fully available to AppleScript.
http://mysite.verizon.net/vzenuoqe/MacSoft.html
And I'm sure there are lots of other tools available!

Does Ubuntu have an analogue of AppleScript? Sorry, I don't know of one.


Naturally, you also need to have an Arduino setup to send the expected data or commands up the wire to the computer. (Beware the unexpected!)
Like I said, sorry I can't help with the Linux detail, but hopefully I've outlined the sort of thing you need to be aiming for!
Logged

0
Offline Offline
Sr. Member
****
Karma: 0
Posts: 430
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I would echo what Anachrocomputer and kg4wsv suggested smiley

You can use Perl and Device::SerialPort to communicate with your arduino and then system("$command"); based on what the arduino sends on the serial port

You can do the same thing with Python and a variety of other languages

Code:
#!/usr/bin/perl

use Device::SerialPort;

my $port = Device::SerialPort->new("/dev/ttyUSB0");
$port->databits(8);
$port->baudrate(9600);
$port->parity("none");
$port->stopbits(1);

$number_of_chars_to_read=1;

$command0="mplayer video.ogv";

$command1="mplayer aud.mp3";

$time="1";

while(my $code=$port->read($number_of_chars_to_read))
{

print "$code";

system("$command0") if($code eq "a");
system("$command1") if($code eq "b");

}

Code:
void setup()
{
  Serial.begin(9600);

}

void loop()
{

// logic

...
...
...

int char = 97;
Serial.print(char);

// prints the ASCII string "97" which is a lowercase "a".

...
...
...

// logic

}

}



This is pseudocode you have to modify it a little

smiley
« Last Edit: January 11, 2009, 07:34:43 pm by gnu_linux » Logged

0
Offline Offline
God Member
*****
Karma: 0
Posts: 511
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I like kg4wsvs suggestion, just do it in bash if you don't mind being linux specific.  It is very powerful once you get the hang of it, you could even:

1. create a file that lists the commands you expect from the arduino and the linux commands to execute when receiving them.

2. read a command from the arduino, and execute the corresponding linux command. (grep & cut).

And you can do the command processing all on like one line after you create the command map file smiley

http://www.arachnoid.com/linux/shell_programming.html

Logged

0
Offline Offline
Sr. Member
****
Karma: 0
Posts: 430
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Shell is very powerful and a great tool but when you have an ATmega168 with 14k usable flash and 1K RAM you are limited in what you can do

Having the arduino put together potentially long complex command lines and send them back to the computer is going to require some fairly complex C code and potentially a very big and complex command table

I think it would be much simpler and much less error prone to send a single ASCII character ( alpha or digit ) to indicate which button was pressed and then your higher level script, be it Perl, Python, Shell, PHP or what have you can take that input and execute the appropriate command on the Linux PC itself

Besides it is a lot easier to work with the command mapping on the Linux PC itself, rather than having to reprogram the arduino every single time you want to change the command

Would you prefer vi my_script.$ext or gedit my_script.$ext or arduino, edit, compile, upload, edit, compile, upload, edit, compile, upload, edit, compile, upload, edit, compile, upload... ?

Having been a Linux person for a long time I can easily imagine a situation where one of my command lines could be much bigger than 1024 characters which would overflow the RAM on the ATmega168

My advice, keep it simple on the Arduino side and move all the complexity to the Host PC side

Perl, Python and PHP are cross platform so your script would work on any host system ( Linux, Unix, BSD, MacOS X, win*****, etc ) with some changes to the commands of course

smiley
Logged

0
Offline Offline
Sr. Member
****
Karma: 0
Posts: 430
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This is how I envision this working

Code:
Button        char        lang               Linux_or_Unix_cmd         action

Button0 -> '0' -> high_level_lang -> system("$command0"); -> play video0

Button1 -> '1' -> high_level_lang -> system("$command1"); -> play song0

Button2 -> '2' -> high_level_lang -> system("$command2"); -> play song1

Button3 -> '3' -> high_level_lang -> system("$command3"); -> play video1

Button4 -> '4' -> high_level_lang -> system("$command4"); -> launch app0


smiley
« Last Edit: January 11, 2009, 11:58:42 pm by gnu_linux » Logged

0
Offline Offline
Sr. Member
****
Karma: 0
Posts: 430
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

There's no doubt that what kg4wsv suggested in indeed very clever, powerful and cool smiley

However it is more flexible to have the arduino signal which button was pressed and nothing else, that way you can move the arduino from a Linux PC to a Solaris box or BSD system or a Mac or even a win****** PC without having to make any modifications to the arduino itself, each host system would have its own version of the scripting language with its command mapping

If you use a cross platform language your code would be 99% + identical on all Unix systems, even on win****** the code changes would be fairly minor

In response to that_chap about AppleScript, I am not familiar with it, but Linux has bash which is the default shell on most distributions, in addition to the command line shell Linux also has a variety of scripting languages such as the ones mentioned earlier ( Perl, Python, PHP, Ruby, etc )

Other shells like ksh, tcsh, csh are also available

These shells are also available on BSD and most Unix systems

Since MacOS X is based on BSD it has inherited some of this functionality as well

Linux and Unix shells can also run on win****** via cygwin

smiley
Logged

England
Offline Offline
Newbie
*
Karma: 0
Posts: 44
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Agree that if you can do it in a Linux/UNIX shell (or PHP/Python/Ruby/whatever) then you should have something that would run with minimal modification on Mac.



There were two points about AppleScript
1/ its relatively easy
2/ its capable of finer interaction than most of the application command line options I've come across. Its even two-way. Mac apps can reply to (hence control) your script, perhaps even passing calculated parameters back to the Arduino.
http://www.apple.com/applescript/firsttutorial/18.html
And with (commercial) additions, AppleScript has simplified access to all elements of most apps' GUI - from a script! (Apple does provide a similar free, but for once less easy, access.) See http://prefabsoftware.com/uibrowser/
This would allow the Arduino to, for example, 'click' any specified button in any dialog box within the application.

Regarding another recent thread, sadly OpenOffice doesn't (not yet anyway) talk AppleScript.
Logged

0
Offline Offline
Sr. Member
****
Karma: 0
Posts: 430
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Perl has an interface to the GTK API so it can interact with the GUI smiley

Other languages do as well

I cannot comment on how that compares to AppleScript since I haven't used it

smiley
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 42
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Wow, I've gotten a lot of responses since yesterday! Thank you all for all the good ideas. I'm going to play around a little bit with what you all have given me, and then report back later.

Thanks again,
computer_freak_8
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 42
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Well, I don't think I've gotten very far yet...

@gnu_linux:
I know that you said I will need to edit it, but I figured that was just in reference to the commands I wanted it to run.

After a bit of investigation, (using Perl's debugger and commenting various things out,) I realized I have the following errors coming up with the code:
Code:
$port->databits(8);
$port->baudrate(9600);
$port->parity("none");
$port->stopbits(1);
all yeild an error in the form of:
Quote
Can't call method "xxxx" on an undefined value at ./trial001.pl line xx.
where xx is the line number, and xxxx is one of the following: databits, baudrate, parity, stopbits.

Oh, and I had another issue which was resolved by running:
Code:
sudo apt-get install libdevice-serialport-perl
(Which installed the serial device support for Perl.)


Any ideas on how to fix this?

@that_chap:
Quote
[...] one preliminary is to sort out exactly what commands you want to issue. That is independent of getting that command triggered somehow.
If you can't do what you want from the Ubuntu command line, then I can't see how you can get it done from an external trigger.
I get what you're saying. I can run them from the command line, and I'm confident I can get the triggering worked out (that is, waiting for a button press, or something similar). For discussion's sake, lets say the commands I want to use are:
Code:
vlc /home/username/file1.wav
fah6
init 6
/home/username/turn-on-wifi-hotspot.sh

@kg4wsv:
Thanks for that, and it will probably make a good temporary fix. I'll have to try it.

@dcb:
I'm working on reading that page you linked to.


Thanks again, all!
Logged

0
Offline Offline
Sr. Member
****
Karma: 0
Posts: 430
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Did you call Device::SerialPort->new first?

Code:
my $port = Device::SerialPort->new("/dev/ttyUSB0");

The "my" is optional

You'll need to replace /dev/ttyUSB0 with the serial port your arduino is attached to, dmesg will show the port used by the arduino shortly after you plug it in


Code:

$command0="/usr/bin/vlc /home/username/file1.wav";
$command1="/dir/fah6";
$command2="/sbin/init 6";
$command3="/home/username/turn-on-wifi-hotspot.sh";

system("$command0") if($button eq "a");
system("$command1") if($button eq "b");

...
...
...


Some minor modifications may be required

smiley
Logged

Pages: [1] 2 3   Go Up
Jump to: