Wiki source code of 05 Lua Script

Version 38.1 by Hunter on 2023/07/07 18:14

Hide last authors
Leo Wei 1.1 1 = **1 64bits Address Basic Algorithm** =
2
3 The example shows how to do the Addition, Subtraction, Multiplication and Division for HMI internal address.
4
5 [[Download link>>https://drive.google.com/file/d/1ji5RbQJzUn_rQ2couX062fYt_6dqQNRb/view?usp=sharing]]
6
Hunter 12.1 7 = **2 MQTT Client** =
Leo Wei 1.1 8
9 The example shows how to connect to the MQTT server.(Test on MaQiaTTo)
10
Hunter 12.1 11 [[Download link>>https://drive.google.com/file/d/1LWLaOlhWB3BRSTLy3_2RC6Yb0wK7slEB/view?usp=sharing]]
Leo Wei 1.1 12
13 = **3 Timer** =
14
15 The example shows how to use the Lua Timer function.
16
17 [[Download link>>https://drive.google.com/file/d/1iwD2Kca9SIAGCYFBaQbn26zdc4kIUuSV/view?usp=sharing]]
18
19 = **4 Trigger** =
20
21 The example shows how to use the Lua Trigger function.
22
23 [[Download link>>https://drive.google.com/file/d/1GDCbbMgNSixonXP2erA0VoJbo7qagsCv/view?usp=sharing]]
24
25 = **5 User Free Protocol** =
26
27 The example shows the simple write and read of User Free protocol
28
Hunter 14.1 29 [[Download link>>https://drive.google.com/file/d/1v8UZvdclq3OGEajkbd78w7_XY2bY2qAe/view?usp=sharing]]
Leo Wei 1.1 30
31 = **6 Arabic Keypad** =
32
33 The example shows how to use the Lua to achieve the Arabic keypad. Also available for other non-English language like Cyrillic.
34
35 [[Download link>>https://drive.google.com/file/d/1JqpTRRqj6db32aLn7tqqO8dWT6EuXBEL/view?usp=sharing]]
Hunter 3.1 36
37 = **7 FTP Client** =
38
Hunter 8.1 39 The example shows how the to establish the FTP server at WIN10 OS and make the HMI as FTP client.
Hunter 3.1 40
Hunter 17.1 41 [[Download link>>https://ftp.we-con.com.cn/Download/WIKI/PI%20HMI/Demo/Lua%20Script/FTP%20Client.zip]]
42
Hunter 15.1 43 ~1. About how to set up the FTP server in Windows PC: **[[Build an FTP Site on IIS>>https://docs.microsoft.com/en-us/iis/publish/using-the-ftp-service/scenario-build-an-ftp-site-on-iis]] **,** [[How to set up and manage an FTP server on Windows 10>>https://www.windowscentral.com/how-set-and-manage-ftp-server-windows-10]]**
Hunter 7.1 44
Hunter 15.1 45 2. Enter PC (FTP Server) IP address and port number (Default 21).
Hunter 7.1 46
Hunter 15.1 47 (% style="text-align:center" %)
48 [[image:LuaFTPDemoIP&Port.png]]
Hunter 8.1 49
Hunter 15.1 50 3. Change the filename of the file that needs to be uploaded. If run on as Offline Simulator, the simulate upload path of "udisk:1.png" will be "C:\WECON\USB\1.png" on the PC.
Hunter 8.1 51
Hunter 15.1 52 (% style="text-align:center" %)
53 [[image:LuaFTPDemoUpload.png]]
54
55 4. Change drive letter of file save path according to drop-down list (There are three option available: Flash, UDisk and SDCard).
56
57 (% style="text-align:center" %)
58 [[image:LuaFTPDemoDropBox.png]]
59
60 5. Change the filename of the file that needs to be downloaded from PC(Server).
61
62 (% style="text-align:center" %)
63 [[image:LuaFTPDemoDownload.png]]
64
65 6. Set the default configuration in file "Script_BG.lua"
66
67 (% style="text-align:center" %)
68 [[image:LuaFTPDemoInitial.png]]
69
Hunter 16.1 70 ✎Note: If want to confirm whether it is downloaded successfully, please add a File list object to check in HMI project. If saved path is root path (same to the all three drive), please set the folder as "~/~/".
Hunter 15.1 71
Hunter 16.1 72 (% style="text-align:center" %)
73 [[image:LuaFTPDemoFilelist.png]]
74
Hunter 4.1 75 = **8 Telegram Notification** =
76
Hunter 5.1 77 The example shows how to use Bot API to push the notification to channel or group of Telegram. Please change the Token and Chat id to users' own one.
Hunter 4.1 78
79 [[Download link>>https://drive.google.com/file/d/1wV0O79TJ2mgUEBkKcjrb53PM8DkTQxVo/view?usp=sharing]]
Theodore Xu 18.1 80
81
82 = 9** LINE Notify** =
83
Theodore Xu 21.1 84 This example shows how to use the LINE Notify to send message into LINE group. When monitoring bit "@B_HDX0.0" changes, it will trigger and send the message. Please replace with your own Token.
Theodore Xu 18.1 85
Theodore Xu 19.1 86 [[Download link>>https://ftp.we-con.com.cn/Download/WIKI/PI%20HMI/Demo/Lua%20Script/line.zip]]
Hunter 21.2 87
88
89 = 10** How to parse value from subscribing MQTT topic** =
90
Hunter 21.3 91 This example shows how to parse value from subscribing MQTT topic. Get the value and calculate the average value for group F1, F2, F3 and F4. The testing topic name is "testtopic/test/no1/123456".
92
Hunter 22.1 93 [[Download link>>https://ftp.we-con.com.cn/Download/WIKI/PI%20HMI/Demo/Lua%20Script/MQTT%20JSON%20parse.zip]]
Hunter 21.3 94
Hunter 21.2 95 And the subscribed content is like following:
96
97 {{code language="json"}}
98 {
99 "Cluster_ID": "2",
100 "timestamp": "07:01:45-18.02.2022",
101 "Cust_id": "TAM-1",
102 "Platform_id": "Pro44ead8a8dc3345ACC",
103 "version": "1.5",
104 "Line_active": "true",
105 "Total_sensor": "5",
106 "F1": [
107 {
108 "Counter": "0",
109 "Value": "11"
110 },
111 {
112 "Counter": "1",
113 "Value": "21.1"
114 },
115 {
116 "Counter": "2",
117 "Value": "9"
118 },
119 {
120 "Counter": "3",
121 "Value": "0.06"
122 },
123 {
124 "Counter": "4",
125 "Value": "9"
126 },
127 {
128 "Counter": "5",
129 "Value": "19"
130 },
131 {
132 "Counter": "6",
133 "Value": "9"
134 },
135 {
136 "Counter": "7",
137 "Value": "19"
138 },
139 {
140 "Counter": "8",
141 "Value": "22.89"
142 }
143 ],
144 "F2": [
145 {
146 "Counter": "0",
147 "Value": "1.1"
148 },
149 {
150 "Counter": "1",
151 "Value": "21.21"
152 },
153 {
154 "Counter": "2",
155 "Value": "19"
156 },
157 {
158 "Counter": "3",
159 "Value": "0.6"
160 },
161 {
162 "Counter": "4",
163 "Value": "8"
164 },
165 {
166 "Counter": "5",
167 "Value": "19.1"
168 },
169 {
170 "Counter": "6",
171 "Value": "5.1"
172 }
173 ],
174 "F4": [
175 {
176 "Counter": "0",
177 "Value": "11"
178 },
179 {
180 "Counter": "1",
181 "Value": "21.1"
182 },
183 {
184 "Counter": "2",
185 "Value": "9"
186 },
187 {
188 "Counter": "3",
189 "Value": "0.06"
190 },
191 {
192 "Counter": "4",
193 "Value": "9"
194 },
195 {
196 "Counter": "5",
197 "Value": "19"
198 },
199 {
200 "Counter": "6",
201 "Value": "9"
202 },
203 {
204 "Counter": "7",
205 "Value": "12.1"
206 },
207 {
208 "Counter": "8",
209 "Value": "18.1"
210 }
211 ]
212 }
213 {{/code}}
214
Hunter 21.3 215 So we decode this json into lua object, and then assign the values from "Value" field into table (addresses HDW1~~HDW101, HDW112~~HDW202, HDW213~~HDW313, HDW324~~HDW424).
Hunter 21.2 216
217 (% style="text-align:center" %)
218 [[image:MQTTScreen.png]]
219
Hunter 21.3 220 Due to the MQTT code is not able to running on the simulator from PC, following picture is the testing result from actual HMI screen:
221
222 (% style="text-align:center" %)
223 [[image:MQTTactualtestingresult.png]]
224
225 The JSON parser function is located on mqttAction.lua, called mqtt_msg_callback.
226
227 (% style="text-align:center" %)
228 [[image:MQTTJSONparsefunction.png]]
229
Hunter 23.1 230 = **11 High-Low Byte Switch** =
Hunter 24.1 231
Hunter 26.1 232 The example shows how to convert high bytes and low bytes for one 16bit address. Main function is like following:
Hunter 25.1 233
234 {{code language="lua"}}
235 local lowByte = val & 0x00ff
236 local shiftHigh = lowByte << 8
237 local highByte = val & 0xff00
238 local shiftLow = highByte >> 8
239 local newVal = shiftHigh|shiftLow
240 {{/code}}
241
Hunter 27.1 242 [[Download link>>https://ftp.we-con.com.cn/Download/WIKI/PI%20HMI/Demo/Lua%20Script/byteswitch.zip]]
Theodore Xu 28.1 243
244 = **12 Week time setting** =
245
Theodore Xu 29.1 246 The example shows how to use day time to control functions:
247
Theodore Xu 28.1 248 [[Download link>>https://ftp.we-con.com.cn/Download/WIKI/PI%20HMI/Demo/Lua%20Script/week%20time.zip]]
Theodore Xu 30.1 249
Theodore Xu 31.1 250 = **13 MQTT Multi-link** =
Theodore Xu 30.1 251
252 The example shows use mqtt realize multi-machine function:
253
254 [[Download link>>https://ftp.we-con.com.cn/Download/WIKI/PI%20HMI/Demo/Lua%20Script/MQTT%20Multi-link.zip]]
Hunter 31.2 255
256 = **14 CSV File Generation** =
257
Hunter 34.1 258 The example shows convert the custom lua table data into csv string, then output as a file:
Hunter 31.2 259
Hunter 32.1 260 [[Download link>>https://ftp.we-con.com.cn/Download/WIKI/PI%20HMI/Demo/Lua%20Script/csvGenerate.zip]]
Hunter 31.2 261
Hunter 32.1 262 The following steps take HMI simulator as example:
263
264 ~1. Click button "Create Table" to draw lua table in screen.
265
266 (% style="text-align:center" %)
267 [[image:csvGenerateSteps.png]]
268
269 2. Click button "Save as csv" to save the file into HMI storage path. On the HMI simulator, user can enter the simulation path of U disk to check the file whether create successfully.
270
271 (% style="text-align:center" %)
272 [[image:csvGenerateSimulationPath.png]]
273
274 3. Click button "upload" to upload csv file into server side. In this case, we can check the upload path for PC.
275
276 (% style="text-align:center" %)
277 [[image:csvGenerateServerPath.png]]
278
Hunter 35.1 279 The csv file generation is mainly using the following function to implement, the customized data is argument tab, and csv file storage path in HMI is "udisk:weather1.csv":
Hunter 32.1 280
281 (% style="text-align:center" %)
282 [[image:csvGenerateLuaCode.png]]
283
Hunter 35.1 284 The table drawing is mainly using the following function to implement:
285
286 (% style="text-align:center" %)
287 [[image:csvLuaTableDrawing.png]]
Hunter 36.1 288
Hunter 37.1 289 = 15 **String customized length word-wrapped** =
Hunter 36.1 290
Hunter 37.1 291 The example shows automatically line break for long string content according to user input length.
292
Hunter 38.1 293 [[Download link>>https://ftp.we-con.com.cn/Download/WIKI/PI%20HMI/Demo/Lua%20Script/CRLF.zip]]
294
Hunter 37.1 295 The following are some demonstrated strings from offline simulator:
296
297 (% style="text-align:center" %)
298 [[image:WordWrapDisplayResult.png]]