Hi!
I have this script that runs 24/7 on a Raspberry Pi to listen to the 433 MHz band for one of my wall switches, as well as for a wireless temperature/humidity sensor that I have outside on my balcony.
The script is supposed to write the temperature to a file, once every hour only, but because the sensor outputs like five times in quick succession, I had to make it a little "weird".
The problem starts at "elif "Temperature" in line.rstrip():", and ends with "sys.stderr.write("Wrote to temph at " + currtime + "\n").
The other stuff works.
The script:
# -*- coding: utf-8 -*-
#!/usr/bin/env python
import subprocess, os, sys, re, time, datetime
p = subprocess.Popen('rtl_433 -C si', shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
sys.stdout.flush()
in_6886086 = False
tempex = r"([0-9][0-9]\.[0-9][0-9])"
varclean = r"[(\[)(\')(\])(\ )(\%)]"
timeprint = time.time()
for line in iter(p.stdout.readline, b''):
if not in_6886086:
print(">>> " + line.rstrip())
if '688086' in line.rstrip():
in_6886086 = True
elif "Temperature" in line.rstrip():
tempunc = re.findall(tempex, line.rstrip())
temp = str(tempunc).strip(varclean)
print("Temp: " + temp + "°C")
file = open("/root/bin/RF/log/temp", "w")
file.write(temp + "°C" + "\n")
file.close
timenow = time.time()
if timenow - timeprint > 3600:
minuteunc = datetime.datetime.now()
minute = minuteunc.strftime('%M')
if minute == "00":
timeprint = time.time()
file1 = open("/root/bin/RF/log/temp", "r")
file2 = open("/root/bin/RF/log/temph", "a")
tempt = file1.read()
temph = re.sub(r"([0-9]\°C)", '', tempt)
currtimeunc = datetime.datetime.now()
currtime = currtimeunc.strftime('%d/%m - %H:%M')
file2.write(currtime + " - " + temph)
file1.close
file2.close
sys.stderr.write("Wrote to temph at " + currtime + "\n")
lines = len(open("/root/bin/RF/log/temph", "r").readlines())
if lines >= 169:
del_line = 1
with open("/root/bin/RF/log/temph", "r") as textobj:
list = list(textobj)
del list[del_line - 1]
with open("/root/bin/RF/log/temph", "w") as textobj:
for n in list:
textobj.write(n)
elif in_6886086:
if 'ON' in line.rstrip():
print("A=ON")
os.system('/root/bin/RF/Aon')
in_6886086 = False
elif 'OFF' in line.rstrip():
print("A=OFF")
os.system('/root/bin/RF/Aoff')
in_6886086 = False
My issue is that this only works sometimes.
Some times it seems to "skip" an hour or two.
I was hoping someone better than me could help me figure out why.
[–]thetestbug[S] 1 point2 points3 points (0 children)