Wiki source code of 05 Lua Script

Version 57.2 by Joe on 2024/09/06 10:53

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