Control Raspberry Pi with Corona SDK + ACE Pi

Hello!

Take your Raspberry Pi projects to the next level using Corona SDK and the new Coronium ACE Pi developer release for Raspbian Jessie.

You have access to the full ACE stack, plus additional modules directed at the RPi (like GPIO, I2C, Serial, RPi Userland, and more.).

As an example of how simple the API is, here is how you would pass your RPi tempature to a Corona app (an ACE Pi instance is assumed):

On the ACE Pi (Raspberry Pi) side:

[lua]
– app/rpi/api.lua

local api = ace.api()

function api.get.temp()

  return { temp = ace.vccmd(‘measure_temp’) }

end

return api
[/lua]

This will create a callable endpoint, using a “GET” method, at < your.raspberry.pi>:8081/rpi/temp

_ On the Corona SDK side: _

[lua]
– main.lua

local function onResponse( evt )

  if evt.phase == ‘ended’ then

    local res = json.decode( evt.response )

    print( res.temp ) --> temperature output

  end

end

network.request( “<your.raspberry.pi>:8081/rpi/temp”, “GET”, onResponse )
[/lua]

You can the display the data however you wish.  Now, a little more “advanced” example, this time using GPIO with getter/setter abilities:

On ACE Pi (Raspberry Pi) side:

[lua]
– app/rpi/api.lua

local api = ace.api()

–well use a POST for the example

function api.post.gpio( in_data )

  local pin = in_data.pin

  local val = in_data.val or nil

  

  local gpio_pin

  if gpio_val then

    – If there is a ‘val’, write to GPIO pin

    gpio_pin = ace.gpio( pin, “in” )

    gpio_pin:write( val )

  else

    – Read the pin value

    gpio_pin = ace.gpio( pin, “out” )

    local val = gpio_pin:read()

  end

  – Clean up

  gpio_pin:close()

  

  – Return some data to Corona

  return { val = val, ok = true }

    

end

return api
[/lua]

This endpoint can be used, via “POST”, at <your.raspberry.pi>:8081/rpi/gpio

_ On the Corona SDK side: _

[lua]
– main.lua

– Set up response listener

local function onReponse( evt )

  if evt.phase == ‘ended’ then

    local res = json.decode( evt.response )

    print( res.ok, res.val )

  end

end

– Call POST network request on your endpoint

network.request( “<your.raspberry.pi>:8081/rpi/gpio”, “POST”, onResponse,

{

  – The POST body, JSON encoded.

  body = json.encode( { pin = 10, val = 1 } )

})
[/lua]

There are plenty of possibilities, especially when you start to add in sensors controlled by your Corona app.

For more information on installing Coronium ACE Pi , click here.

For more information regarding Corona SDK network.request(), click here.

And finally, feedback and discussion can be found here.

Enjoy, and happy coding. :slight_smile: