0

i coded a small datalogger in python3 that gets readings from an Arduino and stores them locally. The code works if i open it manually, but as soon as i reboot and code runs from rc.local it stops writing new files. code is running, i can see it through systemctl status rc-local and it also is getting data from arduino (i have multiple echos and see everything that flows inbetween). i dont know what to look for. i'm running it with sudo python3 /home/pi/mycodedir & both from rc.local and terminal. when its running in terminal everything's fine, when from the rc.local everything's still fine just it does not write files. everything else (or at least what i've checked so far) is working.

Edit

i tried cron as well but no luck. exactly same behaviour. im using pickle to dump the data file

def saveReadings(self):
        for sensor in self.sensors:
            if(sensor.readings!=[]):
                filename="{2}readings/{0}/{1}.{3}sj".format(sensor.id,
                                                            sensor.last_comm.strftime("%d-%m-%Y/%H%M%S"),
                                                            sensor.type,
                                                            "t" if sensor.type=="tilt" else "c" if sensor.type=="crack" else "u")
                if not os.path.exists(os.path.dirname(filename)):
                    os.makedirs(os.path.dirname(filename))
                with open(filename,"wb") as file:
                    self.esrlock.acquire()
                    pickle.dump([sensor.id,sensor.readings],file)
                    self.esrlock.release()
                    sensor.readings=[]

Fix...

it was my fault not thinking about it. cron as well as rc.local run code as root so when i was wrote to save readings at "location/file.type" it was not saving it where i subconsciously thought, at "~/mycodedir/location/file.type", but rather at "/location/file.type".

1 Answer 1

2

Just as an initial observation, is /home/pi/mycodedir actually the name of your executable file?... I ask because it looks like it may be a directory name instead. If that's not the problem, read on:

rc.local is deprecated. You could run it under systemd if you're familiar with that, or you could use cron if you want a simpler method:

From your user pi account, open root crontab for editing:

$ sudo crontab -e

This assumes you actually need sudo privileges for something you do in your Python code; if you don't, then use crontab -e to get pi crontab.

Once the crontab is opened in the editor, add one line:

@reboot /bin/sleep/30; /usr/bin/python3 /home/pi/mycodedir >> /home/pi/myoutput.txt 2>&1

Try this. If you have any questions about it before you try it, feel free to ask. If you try it, and it doesn't work, please add the relevant bits of /home/pi/myoutput.txt file to your question.

2
  • i tried using cron but still the same problem. code works but can't create files with data. i don't know if it will help but im using pickle to dump file. myoutput.txt gets created but is empty
    – LongToeBoy
    Jul 19, 2021 at 21:29
  • @LongToeBoy: Add some sleep time (see revised answer) to see if it's a resource availability issue. If not, then you have some troubleshooting to do. I'd recommend starting with a "Hello World" app in Python - schedule the app to run in the crontab & verify it writes "hello world" to some file.
    – Seamus
    Jul 19, 2021 at 22:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.