Changes for page 05 Lua Script

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

From version 31.2
edited by Hunter
on 2023/04/26 12:00
Change comment: There is no comment for this version
To version 39.1
edited by Hunter
on 2023/07/10 14:03
Change comment: There is no comment for this version

Summary

Details

Page properties
Content
... ... @@ -255,8 +255,270 @@
255 255  
256 256  = **14 CSV File Generation** =
257 257  
258 -The example shows convert the lua table data into csv string, then output as a file:
258 +The example shows convert the custom lua table data into csv string, then output as a file:
259 259  
260 -~1. Click "Create Table" to draw the lua table into HMI
260 +[[Download link>>https://ftp.we-con.com.cn/Download/WIKI/PI%20HMI/Demo/Lua%20Script/csvGenerate.zip]]
261 261  
262 -
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 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 +The table drawing is mainly using the following function to implement:
285 +
286 +(% style="text-align:center" %)
287 +[[image:csvLuaTableDrawing.png]]
288 +
289 += 15 **String customized length word-wrapped** =
290 +
291 +The example shows automatically line break for long string content according to user input length.
292 +
293 +[[Download link>>https://ftp.we-con.com.cn/Download/WIKI/PI%20HMI/Demo/Lua%20Script/CRLF.zip]]
294 +
295 +The following are some demonstrated strings from offline simulator:
296 +
297 +(% style="text-align:center" %)
298 +[[image:WordWrapDisplayResult.png]]
299 +
300 += 16 Weather forecast HTTP API request =
301 +
302 +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/]]
303 +
304 +[[Download link>>https://ftp.we-con.com.cn/Download/WIKI/PI%20HMI/Demo/Lua%20Script/luahttp.zip]]
305 +
306 +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):
307 +
308 +{{code language="JSON"}}
309 +{
310 + "location": {
311 + "name": "London",
312 + "region": "City of London, Greater London",
313 + "country": "United Kingdom",
314 + "lat": 51.52,
315 + "lon": -0.11,
316 + "tz_id": "Europe/London",
317 + "localtime_epoch": 1688961236,
318 + "localtime": "2023-07-10 4:53"
319 + },
320 + "current": {
321 + "last_updated_epoch": 1688960700,
322 + "last_updated": "2023-07-10 04:45",
323 + "temp_c": 14.0,
324 + "temp_f": 57.2,
325 + "is_day": 0,
326 + "condition": {
327 + "text": "Partly cloudy",
328 + "icon": "//cdn.weatherapi.com/weather/64x64/night/116.png",
329 + "code": 1003
330 + },
331 + "wind_mph": 4.3,
332 + "wind_kph": 6.8,
333 + "wind_degree": 220,
334 + "wind_dir": "SW",
335 + "pressure_mb": 1020.0,
336 + "pressure_in": 30.12,
337 + "precip_mm": 0.0,
338 + "precip_in": 0.0,
339 + "humidity": 88,
340 + "cloud": 50,
341 + "feelslike_c": 13.3,
342 + "feelslike_f": 56.0,
343 + "vis_km": 10.0,
344 + "vis_miles": 6.0,
345 + "uv": 1.0,
346 + "gust_mph": 10.5,
347 + "gust_kph": 16.9
348 + },
349 + "forecast": {
350 + "forecastday": [
351 + {
352 + "date": "2023-07-10",
353 + "date_epoch": 1688947200,
354 + "day": {
355 + "maxtemp_c": 24.4,
356 + "maxtemp_f": 75.9,
357 + "mintemp_c": 14.1,
358 + "mintemp_f": 57.4,
359 + "avgtemp_c": 19.6,
360 + "avgtemp_f": 67.2,
361 + "maxwind_mph": 15.4,
362 + "maxwind_kph": 24.8,
363 + "totalprecip_mm": 0.0,
364 + "totalprecip_in": 0.0,
365 + "totalsnow_cm": 0.0,
366 + "avgvis_km": 10.0,
367 + "avgvis_miles": 6.0,
368 + "avghumidity": 60.0,
369 + "daily_will_it_rain": 0,
370 + "daily_chance_of_rain": 0,
371 + "daily_will_it_snow": 0,
372 + "daily_chance_of_snow": 0,
373 + "condition": {
374 + "text": "Partly cloudy",
375 + "icon": "//cdn.weatherapi.com/weather/64x64/day/116.png",
376 + "code": 1003
377 + },
378 + "uv": 5.0
379 + },
380 + "astro": {
381 + "sunrise": "04:55 AM",
382 + "sunset": "09:16 PM",
383 + "moonrise": "12:26 AM",
384 + "moonset": "02:01 PM",
385 + "moon_phase": "Last Quarter",
386 + "moon_illumination": "51",
387 + "is_moon_up": 1,
388 + "is_sun_up": 0
389 + },
390 + "hour": [
391 + {
392 + "time_epoch": 1688943600,
393 + "time": "2023-07-10 00:00",
394 + "temp_c": 16.2,
395 + "temp_f": 61.2,
396 + "is_day": 0,
397 + "condition": {
398 + "text": "Clear",
399 + "icon": "//cdn.weatherapi.com/weather/64x64/night/113.png",
400 + "code": 1000
401 + },
402 + "wind_mph": 7.2,
403 + "wind_kph": 11.5,
404 + "wind_degree": 223,
405 + "wind_dir": "SW",
406 + "pressure_mb": 1021.0,
407 + "pressure_in": 30.14,
408 + "precip_mm": 0.0,
409 + "precip_in": 0.0,
410 + "humidity": 74,
411 + "cloud": 6,
412 + "feelslike_c": 16.2,
413 + "feelslike_f": 61.2,
414 + "windchill_c": 16.2,
415 + "windchill_f": 61.2,
416 + "heatindex_c": 16.2,
417 + "heatindex_f": 61.2,
418 + "dewpoint_c": 11.6,
419 + "dewpoint_f": 52.9,
420 + "will_it_rain": 0,
421 + "chance_of_rain": 0,
422 + "will_it_snow": 0,
423 + "chance_of_snow": 0,
424 + "vis_km": 10.0,
425 + "vis_miles": 6.0,
426 + "gust_mph": 10.7,
427 + "gust_kph": 17.3,
428 + "uv": 1.0
429 + },
430 + {
431 + "time_epoch": 1688947200,
432 + "time": "2023-07-10 01:00",
433 + "temp_c": 15.7,
434 + "temp_f": 60.3,
435 + "is_day": 0,
436 + "condition": {
437 + "text": "Overcast",
438 + "icon": "//cdn.weatherapi.com/weather/64x64/night/122.png",
439 + "code": 1009
440 + },
441 + "wind_mph": 7.2,
442 + "wind_kph": 11.5,
443 + "wind_degree": 232,
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": 75,
450 + "cloud": 100,
451 + "feelslike_c": 15.7,
452 + "feelslike_f": 60.3,
453 + "windchill_c": 15.7,
454 + "windchill_f": 60.3,
455 + "heatindex_c": 15.7,
456 + "heatindex_f": 60.3,
457 + "dewpoint_c": 11.3,
458 + "dewpoint_f": 52.3,
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.5,
466 + "gust_kph": 16.9,
467 + "uv": 1.0
468 + },
469 + ...
470 + {
471 + "time_epoch": 1689026400,
472 + "time": "2023-07-10 23:00",
473 + "temp_c": 19.0,
474 + "temp_f": 66.2,
475 + "is_day": 0,
476 + "condition": {
477 + "text": "Overcast",
478 + "icon": "//cdn.weatherapi.com/weather/64x64/night/122.png",
479 + "code": 1009
480 + },
481 + "wind_mph": 9.4,
482 + "wind_kph": 15.1,
483 + "wind_degree": 192,
484 + "wind_dir": "SSW",
485 + "pressure_mb": 1015.0,
486 + "pressure_in": 29.96,
487 + "precip_mm": 0.0,
488 + "precip_in": 0.0,
489 + "humidity": 66,
490 + "cloud": 100,
491 + "feelslike_c": 19.0,
492 + "feelslike_f": 66.2,
493 + "windchill_c": 19.0,
494 + "windchill_f": 66.2,
495 + "heatindex_c": 19.0,
496 + "heatindex_f": 66.2,
497 + "dewpoint_c": 12.4,
498 + "dewpoint_f": 54.3,
499 + "will_it_rain": 0,
500 + "chance_of_rain": 0,
501 + "will_it_snow": 0,
502 + "chance_of_snow": 0,
503 + "vis_km": 10.0,
504 + "vis_miles": 6.0,
505 + "gust_mph": 13.0,
506 + "gust_kph": 20.9,
507 + "uv": 1.0
508 + }
509 + ]
510 + }
511 + ]
512 + }
513 +}
514 +{{/code}}
515 +
516 +The core code is from file API_WEATHER.lua, and function name is get7DayWeatherapiByCityname.
517 +
518 +(% style="text-align:center" %)
519 +[[image:WeatherHTTPAPI.png]]
520 +
521 +The HMI offline simulator result:
522 +
523 +(% style="text-align:center" %)
524 +[[image:WeatherHTTPtestdisplay.png]]
1688968905886-196.png
Author
... ... @@ -1,0 +1,1 @@
1 +XWiki.Hunter
Size
... ... @@ -1,0 +1,1 @@
1 +26.5 KB
Content
WeatherHTTPAPI.png
Author
... ... @@ -1,0 +1,1 @@
1 +XWiki.Hunter
Size
... ... @@ -1,0 +1,1 @@
1 +55.9 KB
Content
WeatherHTTPtestdisplay.png
Author
... ... @@ -1,0 +1,1 @@
1 +XWiki.Hunter
Size
... ... @@ -1,0 +1,1 @@
1 +36.2 KB
Content
WordWrapDisplayResult.png
Author
... ... @@ -1,0 +1,1 @@
1 +XWiki.Hunter
Size
... ... @@ -1,0 +1,1 @@
1 +24.4 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
csvLuaTableDrawing.png
Author
... ... @@ -1,0 +1,1 @@
1 +XWiki.Hunter
Size
... ... @@ -1,0 +1,1 @@
1 +43.9 KB
Content