UniPi changing relay state



  • Hi

    Sorry I'm new to this and trying to set up the UniPi with my raspberry Pi but i cant get the relays to change state. I cant see what im doing wrong I've followed all instructions.
    Any help greatly appreciated

    Thanks.


  • administrators

    Could you describe what exactly are you using to change the relay state, i.e. which software and image?

    You may find it easiest to use Mervis, at least to try things out.



  • Hi Tomas

    Im using Python to programme it.

    this is my code:

    payload = "{1}"
    
    conn.request("GET", "/json/relay/%7Bcircuit%7D", payload)
    
    res = conn.getresponse()
    data = res.read()
    
    print(data.decode("utf-8"))
    
    ##payload = "{\"1\":\"0\",\"timeout\":\"0\",\"mode\":\"string (optional)\",\"pwm_freq\":\"2000",\"pwm_duty\":\"50\",\"alias\":\"string (optional)\"}"
    ##headers = { 'content-type': "application/json" }
    ##
    ##conn.request("POST", "/json/relay/%7Bcircuit%7D", payload, headers)
    ##
    ##res = conn.getresponse()
    ##data = res.read()
    ##
    ##print(data.decode("utf-8"))
    
    payload = "{2}"
    
    conn.request("GET", "/json/relay/%7Bcircuit%7D", payload)
    
    res = conn.getresponse()
    data = res.read()
    
    print(data.decode("utf-8"))
    
    ##payload = "{\"2\":\"0\",\"timeout\":\"0\",\"mode\":\"string (optional)\",\"pwm_freq\":\"2000",\"pwm_duty\":\"50\",\"alias\":\"string (optional)\"}"
    ##headers = { 'content-type': "application/json" }
    ##
    ##conn.request("POST", "/json/relay/%7Bcircuit%7D", payload, headers)
    ##
    ##res = conn.getresponse()
    ##data = res.read()
    ##
    ##print(data.decode("utf-8"))
    

    I have the changing state code commented out because its giving me errors.


  • administrators

    You need to replace "%7Bcircuit%7D" with e.g. 1_01, or whichever circuit you wish to use. The %7B%7D is caused by the api-docs interface escaping characters '{' and '}', which is meant as a hint for the implementation, to be replaced by the user.

    You can get a list of circuits and device types present by calling/visiting "YOUR-NEURON-IP:PORT/rest/all", sans the quotes and substituting the IP and port for proper values.

    I hope this helps, please do write if you have further issues.



  • @TomasKnot have you any examples of the code? I cant find any online that I can understand.
    Thanks



  • How do I know which circuit is numbered in which way?
    Is there examples online?



  • @TomasKnot said in UniPi changing relay state:

    "YOUR-NEURON-IP:PORT/rest/all"

    Hi Tomas

    Im using the unipi 1.1. does that still work with the
    "YOUR-NEURON-IP:PORT/rest/all" ?

    thanks again


  • administrators

    Yes, it should work for the UniPi 1.1 as well. You can simply go to e.g. 192.168.1.146:80/rest/all via a standard browser, to see the available devices.

    You can look into the source code for the website GUI if you want to see an example of using the rest/json API in practice.



  • @TomasKnot
    Should that ip address work for all or should i use the pi's ip address?


  • administrators

    You'll have to use your own ip address of course, hence the e.g.