I have a Python script on my Raspberry Pi 3 Model B (OS NOOBS) that when run, logs the temperature of the CPU into a .csv file every minute.
What I'd like to know is how can I, from a Python script open a terminal and output the temperature in the terminal rather than log it into the .csv file?
This is the code that I've written so far:
from gpiozero import CPUTemperature
from time import sleep, strftime, time
cpu = CPUTemperature()
def output_temp(temp):
with open("cpu_temp.csv", "a") as log:
log.write("{0}, {1}\n".format(strftime("%Y-%m-%d %H:&M:%S"),str(temp)))
while True:
temp = cpu.temperature
output_temp(temp)
sleep(60)
I'm planning on using what I'm asking for so when the temperature goes above 'x' degrees, the terminal will open, print the temperature there until it drops down below the 'x' mark again. However I'll figure that remaining part on my own as I'm a newbie whom is still learning. But I am stuck on the part where I want to open the terminal and print the variable there.
I'm using the software "Python 3 (IDLE)" to write and run this code. When it's completed I'll integrate it so it's automatically run on startup.
Any assistance would be greatly appreciated! Sincerely, Jocke
I can not test it on raspberry, but OS NOOBS should have lxterminal
available and the following code should work. If there is no lxterminal
on your system, install it or try replacing it in code below with xterm
or gnome-terminal
etc. Tested on Ubuntu 16.
import os
import time
import subprocess
# create custom pipe file
PIPE_PATH = "/tmp/my_pipe"
if os.path.exists(PIPE_PATH):
os.remove(PIPE_PATH)
os.mkfifo(PIPE_PATH)
# open terminal that reads from your pipe file
a = subprocess.Popen(['lxterminal', '-e', 'tail --follow {0}'.format(PIPE_PATH)])
# write to file and it will be displayed in the terminal window
message = "some message to terminal\n"
with open(PIPE_PATH, "w") as p:
p.write(message)
time.sleep(5)
# close the terminal
a.terminate()
You can adjust the writing to file and sleep according to your needs. :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With