ahhhhh google was my friend for once
except requests.exceptions.RequestException as e:
ahhhhh google was my friend for once
except requests.exceptions.RequestException as e:
if not mistaken this short of bit should help out
def emoncms():
try:
seq = (read_temp(), target, my_input, relay)
str_join = ",".join(str(x) for x in seq)
print 'Preparing Data for emoncms.org'
conn = httplib.HTTPConnection(domain)
conn.request("GET", "/"+emoncmspath+"/input/post.json?apikey="+apikey+"&node="+str(nodeid)+"&csv="+str_join)
response = conn.getresponse()
print response.read()
print 'Data sent to emoncms'
except requests.exceptions.RequestException as e:
print 'We had a timeout'
pass
from my limited coding skills this will print to screen before and after sending the data along with the ok from the server
and if a timeout happens it will just continue on but print that a timeout happened
The eror printing you have just added to the except will only report certain errors and it will replace any meaningful error messages with a stock âWe had a timeoutâ whether it was a timeout or not. This will be really misleading.
Instaed just print the raw error message for any fault that occurs using
except Exception, ex:
print 'This error occurred: ' + str(ex)
done⌠i assume that there should be an pass also:
except Exception, ex:
print 'This error occurred: ' + str(ex)
pass
No. The âpassâ does nothing at all, it was only needed in your original because the except cannot be empty, so the âpointless passâ filled the gap without doing anything.
With the print statement in there, itâs not empty, the pass does nothing, itâs effectivly a blank line.
oki
but what will happen if there should be a timeout or anything else ?
will the script exit? my goal is to log the error but not exit
Iâm not sure what your asking. The âexceptâ code is like a âB planâ if the code you put in the âtryâ fails the âB planâ here is to record any info as to why that happened and carry on without crashing. Ideally the âexceptâ is never called, think of it as an insurance policy, if the code in the âtryâ fails, at least the script wonât crash, it will print the error and continue, what comes next is what ever you put next in the code. Thatâs the idea of a try/except, it protects you from a crash so the code can continue.
iâm know at another case of where the feed updates are halted.
but so are the outputs to the log file
it ran for 33 minutes, but no error to emoncms
so i guess next step is to find out why and where it stops⌠to the last thing i see in the log is where print a series of dashâs to seperate the output from each loop
i will change the script to print start and end of loop to make sure i know where it stops in the while tru section
that was what i was asking⌠what happens ifâŚ
in this case we just get the error and it will carry on
it got stuck the same place again and iâm bit out of ideaâs why, but i know where
sample log:
--------------------
start of loop
Loops since start 18
Current temp: 23.0
Running Daytime Schedule
We have an internet connection? True
NOT Using Override Temp
Target temp: 22.0
HEATING OFF
Preparing Data for emoncms.org
ok
Data sent to emoncms
16
end of loop
--------------------
start of loop
Loops since start 19
Current temp: 23.0
Running Daytime Schedule
We have an internet connection? True
NOT Using Override Temp
Target temp: 22.0
HEATING OFF
Preparing Data for emoncms.org
ok
Data sent to emoncms
16
end of loop
--------------------
and i have updated github to reflect the code as is: https://github.com/boelle/Rpi-Heating-Control/blob/master/heatcontrol.py
it âmustâ be this part:
while True:
my_input = wiringpi.digitalRead(3)
if my_input == 1:
print 'Switch is ON'
t15 = 0
if time.strftime('%-M') > t_loop:
print 'start of loop'
print 'Loops since start',
print test
print 'Current temp:',
print read_temp()
schedule()
print 'We have an internet connection?',
print internet_connected()
if t15 < 15:
print 'Using Override Temp'
target = bake_temp
print 'Target temp:',
print target
if target > read_temp(): # Compare temp read in Gmail subject to temp from DS18B20
wiringpi.digitalWrite(0, 1) # sets port 0 to ON
relay = 1
print 'HEATING ON'
t_loop = time.strftime('%-M')
emoncms()
t15 = t15 + 1
print t15
else:
wiringpi.digitalWrite(0, 0) # sets port 0 to OFF
relay = 0
print 'HEATING OFF'
t_loop = time.strftime('%-M')
emoncms()
t15 = t15 + 1
print t15
else:
print 'NOT Using Override Temp'
print 'Target temp:',
print target
if target > read_temp(): # Compare temp read in Gmail subject to temp from DS18B20
wiringpi.digitalWrite(0, 1) # sets port 0 to ON
relay = 1
print 'HEATING ON'
t_loop = time.strftime('%-M')
emoncms()
print t15
else:
wiringpi.digitalWrite(0, 0) # sets port 0 to OFF
relay = 0
print 'HEATING OFF'
t_loop = time.strftime('%-M')
emoncms()
print t15
test = test + 1
print 'end of loop'
print '--------------------'
i just cant see what makes it stuck at the end and not going thrugh the loop again
as noob iâm iâm having the thought that the entire while true loop should be an âtryâ so it will keep on going
could it be my way of timing the loop to 1 minute?
ie the use of time.strftime(â%-Mâ)
?
also i have noted that if i leave a putty session open sometimes it frezzes⌠but i have no touble getting back in. Not sure if that matters thou
did a few other runs and they all stop at the same place, ie
print â--------------------â
try this, I have
Iâm not able to test so it may need âadjustingâ (and I havenât looked at the rest of the code) but this should run a much more relaxed pace and the timing is on a fixed time each loop. I donât exactly know how you intended it to work to Iâm not sure Iâve got it right, this will loop once a second doing nothing but check the time and then every 60s the control code will run, check the input, get the temp, then do the heating control. hopefully it might be more stable.
next_time = 0
while True:
time_now = time.time()
if time_now >= next_time:
print 'start of loop: ' + str(test)
my_input = wiringpi.digitalRead(3)
if my_input == 1:
print 'Switch is ON'
t15 = 15
temp_now = read_temp()
print 'Current temp: ' + str(temp_now)
schedule()
print 'We have an internet connection? ' + str(internet_connected())
print 'Target temp: ' + str(target)
# Control heating
if t15 > 0:
t15 -= 1
if bake_temp > temp_now: # Compare temp read in Gmail subject to temp from DS18B20
wiringpi.digitalWrite(0, 1) # sets port 0 to ON
relay = 1
print 'HEATING ON (Boost)'
else:
wiringpi.digitalWrite(0, 0) # sets port 0 to OFF
relay = 0
print 'HEATING OFF (Boost)'
else:
if target > temp_now: # Compare temp read in Gmail subject to temp from DS18B20
wiringpi.digitalWrite(0, 1) # sets port 0 to ON
relay = 1
print 'HEATING ON'
else:
wiringpi.digitalWrite(0, 0) # sets port 0 to OFF
relay = 0
print 'HEATING OFF'
# Calculate the next loop time by adding interval in secs
next_time = time_now + 60
emoncms()
print "Remaining boost time: " + str(t15) + "mins"
test += 1
print 'end of loop'
print '--------------------'
# Don't loop too fast
time.sleep(1)
oki will give it a shot straight away
time for bed for meâŚ
but it do need a bit of adjusting
sample output:
--------------------
start of loop: 1
Current temp: 23.062
Running Nighttime Schedule
We have an internet connection? True
Target temp: 17.0
HEATING ON (Boost)
Preparing Data for emoncms.org
ok
Data sent to emoncms
Remaining boost time: 14mins
end of loop
--------------------
so it reads temp correct and also it runs the right schedule and target temp
but it starts in boost mode and counts down
itâs prob a small thing but tired now and dont want to screw it up
ahh i think it was because t15 was initial set to 16 at the start of the code
yes t15 should be initialized as 0, only when input == 1 will it go to 15
that one is changed
will let it run overnight with output going to a log file and running a tail on it via ssh
i think that nailed it, number of loops match with the hours it has run, and as a bonus the ssh connection is still up
so a big thanks from me to you Paul
should this part not be outside the timecheck?
my_input = wiringpi.digitalRead(3)
if my_input == 1:
print 'Switch is ON'
t15 = 15
ie move it just under the while true line
i just tested it and it did not register the button push