Community
OpenEnergyMonitor

Community

Python script runs but emoncms.org does not update (Hangs at end of script)

python
Tags: #<Tag:0x00007f1ff2b5aa90>

(Bo Herrmannsen) #21

ahhhhh google was my friend for once

except requests.exceptions.RequestException as e:


(Bo Herrmannsen) #22

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


(Paul) #23

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)

(Bo Herrmannsen) #24

done… i assume that there should be an pass also:

except Exception, ex:
    print 'This error occurred: ' + str(ex)
    pass

(Paul) #25

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.


(Bo Herrmannsen) #26

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


(Paul) #27

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.


(Bo Herrmannsen) #28

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


(Bo Herrmannsen) #29

that was what i was asking… what happens if…

in this case we just get the error and it will carry on


(Bo Herrmannsen) #30

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


(Bo Herrmannsen) #31

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


(Bo Herrmannsen) #32

did a few other runs and they all stop at the same place, ie

print ‘--------------------’


(Paul) #33

try this, I have

  • changed the way the loop is timed,
  • slowed down the outer loop,
  • only reads the input before executing code not every loop,
  • only reads temperature once rather than multiple times
  • moved emoncms() and other code so it is only coded once
  • change the boost to count down rather than up so you have a time remaining print
  • grouped some print lines

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)

(Bo Herrmannsen) #34

oki will give it a shot straight away


(Bo Herrmannsen) #35

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


(Bo Herrmannsen) #36

ahh i think it was because t15 was initial set to 16 at the start of the code


(Paul) #37

yes t15 should be initialized as 0, only when input == 1 will it go to 15


(Bo Herrmannsen) #38

that one is changed

will let it run overnight with output going to a log file and running a tail on it via ssh


(Bo Herrmannsen) #39

@pb66

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 :smiley:


(Bo Herrmannsen) #40

@pb66

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