Python post syntax

Anyone see a problem with this post syntax?

pi@emonpi:/opt/vedirect $ sudo /opt/vedirect/vedirect.py
Connected successfully to 10.0.0.22
Request: /input/post.json?node=28&apikey=xxxxx&json={'SOC':'1000','Relay':'OFF','FW':'0308','I':'0','Alarm':'OFF','PID':'0x203','CE':'0','P':'0','AR':'0','BMV':'700','V':'14141','TTG':'-1'}
Failed uploading data

Here is the code:

domain="10.0.0.22"

def print_data_callback(data):
    data=repr(data)
    data=data.replace(" ","") # no spaces allowed
    try:
       conn = httplib.HTTPSConnection(domain)
    except:
       print("Failed connecting to "+domain)
       sys.exit(1)
    else:
       print("Connected successfully to "+domain)
    try:
       print ( "Request: " + baseurl + data )
       conn.request("POST", baseurl+data)
    except:
       print("Failed uploading data")
       sys.exit(1)
    else:
       print("Uploaded data OK")
       sys.exit(0)

What log does a POST error get sent to? Nothing is showing up in emoncms.log.

Thanks.

[code formatted correctly - Mod]

For future reference, when posting code or bash output, please put in 3 ā€˜backticksā€™ (found at the top left of the keyboard normally) on a line on their own, then the code, then 3 more backticks on a line following the code.

    ```
    code
    ```

If it is something like php you can add a language identifier that after the first 3 backticks so ```php

Run the JSON through jsonlint.com and you will see it is not valid JSON.

[edit]

This is the wrong option

From the API help example

/input/post?node=emontx&fulljson={"power1":100,"power2":200,"power3":300,"time":"2021-05-10T07%3A32%3A48%2B01%3A00"}

fulljson is used as the legacy json option was not true, valid, JSON.

Also, emoncms does not deal with non-numeric values.

Thanks for the tips.

Iā€™m a little confused since this is code that was directly lifted from github:

ā€¦and referenced in the following post:

My solar power setup: Victron BMV to Emoncms

ā€¦and also generates no error when posting to emoncms.org.

emoncms.org and emonCMS run locally are not identical software. Therefore, the facilities available differ, there are also constraints caused by it being multi-user and publicly accessible.

1 Like

I have simplified the code and think I have the syntax correct now:

Connected successfully to 10.0.0.22
Request: /input/post?node=28&apikey=xxxxxxxxx&fulljson={"SOC":1000}
Failed uploading data
pi@emonpi:/opt/vedirect $

Is there a log that the post errors are dropped into so that I can look for messages?

Thanks.

Always a good idea to not post the API key.

Is that the Read/Write API key?

Anything in Emoncms logs (Set that to debug in the settings.ini file).

Where most logs areā€¦

ls -la /var/log/emoncms/

Edit: Try the URL from the Browser. You may need to URLencode the string.

My device is on a closed network behind a firewall with no access for use of the API key. Yes it is a read/write key.

My original post indicated that the emoncms.log file contains no information regarding this failure. I was interested in another possible log.

Thanks for the suggestion of using a browser, good idea. It works as expected from ab browser, though the only reply was ā€œokā€.

Still good practice.

Have you changed the log level to Debug?

That is the expected response.

Did you try to URL encode the string as I suggested?

There were two problems with the code from GitHub - karioja/vedirect: Simple VE.Direct reader for Python: 1) HTTPS connections are not supported for emonpi though python reports a good initial connection that then fails during the post; 2) data must be numerical.

I have reworked a section of code for an HTTP connection rather than HTTPS and added error/exception handling so that failures are reported properly. I also replace some alpha values with numbers. Looks like more work may be required for other Victron models other than the BMV-700 around non-numeric data.

def print_data_callback(data):
    data=repr(data)
    data = data.replace ( " ", "" )     # no spaces allowed
    data = data.replace ( "OFF", "0" )
    data = data.replace ( "ON", "1" )
    data = data.replace ( "x", "0" )
    print ( data )

    try:
       conn = httplib.HTTPConnection(domain)
    except Exception as ex:
#      print ( "Failed connecting to " + domain )
       print ( "Connect Exception: {}".format(type(ex).__name__))
       print ( "Connect Exception: {}".format ( ex ))
       sys.exit(1)
    else:
       print ( "Connected to " + domain )

    try:
       conn.request("POST", baseurl+data)
       r1 = conn.getresponse()
       msg = unicode ( r1.read(), "UTF-8" )
       print r1.status, r1.reason,  msg 
    except Exception as ex:
       print("Failed uploading data")
       print ( "Post Exception: {}".format(type ( ex ).__name__))
       print ( "Post Exception: {}".format ( ex ))
       sys.exit(1)
    else:
#      print("Uploaded data OK")
       sys.exit(0)

    #time.sleep(60)

Just as a side note, the emonhub Victron Interfacer documented at Victron Energy protocol integration with emonhub | Archived Forum and EmonHub Interfacers - Guide | OpenEnergyMonitor is not reliable. It will run for a few minutes then fail.

Thanks everyone for your help.

More correctly, the EmonPi is not configured for HTTPS by default. It can be configured to accept HTTPS.

good you got it working.