1

I got temperature, pressure, and altitude readings on my PI using a sensor:

  1. The problem is, to see the results, I have to execute the code.py every time by myself. I am trying to automate it somehow so it will keep running itself for the time I want.
  2. Once that is automated, would like to save the results and analyze the output after some time.

Is there a way I can write code for both the tasks?

Thank you.

3
  • 1. If you're running Raspbian, you can use the crontab to run any program at intervals. Or time.sleep() in Python to pause. Commented Jun 1, 2018 at 10:11
  • 2. You can save the results to a file (typically csv, see here). Then you read the file later on. Commented Jun 1, 2018 at 10:13
  • Possible duplicate of Looping at a constant rate with high precision for signal sampling Commented Jun 2, 2018 at 5:32

3 Answers 3

2

There are two things required here. First a script i.e code.py to log the functional behavior like temperature, pressure, and altitude readings along with error/response during the process. Another is the script executions logs i.e a success or failure during the scheduled time and other system logs.

For first job, you have to do by your self but ensure to have a logger module in place to log the process flow.

For Second job, you can use OS provided scheduler crontab for Linux based os. For example to execute script every minutes

* * * * * python /home/script/code.py > /home/script/code.log 2>&1

For more about scheduler jobs, you can refer here

Sign up to request clarification or add additional context in comments.

Comments

0

The time module is your friend here. You can set up an infinite loop with while True: and use time.sleep(secs) at the end of the loop (after output).

Comments

0

I'd use additional controller script like this:

import subprocess;
import time;
import sys;

x = True;
while x:
    while exit_code!=0:
        try:
            exit_code = subprocess.check_call(['python', 'collect_data.py', '-cli_args_if_needed']);
        except:
            print(sys.exec_info()[1]);
            print('Relaunching in 5 seconds');
            time.sleep(5)

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.