pythonraspberry-pigrovepi+

python script printing to txt when startet manually but not at startup via rc.local on bootup


i am trying to get my raspberry pi to print data from external sensors to a display and save it to a .txt at the same time.

What i did works fine when launching the script in a shell via ssh. But that stops the script after closing the shell.

So i tried to put it in the rc.local file to launch it while booting. But that works only half way, as it shows the data on the display but it doesn't save it to a file.

import grovepi

from grovepi import *
from grove_rgb_lcd import *
from time import sleep, strftime
from math import isnan


#port definition
dht_sensor_port = 7 
dht_sensor_type = 0 
led_green = 5
led_red = 6
f = open("hwd.txt", "w")

#rgb display color
setRGB(0,255,0)

while True:
    try:
        [ temp,hum ] = dht(dht_sensor_port,dht_sensor_type)
        print("temp =", temp, "C\thumidity =", hum,"%")
        print("{0},{1}\n".format(strftime("%Y-%m-%d %H:%M:%S"),(temp,hum)), file=f)

        if isnan(temp) is True or isnan(hum) is True:
            raise TypeError('nan error')

        t = str(temp)
        h = str(hum)    
        
        setText_norefresh("Temp:" + t + "C\n" + "Humidity :" + h + "%")

        if hum < 40.0 or hum > 60.0:
            grovepi.digitalWrite(led_red,1)
            grovepi.digitalWrite(led_green,0)
        else:
            grovepi.digitalWrite(led_green,1)
            grovepi.digitalWrite(led_red, 0)

    except (IOError, TypeError) as e:
        print(str(e))
        setText("")

    except KeyboardInterrupt as e:
        print(str(e))
        setText("")
        break

    sleep(1)

``

Solution

  • You are running an infinite loop, so you need to close the file stream to save changes into your file. If you run your script without the loop you can see that the changes are saved even without closing the steam and that's because it handles implicitly. So you need to open and close the file stream which can easily be done with the with keyword. I think the code below works fine for you.

    import grovepi
    
    from grovepi import *
    from grove_rgb_lcd import *
    from time import sleep, strftime
    from math import isnan
    
    # port definition
    dht_sensor_port = 7
    dht_sensor_type = 0
    led_green = 5
    led_red = 6
    
    # rgb display color
    setRGB(0, 255, 0)
    
    while True:
        try:
            [temp, hum] = dht(dht_sensor_port, dht_sensor_type)
            print("temp =", temp, "C\thumidity =", hum, "%")
            with open("hwd.txt", "w") as f:
                print("{0},{1}\n".format(strftime("%Y-%m-%d %H:%M:%S"), (temp, hum)), file=f)
    
            if isnan(temp) is True or isnan(hum) is True:
                raise TypeError('nan error')
    
            t = str(temp)
            h = str(hum)
    
            setText_norefresh("Temp:" + t + "C\n" + "Humidity :" + h + "%")
    
            if hum < 40.0 or hum > 60.0:
                grovepi.digitalWrite(led_red, 1)
                grovepi.digitalWrite(led_green, 0)
            else:
                grovepi.digitalWrite(led_green, 1)
                grovepi.digitalWrite(led_red, 0)
    
        except (IOError, TypeError) as e:
            print(str(e))
            setText("")
    
        except KeyboardInterrupt as e:
            print(str(e))
            setText("")
            break
    
        sleep(1)