Wiki source code of 05 Lua Script

Version 66.1 by Molly on 2024/10/23 18:40

Show last authors
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://ftp.we-con.com.cn/Download/WIKI/PI%20HMI/Demo/Lua%20Script/64bitCalculation.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://drive.google.com/file/d/1v8UZvdclq3OGEajkbd78w7_XY2bY2qAe/view?usp=sharing]]
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]]
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://drive.google.com/file/d/1wV0O79TJ2mgUEBkKcjrb53PM8DkTQxVo/view?usp=sharing]]
125
126
127 = 9** LINE Notify** =
128
129 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.
130
131 [[Download link>>https://ftp.we-con.com.cn/Download/WIKI/PI%20HMI/Demo/Lua%20Script/line.zip]]
132
133
134 = 10** How to parse value from subscribing MQTT topic** =
135
136 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".
137
138 [[Download link>>https://ftp.we-con.com.cn/Download/WIKI/PI%20HMI/Demo/Lua%20Script/MQTT%20JSON%20parse.zip]]
139
140 And the subscribed content is like following:
141
142 {{code language="json"}}
143 {
144 "Cluster_ID": "2",
145 "timestamp": "07:01:45-18.02.2022",
146 "Cust_id": "TAM-1",
147 "Platform_id": "Pro44ead8a8dc3345ACC",
148 "version": "1.5",
149 "Line_active": "true",
150 "Total_sensor": "5",
151 "F1": [
152 {
153 "Counter": "0",
154 "Value": "11"
155 },
156 {
157 "Counter": "1",
158 "Value": "21.1"
159 },
160 {
161 "Counter": "2",
162 "Value": "9"
163 },
164 {
165 "Counter": "3",
166 "Value": "0.06"
167 },
168 {
169 "Counter": "4",
170 "Value": "9"
171 },
172 {
173 "Counter": "5",
174 "Value": "19"
175 },
176 {
177 "Counter": "6",
178 "Value": "9"
179 },
180 {
181 "Counter": "7",
182 "Value": "19"
183 },
184 {
185 "Counter": "8",
186 "Value": "22.89"
187 }
188 ],
189 "F2": [
190 {
191 "Counter": "0",
192 "Value": "1.1"
193 },
194 {
195 "Counter": "1",
196 "Value": "21.21"
197 },
198 {
199 "Counter": "2",
200 "Value": "19"
201 },
202 {
203 "Counter": "3",
204 "Value": "0.6"
205 },
206 {
207 "Counter": "4",
208 "Value": "8"
209 },
210 {
211 "Counter": "5",
212 "Value": "19.1"
213 },
214 {
215 "Counter": "6",
216 "Value": "5.1"
217 }
218 ],
219 "F4": [
220 {
221 "Counter": "0",
222 "Value": "11"
223 },
224 {
225 "Counter": "1",
226 "Value": "21.1"
227 },
228 {
229 "Counter": "2",
230 "Value": "9"
231 },
232 {
233 "Counter": "3",
234 "Value": "0.06"
235 },
236 {
237 "Counter": "4",
238 "Value": "9"
239 },
240 {
241 "Counter": "5",
242 "Value": "19"
243 },
244 {
245 "Counter": "6",
246 "Value": "9"
247 },
248 {
249 "Counter": "7",
250 "Value": "12.1"
251 },
252 {
253 "Counter": "8",
254 "Value": "18.1"
255 }
256 ]
257 }
258 {{/code}}
259
260 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).
261
262 (% style="text-align:center" %)
263 [[image:MQTTScreen.png]]
264
265 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:
266
267 (% style="text-align:center" %)
268 [[image:MQTTactualtestingresult.png]]
269
270 The JSON parser function is located on mqttAction.lua, called mqtt_msg_callback.
271
272 (% style="text-align:center" %)
273 [[image:MQTTJSONparsefunction.png]]
274
275 = **11 High-Low Byte Switch** =
276
277 The example shows how to convert high bytes and low bytes for one 16bit address. Main function is like following:
278
279 {{code language="lua"}}
280 local lowByte = val & 0x00ff
281 local shiftHigh = lowByte << 8
282 local highByte = val & 0xff00
283 local shiftLow = highByte >> 8
284 local newVal = shiftHigh|shiftLow
285 {{/code}}
286
287 [[Download link>>https://ftp.we-con.com.cn/Download/WIKI/PI%20HMI/Demo/Lua%20Script/byteswitch.zip]]
288
289 = **12 Week time setting** =
290
291 The example shows how to use day time to control functions:
292
293 [[Download link>>https://ftp.we-con.com.cn/Download/WIKI/PI%20HMI/Demo/Lua%20Script/week%20time.zip]]
294
295 = **13 MQTT Multi-link** =
296
297 The example shows use mqtt realize multi-machine function:
298
299 [[Download link>>https://ftp.we-con.com.cn/Download/WIKI/PI%20HMI/Demo/Lua%20Script/MQTT%20Multi-link.zip]]
300
301 = **14 CSV File Generation** =
302
303 The example shows convert the custom lua table data into csv string, then output as a file:
304
305 [[Download link>>https://ftp.we-con.com.cn/Download/WIKI/PI%20HMI/Demo/Lua%20Script/csvGenerate.zip]]
306
307 The following steps take HMI simulator as example:
308
309 ~1. Click button "Create Table" to draw lua table in screen.
310
311 (% style="text-align:center" %)
312 [[image:csvGenerateSteps.png]]
313
314 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.
315
316 (% style="text-align:center" %)
317 [[image:csvGenerateSimulationPath.png]]
318
319 3. Click button "upload" to upload csv file into server side. In this case, we can check the upload path for PC.
320
321 (% style="text-align:center" %)
322 [[image:csvGenerateServerPath.png]]
323
324 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":
325
326 (% style="text-align:center" %)
327 [[image:csvGenerateLuaCode.png]]
328
329 The table drawing is mainly using the following function to implement:
330
331 (% style="text-align:center" %)
332 [[image:csvLuaTableDrawing.png]]
333
334 = 15 **String customized length word-wrapped** =
335
336 The example shows automatically line break for long string content according to user input length.
337
338 [[Download link>>https://ftp.we-con.com.cn/Download/WIKI/PI%20HMI/Demo/Lua%20Script/CRLF.zip]]
339
340 The following are some demonstrated strings from offline simulator:
341
342 (% style="text-align:center" %)
343 [[image:WordWrapDisplayResult.png]]
344
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]]