Wiki source code of 05 Lua Script

Version 54.1 by Devin Chen on 2023/11/24 17:07

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 46.1 41 [[Download link>>https://ftp.we-con.com.cn/Download/WIKI/PI%20HMI/Demo/Lua%20Script/FTP%20ClientV2.zip]]
Hunter 17.1 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]]
Hunter 38.2 299
300 = 16 Weather forecast HTTP API request =
301
302 The example shows how to parse value from HTTP response body, and display into HMI internal address. The test forecast official website is [[https:~~/~~/www.weatherapi.com/>>url:https://www.weatherapi.com/]]
303
Hunter 39.1 304 [[Download link>>https://ftp.we-con.com.cn/Download/WIKI/PI%20HMI/Demo/Lua%20Script/luahttp.zip]]
Hunter 38.2 305
Hunter 39.1 306 From the API explorer of weatherapi, the one day forecast response would be like the follows(Because the original data of JSON is very long, there we just show the part of it):
307
Hunter 38.2 308 {{code language="JSON"}}
309 {
310 "location": {
311 "name": "London",
312 "region": "City of London, Greater London",
313 "country": "United Kingdom",
314 "lat": 51.52,
315 "lon": -0.11,
316 "tz_id": "Europe/London",
317 "localtime_epoch": 1688961236,
318 "localtime": "2023-07-10 4:53"
319 },
320 "current": {
321 "last_updated_epoch": 1688960700,
322 "last_updated": "2023-07-10 04:45",
323 "temp_c": 14.0,
324 "temp_f": 57.2,
325 "is_day": 0,
326 "condition": {
327 "text": "Partly cloudy",
328 "icon": "//cdn.weatherapi.com/weather/64x64/night/116.png",
329 "code": 1003
330 },
331 "wind_mph": 4.3,
332 "wind_kph": 6.8,
333 "wind_degree": 220,
334 "wind_dir": "SW",
335 "pressure_mb": 1020.0,
336 "pressure_in": 30.12,
337 "precip_mm": 0.0,
338 "precip_in": 0.0,
339 "humidity": 88,
340 "cloud": 50,
341 "feelslike_c": 13.3,
342 "feelslike_f": 56.0,
343 "vis_km": 10.0,
344 "vis_miles": 6.0,
345 "uv": 1.0,
346 "gust_mph": 10.5,
347 "gust_kph": 16.9
348 },
349 "forecast": {
350 "forecastday": [
351 {
352 "date": "2023-07-10",
353 "date_epoch": 1688947200,
354 "day": {
355 "maxtemp_c": 24.4,
356 "maxtemp_f": 75.9,
357 "mintemp_c": 14.1,
358 "mintemp_f": 57.4,
359 "avgtemp_c": 19.6,
360 "avgtemp_f": 67.2,
361 "maxwind_mph": 15.4,
362 "maxwind_kph": 24.8,
363 "totalprecip_mm": 0.0,
364 "totalprecip_in": 0.0,
365 "totalsnow_cm": 0.0,
366 "avgvis_km": 10.0,
367 "avgvis_miles": 6.0,
368 "avghumidity": 60.0,
369 "daily_will_it_rain": 0,
370 "daily_chance_of_rain": 0,
371 "daily_will_it_snow": 0,
372 "daily_chance_of_snow": 0,
373 "condition": {
374 "text": "Partly cloudy",
375 "icon": "//cdn.weatherapi.com/weather/64x64/day/116.png",
376 "code": 1003
377 },
378 "uv": 5.0
379 },
380 "astro": {
381 "sunrise": "04:55 AM",
382 "sunset": "09:16 PM",
383 "moonrise": "12:26 AM",
384 "moonset": "02:01 PM",
385 "moon_phase": "Last Quarter",
386 "moon_illumination": "51",
387 "is_moon_up": 1,
388 "is_sun_up": 0
389 },
390 "hour": [
391 {
392 "time_epoch": 1688943600,
393 "time": "2023-07-10 00:00",
394 "temp_c": 16.2,
395 "temp_f": 61.2,
396 "is_day": 0,
397 "condition": {
398 "text": "Clear",
399 "icon": "//cdn.weatherapi.com/weather/64x64/night/113.png",
400 "code": 1000
401 },
402 "wind_mph": 7.2,
403 "wind_kph": 11.5,
404 "wind_degree": 223,
405 "wind_dir": "SW",
406 "pressure_mb": 1021.0,
407 "pressure_in": 30.14,
408 "precip_mm": 0.0,
409 "precip_in": 0.0,
410 "humidity": 74,
411 "cloud": 6,
412 "feelslike_c": 16.2,
413 "feelslike_f": 61.2,
414 "windchill_c": 16.2,
415 "windchill_f": 61.2,
416 "heatindex_c": 16.2,
417 "heatindex_f": 61.2,
418 "dewpoint_c": 11.6,
419 "dewpoint_f": 52.9,
420 "will_it_rain": 0,
421 "chance_of_rain": 0,
422 "will_it_snow": 0,
423 "chance_of_snow": 0,
424 "vis_km": 10.0,
425 "vis_miles": 6.0,
426 "gust_mph": 10.7,
427 "gust_kph": 17.3,
428 "uv": 1.0
429 },
430 {
431 "time_epoch": 1688947200,
432 "time": "2023-07-10 01:00",
433 "temp_c": 15.7,
434 "temp_f": 60.3,
435 "is_day": 0,
436 "condition": {
437 "text": "Overcast",
438 "icon": "//cdn.weatherapi.com/weather/64x64/night/122.png",
439 "code": 1009
440 },
441 "wind_mph": 7.2,
442 "wind_kph": 11.5,
443 "wind_degree": 232,
444 "wind_dir": "SW",
445 "pressure_mb": 1021.0,
446 "pressure_in": 30.14,
447 "precip_mm": 0.0,
448 "precip_in": 0.0,
449 "humidity": 75,
450 "cloud": 100,
451 "feelslike_c": 15.7,
452 "feelslike_f": 60.3,
453 "windchill_c": 15.7,
454 "windchill_f": 60.3,
455 "heatindex_c": 15.7,
456 "heatindex_f": 60.3,
457 "dewpoint_c": 11.3,
458 "dewpoint_f": 52.3,
459 "will_it_rain": 0,
460 "chance_of_rain": 0,
461 "will_it_snow": 0,
462 "chance_of_snow": 0,
463 "vis_km": 10.0,
464 "vis_miles": 6.0,
465 "gust_mph": 10.5,
466 "gust_kph": 16.9,
467 "uv": 1.0
468 },
469 ...
470 {
471 "time_epoch": 1689026400,
472 "time": "2023-07-10 23:00",
473 "temp_c": 19.0,
474 "temp_f": 66.2,
475 "is_day": 0,
476 "condition": {
477 "text": "Overcast",
478 "icon": "//cdn.weatherapi.com/weather/64x64/night/122.png",
479 "code": 1009
480 },
481 "wind_mph": 9.4,
482 "wind_kph": 15.1,
483 "wind_degree": 192,
484 "wind_dir": "SSW",
485 "pressure_mb": 1015.0,
486 "pressure_in": 29.96,
487 "precip_mm": 0.0,
488 "precip_in": 0.0,
489 "humidity": 66,
490 "cloud": 100,
491 "feelslike_c": 19.0,
492 "feelslike_f": 66.2,
493 "windchill_c": 19.0,
494 "windchill_f": 66.2,
495 "heatindex_c": 19.0,
496 "heatindex_f": 66.2,
497 "dewpoint_c": 12.4,
498 "dewpoint_f": 54.3,
499 "will_it_rain": 0,
500 "chance_of_rain": 0,
501 "will_it_snow": 0,
502 "chance_of_snow": 0,
503 "vis_km": 10.0,
504 "vis_miles": 6.0,
505 "gust_mph": 13.0,
506 "gust_kph": 20.9,
507 "uv": 1.0
508 }
509 ]
510 }
511 ]
512 }
513 }
514 {{/code}}
515
Hunter 39.1 516 The core code is from file API_WEATHER.lua, and function name is get7DayWeatherapiByCityname.
517
518 (% style="text-align:center" %)
519 [[image:WeatherHTTPAPI.png]]
520
521 The HMI offline simulator result:
522
523 (% style="text-align:center" %)
524 [[image:WeatherHTTPtestdisplay.png]]
Hunter 40.1 525
526 = 17 **CSV File Reading** =
527
528 The example shows how to read the csv file into HMI, and then display as table.
529
530 [[Download link>>https://ftp.we-con.com.cn/Download/WIKI/PI%20HMI/Demo/Lua%20Script/csvRead.zip]]
Hunter 41.1 531
Hunter 43.1 532 The core code is from file readcsv.lua:
Hunter 41.1 533
Hunter 43.1 534 1) The file path is set as Sheet_2023_06_13_15_29_45.csv at the root path for USB;
Hunter 42.1 535
536 2) Remember to change csv separator in the second parameter from function csv_csvToTable according to user's need, this demo is using **semicolon** as separator;
537
Hunter 41.1 538 (% style="text-align:center" %)
Hunter 42.1 539 [[image:CSVreadwithsemicolon.png]]
Hunter 43.1 540
541 3) The reading content is a two-dimensional table, can use the table index to locate the specify data cell;
542
543 For example, want to read the 2nd row C column from the table, the table index will be like table[2][3].
544
545 (% style="text-align:center" %)
546 [[image:celllocation.png]]
547
Devin Chen 48.1 548 = 18 How to convert a number to a string =
Devin Chen 47.1 549
550 The example shows how to convert a number to a string using the "tostring()" function.
551
Devin Chen 49.1 552 [[Download link>>https://ftp.we-con.com.cn/Download/WIKI/PI%20HMI/Demo/Lua%20Script/Number%20to%20string.zip]]
Devin Chen 47.1 553
Devin Chen 50.1 554 Note:This demo converts a 64-bit floating to a string,you can change the data format of "@W_HDW0" depand on requirements
Devin Chen 49.1 555
Devin Chen 47.1 556 The core code is from file convert.lua:
557
558 (% style="text-align:center" %)
559 [[image:convert.lua.png]]
560
561 The HMI offline simulator result:
562
563 (% style="text-align:center" %)
564 [[image:Converttestdisplay.png]]
Hunter 51.1 565
Hunter 52.1 566 = 19 HUAWEI CLOUD & OpenCloud =
Hunter 53.1 567
568 About the details, please check the powerpoint or video of Lua MQTT & OpenCloud [[Jump To>>doc:PIStudio.Download.1 Manual.WebHome]]
569
570 [[Download link>>https://ftp.we-con.com.cn/Download/WIKI/PI%20HMI/Demo/Lua%20Script/HuaweiMQ%281%29.zip]]
Devin Chen 54.1 571
572
573 = 20 UDP Multicast =
574
575 The example shows how to use UDP multicast feature to send and receive data.
576
577 [[Download link>>https://ftp.we-con.com.cn/Download/WIKI/PI%20HMI/Demo/Lua%20Script/UDP%20Multicast.zip]]
578
579 About the details, please check the [[UDP module>>https://docs.we-con.com.cn/bin/view/PIStudio/09%20Lua%20Editor/Lua%20Script/#HUDPmodule-1]]