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.

Keshav Pradeep Ramanath
  • 1,623
  • 4
  • 24
  • 33
Pras4
  • 113
  • 2
  • 1. If you're running Raspbian, you can use the `crontab` to run any program at intervals. Or `time.sleep()` in Python to pause. – Aimery Jun 01 '18 at 10:11
  • 2. You can save the results to a file (typically `csv`, see [here](https://docs.python.org/3/library/csv.html)). Then you read the file later on. – Aimery Jun 01 '18 at 10:13
  • Possible duplicate of [Looping at a constant rate with high precision for signal sampling](https://stackoverflow.com/questions/26774186/looping-at-a-constant-rate-with-high-precision-for-signal-sampling) – ivan_pozdeev Jun 02 '18 at 05:32

3 Answers3

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

MaNKuR
  • 2,578
  • 1
  • 19
  • 31
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).

Paula Thomas
  • 1,152
  • 1
  • 8
  • 13
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)