Changes for page 05 Lua Script

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

From version 38.1
edited by Hunter
on 2023/07/07 18:14
Change comment: There is no comment for this version
To version 47.1
edited by Devin Chen
on 2023/08/17 15:33
Change comment: There is no comment for this version

Summary

Details

Page properties
Author
... ... @@ -1,1 +1,1 @@
1 -XWiki.Hunter
1 +XWiki.DevinChen
Content
... ... @@ -38,7 +38,7 @@
38 38  
39 39  The example shows how the to establish the FTP server at WIN10 OS and make the HMI as FTP client.
40 40  
41 -[[Download link>>https://ftp.we-con.com.cn/Download/WIKI/PI%20HMI/Demo/Lua%20Script/FTP%20Client.zip]]
41 +[[Download link>>https://ftp.we-con.com.cn/Download/WIKI/PI%20HMI/Demo/Lua%20Script/FTP%20ClientV2.zip]]
42 42  
43 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 44  
... ... @@ -296,3 +296,267 @@
296 296  
297 297  (% style="text-align:center" %)
298 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]]
525 +
526 += 17 **CSV File Reading** =
527 +
528 +The example shows how to read the csv file into HMI, and then display as table.
529 +
530 +[[Download link>>https://ftp.we-con.com.cn/Download/WIKI/PI%20HMI/Demo/Lua%20Script/csvRead.zip]]
531 +
532 +The core code is from file readcsv.lua:
533 +
534 +1) The file path is set as Sheet_2023_06_13_15_29_45.csv at the root path for USB;
535 +
536 +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;
537 +
538 +(% style="text-align:center" %)
539 +[[image:CSVreadwithsemicolon.png]]
540 +
541 +3) The reading content is a two-dimensional table, can use the table index to locate the specify data cell;
542 +
543 +For example, want to read the 2nd row C column from the table, the table index will be like table[2][3].
544 +
545 +(% style="text-align:center" %)
546 +[[image:celllocation.png]]
547 +
548 += 18 Convert a number to a string =
549 +
550 +The example shows how to convert a number to a string using the "tostring()" function.
551 +
552 +
553 +The core code is from file convert.lua:
554 +
555 +(% style="text-align:center" %)
556 +[[image:convert.lua.png]]
557 +
558 +The HMI offline simulator result:
559 +
560 +(% style="text-align:center" %)
561 +[[image:Converttestdisplay.png]]
562 +
1688968905886-196.png
Author
... ... @@ -1,0 +1,1 @@
1 +XWiki.Hunter
Size
... ... @@ -1,0 +1,1 @@
1 +26.5 KB
Content
CSVreadwithsemicolon.png
Author
... ... @@ -1,0 +1,1 @@
1 +XWiki.Hunter
Size
... ... @@ -1,0 +1,1 @@
1 +51.3 KB
Content
Converttestdisplay.png
Author
... ... @@ -1,0 +1,1 @@
1 +XWiki.DevinChen
Size
... ... @@ -1,0 +1,1 @@
1 +37.0 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
celllocation.png
Author
... ... @@ -1,0 +1,1 @@
1 +XWiki.Hunter
Size
... ... @@ -1,0 +1,1 @@
1 +12.6 KB
Content
convert.lua.png
Author
... ... @@ -1,0 +1,1 @@
1 +XWiki.DevinChen
Size
... ... @@ -1,0 +1,1 @@
1 +28.6 KB
Content
luacsvfilereadcontent.png
Author
... ... @@ -1,0 +1,1 @@
1 +XWiki.Hunter
Size
... ... @@ -1,0 +1,1 @@
1 +46.5 KB
Content