Wiki source code of 05 Lua Script

Version 61.1 by Hunter on 2024/09/27 17:37

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