runShellCommand()

Hi,

I want to run a python script via bridge with "runShellCommand()" command.

The arduino line in my sketch is:
Send.runShellCommand("python /mnt/sd/arduino/sendDT4.py " + EMS_SERVER_IP + " " + EMS_PORT);

The python script:

!/usr/bin/python

UDP_IP = sys.argv[1]  
UDP_PORT = int(sys.argv[2])
print UDP_IP
print UDP_PORT

file = open('addDT4ToStack.txt','r')
temp = file.read()
file.close()

print "that line isn't printed"

The problem is when i'm runing this scrip from linux shell everything works fine, but when i'm runing the same script with arduino sketch i see just 2 first "print" commands of the script insted of 3 prints.

I pretty sure that this:

file = open('addDT4ToStack.txt','r')
temp = file.read()
file.close()

is the problem

It may not be finding the file to open, and stopping the script because of it. Besides the first two lines printing out, are you getting any error messages?

  • When you run it from the Linux shell, I assume you are running it from the directory where the file exists. It finds the file and runs properly.
  • When you run it from the sketch, it's probably not running from the same directory, so it can't find the file.

Perhaps you can change the open command to include the full path name to the file?

file = open('/mnt/sd/arduino/addDT4ToStack.txt','r')

@vicmerlis,

Is this your entire script? I see typos in it - namely

!/usr/bin/python

it should be:

[b]#[/b]!/usr/bin/python

Also, ShapeShifters suggestion is likely the thing to try.

Jesse

jessemonroy650:

[b]#[/b]!/usr/bin/python

Good catch. Although in this case it may not make a huge difference, since he's explicitly calling Python to process the script.

The hash-bang on the first line is there to tell the command line interpreter which program it should call to run the script. But that's really only important when the script file is set to be executable and you're running the script by just typing its name.

Thanks for the help.

about the # i just didn't print it (it was there).

ShapeShifter you right about the directory, i ran os.system("pwd") command and the output was:
/usr/lib/python2.7/bridge

i tried three things:
first that didn't work: (tried your solution)

file = open('/mnt/sd/arduino/addDT4ToStack.txt','r')

second one didn't work: (tried to change the current directory to mnt/sd/arduino directory)

os.system("cd /mnt/sd/arduino")
  • look like that the directory refuse the be changed.

the solution was -> copy addDT4ToStack.txt file to /usr/lib/python2.7/bridge directory :\

vicmerlis:
the solution was -> copy addDT4ToStack.txt file to /usr/lib/python2.7/bridge directory :\

That doesn't sound like a very good solution. :frowning:

I wonder why the absolute path and CD didn't work? Either of those would be much more appropriate.

/mnt/sd/ is a logical link that is created when an SD card with an /arduino/ folder at the root is found. The card is actually at /mnt/sda1/. Clutching at straws, maybe Python isn't liking the link to the disk? I wonder how the file open or cd would work using the physical path /mnt/sda1/arduino/...?

ShapeShifter:
That doesn't sound like a very good solution. :frowning:

I wonder why the absolute path and CD didn't work? Either of those would be much more appropriate.

/mnt/sd/ is a logical link that is created when an SD card with an /arduino/ folder at the root is found. The card is actually at /mnt/sda1/. Clutching at straws, maybe Python isn't liking the link to the disk? I wonder how the file open or cd would work using the physical path /mnt/sda1/arduino/...?

/mnt/sda1/arduino/ also not working :\

You should be catching your exceptions so you can actually find out what the problem is.

try: 
    file = open('mnt/sd/arduino/addDT4ToStack.txt','r')
except IOError as e:
    print 'cannot open ({0}): {1}'.format(e.errno, e.strerror)

Chagrin:
You should be catching your exceptions so you can actually find out what the problem is.
::::SNIP::::

@vicmerlis,
I did something evil. I ran your code. It says, "NameError: name 'sys' is not defined"
errr.... Where is the rest of your code?

Jesse

jessemonroy650:
@vicmerlis,
I did something evil. I ran your code. It says, "NameError: name 'sys' is not defined"
errr.... Where is the rest of your code?

Jesse

#!/usr/bin/python
import socket
import sys
import os

UDP_IP = sys.argv[1]  
UDP_PORT = int(sys.argv[2])
NUM_OF_ITTER = int(sys.argv[3])

try: 
    file = open('/mnt/sda1/arduino/addDT4ToStack.txt','r')
except IOError as e:
    print 'cannot open ({0}): {1}'.format(e.errno, e.strerror)
temp = file.read()
file.close()

MESSAGE = temp.split(';')

for i in range(0,NUM_OF_ITTER):
	print "DT4 number " + str(i+1) + " was sent"
	temp = MESSAGE[i].split(',')
	temp = map(int, temp)
	
	sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # UDP
	sock.sendto(bytearray(temp), (UDP_IP, UDP_PORT))
	sock.close()

:smiley:

Interesting code. maybe could you post some data so I could try your program? :slight_smile:

Jesse