User Tools

Site Tools


integration_with_other_systems

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
integration_with_other_systems [2019/09/03 13:02] – [Example - connecting to a remote module via its web server] emozolyakintegration_with_other_systems [2022/01/14 16:48] (current) – ↷ Links adapted because of a move operation 127.0.0.1
Line 5: Line 5:
 This function is available since version 2.6.4520. This function is available since version 2.6.4520.
  
-An example of a program executing an HTTPS request to send data as a file at URL https://demo.com/upload:+===== POST example ===== 
 + 
 +An example of a program executing an HTTPS request to send data as a file at URL https://example.com/upload:
  
 <code lua> <code lua>
Line 13: Line 15:
  
   c = cURL.easy_init()   c = cURL.easy_init()
-  c:setopt_url("https://demo.com/upload")+  c:setopt_url("https://example.com/upload")
   c:setopt_timeout(5)   c:setopt_timeout(5)
  
Line 32: Line 34:
  
 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. 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: 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:
Line 50: Line 54:
 </code> </code>
  
 +===== Implementing other requests like PUT =====
 +
 +To implement other requests, you can use special field in the cURL for the custom methods.
 +
 +Here is the example of PUT request to WebHMI itself from its Lua. 
 +
 +<code lua>
 +
 +function main (userId)
 +  local new_value = "new string"
 +  write_reg_with_API(1, new_value)
 +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)    -- local host addr indicated
 +  c:setopt_timeout(2)
 +  
 +  c:setopt{[cURL.OPT_CUSTOMREQUEST] = "PUT"}
 +  c:setopt{[cURL.OPT_POSTFIELDS]    = '{"value":"' .. val_ .. '"}'  -- insert value here
 +  
 +  c:setopt_httpheader({
 +      'Content-type: application/json',
 +      'Accept: application/json',
 +      'X-WH-APIKEY: 976FA578CF3B11ADA06564464B1F01823AF81C3D'         -- check APIKEY
 +  })
 +  
 +  c:perform({writefunction = function(res) str = res end })           -- you can print str for debug purposes 
 +  c:close()
 +end 
 +
 +</code>
 ===== Example - connecting to a remote module via its web server ===== ===== 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.  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 with WebHMI and using custom protocol is difficult, there is another option of accessing the module through its embedded web-server with HTTP requests. +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, Eth/IP etc) remote I/O module from Weidmuller. The module has web server for diagnostinc and setup:+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:
  
-{{ ::uremote_config_page.gif?direct&600 |}}+{{ network:uremote_config_page.gif?direct&600 |}}
  
 On this page you press **F12** to get necesssary information for the POST request: On this page you press **F12** to get necesssary information for the POST request:
  
-{{ ::post_info_out.png?direct&600 |}}+{{ network:post_info_out.png?direct&600 |}}
  
 Then, use this information for POST request in Lua script on the WebHMI: Then, use this information for POST request in Lua script on the WebHMI:
  
-{{ ::post_lua_example2.gif?direct&600 |}}+{{ network:post_lua_example2.gif?direct&600 |}} 
 + 
 +The function returns json object which you can then analize and check necessary fileds to read: 
 + 
 +{{ network:json_viewer.png?direct&600 |}} 
 + 
 +===== Notes on cURL usage ===== 
 + 
 +<WRAP center round important 80%> 
 +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.  
 + 
 +If the curl stability is the issue in your project, you can implement HTTP requests using custom protocol. See the [[http_get_custom |example]] 
 + 
 +</WRAP>
  
  
integration_with_other_systems.1567515728.txt.gz · Last modified: 2019/09/03 13:02 by emozolyak

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki