This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm whether you accept or reject these cookies being set.

WebCGI Trigger
#1
Hello,
i am a new user of LM & Lua.


Question: How can i trigger a Web CGI by a switch?

Example: If the input state of a the switch "XY" from  0 to 1 trigger :

http://192.168.5.176/cgi/WebCGI?1500101=...tent=Hello world!!

If possible step by step for beginners...

Many Thanks!
Reply
#2
Try the code below and see what you get in the log. That's the response from your system. Please change the url to the correct url to make it work.

local http = require "socket.http"

local data = ""

local function collect(chunk)
if chunk ~= nil then
data = data .. chunk
end
return true
end

local ok, statusCode, headers, statusText = http.request {
method = "GET",
url = "http://192.168.5.176/cgi/WebCGI?1500101=...tent=Hello",
sink = collect
}

log("ok\t", ok);
log("statusCode", statusCode)
log("statusText", statusText)
log("headers:")
for i,v in pairs(headers) do
log("\t",i, v)
end

log("data", data)
Reply
#3
Many Thanks for your message.
I will test it.

Thanks!
Reply
#4
(12.01.2020, 20:50)JXA Wrote: Many Thanks for your message.
I will test it.

Thanks!

Hello,
i have tested the code.
But is still not working.

# This is a  trigger Message to a SMS Gateway, you can send SMS by WebCGI only with a simple SIM Card!

"Event for Fensterkontakt  - Button A (1/1/20) 14.01.2020 14:29:08
* arg: 1
  * string: data
* arg: 2
  * string: <HTML><HEAD><TITLE>400 Bad Request</TITLE></HEAD>
<BODY><H1>400 Bad Request</H1>
Your client has issued a malformed or illegal request.
</BODY></HTML>"


If you trigger by a Browser all is fine!

Any Idea what's wrong? "Bad Request"

Thanks


Attached Files Image(s)
           
Reply
#5
Your end device might expect some headers or some headers are not handled properly by it. You can try sending raw request without any headers like this:
Code:
ip = '192.168.5.176'
qs = '/cgi/WebCGI?1500101=account=apiuser&password=password&port=1&destination=15880270900&content=Hello'

sock = require('socket').tcp()
sock:settimeout(5)

res, err = sock:connect(ip, 80)
if res then
  sock:send('GET ' .. qs .. ' HTTP/1.1\r\n\r\n')
  res = sock:receive('*a')
  log(res, err)
end

Open dev tools in browser (F12), go to Network tab, right click the request and select "Copy headers". This data can be used to send exactly the same request from LM.
Reply
#6
Hello,
yes the code is working fine without any changes.

But i still have 2 questions:

About the SMS Message    "...... content=Hello this is a test SMS"
-> if you have Space character in your Message it will not be send. (sending via Browser works fine)
Any Placeholder required in Lua?

How can i send the Input Status (0/1) as "var" in the SMS Content?
--> '...... content=Hello the Input status is "var" '    (var means 0 or 1)

Many Thanks for your support!
Reply
#7
If using an event script you can use event.getvalue() to add current value to content variable. Use socket.url.escape to convert spaces and other characters to correct escape sequences.
Code:
require('socket.url')

value = event.getvalue()
content = 'Input status is ' .. tostring(value)
content = socket.url.escape(content)
qs = '/cgi/WebCGI?1500101=account=apiuser&password=password&port=1&destination=15880270900&content=' .. content
Reply


Forum Jump: