This is an old revision of the document!
Table of Contents
Integration into other systems
WebHMI allows you to interact with other systems by running HTTP requests. For this, WebHMI has a library Lua-cURLv3. Using it you can easily send data to third-party systems and also request the necessary data from these systems.
This function is available since version 2.6.4520.
POST example
An example of a program executing an HTTPS request to send data as a file at URL https://demo.com/upload:
function main (userId) INFO("Doing CURL"); c = cURL.easy_init() c:setopt_url("https://demo.com/upload") c:setopt_timeout(5) postdata = { name = { file="dummy.html", data="<html><bold>bold</bold></html>", type="text/html" } } c:post(postdata) c:perform() c:close() INFO("CURL done"); end
Note that for SSL certificates correctly function, you need to have an exact local time. If there is a significant deviation of the local clock, https requests may not work. We recommend using time synchronization via NTP server.
GET example
Example of requesting a number in the GET request at URL http://demo.example.com/read-data?param=32 and writing it to the register with id=4356:
function main (userId) c = cURL.easy_init() c:setopt_timeout(5) c:setopt_url("http://demo.example.com/read-data?param=32") local res = ""; c:perform({ writefunction = function(str) res = res .. str; end}) c:close() WriteReg(4356, res); end
Implementing other requests like PUT
To implement other requests, you can use special field in the cURL for the custom methods:
function main (userId) INFO("Doing CURL"); str = '' c = cURL.easy_init() c:setopt_url("http://192.168.1.230:8000") c:setopt_timeout(15) c:setopt{[cURL.OPT_CUSTOMREQUEST] = "PUT"} -- this is custom method c:setopt{ [cURL.OPT_POSTFIELDS] = 'Content-type: text/html' } c:post({'THIS IS CONTENT OF THE PUT'}) c:perform({writefunction = function(res) str = res end }) c:close() INFO("CURL done\n" .. str); end
function main (userId) -- Add your code here INFO("the sript has run") write_reg_with_API(1, "new string") end function write_reg_with_API(reg_id, val_) c = cURL.easy_init() c:setopt_url("http://127.0.1.1/api/register-values/" .. reg_id) c:setopt_timeout(2) c:setopt{[cURL.OPT_CUSTOMREQUEST] = "PUT"} c:setopt{[cURL.OPT_POSTFIELDS] = '{"value":"' .. val_ .. '"}'} c:setopt_httpheader({ 'Content-type: application/json', 'Accept: application/json', 'X-WH-APIKEY: 976FA578CF3B11ADA06564464B1F01823AF81C3D' }) c:perform({writefunction = function(res) str = res end }) c:close() end
Example - connecting to a remote module via its web server
Most currently produced remote i/o modules have configuration and status web-page for diagnostic and setup. When you have a module which is not supported by the WebHMI and the usage of the custom protocol feature is complicated, there is another option of accessing the module through its embedded web-server with HTTP requests right from WebHMI.
For a example we have the UC20-FBC-xxx (any bus Modubs, Ethernet/IP, DeviceNet etc) remote I/O module from Weidmuller. The module has web server for diagnostinc and setup:
On this page you press F12 to get necesssary information for the POST request:
Then, use this information for POST request in Lua script on the WebHMI:
The function returns json object which you can then analize and check necessary fileds to read:
Notes on cURL usage
In the fw of 4.0 version, the following issue was reported: When there are multiple cURL requests from the WebHMI to the unavailable resource, a system crash may happen. The timeout settings won't affect the sysem behaviour. Use this feature with care when you need cyclical calls of multiple cURLs in your scripts:
- use one script which detect availablity of the resource and locks following execution for a while
- use external watchdog circuit to reset WebHMI if you use it to control mission - critical system.