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 [2021/06/29 14:00] – [Implementing other requests like PUT] emozolyakintegration_with_other_systems [2024/04/04 10:18] (current) – [Troubleshooting CURL requests from WebHMI] emozolyak
Line 7: Line 7:
 ===== POST example ===== ===== POST example =====
  
-An example of a program executing an HTTPS request to send data as a file at URL https://demo.com/upload:+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 15: 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 56: Line 56:
 ===== Implementing other requests like PUT ===== ===== Implementing other requests like PUT =====
  
-To implement other requests, you can use special field in the cURL for the custom methods:+To implement other requests, you can use special field in the cURL for the custom methods.
  
-<code lua> +Here is the example of PUT request to WebHMI itself from its Lua
-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); +<code lua>
-end +
-</code>+
  
-<code lua> 
 function main (userId) function main (userId)
-  -- Add your code here +  local new_value = "new string
-  INFO("the sript has run"+  write_reg_with_API(1, new_value)
-   +
-  write_reg_with_API(1, "new string") +
-  +
 end end
  
Line 94: Line 71:
   c = cURL.easy_init()   c = cURL.easy_init()
      
-  c:setopt_url("http://127.0.1.1/api/register-values/" .. reg_id)+  c:setopt_url("http://127.0.1.1/api/register-values/" .. reg_id)    -- local host addr indicated
   c:setopt_timeout(2)   c:setopt_timeout(2)
      
   c:setopt{[cURL.OPT_CUSTOMREQUEST] = "PUT"}   c:setopt{[cURL.OPT_CUSTOMREQUEST] = "PUT"}
-  c:setopt{[cURL.OPT_POSTFIELDS]    = '{"value":"' .. val_ .. '"}'}+  c:setopt{[cURL.OPT_POSTFIELDS]    = '{"value":"' .. val_ .. '"}'  -- insert value here
      
   c:setopt_httpheader({   c:setopt_httpheader({
       'Content-type: application/json',       'Content-type: application/json',
       'Accept: application/json',       'Accept: application/json',
-      'X-WH-APIKEY: 976FA578CF3B11ADA06564464B1F01823AF81C3D'+      'X-WH-APIKEY: 976FA578CF3B11ADA06564464B1F01823AF81C3D'         -- check APIKEY
   })   })
      
-  c:perform({writefunction = function(res) str = res end })+  c:perform({writefunction = function(res) str = res end })           -- you can print str for debug purposes 
   c:close()   c:close()
 end  end 
Line 118: Line 95:
 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: 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: The function returns json object which you can then analize and check necessary fileds to read:
  
-{{ ::json_viewer.png?direct&600 |}}+{{ network:json_viewer.png?direct&600 |}}
  
 ===== Notes on cURL usage ===== ===== Notes on cURL usage =====
Line 140: Line 117:
   * use external watchdog circuit to reset WebHMI if you use it to control mission - critical system.    * 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> </WRAP>
 +
 +==== HTTP / HTTPS requests ====
 +<WRAP center round info 80%>
 +Please note that HTTPS requests are only possible in **WebHMI Pro** version and virtualization images.
 +</WRAP>
 +
 +==== Troubleshooting CURL requests from WebHMI ====
 +
 +When troubleshooting your requests, you can turn on verbose information in your script. Please refer to the example below:
 +
 +<code lua>
 +function main (userId)
 +    local res = ""    
 +    local c = cURL.easy_init()
 +    c : setopt{ url = 'http://danepubliczne.imgw.pl/api/data/hydro/id/153160210',
 +              timeout = 30, 
 +              httpheader = {'Accept: application/json',
 +                            'Content-Type: gzip, deflate, br',
 +                            -- 'Host: <calculated when request is sent>',
 +                        }
 +             }
 +     
 +     -- sw on verbose info 
 +     c : setopt{[cURL.OPT_VERBOSE] = 1}  
 +     c : setopt{[cURL.OPT_DEBUGFUNCTION] = function(...) 
 +                for i, v in pairs(arg) do DEBUG(i .. ' ' .. v) end 
 +                return 0
 +         end }  
 +     
 +     c : perform{writefunction = function(str)
 +                res = res .. str
 +            end}
 +        c : close()
 +     
 +        INFO(res)
 +        if (res == '') or (res == '%[%]')then 
 +            ERROR('No data in response!'
 +        else 
 +            INFO('\n' .. res) 
 +        end
 +end
 +</code>
 +
 +
  
  
integration_with_other_systems.1624975237.txt.gz · Last modified: 2021/06/29 14:00 by emozolyak

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki