Nathan.Rivard
Posts: 35
Joined: Tue Sep 10, 2019 3:28 pm

Running .py file from boot using rc.local

Wed Oct 09, 2019 4:35 pm

I used the method of starting a program from boot used on https://www.dexterindustries.com/howto/ ... t-startup/, but afterward nothing occurs. Has anyone else ran into the same issues?

User avatar
B.Goode
Posts: 8905
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Running .py file from boot using rc.local

Wed Oct 09, 2019 4:52 pm

Nathan.Rivard wrote:
Wed Oct 09, 2019 4:35 pm
I used the method of starting a program from boot used on https://www.dexterindustries.com/howto/ ... t-startup/, but afterward nothing occurs. Has anyone else ran into the same issues?


afterward nothing occurs


'Nothing'? Does it fail to boot at all? Or just fail to behave as you expect?



Has anyone else ran into the same issues?

Since noone but you knows what you did, it seems unlikely anyone can give a useful answer?

Maybe share a copy of your modified version of /etc/rc.local ? And the corresponding python script.

hippy
Posts: 6104
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Running .py file from boot using rc.local

Wed Oct 09, 2019 5:23 pm

Nathan.Rivard wrote:
Wed Oct 09, 2019 4:35 pm
I used the method of starting a program from boot used on https://www.dexterindustries.com/howto/ ... t-startup/, but afterward nothing occurs.
Are you trying to run your program, or that program which uses espeak ?

If by "nothing occurs" you mean no speech is output that could be because there are ongoing issues with espeak (ALSA) and the way that program is doing it won't currently work.

Post your Python code and and your rc.local.

pcmanbob
Posts: 7140
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Running .py file from boot using rc.local

Wed Oct 09, 2019 5:41 pm

Using rc.local to run a program means it will be run in the background, which means there will be no output to the screen if that's what you mean by its not working.

if you want output on the screen from your program then you need to start your program after the gui has started, how exactly you do this will depend on how you program need to start, IE in a terminal window for example.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

epoch1970
Posts: 3804
Joined: Thu May 05, 2016 9:33 am
Location: Paris, France

Re: Running .py file from boot using rc.local

Wed Oct 09, 2019 6:04 pm

The guys at "Dexter Industries" made a boo-boo.
They profess using sudo within rc.local, but forget to mention sudoers should be modified in this case. Awww snap.

EDIT. Besides, rc.local is executed by root.
Last edited by epoch1970 on Wed Oct 09, 2019 8:11 pm, edited 1 time in total.
"S'il n'y a pas de solution, c'est qu'il n'y a pas de problème." Les Shadoks, J. Rouxel

Nathan.Rivard
Posts: 35
Joined: Tue Sep 10, 2019 3:28 pm

Re: Running .py file from boot using rc.local

Wed Oct 09, 2019 6:08 pm

Here is the python script

Code: Select all

import smbus
bus = smbus.SMBus()
#bus=smbus.SMBus(0)
import sys
sys.path.append('../')
#/home/pi/Desktop/pH_Files_and_Data/DFRobot_PH_Read.py
address=0x48
import time
ADS1115_REG_CONFIG_PGA_6_144V        = 0x00 # 6.144V range = Gain 2/3
ADS1115_REG_CONFIG_PGA_4_096V        = 0x02 # 4.096V range = Gain 1
ADS1115_REG_CONFIG_PGA_2_048V        = 0x04 # 2.048V range = Gain 2 (default)
ADS1115_REG_CONFIG_PGA_1_024V        = 0x06 # 1.024V range = Gain 4
ADS1115_REG_CONFIG_PGA_0_512V        = 0x08 # 0.512V range = Gain 8
ADS1115_REG_CONFIG_PGA_0_256V        = 0x0A # 0.256V range = Gain 16

from DFRobot_ADS1115 import ADS1115
from DFRobot_PH import DFRobot_PH

ads1115 = ADS1115()
ph      = DFRobot_PH()

