Hi
I am struggling to get this code working and would appreciate some help:
I can query any X10 device on my server using a query command sent over HTTP like this:
http://192.168.2.1:8086/?x10command=~queryplc~"G13 ON"
this will return in a browser window a response:
H -Boiler Switch:G13:Power Line:100
I can run that code from Vera using luup.inet.wget
However, I cannot figure out how to grab that result/response and put it into a variable. That part that matters in the response is the last digits - 0 is off and 100 is on. Anything in between is a dimmer result.
My overall goal here is to have a method of keeping virtual switches updated with their real-time statuses.
I have accomplished this in reverse where the X10 server sends HTTP gets to VERA and updates the switch but that is more prone to error and fault due to the software controlling it.
So, to clarify - my question is - How do I grab a result from a HTTP get and store it as usable variables?
ORIGINAL POST:
[s][i]My LUUP code:
[s][code] local http = require(“socket.http”)
– 5 Second timeout
http.TIMEOUT = 5
– The return parameters are in a different order from luup.inet.wget(…)
result, status = http.request(“http://192.168.2.1:8086/?x10command=~queryplc~“G13%20ON””, “run=run”)
luup.inet.wget(“http://autoremotejoaomgcd.appspot.com/sendnotification?key=XXXXXXXX&title=”… status …“&text=”… status …“&sound=1”)[/code]
[i][s]The final part is just some debugging code to send a notice to my phone so I can check results…
I get a status result of 200… so the response is fine…
But I need to some also retrieve the H -Boiler Switch:G13:Power Line:100
My goal here is to have Vera query the device and update its status.[/s][/i]
I read your first post but did not, and still do not, understand what your question is. It may be that I am not the only one in this position. Perhaps you can explain exactly what you want help with?
LOL… fair enough… let me edit the first post in case anyone else reads it…
Update: First Post Updated
Assuming the full string is contained in [tt]result[/tt], you could filter out the specific bit you’re interested in with a pattern. Roughly:
switchState = result:match(" Line:(%d+)")
[quote=“oTi@, post:5, topic:178860”]Assuming the full string is contained in [tt]result[/tt], you could filter out the specific bit you’re interested in with a pattern. Roughly:
switchState = result:match(" Line:(%d+)")
Thanks… but does this mean the variable switchState will now contain the http get result?
see I think this is the problem… when I try to print result… its empty or at least I cannot see any way other than to print it to an alert I send to my phone to test…
I can return the response code (200) without issue…
I have spent a day trying every form of syntax I can find… I guess its not possible to parse the content from a HTTP request…
I tried looking at other plugins, such as the Weather Plugin that must must parse the results to make them usable…
When you do the request in a browser, do you get the expected result?
Here’s an example (that will dump your external IP to the status window):
[code]local http = require(“socket.http”)
result, status = http.request(“http://checkip.dyndns.com”)
local extIP = result:match(“(%d+.%d+.%d+.%d+)”)
luup.task(extIP,2,“External IP”,-1)[/code]
[quote=“oTi@, post:8, topic:178860”]When you do the request in a browser, do you get the expected result?
Here’s an example (that will dump your external IP to the status window):
[code]local http = require(“socket.http”)
result, status = http.request(“http://checkip.dyndns.com”)
local extIP = result:match(“(%d+.%d+.%d+.%d+)”)
luup.task(extIP,2,“External IP”,-1)[/code][/quote]
Hey
Yes
When I run this:
http://192.168.2.1:8086/?x10command=~queryplc~"G13 ON"
I get this:
H -Boiler Switch:G13:Power Line:100
Let me try your ideas…
OK… Your idea worked… I can see the result now…
[code]local http = require(“socket.http”)
result, status = http.request(“http://192.168.2.1:8086/?x10command=~queryplc~“G13%20ON””)
local extIP = result:match(" Line:(%d+)")
luup.task(extIP,2,“External IP”,-1)[/code]
I clearly don’t understand the syntax of the luup.task and need to read up on that in order to refine things… but either way this code got me on track… Thanks for your help!
4 days in total of reading up on this… and I still didn’t get there by myself
Nice!
The [tt]luup.task[/tt] was just a quick/dirty/easy way of showing you a result. (It’s described here.)
Think about using the log, the Variable Container plug-in, etc. as better alternatives.
In case this helps anyone - here is the final code:
[code]local serverurl=(“http://192.168.2.1:8086/?x10command=~queryplc~”)
local switchaddress=(“%22G13%20ON%22”)
local fullcommand=(serverurl … switchaddress)
local http = require(“socket.http”)
result, status = http.request(fullcommand)
local switchstate = result:match(" Line:(%d+)")
luup.task(switchstate,1,“Switchstate”,-1)
if (switchstate == “100”) then
switchstate = “1”
luup.call_action(“urn:upnp-org:serviceId:VSwitch1”, “SetTarget”, {newTargetValue = “1”}, 139)
else
switchstate = “0”
luup.call_action(“urn:upnp-org:serviceId:VSwitch1”, “SetTarget”, {newTargetValue = “0”}, 139)
end[/code]