Author Topic: Switch to send post json data  (Read 119 times)

Offline MrNeil

  • Newbie
  • *
  • Posts: 7
  • Karma: +1/-0
Switch to send post json data
« on: November 13, 2017, 05:23:28 am »
Hi all,

I am looking for a way to integrate my aircon with my vera. I am pretty new at home automation, and very much so a vera/zwave novice. I've seen some posts around the place that use luup to send json payloads, but im not sure how to associate that payload with a switch, or where the luup code is meant to go Can anyone help??

What im wanting to do basically the same as whats being achieved on this fibaro: https://forum.fibaro.com/index.php?/topic/21650-rest-api/&_fromLogin=1

any help appreciated!

Neil.

Offline MrNeil

  • Newbie
  • *
  • Posts: 7
  • Karma: +1/-0
Re: Switch to send post json data
« Reply #1 on: November 13, 2017, 08:19:52 pm »
getting further with this... i figured out that i just need to use virtual switches with lua code. trying to get LUA code to do a curl with the json request. it works natvely with curl, but not with os.execute. This is what im trying to run:

os.execute('curl -X POST -H "Content-Type: application/json" -d  "{\"DA\":{\"amOn\":true}}" https://actron.ninja.is/rest/v0/device/xxxxx?user_access_token=xxxxx')

Anyone know why it may not work?

Offline MrNeil

  • Newbie
  • *
  • Posts: 7
  • Karma: +1/-0
Re: Switch to send post json data
« Reply #2 on: November 14, 2017, 12:38:18 am »
Woo - have it all sorted. ended up using socket http to get my Actron Connect aircon to work with a vera scene/virtual switch. If anyone else happens to need it, i used the following code:

local http=require("socket.http")
local https = require ("ssl.https")
local siteurl = "https://actron.ninja.is/rest/v0/device/xxxx?user_access_token=xxxx"
local payload = [[ {"DA":{"amOn":1}} ]]
local respback = { }
  local res, code, response_headers, status = http.request
  {
    url = siteurl,
    method = "POST",
    headers =
    {
      ["Content-Type"] = "application/json",
      ["Content-Length"] = payload:len()
    },
    source = ltn12.source.string(payload),
    sink = ltn12.sink.table(respback)
  }

All works with vera concierge now too :)
« Last Edit: November 14, 2017, 09:19:20 pm by MrNeil »

Offline akbooer

  • Master Member
  • *******
  • Posts: 5234
  • Karma: +226/-67
  • "Less is more"
Re: Switch to send post json data
« Reply #3 on: November 14, 2017, 02:52:01 am »
Glad you have it working, and kudos for reporting your progress.

I may be wrong, but I think you'll find that even though you're using "https" as a string in the url, this will not go over a secure socket layer unless you explicitly use the https.request() function call.

In fact, the code as it stands is not using the required ssl module at all.
3x Vera Lite-UI5/Edge-UI7, 25x Fibaro, 23x TKB, 9x MiniMote, 2x NorthQ Power, 2x Netatmo, 1x Foscam FI9831P.
Razberry, MySensors Arduino, HomeWave, AltUI, DataYours, openLuup, ZWay, ZeroBrane Studio.

Offline MrNeil

  • Newbie
  • *
  • Posts: 7
  • Karma: +1/-0
Re: Switch to send post json data
« Reply #4 on: November 14, 2017, 08:49:50 pm »
Interesting - I see what you mean and removed the "local https = require ("ssl.https")" block. It seems to still be working, so you must be correct! thanks!