0

With this code I found on the internet and changed a little bit for measuring temperature I cannot write to the log file when the sleep value is more then 1.

The log file is created but stays empty. With sleep value 1 there are no problems.

import subprocess
import logging
import time
from time import sleep, gmtime, strftime

#Read temp from MCP9808 sensor (Using SPI interface)
import smbus
t_reg = 0x05
address = 0x18
bus = smbus.SMBus(1) # change to 0 for older RPi revision

reading = bus.read_i2c_block_data(address, t_reg)
# t = (reading[0] &lt:&lt: 8) + reading[1] 
t = (reading[0] << 8) + reading[1]


#Calculate temperature (see 5.1.3.1 in datasheet)
# temp = t &amp; 0x0FFF
def get_temp():
    temp = t & 0x0FFF
    temp /=  16.0
    return(temp)

# if (t &amp; 0x1000):
if (t & 0x1000):
    temp -= 256
# try:
#   MCP_Temp='%.1f' % (temp*(9.0/5.0)+32.0) #Converts to Farenheit
# except subprocess.CalledProcessError as e:
#   print e

#This will add a timestamp to and temp in a CSV ready format
# output=strftime("%m/%d/%Y %H:%M,") + ',' + str(float(temp))
# print output

with open("cpu_temp.csv", "a") as log:
    while True:
        temp = get_temp()       
        log.write("{0},{1}\n".format(strftime("%Y-%m-%d %H:%M:%S"),str(temp)))
#       sleep(30)
        time.sleep(2)
1
  • Please take the tour and visit the helpcenter to see how things work here.
    – Ghanima
    Jan 26, 2017 at 14:41

1 Answer 1

1

The problem with your code is that you call "get_temp" multiple times, but in there you refer to the variable "t" which is only set on the initial run.

import subprocess
import logging
import time
from time import sleep, gmtime, strftime
import smbus

#Constant things, that don't change during run.
t_reg = 0x05
address = 0x18
bus = smbus.SMBus(1) # change to 0 for older RPi revision

#Calculate temperature (see 5.1.3.1 in datasheet)
# temp = t &amp; 0x0FFF
def get_temp():
    #The reading variable changes every time you run get_temp()
    reading = bus.read_i2c_block_data(address, t_reg)
    t = (reading[0] << 8) + reading[1]
    temp = t & 0x0FFF
    temp /=  16.0
    # if (t &amp; 0x1000):
    if (t & 0x1000):
      temp -= 256
    return(temp)


# try:
#   MCP_Temp='%.1f' % (temp*(9.0/5.0)+32.0) #Converts to Farenheit
# except subprocess.CalledProcessError as e:
#   print e

#This will add a timestamp to and temp in a CSV ready format
# output=strftime("%m/%d/%Y %H:%M,") + ',' + str(float(temp))
# print output

with open("cpu_temp.csv", "a") as log:
    while True:
        temp = get_temp()       
        log.write("{0},{1}\n".format(strftime("%Y-%m-%d %H:%M:%S"),str(temp)))
#       sleep(30)
        time.sleep(2)
2
  • Is it possible that you rewrite the code like you describe
    – user59948
    Jan 24, 2017 at 8:05
  • I did. Have a look at line 14 and following.
    – nee6AGag
    Jan 24, 2017 at 15:26

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.