User Tools

Site Tools


lua:introduction_to_lua_programming

Differences

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

Link to this comparison view

Next revision
Previous revision
introduction_to_lua_programming [2019/01/09 10:03] – created akuzmuklua:introduction_to_lua_programming [2022/01/17 20:58] (current) – ↷ Links adapted because of a move operation 127.0.0.1
Line 1: Line 1:
 +{{ network:lua_logo.png?nolink&100|}} 
 +
 ====== Introduction to Lua programming ====== ====== Introduction to Lua programming ======
-WebHMI allows you to create custom programs (scripts) in [[https://en.wikipedia.org/wiki/Lua | Lua]] 5.1 language+WebHMI allows you to create custom programs (scripts) in [[https://en.wikipedia.org/wiki/Lua | Lua]] 5.1 language. 
-There is a useful link for quick understanding  [[ https://gist.github.com/pateltej/bfec90ff449595f12236 | Lua in 15 minutes]]+
 The scripts are managed in the **Setup -> Scripts** menu item.  The scripts are managed in the **Setup -> Scripts** menu item. 
  
-Each script should contain a function named **main**. It will be called at the right time (depending on the settings of the script - in each cycle, when you act on the dashboard or when changing the value of the register, etc). Also, the entire script will be executed once when the daemon is started. This is necessary to compile the Lua script and validate it. Mis means that all the code that is in the global scope (that is, outside the main function, the main function will not be called) will be executed. This can be useful for initialising some variables, etc.+If you have no experience with Lua language read this tutorial at first  [[ https://gist.github.com/pateltej/bfec90ff449595f12236 | Lua in 15 minutes]].  
 + 
 +Each WebHMI script should contain a function named **main**. It will be called at the right time (depending on the settings of the script - in each cycle, when you act on the dashboard or when changing the value of the register, etc). Also, the entire script will be executed once when the daemon is started. This is necessary to compile the Lua script and validate it. This means that all the code from the global scope (that is, outside the main function, the main function will not be called) will be executed. This can be useful for initializing some variables, etc.
  
 Each script has its own global scope. It is impossible to access global variables and functions from one script in another. Each script has its own global scope. It is impossible to access global variables and functions from one script in another.
    
-The **main** function will receive one parameter //userId//. This is ID of the user that executed this script. If the script is being executed in each loop or when the register is changed, **userId** will be zero.+The **main** function will receive one special parameter //userId//. This is ID of the user that executed this script. If the script is being executed in each loop or when the register is changed, **userId** will be zero. If script is being executed from any dashboard then userId will contain user's id.
  
-The example of a simple script for WebHMI:+Here is an example of a simple script for WebHMI:
 <code lua> <code lua>
 --[[ --[[
  these vars will be assigned only once  these vars will be assigned only once
  global var will keep their values between scans  global var will keep their values between scans
- BUT NOT between project restarts!!! -- use DSxxx registers for saving data between project restarts+ BUT NOT between project restarts!!! -- for saving data between project restarts use D or DSxxx registers  or key-value storage
 --]] --]]
 first_scan = true -- first scan flag  first_scan = true -- first scan flag 
Line 44: Line 47:
 WebHMI imposes the following restrictions on standard Lua functions and libraries: WebHMI imposes the following restrictions on standard Lua functions and libraries:
  
-  *such functions and tables are __not available__: require, print, loadfile, dofile, os.execute, os.getenv, os.remove, os.rename, os.tmpname, package , debug.debug, debug.getfenv, debug.getregistry, arg +  * such functions and tables are __not available__: require, print, loadfile, dofile, os.execute, os.getenv, os.remove, os.rename, os.tmpname, package, debug.debug, debug.getfenv, debug.getregistry, arg 
-  *libraries are __not available__: io+  * libraries are __not available__: io
  
-In addition to the standard Lua functions, WebHMI also defines additional functions which are decsribed in their respective sections below+In addition to the standard Lua functions, WebHMI also defines additional functions which are described in this manual
  
-For the convenience of working with registers from Lua programs, each register can be assigned a symbolic name and be accessed from Lua already with this name. This name is specified in the register settings in the **Alias** field:+For the convenience of working with registers from Lua programs, each register can be assigned a symbolic name and be accessed from Lua with this name. This name is specified in the register settings in the **Script alias** field:
  
-{{ :alias_in_reg_list2.png?800 |}}+{{ network:alias_in_reg_list2.png?800 |}}
  
-In the example above, this name "systemTime" was used in GetReg function to read system time from internal WebHMI register with id = 1.+In the example above, alias "systemTime" was used in GetReg function to read system time from internal WebHMI register with id = 1.
  
 An example of a simple script that shows the calls of some WebHMI functions: An example of a simple script that shows the calls of some WebHMI functions:
Line 75: Line 78:
 </code> </code>
  
-Despite the fact that the built-in editor checks the syntax of the program, it is not able to detect all the errors that can occur during the execution of the script. In the event that any errors occur during its execution, they will be displayed in communication log (**Maintenance->WebHMI Log**).+Despite the fact that the built-in editor checks the syntax of the program, it is not able to detect all the errors that can occur during the execution of the script. If any error occurs during script execution, it will be displayed in communication log (**Maintenance->WebHMI Log**).
  
-For example, once this script will be executed, an error will occur because variable **random** is not declared.+For example, once this script will be executed, an error will occur because variable **random** was not declared.
  
 <code lua> <code lua>
Line 83: Line 86:
          
   local v1 = GetReg("Drying_Humidity1_Value");   local v1 = GetReg("Drying_Humidity1_Value");
-  v1 = v1+random; -- error is here, without random everything is ok+  v1 = v1 + random; -- error is here, without random everything is ok
      
   SetReg("Drying_Humidity1_Value", v1);   SetReg("Drying_Humidity1_Value", v1);
Line 100: Line 103:
 </code> </code>
  
-Here we see that the error occurred while trying to perform an arithmetic operation with an undefined variable (value is **nil**). The error occurred in script number 1, its name was 'Calculate humidity', the error occurred in line number 4 at 12:09:27.047. The variable that caused the error is called 'random'.+Here we see that the error occurred while trying to perform an arithmetic operation with an undefined variable (value is **nil**). The error occurred in script with id=1, its name was 'Calculate humidity', the error occurred in line number 4 at 12:09:27.047. The variable that caused the error is called 'random'.
  
-Thus, using this log you can find all runtime errors in your scripts. Also for debugging you can use watch console window, which is available in script editor.+Thus, using this log you can find all runtime errors in your scripts. Also for debugging you can use watch console window, which is available in [[lua:scripts_editor]].
lua/introduction_to_lua_programming.1547028183.txt.gz · Last modified: 2019/01/09 10:03 by akuzmuk

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki