Looks like your example wget is doing an http POST request. I’ve put some sample code below that I use for doing this in lua. Of course you can continue to use the os.execute if you wish. What I’m not sure about is how to add the login/logout that you need in the sample code. You’ll need to do some research there or maybe someone else will have a suggestion. Also, this is for sending JSON - you may need a change there too.
[code]local http = require(“socket.http”)
local ltn12 = require(“ltn12”)
local path = “http://192.168.1.203/tstat”
local payload = [[ {“t_mode”:1,“t_heat”:61} ]]
I’m not sure how much flexibility the power strip I’m looking to control has, it seems reasonable basic - so I’m continuing to research, both online (wget Command in Linux) and the Vera wiki which stats the following
Module: luup.inet
function: wget
parameters: URL (String), Timeout (Number), Username (String), Password (String)
returns: operationStatusCode (Number), content (String), httpStatusCode (Number)
This reads the URL and returns 3 variables: the first is a numeric error code which is 0 if successful. The second variable is a string containing the contents of the page. The third variable is the HTTP status code. If Timeout is specified, the function will timeout after that many seconds. The default value for Timeout is 5 seconds. If Username and Password are specified, they will be used for HTTP Basic Authentication.
As for wget or luup.inet.wget; I’ve seen a few examples used.
I now have examples of command line entries that work for my Energenie PMS-LAN, the quest now is to see how I can get Vera to run them. As it seem both curl or wget can do the job.
The first thing you need to do is login (the default password is 1, so I have left it as that for those examples) - and this times out after I think 3-5 mins
My suggestion is to turn on the verbose option in curl to see what is doing. (I’m sure you can do the same in wget but I haven’t tried it.) Look for this option for curl. Then you can see what it is doing and piece it back together in lua. I’m sure someone has done something similar, but this will give you a better idea of what you need.
-v, --verbose
Makes curl verbose during the operation. Useful for debugging and seeing what’s going on “under the hood”. A line starting with ‘>’ means “header data” sent by curl, ‘<’ means “header data” received by curl that is hidden in normal cases, and a line starting with ‘*’ means additional info provided by curl.
If you only want HTTP headers in the output, -i, --include might be the option you’re looking for.
If you think this option still doesn’t give you enough details, consider using --trace or --trace-ascii instead.
I suggest accessing Vera over SSH, and trying these commands on the shell to see if you get any errors or not. If they work, you can run the commands from LUUP using
@parkerc, do you have AltUI installed? I’ve been looking at something similar for other reasons and I think what you need isn’t too hard, but you’ll need to do a little experimentation. The test code window in AltUI makes this very easy. Otherwise you can use luup.log() instead of the print’s below.
[code]
function testFunc()
local http = require(“socket.http”)
local ltn12 = require(“ltn12”)
local path = “http://192.168.1.90/”
local reqbody = “cte4=1” – your function can change this as needed, and you can combine with others such as the pw like this:
– pw=1&cte4=1
local response_body = { }
local res, code, response_headers, status = http.request
{
url = path,
method = “POST”,
headers =
{
[“Content-Type”] = “text/html”, – this may need some adjustment, but give it a try
[“Content-Length”] = reqbody:len()
},
source = ltn12.source.string(reqbody),
sink = ltn12.sink.table(response_body) – can possibly delete this
}
print("res: " … res)
print("code: " … code)
print("status: " … status)
end
Any chance you could try this command on something other than the Vera?
curl -v -d "cte4=1" http://192.168.1.90/
The -s makes it silent so there is no debug info there. (If you have nothing else, try the above on the Vera, replace -s with -d)
I’m surprised you got a nil result. That makes it sound like it didn’t get to talk to the server. I just tried the code to a local web server and I got unauthorized which is what I would expect. However, this is running on openLuup - it is possible there is a difference with the real Vera. I’ll have to try that when I get a chance. I think you are close but it isn’t quite right yet.
function testFunc()
local http = require(“socket.http”)
local ltn12 = require(“ltn12”)
local path = “http://192.168.1.90/”
local reqbody = “cte4=1” – your function can change this as needed, and you can combine with others such as the pw like this:
– pw=1&cte4=1
local response_body = { }
local res, code, response_headers, status = http.request
{
url = path,
method = “POST”,
headers =
{
[“Content-Type”] = “text/html”, – this may need some adjustment, but give it a try
[“Content-Length”] = reqbody:len()
},
source = ltn12.source.string(reqbody),
sink = ltn12.sink.table(response_body) – can possibly delete this
}
print("res: ", res)
print("code: ", code)
print("status: ", status)
end
try changing the content-type header in the script from text/html to application/x-www-form-urlencoded as shown in your debug output.
see if you can do the login instead of the cte as a test. Change the path to this: http://192.168.1.90/login.html, and change the reqbody to “pw=1”. See if that behaves any differently. Still a timeout?
You are seeing a timeout - the question is why? The server must not like something that is being sent, but I’m not sure what it doesn’t like… I suppose you could add the ‘Accept: /’ header as well, but I don’t see why that would case a timeout.
Maybe we are trying to be too complicated. Try the simple form instead:
local status, statusMsg = http.request{
url = "http://192.168.1.90",
body = "cte4=1"}
I’m trying to think of why you get a timeout. One thing that popped to mind was that in the code we have ‘sink’ set to a table - but what is being returned is html. Perhaps that is causing the timeout as the request is waiting for something else? The above shouldn’t have that issue. And you could try the other code without the sink line.
If this doesn’t work, it might be time for Wireshark. I still think this is almost working and there is just a small problem. It really shouldn’t be rocket science.
It would be great to be able to use the LUA libraries and void using os.execute, but for some reason any combination I’ve tried does not seem to work. Which seems so strange if it works via Curl ??!??
What I have found is that I can send multiple requests via a single curl request - so it logs on, carries out the change and then logs out - so that makes a single command line entry much easier to complete.
Okay, we’ll move over to the other thread. I am 99.9% sure you can do this from within lua and don’t need the os.execute with curl. And you can do multiple settings with the http request such as: body = “pw=1&cte4=1”. But debugging what is going on isn’t easy.
Best Home Automation shopping experience. Shop at Ezlo!