I am new to coding and Rasperry Pi. I have been working on a Python script that records Temperature and Humidity from the SenseHat and the CPU temp at a specified interval.
I can get the data to display on the sensehat display as well as on the shell screen. I was looking for a simple way to take the data from the shell and put it into a text file.
All of the tutorials I have seen are either way over my head or did not work in Idle3.
Here is my code
Code: Select all
from sense_hat import SenseHat
import time
import datetime
from time import sleep
import os
sense = SenseHat()
sense.set_rotation(180)
sense.low_light = True
delay=0
while True:
def CPU_temp():
temp = os.popen('vcgencmd measure_temp').readline()
return(temp.replace("temp=","").replace("'C\n",""))
hour = time.localtime().tm_hour
minute = time.localtime().tm_min
sec = time.localtime().tm_sec
temp = round(sense.get_temperature()*1.8+32)
humidity = round(sense.get_humidity(),1)
cputmp = round(float(CPU_temp()))
sleep(delay)
delay=44
message = "%d:%d:%d Temp %dF, humidity %d%%rh, CPU %dC"%(hour,minute,sec,temp,humidity,cputmp)
print ("%d:%d:%d, %dF, %d%% %dC"%(hour,minute,sec,temp,humidity,cputmp))
sense.show_message(message,scroll_speed=(0.08),text_colour=[255,255,0],back_colour=[0,0,0])
sense.clear()
and my shell displays this:
11:9:16, 90F, 37% 54C
11:10:19, 90F, 36% 54C
11:11:23, 90F, 37% 54C
11:12:27, 90F, 37% 54C
11:13:31, 91F, 35% 54C
11:14:35, 91F, 36% 54C
Is there a simple way to capture the shell results to a text file.
Thanks
Neil