Thank you for your donation!


Cloudsmith graciously provides open-source package management and distribution for our project.


enable gpio output when boot is complete
#11
(01-07-2024, 09:23 PM)Nutul Wrote:
(01-07-2024, 09:09 PM)Wallyboy Wrote: Can I run them as root within a script file?  Not sure how to do that.
Just prepend sudo to the command; although that rings a bell... about the "echo" thing...
Maybe it's better you include the three commands in a shell script, and then invoke the script with:

sudo ./yourscript.sh

although I am not sure if that would wait for the password... maybe making "root" the script owner, and then setting its sticky flag would have better chances of running.

I'm having no luck with the sh script.  I would rather use a python script but I cannot figure out how to do the moodeutl test as described above..  I have attached the code snippet below as suggested for the sh script.  Anyone know how to implement this in python? 
Code:
until test $(moodeutl -q "select value from cfg_system where param='wrkready'") = "1"; do
Reply
#12
(01-08-2024, 09:18 PM)Wallyboy Wrote:
(01-07-2024, 09:23 PM)Nutul Wrote:
(01-07-2024, 09:09 PM)Wallyboy Wrote: Can I run them as root within a script file?  Not sure how to do that.
Just prepend sudo to the command; although that rings a bell... about the "echo" thing...
Maybe it's better you include the three commands in a shell script, and then invoke the script with:

sudo ./yourscript.sh

although I am not sure if that would wait for the password... maybe making "root" the script owner, and then setting its sticky flag would have better chances of running.

I'm having no luck with the sh script.  I would rather use a python script but I cannot figure out how to do the moodeutl test as described above..  I have attached the code snippet below as suggested for the sh script.  Anyone know how to implement this in python? 
Code:
until test $(moodeutl -q "select value from cfg_system where param='wrkready'") = "1"; do

Not in this very moment; but it is a program call, something in the lines of sysCall("moodeutl -q ... ") = "1", or exec(" ... ")...

wait a sec:

import subprocess

subprocess.run(["ls", "-l"])

1st element in the array is the program/command, and further ones are the arguments to such program.
Reply
#13
(01-08-2024, 09:46 PM)Nutul Wrote:
(01-08-2024, 09:18 PM)Wallyboy Wrote:
(01-07-2024, 09:23 PM)Nutul Wrote:
(01-07-2024, 09:09 PM)Wallyboy Wrote: Can I run them as root within a script file?  Not sure how to do that.
Just prepend sudo to the command; although that rings a bell... about the "echo" thing...
Maybe it's better you include the three commands in a shell script, and then invoke the script with:

sudo ./yourscript.sh

although I am not sure if that would wait for the password... maybe making "root" the script owner, and then setting its sticky flag would have better chances of running.

I'm having no luck with the sh script.  I would rather use a python script but I cannot figure out how to do the moodeutl test as described above..  I have attached the code snippet below as suggested for the sh script.  Anyone know how to implement this in python? 
Code:
until test $(moodeutl -q "select value from cfg_system where param='wrkready'") = "1"; do

Not in this very moment; but it is a program call, something in the lines of sysCall("moodeutl -q ... ") = "1", or exec(" ... ")...

wait a sec:

import subprocess

subprocess.run(["ls", "-l"])

1st element in the array is the program/command, and further ones are the arguments to such program.

I tried that previously and the query does not seem to be recognized using : subprocess.Popen (["/usr/local/bin/moodeutl", " -q","select value from cfg_system where param='wrkready'"])
Code:
sudo nano boot_indicator.py
pi@pi:/usr/local/bin $ sudo ./boot_indicator.py
Usage: moodeutl [OPTION]
Moode utility programs

With no OPTION print the help text and exit.

-a        Print features availability
-A [add|rm N]     Update features availability
-d        Dump session file (requires sudo)
-D [var name]     Delete session variable (requires sudo)
-e [filename]    Export settings to [filename] or ~/moodecfg-<timestamp>.ini (requires sudo)
-i        Import settings from /boot/moodecfg.ini (requires sudo)
-f        Print supported audio formats
-F        Print full alsacap info
-l        Print moode log
-m        Run system monitor
-s        Print system info
-c        Detect CIFS (SMB) hosts
-C        Detect CIFS (SMB) shares [host or ip address]
-N        Detect NFS hosts
-M        Detect MPD hosts
-o        List audio overlays
-q        Query sql database
-r        Restart servers
-R        Restart renderer [--bluetooth | --airplay | --spotify | --squeezelite | --roonbridge]
-t        Print last 10 lines of moode log and wait
-u        Check radio station urls for playability
-U        Check station urls, list only unplayable
--hwparams    Print ALSA hardware params
--btreset    Reset Bluetooth to off
--mpdlog    Print MPD log
--updlog    Print in-place update log
--mountmonlog    Print mount monitor log
--autocfglog    Print auto config update log
--sleepint    Print daemon sleep intervals
--mooderel    Print moOde release
--pirev    Print Pi revision
--osinfo    Print OS info
--version    Print program version
--help        Print this help text
pi@pi:/usr/local/bin $
Reply
#14
(01-08-2024, 10:17 PM)Wallyboy Wrote:
(01-08-2024, 09:46 PM)Nutul Wrote:
(01-08-2024, 09:18 PM)Wallyboy Wrote:
(01-07-2024, 09:23 PM)Nutul Wrote:
(01-07-2024, 09:09 PM)Wallyboy Wrote: Can I run them as root within a script file?  Not sure how to do that.
Just prepend sudo to the command; although that rings a bell... about the "echo" thing...
Maybe it's better you include the three commands in a shell script, and then invoke the script with:

sudo ./yourscript.sh

although I am not sure if that would wait for the password... maybe making "root" the script owner, and then setting its sticky flag would have better chances of running.

I'm having no luck with the sh script.  I would rather use a python script but I cannot figure out how to do the moodeutl test as described above..  I have attached the code snippet below as suggested for the sh script.  Anyone know how to implement this in python? 
Code:
until test $(moodeutl -q "select value from cfg_system where param='wrkready'") = "1"; do

Not in this very moment; but it is a program call, something in the lines of sysCall("moodeutl -q ... ") = "1", or exec(" ... ")...

wait a sec:

import subprocess

subprocess.run(["ls", "-l"])

1st element in the array is the program/command, and further ones are the arguments to such program.

I tried that previously and the query does not seem to be recognized using : subprocess.Popen (["/usr/local/bin/moodeutl", " -q","select value from cfg_system where param='wrkready'"])
Code:
sudo nano boot_indicator.py
pi@pi:/usr/local/bin $ sudo ./boot_indicator.py
Usage: moodeutl [OPTION]
Moode utility programs

With no OPTION print the help text and exit.

-a        Print features availability
-A [add|rm N]     Update features availability
-d        Dump session file (requires sudo)
-D [var name]     Delete session variable (requires sudo)
-e [filename]    Export settings to [filename] or ~/moodecfg-<timestamp>.ini (requires sudo)
-i        Import settings from /boot/moodecfg.ini (requires sudo)
-f        Print supported audio formats
-F        Print full alsacap info
-l        Print moode log
-m        Run system monitor
-s        Print system info
-c        Detect CIFS (SMB) hosts
-C        Detect CIFS (SMB) shares [host or ip address]
-N        Detect NFS hosts
-M        Detect MPD hosts
-o        List audio overlays
-q        Query sql database
-r        Restart servers
-R        Restart renderer [--bluetooth | --airplay | --spotify | --squeezelite | --roonbridge]
-t        Print last 10 lines of moode log and wait
-u        Check radio station urls for playability
-U        Check station urls, list only unplayable
--hwparams    Print ALSA hardware params
--btreset    Reset Bluetooth to off
--mpdlog    Print MPD log
--updlog    Print in-place update log
--mountmonlog    Print mount monitor log
--autocfglog    Print auto config update log
--sleepint    Print daemon sleep intervals
--mooderel    Print moOde release
--pirev    Print Pi revision
--osinfo    Print OS info
--version    Print program version
--help        Print this help text
pi@pi:/usr/local/bin $

try removing the spave before -q, so that is reads "-q", and not " -q"
Reply
#15
(01-08-2024, 10:31 PM)Nutul Wrote:
(01-08-2024, 10:17 PM)Wallyboy Wrote:
(01-08-2024, 09:46 PM)Nutul Wrote:
(01-08-2024, 09:18 PM)Wallyboy Wrote:
(01-07-2024, 09:23 PM)Nutul Wrote: Just prepend sudo to the command; although that rings a bell... about the "echo" thing...
Maybe it's better you include the three commands in a shell script, and then invoke the script with:

sudo ./yourscript.sh

although I am not sure if that would wait for the password... maybe making "root" the script owner, and then setting its sticky flag would have better chances of running.

I'm having no luck with the sh script.  I would rather use a python script but I cannot figure out how to do the moodeutl test as described above..  I have attached the code snippet below as suggested for the sh script.  Anyone know how to implement this in python? 
Code:
until test $(moodeutl -q "select value from cfg_system where param='wrkready'") = "1"; do

Not in this very moment; but it is a program call, something in the lines of sysCall("moodeutl -q ... ") = "1", or exec(" ... ")...

wait a sec:

import subprocess

subprocess.run(["ls", "-l"])

1st element in the array is the program/command, and further ones are the arguments to such program.

I tried that previously and the query does not seem to be recognized using : subprocess.Popen (["/usr/local/bin/moodeutl", " -q","select value from cfg_system where param='wrkready'"])
Code:
sudo nano boot_indicator.py
pi@pi:/usr/local/bin $ sudo ./boot_indicator.py
Usage: moodeutl [OPTION]
Moode utility programs

With no OPTION print the help text and exit.

-a        Print features availability
-A [add|rm N]     Update features availability
-d        Dump session file (requires sudo)
-D [var name]     Delete session variable (requires sudo)
-e [filename]    Export settings to [filename] or ~/moodecfg-<timestamp>.ini (requires sudo)
-i        Import settings from /boot/moodecfg.ini (requires sudo)
-f        Print supported audio formats
-F        Print full alsacap info
-l        Print moode log
-m        Run system monitor
-s        Print system info
-c        Detect CIFS (SMB) hosts
-C        Detect CIFS (SMB) shares [host or ip address]
-N        Detect NFS hosts
-M        Detect MPD hosts
-o        List audio overlays
-q        Query sql database
-r        Restart servers
-R        Restart renderer [--bluetooth | --airplay | --spotify | --squeezelite | --roonbridge]
-t        Print last 10 lines of moode log and wait
-u        Check radio station urls for playability
-U        Check station urls, list only unplayable
--hwparams    Print ALSA hardware params
--btreset    Reset Bluetooth to off
--mpdlog    Print MPD log
--updlog    Print in-place update log
--mountmonlog    Print mount monitor log
--autocfglog    Print auto config update log
--sleepint    Print daemon sleep intervals
--mooderel    Print moOde release
--pirev    Print Pi revision
--osinfo    Print OS info
--version    Print program version
--help        Print this help text
pi@pi:/usr/local/bin $

try removing the spave before -q, so that is reads "-q", and not " -q"

Thank you.  That worked.  It returns a "1" to the console .  Could you suggest how I would code testing the result within the python script.
Reply
#16
Just like it is done in the script:

until (subprocess.popen( ... ) == "1")

or == 1

depending on the subsystem.popen return type (string/char vs int), I am not proficient in Python...
Reply
#17
(01-09-2024, 09:54 AM)Nutul Wrote: Just like it is done in the script:

until (subprocess.popen( ... ) == "1")

or == 1

depending on the subsystem.popen return type (string/char vs int), I am not proficient in Python...

I know this looks like a hack job (I'm not a coder by any stretch) but it works.  Any suggestions on how to make it more elegant?
Code:
#!/usr/bin/env python

import RPi.GPIO as GPIO
import os
import subprocess


GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(16, GPIO.OUT)

result = subprocess.run (["/usr/local/bin/moodeutl", "-q","select value from cfg_system where param='wrkready'"],capture_output=True,text=True )
x = (result.stdout)
y = int(x)
print (y)

if y == 1:
       GPIO.output(16,True)
else:
       GPIO.output(16,False)
Reply
#18
(01-09-2024, 10:01 PM)Wallyboy Wrote: Try these small changes, cosmetics...
Code:
#!/usr/bin/env python

import RPi.GPIO as GPIO
import os
import subprocess

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(16, GPIO.OUT)

boot_complete = int(subprocess.run(["/usr/local/bin/moodeutl", "-q", "select value from cfg_system where param='wrkready'"], capture_output=True, text=True).stdout)
print(boot_complete)

if boot_complete == 1:
       GPIO.output(16, True)
else:
       GPIO.output(16, False)

I recommend you keep your file safe, then try my changes, and keep them ONLY if they work (I might have introduced typos, or simply the Python engine my changes might not like my code shrinking...)

You can also try this, instead of the IF/ELSE:

Code:
GPIO.output(16, boot_complete == 1 ? True : False)

And remove the print(boot_complete) if you like, or comment it by adding a # at the beginning of the line, with no spaces before nor after, so that the line reads

Code:
#print(boot_complete)

That's it.

Good to hear you managed to accomplish then task, and that's what's important.
Reply
#19
(01-10-2024, 02:07 AM)Nutul Wrote:
(01-09-2024, 10:01 PM)Wallyboy Wrote: Try these small changes, cosmetics...
Code:
#!/usr/bin/env python

import RPi.GPIO as GPIO
import os
import subprocess

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(16, GPIO.OUT)

boot_complete = int(subprocess.run(["/usr/local/bin/moodeutl", "-q", "select value from cfg_system where param='wrkready'"], capture_output=True, text=True).stdout)
print(boot_complete)

if boot_complete == 1:
       GPIO.output(16, True)
else:
       GPIO.output(16, False)

I recommend you keep your file safe, then try my changes, and keep them ONLY if they work (I might have introduced typos, or simply the Python engine my changes might not like my code shrinking...)

You can also try this, instead of the IF/ELSE:

Code:
GPIO.output(16, boot_complete == 1 ? True : False)

And remove the print(boot_complete) if you like, or comment it by adding a # at the beginning of the line, with no spaces before nor after, so that the line reads

Code:
#print(boot_complete)

That's it.

Good to hear you managed to accomplish then task, and that's what's important.

Thanks so much for your help.  I implemented your changes and it works perfectly.  A much more elegant solution

Kent
Reply


Forum Jump: