Wiki source code of 05 Lua Script
Version 29.1 by Theodore Xu on 2023/04/11 17:26
Hide last authors
author | version | line-number | content |
---|---|---|---|
![]() |
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 | |||
![]() |
12.1 | 7 | = **2 MQTT Client** = |
![]() |
1.1 | 8 | |
9 | The example shows how to connect to the MQTT server.(Test on MaQiaTTo) | ||
10 | |||
![]() |
12.1 | 11 | [[Download link>>https://drive.google.com/file/d/1LWLaOlhWB3BRSTLy3_2RC6Yb0wK7slEB/view?usp=sharing]] |
![]() |
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 | |||
![]() |
14.1 | 29 | [[Download link>>https://drive.google.com/file/d/1v8UZvdclq3OGEajkbd78w7_XY2bY2qAe/view?usp=sharing]] |
![]() |
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]] | ||
![]() |
3.1 | 36 | |
37 | = **7 FTP Client** = | ||
38 | |||
![]() |
8.1 | 39 | The example shows how the to establish the FTP server at WIN10 OS and make the HMI as FTP client. |
![]() |
3.1 | 40 | |
![]() |
17.1 | 41 | [[Download link>>https://ftp.we-con.com.cn/Download/WIKI/PI%20HMI/Demo/Lua%20Script/FTP%20Client.zip]] |
42 | |||
![]() |
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]]** |
![]() |
7.1 | 44 | |
![]() |
15.1 | 45 | 2. Enter PC (FTP Server) IP address and port number (Default 21). |
![]() |
7.1 | 46 | |
![]() |
15.1 | 47 | (% style="text-align:center" %) |
48 | [[image:LuaFTPDemoIP&Port.png]] | ||
![]() |
8.1 | 49 | |
![]() |
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. |
![]() |
8.1 | 51 | |
![]() |
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 | |||
![]() |
16.1 | 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 "~/~/". |
![]() |
15.1 | 71 | |
![]() |
16.1 | 72 | (% style="text-align:center" %) |
73 | [[image:LuaFTPDemoFilelist.png]] | ||
74 | |||
![]() |
4.1 | 75 | = **8 Telegram Notification** = |
76 | |||
![]() |
5.1 | 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. |
![]() |
4.1 | 78 | |
79 | [[Download link>>https://drive.google.com/file/d/1wV0O79TJ2mgUEBkKcjrb53PM8DkTQxVo/view?usp=sharing]] | ||
![]() |
18.1 | 80 | |
81 | |||
82 | = 9** LINE Notify** = | ||
83 | |||
![]() |
21.1 | 84 | 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. |
![]() |
18.1 | 85 | |
![]() |
19.1 | 86 | [[Download link>>https://ftp.we-con.com.cn/Download/WIKI/PI%20HMI/Demo/Lua%20Script/line.zip]] |
![]() |
21.2 | 87 | |
88 | |||
89 | = 10** How to parse value from subscribing MQTT topic** = | ||
90 | |||
![]() |
21.3 | 91 | 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". |
92 | |||
![]() |
22.1 | 93 | [[Download link>>https://ftp.we-con.com.cn/Download/WIKI/PI%20HMI/Demo/Lua%20Script/MQTT%20JSON%20parse.zip]] |
![]() |
21.3 | 94 | |
![]() |
21.2 | 95 | And the subscribed content is like following: |
96 | |||
97 | {{code language="json"}} | ||
98 | { | ||
99 | "Cluster_ID": "2", | ||
100 | "timestamp": "07:01:45-18.02.2022", | ||
101 | "Cust_id": "TAM-1", | ||
102 | "Platform_id": "Pro44ead8a8dc3345ACC", | ||
103 | "version": "1.5", | ||
104 | "Line_active": "true", | ||
105 | "Total_sensor": "5", | ||
106 | "F1": [ | ||
107 | { | ||
108 | "Counter": "0", | ||
109 | "Value": "11" | ||
110 | }, | ||
111 | { | ||
112 | "Counter": "1", | ||
113 | "Value": "21.1" | ||
114 | }, | ||
115 | { | ||
116 | "Counter": "2", | ||
117 | "Value": "9" | ||
118 | }, | ||
119 | { | ||
120 | "Counter": "3", | ||
121 | "Value": "0.06" | ||
122 | }, | ||
123 | { | ||
124 | "Counter": "4", | ||
125 | "Value": "9" | ||
126 | }, | ||
127 | { | ||
128 | "Counter": "5", | ||
129 | "Value": "19" | ||
130 | }, | ||
131 | { | ||
132 | "Counter": "6", | ||
133 | "Value": "9" | ||
134 | }, | ||
135 | { | ||
136 | "Counter": "7", | ||
137 | "Value": "19" | ||
138 | }, | ||
139 | { | ||
140 | "Counter": "8", | ||
141 | "Value": "22.89" | ||
142 | } | ||
143 | ], | ||
144 | "F2": [ | ||
145 | { | ||
146 | "Counter": "0", | ||
147 | "Value": "1.1" | ||
148 | }, | ||
149 | { | ||
150 | "Counter": "1", | ||
151 | "Value": "21.21" | ||
152 | }, | ||
153 | { | ||
154 | "Counter": "2", | ||
155 | "Value": "19" | ||
156 | }, | ||
157 | { | ||
158 | "Counter": "3", | ||
159 | "Value": "0.6" | ||
160 | }, | ||
161 | { | ||
162 | "Counter": "4", | ||
163 | "Value": "8" | ||
164 | }, | ||
165 | { | ||
166 | "Counter": "5", | ||
167 | "Value": "19.1" | ||
168 | }, | ||
169 | { | ||
170 | "Counter": "6", | ||
171 | "Value": "5.1" | ||
172 | } | ||
173 | ], | ||
174 | "F4": [ | ||
175 | { | ||
176 | "Counter": "0", | ||
177 | "Value": "11" | ||
178 | }, | ||
179 | { | ||
180 | "Counter": "1", | ||
181 | "Value": "21.1" | ||
182 | }, | ||
183 | { | ||
184 | "Counter": "2", | ||
185 | "Value": "9" | ||
186 | }, | ||
187 | { | ||
188 | "Counter": "3", | ||
189 | "Value": "0.06" | ||
190 | }, | ||
191 | { | ||
192 | "Counter": "4", | ||
193 | "Value": "9" | ||
194 | }, | ||
195 | { | ||
196 | "Counter": "5", | ||
197 | "Value": "19" | ||
198 | }, | ||
199 | { | ||
200 | "Counter": "6", | ||
201 | "Value": "9" | ||
202 | }, | ||
203 | { | ||
204 | "Counter": "7", | ||
205 | "Value": "12.1" | ||
206 | }, | ||
207 | { | ||
208 | "Counter": "8", | ||
209 | "Value": "18.1" | ||
210 | } | ||
211 | ] | ||
212 | } | ||
213 | {{/code}} | ||
214 | |||
![]() |
21.3 | 215 | 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). |
![]() |
21.2 | 216 | |
217 | (% style="text-align:center" %) | ||
218 | [[image:MQTTScreen.png]] | ||
219 | |||
![]() |
21.3 | 220 | 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: |
221 | |||
222 | (% style="text-align:center" %) | ||
223 | [[image:MQTTactualtestingresult.png]] | ||
224 | |||
225 | The JSON parser function is located on mqttAction.lua, called mqtt_msg_callback. | ||
226 | |||
227 | (% style="text-align:center" %) | ||
228 | [[image:MQTTJSONparsefunction.png]] | ||
229 | |||
![]() |
23.1 | 230 | = **11 High-Low Byte Switch** = |
![]() |
24.1 | 231 | |
![]() |
26.1 | 232 | The example shows how to convert high bytes and low bytes for one 16bit address. Main function is like following: |
![]() |
25.1 | 233 | |
234 | {{code language="lua"}} | ||
235 | local lowByte = val & 0x00ff | ||
236 | local shiftHigh = lowByte << 8 | ||
237 | local highByte = val & 0xff00 | ||
238 | local shiftLow = highByte >> 8 | ||
239 | local newVal = shiftHigh|shiftLow | ||
240 | {{/code}} | ||
241 | |||
![]() |
27.1 | 242 | [[Download link>>https://ftp.we-con.com.cn/Download/WIKI/PI%20HMI/Demo/Lua%20Script/byteswitch.zip]] |
![]() |
28.1 | 243 | |
244 | = **12 Week time setting** = | ||
245 | |||
![]() |
29.1 | 246 | The example shows how to use day time to control functions: |
247 | |||
![]() |
28.1 | 248 | [[Download link>>https://ftp.we-con.com.cn/Download/WIKI/PI%20HMI/Demo/Lua%20Script/week%20time.zip]] |