Log data

I need to do

pip install psutil 

in the terminal to get the library I need.

Do some reading about this library herearrow-up-right.

In os_utilities I add the line

import psutil 

beside the other import statements.

Then I add a function cpu_load() in os_utilities.py to gather useful information, it almost doesn’t matter what I gather, this is meant as an example only.

def cpu_load():
    # Return significant numbers relating to the CPU
    #print(f"Number of CPUs: {psutil.cpu_count()}" )
    #print(f"CPU load: {psutil.cpu_percent()}")
    return(psutil.cpu_count(), psutil.cpu_percent())

I used the print statements for testing and then commented them out.

Back in Section 7 we learned how to do basic file handling, I’m going to reuse that code now. I modify my main program and call it cpu_log.py and I change the file extension to “.csv”.

I use time.sleep to create a one second delay.

If you have a program running in an endless loop, in Visual Studio Code, click into the terminal window and press [ctrl][c] to exit. If you run code in the Linux or Windows terminal window, the same approach works.

I wrote edited main.py

Note that I used the log_file_name() function with no extension to generate a timestamp for each line in the logfile. Read and understand this program, then run it.

Testing

Run the code for a few minutes to verify it saves data once per second. Do that more than once, verify that a new logfile is created each time.

A comma space delimited file is a typical way to exchange sensor data.

You should be able to open and process the log file in Excel, check that now.

Last updated