Changes for page 05 Lua Script

Last modified by Devin Chen on 2025/03/04 14:07

From version 20.1
edited by Theodore Xu
on 2022/09/21 14:58
Change comment: There is no comment for this version
To version 34.1
edited by Hunter
on 2023/04/26 19:00
Change comment: There is no comment for this version

Summary

Details

Page properties
Author
... ... @@ -1,1 +1,1 @@
1 -XWiki.AiXia
1 +XWiki.Hunter
Content
... ... @@ -81,6 +81,204 @@
81 81  
82 82  = 9** LINE Notify** =
83 83  
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.
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.
85 85  
86 86  [[Download link>>https://ftp.we-con.com.cn/Download/WIKI/PI%20HMI/Demo/Lua%20Script/line.zip]]
87 +
88 +
89 += 10** How to parse value from subscribing MQTT topic** =
90 +
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 +
93 +[[Download link>>https://ftp.we-con.com.cn/Download/WIKI/PI%20HMI/Demo/Lua%20Script/MQTT%20JSON%20parse.zip]]
94 +
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 +
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).
216 +
217 +(% style="text-align:center" %)
218 +[[image:MQTTScreen.png]]
219 +
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 +
230 += **11 High-Low Byte Switch** =
231 +
232 +The example shows how to convert high bytes and low bytes for one 16bit address. Main function is like following:
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 +
242 +[[Download link>>https://ftp.we-con.com.cn/Download/WIKI/PI%20HMI/Demo/Lua%20Script/byteswitch.zip]]
243 +
244 += **12 Week time setting** =
245 +
246 +The example shows how to use day time to control functions:
247 +
248 +[[Download link>>https://ftp.we-con.com.cn/Download/WIKI/PI%20HMI/Demo/Lua%20Script/week%20time.zip]]
249 +
250 += **13 MQTT Multi-link** =
251 +
252 +The example shows use mqtt realize multi-machine function:
253 +
254 +[[Download link>>https://ftp.we-con.com.cn/Download/WIKI/PI%20HMI/Demo/Lua%20Script/MQTT%20Multi-link.zip]]
255 +
256 += **14 CSV File Generation** =
257 +
258 +The example shows convert the custom lua table data into csv string, then output as a file:
259 +
260 +[[Download link>>https://ftp.we-con.com.cn/Download/WIKI/PI%20HMI/Demo/Lua%20Script/csvGenerate.zip]]
261 +
262 +The following steps take HMI simulator as example:
263 +
264 +~1. Click button "Create Table" to draw lua table in screen.
265 +
266 +(% style="text-align:center" %)
267 +[[image:csvGenerateSteps.png]]
268 +
269 +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.
270 +
271 +(% style="text-align:center" %)
272 +[[image:csvGenerateSimulationPath.png]]
273 +
274 +3. Click button "upload" to upload csv file into server side. In this case, we can check the upload path for PC.
275 +
276 +(% style="text-align:center" %)
277 +[[image:csvGenerateServerPath.png]]
278 +
279 +The table drawing and 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":
280 +
281 +(% style="text-align:center" %)
282 +[[image:csvGenerateLuaCode.png]]
283 +
284 +
MQTTJSONparsefunction.png
Author
... ... @@ -1,0 +1,1 @@
1 +XWiki.Hunter
Size
... ... @@ -1,0 +1,1 @@
1 +124.0 KB
Content
MQTTScreen.png
Author
... ... @@ -1,0 +1,1 @@
1 +XWiki.Hunter
Size
... ... @@ -1,0 +1,1 @@
1 +48.7 KB
Content
MQTTactualtestingresult.png
Author
... ... @@ -1,0 +1,1 @@
1 +XWiki.Hunter
Size
... ... @@ -1,0 +1,1 @@
1 +945.6 KB
Content
csvGenerateLuaCode.png
Author
... ... @@ -1,0 +1,1 @@
1 +XWiki.Hunter
Size
... ... @@ -1,0 +1,1 @@
1 +50.2 KB
Content
csvGenerateServerPath.png
Author
... ... @@ -1,0 +1,1 @@
1 +XWiki.Hunter
Size
... ... @@ -1,0 +1,1 @@
1 +56.3 KB
Content
csvGenerateSimulationPath.png
Author
... ... @@ -1,0 +1,1 @@
1 +XWiki.Hunter
Size
... ... @@ -1,0 +1,1 @@
1 +29.7 KB
Content
csvGenerateSteps.png
Author
... ... @@ -1,0 +1,1 @@
1 +XWiki.Hunter
Size
... ... @@ -1,0 +1,1 @@
1 +32.5 KB
Content
image-20221213120221-1.png
Author
... ... @@ -1,0 +1,1 @@
1 +XWiki.Hunter
Size
... ... @@ -1,0 +1,1 @@
1 +62.4 KB
Content