Changes for page 05 Lua Script

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

From version 37.1
edited by Hunter
on 2023/07/07 17:30
Change comment: There is no comment for this version
To version 67.1
edited by Molly
on 2024/10/23 18:42
Change comment: There is no comment for this version

Summary

Details

Page properties
Author
... ... @@ -1,1 +1,1 @@
1 -XWiki.Hunter
1 +XWiki.Molly
Content
... ... @@ -2,31 +2,31 @@
2 2  
3 3  The example shows how to do the Addition, Subtraction, Multiplication and Division for HMI internal address.
4 4  
5 -[[Download link>>https://drive.google.com/file/d/1ji5RbQJzUn_rQ2couX062fYt_6dqQNRb/view?usp=sharing]]
5 +[[Download link>>https://ftp.we-con.com.cn/Download/WIKI/PI%20HMI/Demo/Lua%20Script/64bitCalculation.zip]]
6 6  
7 7  = **2 MQTT Client** =
8 8  
9 9  The example shows how to connect to the MQTT server.(Test on MaQiaTTo)
10 10  
11 -[[Download link>>https://drive.google.com/file/d/1LWLaOlhWB3BRSTLy3_2RC6Yb0wK7slEB/view?usp=sharing]]
11 +[[Download link>>https://ftp.we-con.com.cn/Download/WIKI/PI%20HMI/Demo/Lua%20Script/Lua%20MQTT%20Client.zip]]
12 12  
13 13  = **3 Timer** =
14 14  
15 15  The example shows how to use the Lua Timer function.
16 16  
17 -[[Download link>>https://drive.google.com/file/d/1iwD2Kca9SIAGCYFBaQbn26zdc4kIUuSV/view?usp=sharing]]
17 +[[Download link>>https://ftp.we-con.com.cn/Download/WIKI/PI%20HMI/Demo/Lua%20Script/LuaTimer.zip]]
18 18  
19 19  = **4 Trigger** =
20 20  
21 21  The example shows how to use the Lua Trigger function.
22 22  
23 -[[Download link>>https://drive.google.com/file/d/1GDCbbMgNSixonXP2erA0VoJbo7qagsCv/view?usp=sharing]]
23 +[[Download link>>https://ftp.we-con.com.cn/Download/WIKI/PI%20HMI/Demo/Lua%20Script/LuaTrigger.zip]]
24 24  
25 25  = **5 User Free Protocol** =
26 26  
27 27  The example shows the simple write and read of User Free protocol
28 28  
29 -[[Download link>>https://drive.google.com/file/d/1v8UZvdclq3OGEajkbd78w7_XY2bY2qAe/view?usp=sharing]]
29 +[[Download link>>https://ftp.we-con.com.cn/Download/WIKI/PI%20HMI/Demo/Lua%20Script/Lua%20User%20Free%20Protocol.zip]]
30 30  
31 31  = **6 Arabic Keypad** =
32 32  
... ... @@ -36,10 +36,18 @@
36 36  
37 37  = **7 FTP Client** =
38 38  
39 -The example shows how the to establish the FTP server at WIN10 OS and make the HMI as FTP client.
39 +The example shows how  to establish the FTP server at WIN10 OS and make V2.0 HMI as FTP client.
40 40  
41 -[[Download link>>https://ftp.we-con.com.cn/Download/WIKI/PI%20HMI/Demo/Lua%20Script/FTP%20Client.zip]]
41 +[[Download link>>https://ftp.we-con.com.cn/Download/WIKI/PI%20HMI/Demo/Lua%20Script/FTP%20ClientV2.zip]]
42 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 +
43 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]]**
44 44  
45 45  2. Enter PC (FTP Server) IP address and port number (Default 21).
... ... @@ -67,11 +67,48 @@
67 67  (% style="text-align:center" %)
68 68  [[image:LuaFTPDemoInitial.png]]
69 69  
78 +(% class="box infomessage" %)
79 +(((
70 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 "~/~/".
81 +)))
71 71  
72 72  (% style="text-align:center" %)
73 73  [[image:LuaFTPDemoFilelist.png]]
74 74  
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 +
75 75  = **8 Telegram Notification** =
76 76  
77 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.
... ... @@ -290,9 +290,317 @@
290 290  
291 291  The example shows automatically line break for long string content according to user input length.
292 292  
338 +[[Download link>>https://ftp.we-con.com.cn/Download/WIKI/PI%20HMI/Demo/Lua%20Script/CRLF.zip]]
339 +
293 293  The following are some demonstrated strings from offline simulator:
294 294  
295 295  (% style="text-align:center" %)
296 296  [[image:WordWrapDisplayResult.png]]
297 297  
298 -[[Download link>>https://ftp.we-con.com.cn/Download/WIKI/PI%20HMI/Demo/Lua%20Script/CRLF.zip]]
345 += 16 Weather forecast HTTP API request =
346 +
347 +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/]]
348 +
349 +[[Download link>>https://ftp.we-con.com.cn/Download/WIKI/PI%20HMI/Demo/Lua%20Script/luahttp.zip]]
350 +
351 +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):
352 +
353 +{{code language="JSON"}}
354 +{
355 + "location": {
356 + "name": "London",
357 + "region": "City of London, Greater London",
358 + "country": "United Kingdom",
359 + "lat": 51.52,
360 + "lon": -0.11,
361 + "tz_id": "Europe/London",
362 + "localtime_epoch": 1688961236,
363 + "localtime": "2023-07-10 4:53"
364 + },
365 + "current": {
366 + "last_updated_epoch": 1688960700,
367 + "last_updated": "2023-07-10 04:45",
368 + "temp_c": 14.0,
369 + "temp_f": 57.2,
370 + "is_day": 0,
371 + "condition": {
372 + "text": "Partly cloudy",
373 + "icon": "//cdn.weatherapi.com/weather/64x64/night/116.png",
374 + "code": 1003
375 + },
376 + "wind_mph": 4.3,
377 + "wind_kph": 6.8,
378 + "wind_degree": 220,
379 + "wind_dir": "SW",
380 + "pressure_mb": 1020.0,
381 + "pressure_in": 30.12,
382 + "precip_mm": 0.0,
383 + "precip_in": 0.0,
384 + "humidity": 88,
385 + "cloud": 50,
386 + "feelslike_c": 13.3,
387 + "feelslike_f": 56.0,
388 + "vis_km": 10.0,
389 + "vis_miles": 6.0,
390 + "uv": 1.0,
391 + "gust_mph": 10.5,
392 + "gust_kph": 16.9
393 + },
394 + "forecast": {
395 + "forecastday": [
396 + {
397 + "date": "2023-07-10",
398 + "date_epoch": 1688947200,
399 + "day": {
400 + "maxtemp_c": 24.4,
401 + "maxtemp_f": 75.9,
402 + "mintemp_c": 14.1,
403 + "mintemp_f": 57.4,
404 + "avgtemp_c": 19.6,
405 + "avgtemp_f": 67.2,
406 + "maxwind_mph": 15.4,
407 + "maxwind_kph": 24.8,
408 + "totalprecip_mm": 0.0,
409 + "totalprecip_in": 0.0,
410 + "totalsnow_cm": 0.0,
411 + "avgvis_km": 10.0,
412 + "avgvis_miles": 6.0,
413 + "avghumidity": 60.0,
414 + "daily_will_it_rain": 0,
415 + "daily_chance_of_rain": 0,
416 + "daily_will_it_snow": 0,
417 + "daily_chance_of_snow": 0,
418 + "condition": {
419 + "text": "Partly cloudy",
420 + "icon": "//cdn.weatherapi.com/weather/64x64/day/116.png",
421 + "code": 1003
422 + },
423 + "uv": 5.0
424 + },
425 + "astro": {
426 + "sunrise": "04:55 AM",
427 + "sunset": "09:16 PM",
428 + "moonrise": "12:26 AM",
429 + "moonset": "02:01 PM",
430 + "moon_phase": "Last Quarter",
431 + "moon_illumination": "51",
432 + "is_moon_up": 1,
433 + "is_sun_up": 0
434 + },
435 + "hour": [
436 + {
437 + "time_epoch": 1688943600,
438 + "time": "2023-07-10 00:00",
439 + "temp_c": 16.2,
440 + "temp_f": 61.2,
441 + "is_day": 0,
442 + "condition": {
443 + "text": "Clear",
444 + "icon": "//cdn.weatherapi.com/weather/64x64/night/113.png",
445 + "code": 1000
446 + },
447 + "wind_mph": 7.2,
448 + "wind_kph": 11.5,
449 + "wind_degree": 223,
450 + "wind_dir": "SW",
451 + "pressure_mb": 1021.0,
452 + "pressure_in": 30.14,
453 + "precip_mm": 0.0,
454 + "precip_in": 0.0,
455 + "humidity": 74,
456 + "cloud": 6,
457 + "feelslike_c": 16.2,
458 + "feelslike_f": 61.2,
459 + "windchill_c": 16.2,
460 + "windchill_f": 61.2,
461 + "heatindex_c": 16.2,
462 + "heatindex_f": 61.2,
463 + "dewpoint_c": 11.6,
464 + "dewpoint_f": 52.9,
465 + "will_it_rain": 0,
466 + "chance_of_rain": 0,
467 + "will_it_snow": 0,
468 + "chance_of_snow": 0,
469 + "vis_km": 10.0,
470 + "vis_miles": 6.0,
471 + "gust_mph": 10.7,
472 + "gust_kph": 17.3,
473 + "uv": 1.0
474 + },
475 + {
476 + "time_epoch": 1688947200,
477 + "time": "2023-07-10 01:00",
478 + "temp_c": 15.7,
479 + "temp_f": 60.3,
480 + "is_day": 0,
481 + "condition": {
482 + "text": "Overcast",
483 + "icon": "//cdn.weatherapi.com/weather/64x64/night/122.png",
484 + "code": 1009
485 + },
486 + "wind_mph": 7.2,
487 + "wind_kph": 11.5,
488 + "wind_degree": 232,
489 + "wind_dir": "SW",
490 + "pressure_mb": 1021.0,
491 + "pressure_in": 30.14,
492 + "precip_mm": 0.0,
493 + "precip_in": 0.0,
494 + "humidity": 75,
495 + "cloud": 100,
496 + "feelslike_c": 15.7,
497 + "feelslike_f": 60.3,
498 + "windchill_c": 15.7,
499 + "windchill_f": 60.3,
500 + "heatindex_c": 15.7,
501 + "heatindex_f": 60.3,
502 + "dewpoint_c": 11.3,
503 + "dewpoint_f": 52.3,
504 + "will_it_rain": 0,
505 + "chance_of_rain": 0,
506 + "will_it_snow": 0,
507 + "chance_of_snow": 0,
508 + "vis_km": 10.0,
509 + "vis_miles": 6.0,
510 + "gust_mph": 10.5,
511 + "gust_kph": 16.9,
512 + "uv": 1.0
513 + },
514 + ...
515 + {
516 + "time_epoch": 1689026400,
517 + "time": "2023-07-10 23:00",
518 + "temp_c": 19.0,
519 + "temp_f": 66.2,
520 + "is_day": 0,
521 + "condition": {
522 + "text": "Overcast",
523 + "icon": "//cdn.weatherapi.com/weather/64x64/night/122.png",
524 + "code": 1009
525 + },
526 + "wind_mph": 9.4,
527 + "wind_kph": 15.1,
528 + "wind_degree": 192,
529 + "wind_dir": "SSW",
530 + "pressure_mb": 1015.0,
531 + "pressure_in": 29.96,
532 + "precip_mm": 0.0,
533 + "precip_in": 0.0,
534 + "humidity": 66,
535 + "cloud": 100,
536 + "feelslike_c": 19.0,
537 + "feelslike_f": 66.2,
538 + "windchill_c": 19.0,
539 + "windchill_f": 66.2,
540 + "heatindex_c": 19.0,
541 + "heatindex_f": 66.2,
542 + "dewpoint_c": 12.4,
543 + "dewpoint_f": 54.3,
544 + "will_it_rain": 0,
545 + "chance_of_rain": 0,
546 + "will_it_snow": 0,
547 + "chance_of_snow": 0,
548 + "vis_km": 10.0,
549 + "vis_miles": 6.0,
550 + "gust_mph": 13.0,
551 + "gust_kph": 20.9,
552 + "uv": 1.0
553 + }
554 + ]
555 + }
556 + ]
557 + }
558 +}
559 +{{/code}}
560 +
561 +The core code is from file API_WEATHER.lua, and function name is get7DayWeatherapiByCityname.
562 +
563 +(% style="text-align:center" %)
564 +[[image:WeatherHTTPAPI.png]]
565 +
566 +The HMI offline simulator result:
567 +
568 +(% style="text-align:center" %)
569 +[[image:WeatherHTTPtestdisplay.png]]
570 +
571 += 17 **CSV File Reading** =
572 +
573 +The example shows how to read the csv file into HMI, and then display as table.
574 +
575 +[[Download link>>https://ftp.we-con.com.cn/Download/WIKI/PI%20HMI/Demo/Lua%20Script/csvRead.zip]]
576 +
577 +The core code is from file readcsv.lua:
578 +
579 +1) The file path is set as Sheet_2023_06_13_15_29_45.csv at the root path for USB;
580 +
581 +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;
582 +
583 +(% style="text-align:center" %)
584 +[[image:CSVreadwithsemicolon.png]]
585 +
586 +3) The reading content is a two-dimensional table, can use the table index to locate the specify data cell;
587 +
588 +For example, want to read the 2nd row C column from the table, the table index will be like table[2][3].
589 +
590 +(% style="text-align:center" %)
591 +[[image:celllocation.png]]
592 +
593 += 18 How to convert a number to a string =
594 +
595 +The example shows how to convert a number to a string using the "tostring()" function.
596 +
597 +[[Download link>>https://ftp.we-con.com.cn/Download/WIKI/PI%20HMI/Demo/Lua%20Script/Number%20to%20string.zip]]
598 +
599 +Note:This demo converts a 64-bit floating to a string,you can change the data format of "@W_HDW0" depand on requirements
600 +
601 +The core code is from file convert.lua:
602 +
603 +(% style="text-align:center" %)
604 +[[image:convert.lua.png]]
605 +
606 +The HMI offline simulator result:
607 +
608 +(% style="text-align:center" %)
609 +[[image:Converttestdisplay.png]]
610 +
611 += 19 HUAWEI CLOUD & OpenCloud =
612 +
613 +About the details, please check the powerpoint or video of Lua MQTT & OpenCloud [[Jump To>>doc:PIStudio.Download.1 Manual.WebHome]]
614 +
615 +[[Download link>>https://ftp.we-con.com.cn/Download/WIKI/PI%20HMI/Demo/Lua%20Script/HuaweiMQ%281%29.zip]]
616 +
617 += 20 UDP Multicast =
618 +
619 +The example shows how to use UDP multicast feature to send and receive data.
620 +
621 +[[Download link>>https://ftp.we-con.com.cn/Download/WIKI/PI%20HMI/Demo/Lua%20Script/UDP%20Multicast.zip]]
622 +
623 +About the details, please check the [[UDP module>>https://docs.we-con.com.cn/bin/view/PIStudio/09%20Lua%20Editor/Lua%20Script/#HUDPmodule-1]]
624 +
625 += 21 ATC module **send SMS** =
626 +
627 +The example shows how to use HMI to customize communication with the ATC SMS module via Lua script to send SMS.
628 +
629 +[[Download link>>https://ftp.we-con.com.cn/Download/WIKI/PI%20HMI/Demo/Lua%20Script/SMS.zip]]
630 +
631 += 22 TCP Client =
632 +
633 +The example shows how to use the Lua Socket TCP module to connect with a TCP server.
634 +
635 +[[Download link>>https://ftp.we-con.com.cn/Download/WIKI/PI%20HMI/Demo/Lua%20Script/TCP%20ClientV1.1.zip]]
636 +
637 +~1. Set the initial values of Server IP and port number.
638 +
639 +(% style="text-align:center" %)
640 +[[image:vcbt6CE6EL.png]]
641 +
642 +2. Set the actual receive length of TCP message according to the actual needs.
643 +
644 +(% style="text-align:center" %)
645 +[[image:tPN1mbz7ny.png]]
646 +
647 += 23 Lua SMS =
648 +
649 +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).
650 +
651 +[[Download link>>https://ftp.we-con.com.cn/Download/WIKI/PI%20HMI/Demo/Lua%20Script/Lua%20SMS%20Library.zip]]
1688968905886-196.png
Author
... ... @@ -1,0 +1,1 @@
1 +XWiki.Hunter
Size
... ... @@ -1,0 +1,1 @@
1 +26.5 KB
Content
1721298723226-463.png
Author
... ... @@ -1,0 +1,1 @@
1 +XWiki.Hunter
Size
... ... @@ -1,0 +1,1 @@
1 +33.9 KB
Content
4ALQq7niTV.png
Author
... ... @@ -1,0 +1,1 @@
1 +XWiki.joe
Size
... ... @@ -1,0 +1,1 @@
1 +20.7 KB
Content
CSVreadwithsemicolon.png
Author
... ... @@ -1,0 +1,1 @@
1 +XWiki.Hunter
Size
... ... @@ -1,0 +1,1 @@
1 +51.3 KB
Content
Converttestdisplay.png
Author
... ... @@ -1,0 +1,1 @@
1 +XWiki.DevinChen
Size
... ... @@ -1,0 +1,1 @@
1 +37.0 KB
Content
WeatherHTTPAPI.png
Author
... ... @@ -1,0 +1,1 @@
1 +XWiki.Hunter
Size
... ... @@ -1,0 +1,1 @@
1 +55.9 KB
Content
WeatherHTTPtestdisplay.png
Author
... ... @@ -1,0 +1,1 @@
1 +XWiki.Hunter
Size
... ... @@ -1,0 +1,1 @@
1 +36.2 KB
Content
celllocation.png
Author
... ... @@ -1,0 +1,1 @@
1 +XWiki.Hunter
Size
... ... @@ -1,0 +1,1 @@
1 +12.6 KB
Content
convert.lua.png
Author
... ... @@ -1,0 +1,1 @@
1 +XWiki.DevinChen
Size
... ... @@ -1,0 +1,1 @@
1 +28.6 KB
Content
dAuz25bdvM.png
Author
... ... @@ -1,0 +1,1 @@
1 +XWiki.joe
Size
... ... @@ -1,0 +1,1 @@
1 +245.8 KB
Content
dZyxVCtWDz.png
Author
... ... @@ -1,0 +1,1 @@
1 +XWiki.joe
Size
... ... @@ -1,0 +1,1 @@
1 +53.7 KB
Content
fdsxie8WHc.png
Author
... ... @@ -1,0 +1,1 @@
1 +XWiki.joe
Size
... ... @@ -1,0 +1,1 @@
1 +22.4 KB
Content
luacsvfilereadcontent.png
Author
... ... @@ -1,0 +1,1 @@
1 +XWiki.Hunter
Size
... ... @@ -1,0 +1,1 @@
1 +46.5 KB
Content
qCzqnKTUhp.png
Author
... ... @@ -1,0 +1,1 @@
1 +XWiki.joe
Size
... ... @@ -1,0 +1,1 @@
1 +30.0 KB
Content
rxQIJkX8ll.png
Author
... ... @@ -1,0 +1,1 @@
1 +XWiki.joe
Size
... ... @@ -1,0 +1,1 @@
1 +22.1 KB
Content
tPN1mbz7ny.png
Author
... ... @@ -1,0 +1,1 @@
1 +XWiki.Hunter
Size
... ... @@ -1,0 +1,1 @@
1 +59.4 KB
Content
vcbt6CE6EL.png
Author
... ... @@ -1,0 +1,1 @@
1 +XWiki.Hunter
Size
... ... @@ -1,0 +1,1 @@
1 +33.9 KB
Content
vfrt3NElMX.png
Author
... ... @@ -1,0 +1,1 @@
1 +XWiki.joe
Size
... ... @@ -1,0 +1,1 @@
1 +234.9 KB
Content
vmG8WGu5P2.png
Author
... ... @@ -1,0 +1,1 @@
1 +XWiki.joe
Size
... ... @@ -1,0 +1,1 @@
1 +22.1 KB
Content