Crimson0087
Posts: 10
Joined: Fri Jun 19, 2020 12:03 pm

Help with Code

Sun Feb 14, 2021 1:44 am

Hello,

I need help with something. My python code's directory is /home/pi/Temperature.py. I have set it up using rc.local to run on boot. Now, when I run this program myself from within the programs directory it saves TotalHours.txt to the current directory. When I reboot the raspberry pi and it starts running the program in the background it saves TotalHours.txt to the parent directory so it is just /TotalHours.txt. What I would like to do is to have it ALWAYS save TotalHours.txt to the /home/pi/TotalHours.txt. Additionally is there a way to make this program run constantly in the background so that when I open up the raspberry command line and do other things it does not interrupt this program and if it does it automatically starts back? The purpose of the program is to count and add up the number of hours in the year that the temperature is between 32 and 45 degrees so it is setting on my covered porch but Id like to be able to ssh in and do other things with it from time to time like say Check the TotalHours.txt file without interuppting. Anyway thanks

Code: Select all

import adafruit_dht
from board import pin
from time import sleep
dht_device=adafruit_dht.DHT22(4)

# Initialize the total hours
total_hours=0

while True:
        # Open File for read only
        file1=open("TotalHours.txt","r")
        # Read File for initial total hours and convert string to integer
        total_hours= int(file1.read())
        # Close File
        file1.close()
        # Check Temperature
        temperature_c=dht_device.temperature
        # Convert temperature to fahrenheit
        temperature_f=((temperature_c*1.8)+32)
        #Increment Counter for chill hours
        if temperature_f>32 and temperature_f<45:
                total_hours=total_hours+1
        # Open file for re-write
        file1= open("TotalHours.txt","w")
        # Re-write file and convert integer to string
        file1.write(str(total_hours))
        # Close file
        file1.close()
        #Sleep for 1 hour
        sleep(3600)

klricks
Posts: 7853
Joined: Sat Jan 12, 2013 3:01 am
Location: Grants Pass, OR, USA
Contact: Website

Re: Help with Code

Sun Feb 14, 2021 2:50 am

Use the full path in all of your open file statements.

Code: Select all

file1=open("/home/pi/TotalHours.txt","r")
Unless specified otherwise my response is based on the latest and fully updated RPiOS Buster w/ Desktop OS.

ghp
Posts: 2001
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: Help with Code

Sun Feb 14, 2021 6:10 am

For starting the program in the background, see https://www.raspberrypi.org/documentati ... systemd.md
When running programs in background, consider to implement signal handler to gracefully terminate the program on shutdown: https://docs.python.org/3/library/signal.html

User avatar
paddyg
Posts: 2615
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: Help with Code

Sun Feb 14, 2021 8:16 am

systemd is definitely the way to go. But IMO extremely hard to debug until you discover journalctl
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

danieldean
Posts: 40
Joined: Tue Nov 24, 2020 4:44 pm

Re: Help with Code

Sun Feb 14, 2021 8:28 am

You could try this, it uses the scripts directory so would be more portable:

Code: Select all

import os

# Use paths relative to the script.
dirname = os.path.dirname(__file__)

f = open(dirname + '/file.txt', 'r')

# Do stuff with file contents.
As others have said use systemd for starting at boot.

danieldean
Posts: 40
Joined: Tue Nov 24, 2020 4:44 pm

Re: Help with Code

Sun Feb 14, 2021 8:46 am

Also if this only runs every hour it strikes me that using cron to schedule it might be a better way to go:

https://www.raspberrypi.org/documentati ... ge/cron.md

This way it could start and run every hour when the RPi is on. You would need to rewrite the script without the loop so it does everything it needs to for a single execution.

User avatar
jahboater
Posts: 7193
Joined: Wed Feb 04, 2015 6:38 pm
Location: Wonderful West Dorset

Re: Help with Code

Sun Feb 14, 2021 10:31 am

There are many solutions :)
You could simply add your program to /etc/rc.local with a trailing & to run it in the background.
For example, my clock is started like this:

/home/pi/clock.py &

Return to “Python”