Wiki source code of 05 Lua Script

Version 59.1 by Joe on 2024/09/06 11:14

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