ph.begin()
while True :
	#Read your temperature sensor to execute temperature compensation
    temperature = 25
	#Set the IIC address
    ads1115.setAddr_ADS1115(0x48)
	#Sets the gain and input voltage range.
    ads1115.setGain(ADS1115_REG_CONFIG_PGA_6_144V)
	#Get the Digital Value of Analog of selected channel
    adc0 = ads1115.readVoltage(0)
	#Convert voltage to PH with temperature compensation
    PH = ph.readPH(adc0['r'],temperature)

    VoltageArray = [4090, 3912, 3732, 3554, 3440, 3330, 3215]
    PHArray = [4, 5, 6, 7, 8, 9, 10]
    VoltageRead = adc0['r']
    xold = VoltageArray[0]

    for x in VoltageArray:
        xnew = x
        if(xold > VoltageRead > xnew):
            VoltageHigh = xold
            VoltageHighIndex = VoltageArray.index(VoltageHigh)

            VoltageLow = xnew
            VoltageLowIndex = VoltageArray.index(VoltageLow)

            break
        else:
            xold = x

    PHHigh = PHArray[VoltageHighIndex]
    PHLow = PHArray[VoltageLowIndex]
    phval = (((VoltageRead-VoltageLow)*(PHHigh-PHLow))/(VoltageHigh-VoltageLow))+PHLow

	#print "Temperature:%.1f ^C PH:%.2f" %(temperature,PH)
    #PH = abs(PH)
    #print("PH: " + str(PH))
    #print("A0:" + str(adc0['r']) + "mV")
    #print("phval: " + str(phval))
    #from os.path import join as pjoin
    #filename = "ph.csv"
    #path_to_file = pjoin("/media/pi/PHDATA/Ph_Data_Type/ph.csv" , "ph.csv")
    #File = open("/media/pi/PHDATA/Ph_Data_Type/ph.csv" , 'a')
    #File.write(str(time.asctime(time.localtime())) + "," + str(phval) + "," + str(adc0['r']) + "\n")
    #File.close()
    ph_file = open("ph.csv", 'a')
    ph_file.write(str(time.asctime(time.localtime())) + "," + str(phval) + "," + str(adc0['r']) + "\n")
    ph_file.close()

    time.sleep(60)
It's a heavily modified version of the DFRobot pH Master

Code: Select all

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

# Print the IP address
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
  printf "My IP address is %s\n" "$_IP"
fi
sudo python /home/pi/Desktop/pH_Files_and_Data/DFRobot_PH_Read.py &
exit 0
This is my rc.local

epoch1970
Posts: 3804
Joined: Thu May 05, 2016 9:33 am
Location: Paris, France

Re: Running .py file from boot using rc.local

Wed Oct 09, 2019 6:10 pm

As I said. Remove sudo. It is an interactive command and cannot run from rc.local.

EDIT. Besides, rc.local is executed by root.
Last edited by epoch1970 on Wed Oct 09, 2019 8:12 pm, edited 1 time in total.
"S'il n'y a pas de solution, c'est qu'il n'y a pas de problème." Les Shadoks, J. Rouxel

pcmanbob
Posts: 7140
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Running .py file from boot using rc.local

Wed Oct 09, 2019 6:27 pm

epoch1970 wrote:
Wed Oct 09, 2019 6:10 pm
As I said. Remove sudo. It is an interactive command and cannot run from rc.local.
rc.local is already running the program as root anyway so using sudo has no affect.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Nathan.Rivard
Posts: 35
Joined: Tue Sep 10, 2019 3:28 pm

Re: Running .py file from boot using rc.local

Wed Oct 09, 2019 6:28 pm

epoch1970 wrote:
Wed Oct 09, 2019 6:10 pm
As I said. Remove sudo. It is an interactive command and cannot run from rc.local.
I removed sudo and did a reboot and it successfully rebooted but the program did not begin

pcmanbob
Posts: 7140
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Running .py file from boot using rc.local

Wed Oct 09, 2019 6:34 pm

So looking at the code all it seems to do is read a sensor and write the results to a file ,
but I can see that your file open line does not have the full path included in the open command which means you file is probably not being written were you expect it ,
as rc.local will be operating in a different environment to the command line.

look at the example in the file open line you have commented out it is the correct way to do it with the full path along with the file name.

#File = open("/media/pi/PHDATA/Ph_Data_Type/ph.csv" , 'a') = correct

ph_file = open("ph.csv", 'a') = incorrect no path..............
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

User avatar
B.Goode
Posts: 8905
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Running .py file from boot using rc.local

Wed Oct 09, 2019 6:58 pm

Another issue to add to your list..

Code: Select all

sys.path.append('../')
#/home/pi/Desktop/pH_Files_and_Data/DFRobot_PH_Read.py
You will not end up at that location if you run this as the root user during startup.

hippy
Posts: 6104
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Running .py file from boot using rc.local

Wed Oct 09, 2019 8:18 pm

Code: Select all

sudo python /home/pi/Desktop/pH_Files_and_Data/DFRobot_PH_Read.py &
Try -

Code: Select all

