Page 1 of 2

python script to lua

Posted: Saturday 09 December 2017 16:52
by edwin1234
can someone help me to convert this pyhon script to a lua script because the script works in the command line but not in domoticz on action.
treid everything


this is the python script:

#!/usr/bin/python3

#import libraries
import os
import time

# Start the servo motors
os.system('sudo /home/pi/PiBits/ServoBlaster/user/servod')

# turn servo 0 to 10% position
os.system("echo 7=10% > /dev/servoblaster")
time.sleep(2.5)

# turn servo 0 to 60% position
os.system("echo 7=50% > /dev/servoblaster")
time.sleep(3)

os.system("echo 7=10% > /dev/servoblaster")
time.sleep(2)

os.system("echo 7=100% > /dev/servoblaster")
time.sleep(1)

os.system("echo 7=10% > /dev/servoblaster")
time.sleep(2)


# Stop the servo motors
os.system('sudo killall servod')

Re: python script to lua

Posted: Saturday 09 December 2017 17:04
by jvdz
I am doing something similar but use a bash script in the On action, which then shells another Bash script.
Main reason for this approach was to avoid hanging the event system while the action was running.
You could use a similar approach and shell a python script by just updating the below code.
The last & ensures the script returns immediately leaving the second script running in the background.

Code: Select all

#!/bin/bash
#--Check if script is already running and only start it when it isn't running yet.
chk=`sudo ps x | grep "Blinds_Set.sh 0" | grep -cv grep`
if  [ "$chk" = "0" ] ; then
	sh /home/pi/domoticz/scripts/Blinds_Set.sh  >> /var/tmp/blinds.log 2>&1 &
#--else
#--	echo "$(date +%x) $(date +%X) Blinds_Set.sh is already running" >> /var/tmp/blinds.log
fi
Jos

Re: python script to lua

Posted: Saturday 09 December 2017 17:19
by edwin1234
i tried to run the python script trough a bash script but it didnt work,
in comend line it did
thats why i want it in a lua script. but i dont know how to do it :oops:

Re: python script to lua

Posted: Saturday 09 December 2017 17:48
by jvdz
How does the commandline you want to start look exactly?
Do you use the fully qualified path for the python file?

Jos

Re: python script to lua

Posted: Saturday 09 December 2017 18:25
by edwin1234
I used for on script:///home/domoticz/scripts/python/servoscript.py

And also tried script:///home/domoticz/scripts/servoscript.sh

And
Script://scripts/python/servoscript.py

Re: python script to lua

Posted: Saturday 09 December 2017 18:44
by jvdz
No ... I mean what is the commandline you used in the Bash file that isn't working and the commandline that is working?
Also: what is the content of script: script:///home/domoticz/scripts/servoscript.sh ?

Jos

Re: python script to lua

Posted: Saturday 09 December 2017 19:26
by edwin1234
Content is:
#!/bin/bash

python3 /home/pi/domoticz/scripts/python/servoscript.py




exit 0

From the command line i do: python servoscript.py this works
And : ./servoscript.sh also works from the commandline

Re: python script to lua

Posted: Saturday 09 December 2017 19:31
by jvdz
Did you set the x attribute for them to allow them to be executed?
Don't forget to add the & to the end of the command to avoid hanging the event system for the duration of the script.

Jos

Re: python script to lua

Posted: Saturday 09 December 2017 19:37
by edwin1234
I did chmod +x
And tried sudo chmod +x

Re: python script to lua

Posted: Saturday 09 December 2017 19:46
by jvdz
So what happens when you have this in /home/domoticz/scripts/servoscript.sh:

Code: Select all

#!/bin/bash
python3 /home/pi/domoticz/scripts/python/servoscript.py >> /var/tmp/test.log 2>&1 &
exit 0
.. and you check the logfile for any feeedback? : /var/tmp/test.log
Change the path for the logfile to anything your system supports.
And use the On Action: script:///home/domoticz/scripts/servoscript.sh

Jos

Re: python script to lua

Posted: Saturday 09 December 2017 19:54
by febalci
Did you tried:
script:///usr/bin/python3 /home/domoticz/scripts/python/servoscript.py (assuming python3 is under /usr/bin)

Also the same goes for bash:
#!/bin/bash
/usr/bin/python3 /home/pi/domoticz/scripts/python/servoscript.py

And you better put your bash file under:
/home/domoticz/scripts/bash/

Additionally maybe there could be something wrong with:
os.system('sudo /home/pi/PiBits/ServoBlaster/user/servod')

It might require user input like password??

Re: python script to lua

Posted: Saturday 09 December 2017 20:53
by edwin1234
Thanks for your help, i will try it tomorow,
I am at a party now 😃

Re: python script to lua

Posted: Sunday 10 December 2017 7:14
by edwin1234
hi jvz
script:///home/domoticz/scripts/servoscript.sh
Gives this in the domoticz log:
Error: Error executing script command (/usr/bin/python3). returned: -1

and in /var/tmp/test.log i get nothing

i just dont get it if i ssh into the pi and execute the script it works perfectly

Re: python script to lua

Posted: Sunday 10 December 2017 10:11
by edwin1234
hi jvdz

i did what you asked, i get no results in the log file with the on action.
if i ssh in to the pi and execute servoscript.sh there
i get this in var/tmp/test.log:

Board model: 1
GPIO configuration: P1 (40 pins)
Using hardware: PWM
Using DMA channel: 14
Idle timeout: Disabled
Number of servos: 8
Servo cycle time: 20000us
Pulse increment step size: 10us
Minimum width value: 50 (500us)
Maximum width value: 250 (2500us)
Output levels: Normal

Using P1 pins: 7,11,12,13,15,16,18,22

Servo mapping:
0 on P1-7 GPIO-4
1 on P1-11 GPIO-17
sorry if i dont understand you but iam new to linux and scripting

Re: python script to lua

Posted: Sunday 10 December 2017 10:13
by jvdz
edwin1234 wrote: Sunday 10 December 2017 7:14 Error: Error executing script command (/usr/bin/python3). returned: -1
Are you sure you did put this in there: "script:///home/domoticz/scripts/servoscript.sh"
..and not "script:///usr/bin/python3 /home/domoticz/scripts/python/servoscript.py " like @febalci suggested?

Jos

Re: python script to lua

Posted: Sunday 10 December 2017 10:18
by jvdz
Another thing to check is whether the permissions are set correctly for both files!

Jos

Re: python script to lua

Posted: Sunday 10 December 2017 10:20
by edwin1234
no with youre sugestion i get this:
10:18:35.495 Error: Error executing script command (/home/pi/domoticz/scripts/servoscript.sh). returned: -1

Re: python script to lua

Posted: Sunday 10 December 2017 10:22
by edwin1234
must i do chmod +x for both files or
sudo chmod +x for both files?

Re: python script to lua

Posted: Sunday 10 December 2017 10:30
by jvdz
The "sudo chmod +x " will only set the execute flag.
To allow everybody access you could do : "sudo chmod 777" for both files.

Jos

Re: python script to lua

Posted: Sunday 10 December 2017 10:35
by edwin1234
i did sudo chmod 777 on both files
but still the same error in the domoticz log
Error: Error executing script command (/home/pi/domoticz/scripts/servoscript.sh). returned: -1