Community
OpenEnergyMonitor

Community

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

python
Tags: #<Tag:0x00007f1fe19a62c8>

(Bo Herrmannsen) #1

not sure if this is the right cat but i could not find any better

so i have made a script that should control an electrical heating panel and report to emoncms.org

i have run the script in a putty session and it seems to work… ie it loops correctly first reading the temp from a ds18b20, then checking if it should target daytime temp or nighttime temp, then figure if relay should be on or off and lastly report to emoncms.org about the status of things (temp read, target temp, relay status, button status)

i have observed that the script do go from daytime to nighttime and back, and that the relay goes on and off if correct temp is reached.

also there’s a button that when pressed do raise the target temp for 15 mins

but for some reason it goes dead on reporting to emoncms, a reboot helps but it just goes dead again after the first reporting to emoncms

the code is here: https://github.com/boelle/Rpi-Heating-Control/blob/master/heatcontrol.py

i also have a bash script that via cron every 5 mins checks if the script is running and if not start it

that bash script is here: https://github.com/boelle/Rpi-Heating-Control/blob/master/scriptcheck.sh

i will try and run the script via putty again but without reporting to emoncms.org and see if that changes anything


(Bo Herrmannsen) #2

So far i have been able to run the script with the reporting to emoncms disabled for 50 mins

i had one case where the SSH connection was closed, not sure why but maybe due to ISP issues of some kind

the current attempt has run for 42 mins


(Brian Orpin) #3

if I was trying to debug this, I’d print out the URI generated and try it manually.

I would also do something with the response. It might be enlightening (APIKEY might be incorrect for instance).

I don’t use emoncms.org and I am not familiar with the httplib.HTTPConnection protocol. There seem to be some issues with HTTPS and versions of python https://stackoverflow.com/questions/2146383/https-connection-python

One final thing, I think emoncms.org only uses HTTPS.

HTH


(Bo Herrmannsen) #4

i know the apikey is correct as the first time it does update emoncms.org, but of course i do not tell the public what the key is

but will se if https changes anything


(Brian Orpin) #5

That was just an example. Actually look at the response and it might tell you what is wrong.


(Bo Herrmannsen) #6

will do

thanks for the pointer


(Bo Herrmannsen) #7

if i simple ask python to print the reponse to screen i get:

<httplib.HTTPResponse instance at 0x7679df58>


(Bo Herrmannsen) #8

sometimes its: <httplib.HTTPResponse instance at 0x7679d238>


(Bo Herrmannsen) #9

also just updated the github to reflect how i’m printting debug :https://github.com/boelle/Rpi-Heating-Control/blob/master/heatcontrol.py


(Daniel Bates) #10

I recently made a script for getting emoncms data, rather than posting. I have limited undersatanding of posting but I used the ‘requests’ module as a json tool.

I don’t know if ‘requests’ if available on the pi. I found it very easy to use and might be a simpler starting point.

import requests

incoming_data = requests.get("https://emoncms.org/feed/value.json?id=123456789&apikey=b9033cf185b17e***********b9dca2")

parsed_data = incoming_data.json() # to create printable data

Was my starting point. Ideally the third line is inside an “if incoming_data.status_code == 200:”
Seems like .GET and .POST json methods are both available as methods to POST data, which is simple.


(Bo Herrmannsen) #11

i changed line 142 in my script: https://github.com/boelle/Rpi-Heating-Control/blob/master/heatcontrol.py#L142

from:

print response

to

print response.read()

i now get an ok back each time it contact emoncms.org

i used this example: https://github.com/openenergymonitor/EmoncmsPythonLink/blob/master/pylink.py


(Bo Herrmannsen) #12

sample output:

Loops since start 10
Current temp: 22.937
Running Daytime Schedule
We have an internet connection? True
NOT Using Override Temp
Target temp: 22.0
HEATING OFF
ok
Data sent to emoncms
16

first line is a baisc indicator if the script as such is running, it will increment by 1 for each loop, and is loop is 60 sec

the ok just before “Data sent to emoncms” is the response from emoncms.org

16 is the variable for the override temp, when button is pressed this will become 0 and target temp is locked to bake_temp until the variable gets to 15 or above, the variable also increments by 1 for each loop


(Brian Orpin) #13

You can get an ‘OK’ back from an emoncms server even if the data is not valid.

As I said before, you would be best to build the URI string outside the request call so you can actually print out what has been sent. It is likely to be an error in how you are building the string.


(Bo Herrmannsen) #14

even if emoncms updates? i’m looking at the output from the logfile via a tail command

and at same time looking at the input’s at emoncms.org


(Brian Orpin) #15

You can get an ‘ok’ from the server. All this says is that the data was received not that it was valid and could be processed.

BTW, you can only use integers in a CSV input (I beleive)


(Bo Herrmannsen) #16

the data do get processed


(Brian Orpin) #17

OK, so what is the issue?


(Bo Herrmannsen) #18

at random it just stops update/processing

and not sure if the fault is at my end, my isp or at emoncms.org


(Brian Orpin) #19

I doubt it is emoncms.org if the API call works. You could check and see by printing a debug message just before the HTTP call. If it is the last thing you see, that is the issue. However, it is more likely to be ISP/network issue rather than the emoncms.org server itself. You will need to investigate how to handle a timeout in making the HTTP call.


(Bo Herrmannsen) #20

thought i had allready done so with the try… ie:

def emoncms():

try:
    seq = (read_temp(), target, my_input, relay)
    str_join = ",".join(str(x) for x in seq)

    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 Exception, ex:
    pass