Logic Machine Forum
WebCGI Trigger - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: WebCGI Trigger (/showthread.php?tid=2422)



WebCGI Trigger - JXA - 10.01.2020

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=account=apiuser&password=password&port=1&destination=15880270900&content=Hello world!!

If possible step by step for beginners...

Many Thanks!


RE: WebCGI Trigger - Joep - 11.01.2020

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)


RE: WebCGI Trigger - JXA - 12.01.2020

Many Thanks for your message.
I will test it.

Thanks!


RE: WebCGI Trigger - JXA - 14.01.2020

(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


RE: WebCGI Trigger - admin - 14.01.2020

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.


RE: WebCGI Trigger - JXA - 14.01.2020

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!


RE: WebCGI Trigger - admin - 15.01.2020

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