1

apologies if this question has been asked somewhere before, but I couldn't find a solution / info i was looking for.

So: I am running a raspberry pi 0W with a python script reading 4 temperature sensors monitoring some stuff at home. I am reading these sensors every 2 minutes and write the output into a CSV (for the time being sufficient for me). When I ssh my PI and run my script in TMUX, I realised that when calling my "Sensordata.csv" it only updates, once I close the script in my TMUX session. Ideally I want my "Sensordata.CSV" file update after each polling cycle.

I believe its not the code, as my code opens, writes and closes the file while running in the shell normally. Hope someone can help me out =)


import time
import csv
from datetime import datetime



def get_temp(dev_file):
    f = open(dev_file,"r")
    contents = f.readlines()
    f.close()
    index = contents[-1].find("t=")
    if index != -1 :
        temperature = contents[-1][index+2:]
        cels =float(temperature)/1000
        return cels

time_for_csv=time.asctime( time.localtime(time.time()))
f=open("Sensordata.csv", "a")
c=csv.writer(f)





if __name__ =="__main__":
    while True:
        dateTimeObj = datetime.now()
        timestampStr = dateTimeObj.strftime("%d-%b-%Y (%H:%M:%S.%f)")
        temp = get_temp("//sys/bus/w1/devices/28-0301a2799ec4/w1_slave")
        temp2 = get_temp("//sys/bus/w1/devices/28-0301a2790081/w1_slave")
        temp3 = get_temp("//sys/bus/w1/devices/28-012020be268d/w1_slave")
        tempout = get_temp("//sys/bus/w1/devices/28-00000b0f6540/w1_slave")
        print('Current Timestamp : ', timestampStr, "28-00000b0f6540 - Outside Sensor", tempout)
        print('Current Timestamp : ', timestampStr, "28-0301a2799ec4 - Floorheating out", temp)
        print('Current Timestamp : ', timestampStr, "28-0301a2790081 - Floorheating in", temp2)
        print('Current Timestamp : ', timestampStr, "28-012020be268d - Room Sensor", temp3)
        f = open("Sensordata.csv", "a")
        c = csv.writer(f)
        c.writerow([timestampStr, temp, temp2, temp3, tempout])
        f.close()
        time.sleep(120)
  • [Run Linux command in background and keep runing after closing SSH - Stack Overflow](https://stackoverflow.com/questions/57292884/run-linux-command-in-background-and-keep-runing-after-closing-ssh) ? – user202729 Jan 26 '21 at 12:38
  • I have tried nohup and disown process as well - same problem as above, the csv doesn't get updated after each polling cycle. ?? – Robin Skoge Jan 26 '21 at 12:42
  • Consider to run a cron job every 2 minutes: https://www.raspberrypi.org/documentation/linux/usage/cron.md – VPfB Jan 26 '21 at 13:02
  • @ VPfB dakjuem =) Thanks, this could actually be a work-around. (However, it isn't really solving or answering the question, why the csv is not updated while running a script in TMUX) – Robin Skoge Jan 26 '21 at 13:20

1 Answers1

1

I don't think that Tmux is part of the issue.

Try removing these 3 lines of code in your script:

time_for_csv=time.asctime( time.localtime(time.time()))
f=open("Sensordata.csv", "a")
c=csv.writer(f)

time_for_csv is unused.

When opening a file for writing or appending, a safer way is to use the following:

with open("Sensordata.csv", "a") as f:
    c=csv.writer(f)
    c.writerow([timestampStr, temp, temp2, temp3, tempout])

The file object will always be closed even if an exception is raised. You do not have to close it explicitly. See With statement in Python

I am guessing that your opening a file object in the middle of the script leaves a file open, and then you re-define f after the if __name__ == "__main__. Leaving file objects open can have unpredictable results like the one you're experiencing.

dmmfll
  • 2,666
  • 2
  • 35
  • 41
  • 1
    Thanks a lot, taking out the middle section was actually the issue. And I cleaned the code with the with open option - works just fine know. Thank you =) – Robin Skoge Jan 26 '21 at 22:28