/usr/bin/python2 /home/pi/Desktop/pH_Files_and_Data/DFRobot_PH_Read.py &
Or perhaps "python3".

Nathan.Rivard
Posts: 35
Joined: Tue Sep 10, 2019 3:28 pm

Re: Running .py file from boot using rc.local

Thu Oct 10, 2019 3:57 pm

hippy wrote:
Wed Oct 09, 2019 8:18 pm

Code: Select all

sudo python /home/pi/Desktop/pH_Files_and_Data/DFRobot_PH_Read.py &
Try -

Code: Select all

/usr/bin/python2 /home/pi/Desktop/pH_Files_and_Data/DFRobot_PH_Read.py &
Or perhaps "python3".
There is still no startup of the program upon boot

User avatar
B.Goode
Posts: 8905
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Running .py file from boot using rc.local

Thu Oct 10, 2019 4:05 pm

There is still no startup of the program upon boot


How do you know that?


And perhaps share copies of your modified versions of /etc/rc.local and /home/pi/Desktop/pH_Files_and_Data/DFRobot_PH_Read.py

Nathan.Rivard
Posts: 35
Joined: Tue Sep 10, 2019 3:28 pm

Re: Running .py file from boot using rc.local

Thu Oct 10, 2019 4:24 pm

B.Goode wrote:
Thu Oct 10, 2019 4:05 pm
There is still no startup of the program upon boot


How do you know that?


And perhaps share copies of your modified versions of /etc/rc.local and /home/pi/Desktop/pH_Files_and_Data/DFRobot_PH_Read.py
I check the .csv file that the code writes an output to and there is no update in the information on it.
The .py file is the same as what I posted above but I'll share what the rc.local is

Code: Select all

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

# Print the IP address
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
  printf "My IP address is %s\n" "$_IP"
fi
/usr/bin/python3  /home/pi/Desktop/pH_Files_and_Data/DFRobot_PH_Read.py &
exit 0

User avatar
B.Goode
Posts: 8905
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Running .py file from boot using rc.local

Thu Oct 10, 2019 4:30 pm

I check the .csv file that the code writes an output to and there is no update in the information on it.

Where, precisely, in your filesystem are you expecting to find the output file ph.csv ?

hippy
Posts: 6104
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Running .py file from boot using rc.local

Thu Oct 10, 2019 4:32 pm

Nathan.Rivard wrote:
Thu Oct 10, 2019 3:57 pm
There is still no startup of the program upon boot
When I first started with trying to get something to run at start-up I started simple -

/home/pi/magic.py

Code: Select all

with open("/home/pi/magic.txt", "w") as f:
  f.write("Yippee! It works!\n")
/etc/rc.local addition

Code: Select all

/usr/bin/python2 /home/pi/magic.py &
Reboot and '/home/pi/magic.txt' should be there for you to see and read.

Start simple, get it working, build up from there.

Edit : Corrected "with file" to be "with open" - Sorry about that.
Last edited by hippy on Thu Oct 10, 2019 5:39 pm, edited 1 time in total.

Nathan.Rivard
Posts: 35
Joined: Tue Sep 10, 2019 3:28 pm

Re: Running .py file from boot using rc.local

Thu Oct 10, 2019 4:54 pm

B.Goode wrote:
Thu Oct 10, 2019 4:30 pm
I check the .csv file that the code writes an output to and there is no update in the information on it.

Where, precisely, in your filesystem are you expecting to find the output file ph.csv ?
Yes, that is where the program writes to after acquiring data and it should be updating automatically after launch of the device

User avatar
B.Goode
Posts: 8905
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Running .py file from boot using rc.local

Thu Oct 10, 2019 5:01 pm

Where, precisely, in your filesystem are you expecting to find the output file?

"Yes" isn't a sensible answer...


Hint: if you are looking in some subdirectory of /home/pi/ you are probably looking in the wrong place...

See the recent suggestion about 'starting simple.'

Nathan.Rivard
Posts: 35
Joined: Tue Sep 10, 2019 3:28 pm

Re: Running .py file from boot using rc.local

Thu Oct 10, 2019 6:10 pm

B.Goode wrote:
Thu Oct 10, 2019 5:01 pm
Where, precisely, in your filesystem are you expecting to find the output file?

"Yes" isn't a sensible answer...


Hint: if you are looking in some subdirectory of /home/pi/ you are probably looking in the wrong place...

See the recent suggestion about 'starting simple.'
I was replying as in like yes, the .csv file is what the program writes to, is there something else I should know with this. I apologize I'm fairly new to this

Return to “Troubleshooting”