Wiki source code of 05 Lua Script

Last modified by Devin Chen on 2025/03/04 14:07

Show last authors
1 = **1 **Arithmetic operations =
2
3 The example shows how to do the Addition, Subtraction, Multiplication and Division for HMI internal address.
4
5 [[Download link>>https://ftp.we-con.com.cn/Download/WIKI/PI%20HMI/Demo/Lua%20Script/Arithmetic%20operations%20by%20Lua%20V1.0.zip]]
6
7 = **2 MQTT Client** =
8
9 The example shows how to connect to the MQTT server.(Test on MaQiaTTo)
10
11 [[Download link>>https://ftp.we-con.com.cn/Download/WIKI/PI%20HMI/Demo/Lua%20Script/Lua%20MQTT%20Client.zip]]
12
13 = **3 Timer** =
14
15 The example shows how to use the Lua Timer function.
16
17 [[Download link>>https://ftp.we-con.com.cn/Download/WIKI/PI%20HMI/Demo/Lua%20Script/LuaTimer.zip]]
18
19 = **4 Trigger** =
20
21 The example shows how to use the Lua Trigger function.
22
23 [[Download link>>https://ftp.we-con.com.cn/Download/WIKI/PI%20HMI/Demo/Lua%20Script/LuaTrigger.zip]]
24
25 = **5 User Free Protocol** =
26
27 The example shows the simple write and read of User Free protocol
28
29 [[Download link>>https://ftp.we-con.com.cn/Download/WIKI/PI%20HMI/Demo/Lua%20Script/Lua%20User%20Free%20Protocol.zip]]
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://ftp.we-con.com.cn/Download/WIKI/PI%20HMI/Demo/Lua%20Script/ArabicKeyboard.zip]]
36
37 = **7 FTP Client** =
38
39 The example shows how  to establish the FTP server at WIN10 OS and make V2.0 HMI as FTP client.
40
41 [[Download link>>https://ftp.we-con.com.cn/Download/WIKI/PI%20HMI/Demo/Lua%20Script/FTP%20ClientV2.zip]]
42
43 (% class="box infomessage" %)
44 (((
45 Note: The above demo can work normal in OS V2.0. But if HMI OS  is V3.0, please change "r+" to "r" in the script.
46 )))
47
48 (% style="text-align:center" %)
49 [[image:dZyxVCtWDz.png]]
50
51 ~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]]**
52
53 2. Enter PC (FTP Server) IP address and port number (Default 21).
54
55 (% style="text-align:center" %)
56 [[image:LuaFTPDemoIP&Port.png]]
57
58 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.
59
60 (% style="text-align:center" %)
61 [[image:LuaFTPDemoUpload.png]]
62
63 4. Change drive letter of file save path according to drop-down list (There are three option available: Flash, UDisk and SDCard).
64
65 (% style="text-align:center" %)
66 [[image:LuaFTPDemoDropBox.png]]
67
68 5. Change the filename of the file that needs to be downloaded from PC(Server).
69
70 (% style="text-align:center" %)
71 [[image:LuaFTPDemoDownload.png]]
72
73 6. Set the default configuration in file "Script_BG.lua"
74
75 (% style="text-align:center" %)
76 [[image:LuaFTPDemoInitial.png]]
77
78 (% class="box infomessage" %)
79 (((
80 ✎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 "~/~/".
81 )))
82
83 (% style="text-align:center" %)
84 [[image:LuaFTPDemoFilelist.png]]
85
86 **Common error:**
87
88 In the demo project, if there are some errors in HMI for testing, please check the following steps.
89
90 Error 1: 550 Access is denied
91
92 Open Windows Settings → Update & Security→ Windows Security→ Firewall & network protection→ Advanced setting→ Inbound Rules→ New Rules→ Next → choose "This program path"→ input ‘’C:\Windows\System32\svchost.exe‘’→ Click “NEXT” continuously
93
94 (% style="text-align:center" %)
95 [[image:qCzqnKTUhp.png]]
96
97 (% style="text-align:center" %)
98 [[image:vfrt3NElMX.png]]
99
100 (% style="text-align:center" %)
101 [[image:dAuz25bdvM.png]]
102
103 (% style="text-align:center" %)
104 [[image:rxQIJkX8ll.png]]
105
106 (% style="text-align:center" %)
107 [[image:vmG8WGu5P2.png]]
108
109
110 Error 2: No route to host
111
112 The previous steps are the same as Error 1 → Port → specific local ports→ input port number “21”→ Click “NEXT” continuously
113
114 (% style="text-align:center" %)
115 [[image:fdsxie8WHc.png]]
116
117 (% style="text-align:center" %)
118 [[image:4ALQq7niTV.png]]
119
120 = **8 Telegram Notification** =
121
122 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.
123
124 [[Download link>>https://ftp.we-con.com.cn/Download/WIKI/PI%20HMI/Demo/Lua%20Script/Telegram.zip]]
125
126 = 9** LINE Notify** =
127
128 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.
129
130 [[Download link>>https://ftp.we-con.com.cn/Download/WIKI/PI%20HMI/Demo/Lua%20Script/line.zip]]
131
132
133 = 10** How to parse value from subscribing MQTT topic** =
134
135 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".
136
137 [[Download link>>https://ftp.we-con.com.cn/Download/WIKI/PI%20HMI/Demo/Lua%20Script/MQTT%20JSON%20parse.zip]]
138
139 And the subscribed content is like following:
140
141 {{code language="json"}}
142 {
143 "Cluster_ID": "2",
144 "timestamp": "07:01:45-18.02.2022",
145 "Cust_id": "TAM-1",
146 "Platform_id": "Pro44ead8a8dc3345ACC",
147 "version": "1.5",
148 "Line_active": "true",
149 "Total_sensor": "5",
150 "F1": [
151 {
152 "Counter": "0",
153 "Value": "11"
154 },
155 {
156 "Counter": "1",
157 "Value": "21.1"
158 },
159 {
160 "Counter": "2",
161 "Value": "9"
162 },
163 {
164 "Counter": "3",
165 "Value": "0.06"
166 },
167 {
168 "Counter": "4",
169 "Value": "9"
170 },
171 {
172 "Counter": "5",
173 "Value": "19"
174 },
175 {
176 "Counter": "6",
177 "Value": "9"
178 },
179 {
180 "Counter": "7",
181 "Value": "19"
182 },
183 {
184 "Counter": "8",
185 "Value": "22.89"
186 }
187 ],
188 "F2": [
189 {
190 "Counter": "0",
191 "Value": "1.1"
192 },
193 {
194 "Counter": "1",
195 "Value": "21.21"
196 },
197 {
198 "Counter": "2",
199 "Value": "19"
200 },
201 {
202 "Counter": "3",
203 "Value": "0.6"
204 },
205 {
206 "Counter": "4",
207 "Value": "8"
208 },
209 {
210 "Counter": "5",
211 "Value": "19.1"
212 },
213 {
214 "Counter": "6",
215 "Value": "5.1"
216 }
217 ],
218 "F4": [
219 {
220 "Counter": "0",
221 "Value": "11"
222 },
223 {
224 "Counter": "1",
225 "Value": "21.1"
226 },
227 {
228 "Counter": "2",
229 "Value": "9"
230 },
231 {
232 "Counter": "3",
233 "Value": "0.06"
234 },
235 {
236 "Counter": "4",
237 "Value": "9"
238 },
239 {
240 "Counter": "5",
241 "Value": "19"
242 },
243 {
244 "Counter": "6",
245 "Value": "9"
246 },
247 {
248 "Counter": "7",
249 "Value": "12.1"
250 },
251 {
252 "Counter": "8",
253 "Value": "18.1"
254 }
255 ]
256 }
257 {{/code}}
258
259 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).
260
261 (% style="text-align:center" %)
262 [[image:MQTTScreen.png]]
263
264 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:
265
266 (% style="text-align:center" %)
267 [[image:MQTTactualtestingresult.png]]
268
269 The JSON parser function is located on mqttAction.lua, called mqtt_msg_callback.
270
271 (% style="text-align:center" %)
272 [[image:MQTTJSONparsefunction.png]]
273
274 = **11 High-Low Byte Switch** =
275
276 The example shows how to convert high bytes and low bytes for one 16bit address. Main function is like following:
277
278 {{code language="lua"}}
279 local lowByte = val & 0x00ff
280 local shiftHigh = lowByte << 8
281 local highByte = val & 0xff00
282 local shiftLow = highByte >> 8
283 local newVal = shiftHigh|shiftLow
284 {{/code}}
285
286 [[Download link>>https://ftp.we-con.com.cn/Download/WIKI/PI%20HMI/Demo/Lua%20Script/byteswitch.zip]]
287
288 = **12 Week time setting** =
289
290 The example shows how to use day time to control functions:
291
292 [[Download link>>https://ftp.we-con.com.cn/Download/WIKI/PI%20HMI/Demo/Lua%20Script/week%20time.zip]]
293
294 = **13 MQTT Multi-link** =
295
296 The example shows use mqtt realize multi-machine function:
297
298 [[Download link>>https://ftp.we-con.com.cn/Download/WIKI/PI%20HMI/Demo/Lua%20Script/MQTT%20Multi-link.zip]]
299
300 = **14 CSV File Generation** =
301
302 The example shows convert the custom lua table data into csv string, then output as a file:
303
304 [[Download link>>https://ftp.we-con.com.cn/Download/WIKI/PI%20HMI/Demo/Lua%20Script/csvGenerate.zip]]
305
306 The following steps take HMI simulator as example:
307
308 ~1. Click button "Create Table" to draw lua table in screen.
309
310 (% style="text-align:center" %)
311 [[image:csvGenerateSteps.png]]
312
313 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.
314
315 (% style="text-align:center" %)
316 [[image:csvGenerateSimulationPath.png]]
317
318 3. Click button "upload" to upload csv file into server side. In this case, we can check the upload path for PC.
319
320 (% style="text-align:center" %)
321 [[image:csvGenerateServerPath.png]]
322
323 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":
324
325 (% style="text-align:center" %)
326 [[image:csvGenerateLuaCode.png]]
327
328 The table drawing is mainly using the following function to implement:
329
330 (% style="text-align:center" %)
331 [[image:csvLuaTableDrawing.png]]
332
333 = 15 **String customized length word-wrapped** =
334
335 The example shows automatically line break for long string content according to user input length.
336
337 [[Download link>>https://ftp.we-con.com.cn/Download/WIKI/PI%20HMI/Demo/Lua%20Script/CRLF.zip]]
338
339 The following are some demonstrated strings from offline simulator:
340
341 (% style="text-align:center" %)
342 [[image:WordWrapDisplayResult.png]]
343
344 = 16 Weather forecast HTTP API request =
345
346 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/]]
347
348 [[Download link>>https://ftp.we-con.com.cn/Download/WIKI/PI%20HMI/Demo/Lua%20Script/luahttp.zip]]
349
350 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):
351
352 {{code language="JSON"}}
353 {
354 "location": {
355 "name": "London",
356 "region": "City of London, Greater London",
357 "country": "United Kingdom",
358 "lat": 51.52,
359 "lon": -0.11,
360 "tz_id": "Europe/London",
361 "localtime_epoch": 1688961236,
362 "localtime": "2023-07-10 4:53"
363 },
364 "current": {
365 "last_updated_epoch": 1688960700,
366 "last_updated": "2023-07-10 04:45",
367 "temp_c": 14.0,
368 "temp_f": 57.2,
369 "is_day": 0,
370 "condition": {
371 "text": "Partly cloudy",
372 "icon": "//cdn.weatherapi.com/weather/64x64/night/116.png",
373 "code": 1003
374 },
375 "wind_mph": 4.3,
376 "wind_kph": 6.8,
377 "wind_degree": 220,
378 "wind_dir": "SW",
379 "pressure_mb": 1020.0,
380 "pressure_in": 30.12,
381 "precip_mm": 0.0,
382 "precip_in": 0.0,
383 "humidity": 88,
384 "cloud": 50,
385 "feelslike_c": 13.3,
386 "feelslike_f": 56.0,
387 "vis_km": 10.0,
388 "vis_miles": 6.0,
389 "uv": 1.0,
390 "gust_mph": 10.5,
391 "gust_kph": 16.9
392 },
393 "forecast": {
394 "forecastday": [
395 {
396 "date": "2023-07-10",
397 "date_epoch": 1688947200,
398 "day": {
399 "maxtemp_c": 24.4,
400 "maxtemp_f": 75.9,
401 "mintemp_c": 14.1,
402 "mintemp_f": 57.4,
403 "avgtemp_c": 19.6,
404 "avgtemp_f": 67.2,
405 "maxwind_mph": 15.4,
406 "maxwind_kph": 24.8,
407 "totalprecip_mm": 0.0,
408 "totalprecip_in": 0.0,
409 "totalsnow_cm": 0.0,
410 "avgvis_km": 10.0,
411 "avgvis_miles": 6.0,
412 "avghumidity": 60.0,
413 "daily_will_it_rain": 0,
414 "daily_chance_of_rain": 0,
415 "daily_will_it_snow": 0,
416 "daily_chance_of_snow": 0,
417 "condition": {
418 "text": "Partly cloudy",
419 "icon": "//cdn.weatherapi.com/weather/64x64/day/116.png",
420 "code": 1003
421 },
422 "uv": 5.0
423 },
424 "astro": {
425 "sunrise": "04:55 AM",
426 "sunset": "09:16 PM",
427 "moonrise": "12:26 AM",
428 "moonset": "02:01 PM",
429 "moon_phase": "Last Quarter",
430 "moon_illumination": "51",
431 "is_moon_up": 1,
432 "is_sun_up": 0
433 },
434 "hour": [
435 {
436 "time_epoch": 1688943600,
437 "time": "2023-07-10 00:00",
438 "temp_c": 16.2,
439 "temp_f": 61.2,
440 "is_day": 0,
441 "condition": {
442 "text": "Clear",
443 "icon": "//cdn.weatherapi.com/weather/64x64/night/113.png",
444 "code": 1000
445 },
446 "wind_mph": 7.2,
447 "wind_kph": 11.5,
448 "wind_degree": 223,
449 "wind_dir": "SW",
450 "pressure_mb": 1021.0,
451 "pressure_in": 30.14,
452 "precip_mm": 0.0,
453 "precip_in": 0.0,
454 "humidity": 74,
455 "cloud": 6,
456 "feelslike_c": 16.2,
457 "feelslike_f": 61.2,
458 "windchill_c": 16.2,
459 "windchill_f": 61.2,
460 "heatindex_c": 16.2,
461 "heatindex_f": 61.2,
462 "dewpoint_c": 11.6,
463 "dewpoint_f": 52.9,
464 "will_it_rain": 0,
465 "chance_of_rain": 0,
466 "will_it_snow": 0,
467 "chance_of_snow": 0,
468 "vis_km": 10.0,
469 "vis_miles": 6.0,
470 "gust_mph": 10.7,
471 "gust_kph": 17.3,
472 "uv": 1.0
473 },
474 {
475 "time_epoch": 1688947200,
476 "time": "2023-07-10 01:00",
477 "temp_c": 15.7,
478 "temp_f": 60.3,
479 "is_day": 0,
480 "condition": {
481 "text": "Overcast",
482 "icon": "//cdn.weatherapi.com/weather/64x64/night/122.png",
483 "code": 1009
484 },
485 "wind_mph": 7.2,
486 "wind_kph": 11.5,
487 "wind_degree": 232,
488 "wind_dir": "SW",
489 "pressure_mb": 1021.0,
490 "pressure_in": 30.14,
491 "precip_mm": 0.0,
492 "precip_in": 0.0,
493 "humidity": 75,
494 "cloud": 100,
495 "feelslike_c": 15.7,
496 "feelslike_f": 60.3,
497 "windchill_c": 15.7,
498 "windchill_f": 60.3,
499 "heatindex_c": 15.7,
500 "heatindex_f": 60.3,
501 "dewpoint_c": 11.3,
502 "dewpoint_f": 52.3,
503 "will_it_rain": 0,
504 "chance_of_rain": 0,
505 "will_it_snow": 0,
506 "chance_of_snow": 0,
507 "vis_km": 10.0,
508 "vis_miles": 6.0,
509 "gust_mph": 10.5,
510 "gust_kph": 16.9,
511 "uv": 1.0
512 },
513 ...
514 {
515 "time_epoch": 1689026400,
516 "time": "2023-07-10 23:00",
517 "temp_c": 19.0,
518 "temp_f": 66.2,
519 "is_day": 0,
520 "condition": {
521 "text": "Overcast",
522 "icon": "//cdn.weatherapi.com/weather/64x64/night/122.png",
523 "code": 1009
524 },
525 "wind_mph": 9.4,
526 "wind_kph": 15.1,
527 "wind_degree": 192,
528 "wind_dir": "SSW",
529 "pressure_mb": 1015.0,
530 "pressure_in": 29.96,
531 "precip_mm": 0.0,
532 "precip_in": 0.0,
533 "humidity": 66,
534 "cloud": 100,
535 "feelslike_c": 19.0,
536 "feelslike_f": 66.2,
537 "windchill_c": 19.0,
538 "windchill_f": 66.2,
539 "heatindex_c": 19.0,
540 "heatindex_f": 66.2,
541 "dewpoint_c": 12.4,
542 "dewpoint_f": 54.3,
543 "will_it_rain": 0,
544 "chance_of_rain": 0,
545 "will_it_snow": 0,
546 "chance_of_snow": 0,
547 "vis_km": 10.0,
548 "vis_miles": 6.0,
549 "gust_mph": 13.0,
550 "gust_kph": 20.9,
551 "uv": 1.0
552 }
553 ]
554 }
555 ]
556 }
557 }
558 {{/code}}
559
560 The core code is from file API_WEATHER.lua, and function name is get7DayWeatherapiByCityname.
561
562 (% style="text-align:center" %)
563 [[image:WeatherHTTPAPI.png]]
564
565 The HMI offline simulator result:
566
567 (% style="text-align:center" %)
568 [[image:WeatherHTTPtestdisplay.png]]
569
570 = 17 **CSV File Reading** =
571
572 The example shows how to read the csv file into HMI, and then display as table.
573
574 [[Download link>>https://ftp.we-con.com.cn/Download/WIKI/PI%20HMI/Demo/Lua%20Script/csvRead.zip]]
575
576 The core code is from file readcsv.lua:
577
578 1) The file path is set as Sheet_2023_06_13_15_29_45.csv at the root path for USB;
579
580 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;
581
582 (% style="text-align:center" %)
583 [[image:CSVreadwithsemicolon.png]]
584
585 3) The reading content is a two-dimensional table, can use the table index to locate the specify data cell;
586
587 For example, want to read the 2nd row C column from the table, the table index will be like table[2][3].
588
589 (% style="text-align:center" %)
590 [[image:celllocation.png]]
591
592 = 18 How to convert a number to a string =
593
594 The example shows how to convert a number to a string using the "tostring()" function.
595
596 [[Download link>>https://ftp.we-con.com.cn/Download/WIKI/PI%20HMI/Demo/Lua%20Script/Number%20to%20string.zip]]
597
598 Note:This demo converts a 64-bit floating to a string,you can change the data format of "@W_HDW0" depand on requirements
599
600 The core code is from file convert.lua:
601
602 (% style="text-align:center" %)
603 [[image:convert.lua.png]]
604
605 The HMI offline simulator result:
606
607 (% style="text-align:center" %)
608 [[image:Converttestdisplay.png]]
609
610 = 19 HUAWEI CLOUD & OpenCloud =
611
612 About the details, please check the powerpoint or video of Lua MQTT & OpenCloud [[Jump To>>doc:PIStudio.Download.1 Manual.WebHome]]
613
614 [[Download link>>https://ftp.we-con.com.cn/Download/WIKI/PI%20HMI/Demo/Lua%20Script/HuaweiMQ%281%29.zip]]
615
616 = 20 UDP Multicast =
617
618 The example shows how to use UDP multicast feature to send and receive data.
619
620 [[Download link>>https://ftp.we-con.com.cn/Download/WIKI/PI%20HMI/Demo/Lua%20Script/UDP%20Multicast.zip]]
621
622 About the details, please check the [[UDP module>>https://docs.we-con.com.cn/bin/view/PIStudio/09%20Lua%20Editor/Lua%20Script/#HUDPmodule-1]]
623
624 = 21 ATC module **send SMS** =
625
626 The example shows how to use HMI to customize communication with the ATC SMS module via Lua script to send SMS.
627
628 [[Download link>>https://ftp.we-con.com.cn/Download/WIKI/PI%20HMI/Demo/Lua%20Script/SMS.zip]]
629
630 = 22 TCP Client =
631
632 The example shows how to use the Lua Socket TCP module to connect with a TCP server.
633
634 [[Download link>>https://ftp.we-con.com.cn/Download/WIKI/PI%20HMI/Demo/Lua%20Script/TCP%20ClientV1.1.zip]]
635
636 ~1. Set the initial values of Server IP and port number.
637
638 (% style="text-align:center" %)
639 [[image:vcbt6CE6EL.png]]
640
641 2. Set the actual receive length of TCP message according to the actual needs.
642
643 (% style="text-align:center" %)
644 [[image:tPN1mbz7ny.png]]
645
646 = 23 Lua SMS =
647
648 The example shows how to use HMI built-in 4G module to send SMS via Lua script. This demo requires the HMI model name with (G) or (AG).
649
650 [[Download link>>https://ftp.we-con.com.cn/Download/WIKI/PI%20HMI/Demo/Lua%20Script/Lua%20SMS%20Library.zip]]
651
652 = 24 ATC module Calling =
653
654 The example shows how to use HMI to customize communication with the ATC-3101 module via Lua script to make phone call
655
656 .[[Download link>>https://ftp.we-con.com.cn/Download/WIKI/PI%20HMI/Demo/Lua%20Script/lua%20ATC%20calling.rar]]
657
658 (% class="box infomessage" %)
659 (((
660 Note:Due to differences in operators, it is recommended to test before use.
661 )))