Relay Command with Python



  • Hi

    I'm trying to command a relay with python using the jsonrpclib, my relay is worked well but I have an error.
    Can you help me?

    from jsonrpclib import Server
    
    s=Server("http://IPAdressUnipi/rpc")
    
    relay1=s.relay_get(1)
    print relay1[0]
    value=int(1-relay1[0])
    print value
    
    if value:
            s.relay_set(1,1)
    else:
            s.relay_set(1,0)
    
    

    Traceback (most recent call last):
    File "unipi.py", line 12, in <module>
    s.relay_set(1,1)
    File "/usr/local/lib/python2.7/dist-packages/jsonrpclib/jsonrpc.py", line 288, in call
    return self.__send(self.__name, args)
    File "/usr/local/lib/python2.7/dist-packages/jsonrpclib/jsonrpc.py", line 239, in _request
    return response['result']
    TypeError: 'NoneType' object has no attribute 'getitem'



  • Hi

    I'm trying to command a relay with python using the jsonrpclib, my relay is worked well but I have an error.
    Can you help me?

    from jsonrpclib import Server
    
    s=Server("http://IPAdressUnipi/rpc")
    
    relay1=s.relay_get(1)
    print relay1[0]
    value=int(1-relay1[0])
    print value
    
    if value:
            s.relay_set(1,1)
    else:
            s.relay_set(1,0)
    
    

    Traceback (most recent call last):
    File "unipi.py", line 12, in <module>
    s.relay_set(1,1)
    File "/usr/local/lib/python2.7/dist-packages/jsonrpclib/jsonrpc.py", line 288, in call
    return self.__send(self.__name, args)
    File "/usr/local/lib/python2.7/dist-packages/jsonrpclib/jsonrpc.py", line 239, in _request
    return response['result']
    TypeError: 'NoneType' object has no attribute 'getitem'



  • Hey everyone

    I had the same problem with python using the jsonrpclib.
    After hours of debugging i found out where the error cames from.
    Its in file: "/usr/lib/python2.7/socket.py" , in class: "_fileobject(object)" , line: 267 : "self._rbuf = StringIO()" .
    The problem is, that "StringIO()" dont receives (most of the time) the "out-put-stream" of the relay, which must return something like a "result" (callback).
    Like i said, sometimes it works well and it receives the data, which is a dict obj, but most of the time it doesnt.
    So when "StringIO()" doesnt success to receives the data, then it returns an object with "None".
    And this "None"-Object occours the TypeError, in: "/usr/local/lib/python2.7/dist-packages/jsonrpclib/jsonrpc.py" , class ServerProxy, def _request() : "return response['result']".
    This func trys to return the value of the dict key "result", but if "StringIO()" returns us "None" Object then the Type Error occours, because getitem is a method for dict objects.
    ("response" is the object, which we get with "StringIO()"…)

    The next step i think would be to find out why "StringIO()" cant receive "most of the time" the right object(Callback) when changing the relay state!

    But first a workaround:
    1- sudo nano /usr/local/lib/python2.7/dist-packages/jsonrpclib/jsonrpc.py
    2- Press "Strg + w" to search for "return response['result']"
    3- editdef _request(self, methodname, params, rpcid=None): request = dumps(params, methodname, encoding=self.__encoding, rpcid=rpcid, version=self.__version) response = self._run_request(request) check_for_errors(response) return response['result']to```
    def _request(self, methodname, params, rpcid=None):
    request = dumps(params, methodname, encoding=self.__encoding,
    rpcid=rpcid, version=self.__version)
    response = self._run_request(request)
    check_for_errors(response)
    if response == None:
    return response
    else:
    return response['result']

    5- Press "Strg + x" to exit
    6- Finish ![;)]({SMILIES_PATH}/icon_e_wink.gif "Wink")
    
    I hope i could help you all!!! And Tomas enjoy your time in Japan. ![:)]({SMILIES_PATH}/icon_e_smile.gif "Smile")
    
    Best regards,
    Martin M.
    
    Test-Script:
    

    #!/usr/bin/python

    from jsonrpclib import Server

    s=Server("http://192.168.101.58/rpc")
    print s.relay_get(1)
    s.relay_set(1, 1)
    print s.relay_get(1)

    Output if "StringIO()" receives "None":
    

    pi@raspberrypi ~ $ sudo ./unipi-test.py
    ['{"jsonrpc": "2.0", "result": [1, false], "id": "anxlmi4b"}']#print data by "StringIO()"
    [1, False] #print s.relay_get(1)
    #print data by "StringIO()"
    Traceback (most recent call last):
    File "./unipi-test.py", line 10, in <module>
    s.relay_set(1, 1)
    File "/usr/local/lib/python2.7/dist-packages/jsonrpclib/jsonrpc.py", line 287, in call
    return self.__send(self.__name, args)
    File "/usr/local/lib/python2.7/dist-packages/jsonrpclib/jsonrpc.py", line 239, in _request
    return response['result']
    TypeError: 'NoneType' object has no attribute 'getitem'

    
    Output if "StringIO()" magically receives a dict object:
    

    pi@raspberrypi ~ $ sudo ./unipi-test.py
    ['{"jsonrpc": "2.0", "result": [1, false], "id": "ysh1pxs3"}'] #print data by "StringIO()"
    [1, False] #print s.relay_get(1)
    ['{"jsonrpc": "2.0", "result": 1, "id": "uzlgs48r"}'] #print data by "StringIO()"
    ['{"jsonrpc": "2.0", "result": [1, false], "id": "6q6rlvc8"}'] #print data by "StringIO()"
    [1, False] #print s.relay_get(1)



  • Hello everyone,

    I think it is not related to evok itself but to the jsonrpclib. According to the github https://github.com/joshmarshall/jsonrpc ... its/master there were some recent changes so please try to install older version of jsonrpclib....

    I am currently in Japan but will be back in two weeks so I will take a look at it then.



  • Dear Community,

    we need to find out why "StringIO()" cant receive "most of the time" the right object(Callback) when changing the relay state!
    Because at the moment I just found a workaround but not a solution for this problem.
    So i would really appreciate if someone have an idea!!!

    Best regards,
    Martin M.



  • Thanks this is working for a while…
    Regards,



  • I have the same problem, do you have any solution?



  • I tried to replicate the issue but did not succeeded. Could you descibe the whole setup (run the script on the same host?, os version?, …)? basicaly everything that might help me to get this error.



  • Hello everyone,
    @Andres atm just a work around but not a real solution.
    @Tomas I ve tested it on a fresh install of RASPBIAN JESSIE and installed your API with "install-evok.sh" like in your Guide.
    Then i ve just run my script like described above.
    What do u mean with, "on the same host" ?



  • Ok, now I get it. The new version of debian might be the issue… I have not tested it because a lot of things have changed. I recommend moving back to wheezy...



  • Are we going to get a solution?



  • Like I said. The solution is to get Raspbian wheezy. Jessie is not supported



  • I tried with Raspian Weezy, and a get the same result.



  • Hey again unipi users,

    like i tought… it has nothing to do with the dist version or the jsonrpclib.
    Tomas i ve descripded excatly where the problem cames from, so you just need to find out,
    why the function "StringIO" doesnt get the right callback.
    I dont know how the unipi and Your API are communicating, so its up to you.
    It would be a pleasur if you could find time to explain me your API base, then i also could find out,
    why this problem occours.

    Kind regards,
    Martin M.



  • I would like to help you guys but I still do not have enough information because this issue does not appear in my setup. What RPi do you use? Are you polling the API from the same machine (UniPi) or from different computer? Any other packages installed? What OS+version? Basically anything that might help me to achieve this behavior.



  • Hey guys,

    i just bought one of your UniPis and I am having problems using evok with python.
    I am using a brand new Raspberry Pi 3 B with the very newest Raspbian Jessie.

    my python script:

        from jsonrpclib import Server
        s=Server("http://raspberrypi")
        #s.relay_set(1,1)
        s.relay_get(1)
    

    console's response:
    @1zw2mn99:

    Traceback (most recent call last):
    File "unipi.py", line 4, in <module>
    s.relay_get(1)
    File "/usr/local/lib/python2.7/dist-packages/jsonrpclib/jsonrpc.py", line 288, in call
    return self.__send(self.__name, args)
    File "/usr/local/lib/python2.7/dist-packages/jsonrpclib/jsonrpc.py", line 237, in _request
    response = self._run_request(request)
    File "/usr/local/lib/python2.7/dist-packages/jsonrpclib/jsonrpc.py", line 255, in _run_request
    verbose=self.__verbose
    File "/usr/lib/python2.7/xmlrpclib.py", line 1273, in request
    return self.single_request(host, handler, request_body, verbose)
    File "/usr/lib/python2.7/xmlrpclib.py", line 1321, in single_request
    response.msg,

    I have enabled the real time clock as described in your wiki (http://unipi.technology/wiki/Installing ... d_software)
    $ sudo hwclock -r
    Fri 27 May 2016 11:59:50 UTC -0.870418 seconds
    so, works.

    I would appreciate if you have any advide for me!



  • ..and my UniPi control panel does not work either. there are no buttons to change the states of any output. maybe this can be a hint about me having forgotten any installation (but i dont think so!)



  • and it may also be interesting to see my i2detect output:

    pi@unipi:~ $ i2cdetect -y 1
    
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
    

    00: -- -- -- -- -- -- -- -- -- -- -- -- --
    10: -- -- -- -- -- -- -- -- 18 -- -- -- -- -- -- --
    20: 20 -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    50: 50 -- -- -- -- -- -- 57 -- -- -- -- -- -- -- --
    60: -- -- -- -- -- -- -- -- 68 -- -- -- -- -- -- UU
    70: -- -- -- -- -- -- -- --



  • okay I am finding more and more information that might be helpful:
    I have this error under http://unipi/rest/relay/1/value
    500: Internal Server Error
    some people wrote about the same issue--it always got solved anyhow, but it could never really determined where the problem comes from.
    i installed with wget and with git, I plugged it to different plug sockets. i am using the delivered power supply.



  • I forgot to put a CR2023 battery on the unipi! damn it i will get one and then come back and tell you if this was the problem!