Sep-23-2020, 02:25 PM
I have a program that monitors a DHT11 temperature sensor and logs the data to a txt document. The program loops every 3 minutes, so it gets a new line quite often. I'd like to have it purge out the old stuff, maybe delete the first line and add a new last line. Another option I would consider is to bulk delete several lines every hour, using a different program if necessary.
import Adafruit_DHT import time from gpiozero import LED import requests from datetime import datetime DHT_SENSOR1 = Adafruit_DHT.DHT11 DHT_PIN1 = 14 LED1 = LED(21) #Pulses as long as program is running LED2 = LED(16) #Amber on light LED2.on() with open("/home/pi/LittleFridgeLog.txt", "a") as file: results = "Monitor service restarted" + "\n" file.write(results) #r = requests.post("https://bit.ly/EDITEDFORPRIVACY") #Message that service has started r = requests.post("https://joinjoaomgcd.appspot.com/_ah/api/messaging/v1/sendPush?deviceId=group.android&text=LFTempMonitorStart&apikey=EDITEDFORPRIVACY") while True: humidity1, temperature1 = Adafruit_DHT.read_retry(DHT_SENSOR1, DHT_PIN1) #Little fridge LittleFridgeF = (temperature1 * 9/5) + 32 LittleFridgeF = round(LittleFridgeF,2) now = datetime.now() current_time = now.strftime("%H:%M:%S") # Log time and temp to text file with open("/home/pi/LittleFridgeLog.txt", "a") as file: results = current_time + " Little Fridge:" + str(LittleFridgeF) + "\n" file.write(results) if LittleFridgeF < 40: #Less than this temp is good. Blue light on. print(current_time) print("Little Fridge:", LittleFridgeF) print("") LED1.blink(.05,5) else: LED1.blink(.3,.3) print(current_time) print("Little Fridge overtemp:", LittleFridgeF) print("") alert = "overtemplittlefridge " + str(LittleFridgeF) base = "autoremotejoaomgcd.appspot.com/sendmessage?key=EDITEDFORPRIVACY&message=" alerturl = "https://"+base+alert r = requests.post(alerturl) time.sleep(180) time.sleep(1)Can this be done?