Send command via rest api

Hi Florian, or whoever can help me.

I’m looking for the simple way to send an output gpio command. Through ssh root it executes correctly:

echo 1 > /sys/class/gpio/gpio18/value

Assuming export and direction out of gpio18,

but when trying via the hosted api rest :

curl -u :APIKEY -d “{echo 1 >}” -X POST https://info-beamer.com/api/v1/device/31376/node/sys/class/gpio/gpio18/value

I receive an “ok” response, however the command is not executed. The logread shows me this: [u’sys/class/gpio/gpio18/value’, u’'] which suggests that two elements are expected.

I do this to change the state of a relay connected to an hdmi switch to change the inputs.

How can I fix this, without having to create a service package just to change send a value to a gpio?

Thank you.

The device command API isn’t meant for sending arbitrary data to files. It’s only for sending data directly into the running Lua code within the info-beamer process on the Pi so it can be handled by a callback set up with the util.data_mapper function.

Right now there isn’t an easy way to achieve what you’re trying to do. To make this easier, there will soon be a way to also send messages directly to a package service. Once that’s available you can create a small service that, upon receiving an event will write to the file in /sys. Doing any of that without a package service still won’t be possible, but this approach will make it easier. I’ll see if this can be released by tomorrow.

1 Like

Nevermind. It’s released now. There’s now a new service command API call. It allows you to send small data packets to a running package service. Here’s how it works for your use case:

  • Add a small package service to your package to handle the incoming data. A package service implementation might look like this:

    #!/usr/bin/python 
    import socket, os 
    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) 
    sock.bind(("127.0.0.1", int(os.environ["SERVICE_DATA_PORT"]))) 
    while 1: 
      pkt, _ = sock.recvfrom(1000) 
      path, value = pkt.split(':', 1) 
      if path == "gpio": 
        with open("/sys/class/gpio/gpio18/value", "wb") as f: 
          f.write(value)
    

    This binds a UDP socket to the port provided to the package service via the SERVICE_DATA_PORT environment variable and waits until it receives an incoming packet.

  • You can now send such packets using the new API endpoint:

    curl https://info-beamer.com/api/v1/device/$DEVICE_ID/service/root/gpio \
     -u:$API_KEY -d "data=1"
    
  • Assuming the device runs the latest testing release, this will result in a UDP packet with the content gpio:1 sent to that package service. the recvfrom will receive that packet, split it at the first : and write the data value to the GPIO sys file.

1 Like

Thanks Florian for implementing this so quickly, it works great.

That’s good to hear! This is something that I felt was missing anyway so it’s a good opportunity to add it now.

1 Like