05 Lua Script

Last modified by Theodore Xu on 2024/01/23 14:03

1 64bits Address Basic Algorithm

The example shows how to do the Addition, Subtraction, Multiplication and Division for HMI internal address. 

Download link

2 MQTT Client

The example shows how to connect to the MQTT server.(Test on MaQiaTTo)

Download link

3 Timer

The example shows how to use the Lua Timer function.

Download link

4 Trigger

The example shows how to use the Lua Trigger function.

Download link

5 User Free Protocol

The example shows the simple write and read of User Free protocol

Download link

6 Arabic Keypad

The example shows how to use the Lua to achieve the Arabic keypad. Also available for other non-English language like Cyrillic.

Download link

7 FTP Client

The example shows how the to establish the FTP server at WIN10 OS and make the HMI as FTP client.

Download link

1. About how to set up the FTP server in Windows PC: Build an FTP Site on IIS , How to set up and manage an FTP server on Windows 10

2. Enter PC (FTP Server) IP address and port number (Default 21).

LuaFTPDemoIP&Port.png

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.

LuaFTPDemoUpload.png

4. Change drive letter of file save path according to drop-down list (There are three option available: Flash, UDisk and SDCard). 

LuaFTPDemoDropBox.png

5. Change the filename of the file that needs to be downloaded from PC(Server).

LuaFTPDemoDownload.png

6. Set the default configuration in file "Script_BG.lua"

LuaFTPDemoInitial.png

✎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 "//".

LuaFTPDemoFilelist.png

8 Telegram Notification

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.

Download link

9 LINE Notify

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.

Download link

10 How to parse value from subscribing MQTT topic

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". 

Download link

And the subscribed content is like following:

{
  "Cluster_ID": "2",
  "timestamp": "07:01:45-18.02.2022",
  "Cust_id": "TAM-1",
  "Platform_id": "Pro44ead8a8dc3345ACC",
  "version": "1.5",
  "Line_active": "true",
  "Total_sensor": "5",
  "F1": [
      {
        "Counter": "0",
        "Value": "11"
      },
      {
        "Counter": "1",
        "Value": "21.1"
      },
      {
        "Counter": "2",
        "Value": "9"
      },
      {
        "Counter": "3",
        "Value": "0.06"
      },
      {
        "Counter": "4",
        "Value": "9"
      },
      {
        "Counter": "5",
        "Value": "19"
      },
      {
        "Counter": "6",
        "Value": "9"
      },
      {
        "Counter": "7",
        "Value": "19"
      },
      {
        "Counter": "8",
        "Value": "22.89"
      }
   ],
  "F2": [
      {
        "Counter": "0",
        "Value": "1.1"
      },
      {
        "Counter": "1",
        "Value": "21.21"
      },
      {
        "Counter": "2",
        "Value": "19"
      },
      {
        "Counter": "3",
        "Value": "0.6"
      },
      {
        "Counter": "4",
        "Value": "8"
      },
      {
        "Counter": "5",
        "Value": "19.1"
      },
      {
        "Counter": "6",
        "Value": "5.1"
      }
   ],
  "F4": [
      {
        "Counter": "0",
        "Value": "11"
      },
      {
        "Counter": "1",
        "Value": "21.1"
      },
      {
        "Counter": "2",
        "Value": "9"
      },
      {
        "Counter": "3",
        "Value": "0.06"
      },
      {
        "Counter": "4",
        "Value": "9"
      },
      {
        "Counter": "5",
        "Value": "19"
      },
      {
        "Counter": "6",
        "Value": "9"
      },
      {
        "Counter": "7",
        "Value": "12.1"
      },
      {
        "Counter": "8",
        "Value": "18.1"
      }
   ]
}

 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).

MQTTScreen.png

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:

MQTTactualtestingresult.png

The JSON parser function is located on mqttAction.lua, called mqtt_msg_callback.

MQTTJSONparsefunction.png

11 High-Low Byte Switch

The example shows how to convert high bytes and low bytes for one 16bit address. Main function is like following:

local lowByte = val & 0x00ff
local shiftHigh = lowByte << 8
local highByte = val & 0xff00
local shiftLow = highByte >> 8
local newVal = shiftHigh|shiftLow

Download link

12 Week time setting

The example shows how to use day time to control functions:

Download link

13 MQTT Multi-link

The example shows use mqtt realize multi-machine function:

Download link

14 CSV File Generation

The example shows convert the custom lua table data into csv string, then output as a file:

Download link

The following steps take HMI simulator as example: 

1. Click button "Create Table" to draw lua table in screen.

csvGenerateSteps.png

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. 

csvGenerateSimulationPath.png

3. Click button "upload" to upload csv file into server side. In this case, we can check the upload path for PC.

csvGenerateServerPath.png

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":

csvGenerateLuaCode.png

The table drawing is mainly using the following function to implement:

csvLuaTableDrawing.png

15 String customized length word-wrapped

The example shows automatically line break for long string content according to user input length. 

Download link

The following are some demonstrated strings from offline simulator:

WordWrapDisplayResult.png

16 Weather forecast HTTP API request

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/

Download link

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):

{
   "location": {
       "name": "London",
       "region": "City of London, Greater London",
       "country": "United Kingdom",
       "lat": 51.52,
       "lon": -0.11,
       "tz_id": "Europe/London",
       "localtime_epoch": 1688961236,
       "localtime": "2023-07-10 4:53"
    },
   "current": {
       "last_updated_epoch": 1688960700,
       "last_updated": "2023-07-10 04:45",
       "temp_c": 14.0,
       "temp_f": 57.2,
       "is_day": 0,
       "condition": {
           "text": "Partly cloudy",
           "icon": "//cdn.weatherapi.com/weather/64x64/night/116.png",
           "code": 1003
        },
       "wind_mph": 4.3,
       "wind_kph": 6.8,
       "wind_degree": 220,
       "wind_dir": "SW",
       "pressure_mb": 1020.0,
       "pressure_in": 30.12,
       "precip_mm": 0.0,
       "precip_in": 0.0,
       "humidity": 88,
       "cloud": 50,
       "feelslike_c": 13.3,
       "feelslike_f": 56.0,
       "vis_km": 10.0,
       "vis_miles": 6.0,
       "uv": 1.0,
       "gust_mph": 10.5,
       "gust_kph": 16.9
    },
   "forecast": {
       "forecastday": [
            {
               "date": "2023-07-10",
               "date_epoch": 1688947200,
               "day": {
                   "maxtemp_c": 24.4,
                   "maxtemp_f": 75.9,
                   "mintemp_c": 14.1,
                   "mintemp_f": 57.4,
                   "avgtemp_c": 19.6,
                   "avgtemp_f": 67.2,
                   "maxwind_mph": 15.4,
                   "maxwind_kph": 24.8,
                   "totalprecip_mm": 0.0,
                   "totalprecip_in": 0.0,
                   "totalsnow_cm": 0.0,
                   "avgvis_km": 10.0,
                   "avgvis_miles": 6.0,
                   "avghumidity": 60.0,
                   "daily_will_it_rain": 0,
                   "daily_chance_of_rain": 0,
                   "daily_will_it_snow": 0,
                   "daily_chance_of_snow": 0,
                   "condition": {
                       "text": "Partly cloudy",
                       "icon": "//cdn.weatherapi.com/weather/64x64/day/116.png",
                       "code": 1003
                    },
                   "uv": 5.0
                },
               "astro": {
                   "sunrise": "04:55 AM",
                   "sunset": "09:16 PM",
                   "moonrise": "12:26 AM",
                   "moonset": "02:01 PM",
                   "moon_phase": "Last Quarter",
                   "moon_illumination": "51",
                   "is_moon_up": 1,
                   "is_sun_up": 0
                },
               "hour": [
                    {
                       "time_epoch": 1688943600,
                       "time": "2023-07-10 00:00",
                       "temp_c": 16.2,
                       "temp_f": 61.2,
                       "is_day": 0,
                       "condition": {
                           "text": "Clear",
                           "icon": "//cdn.weatherapi.com/weather/64x64/night/113.png",
                           "code": 1000
                        },
                       "wind_mph": 7.2,
                       "wind_kph": 11.5,
                       "wind_degree": 223,
                       "wind_dir": "SW",
                       "pressure_mb": 1021.0,
                       "pressure_in": 30.14,
                       "precip_mm": 0.0,
                       "precip_in": 0.0,
                       "humidity": 74,
                       "cloud": 6,
                       "feelslike_c": 16.2,
                       "feelslike_f": 61.2,
                       "windchill_c": 16.2,
                       "windchill_f": 61.2,
                       "heatindex_c": 16.2,
                       "heatindex_f": 61.2,
                       "dewpoint_c": 11.6,
                       "dewpoint_f": 52.9,
                       "will_it_rain": 0,
                       "chance_of_rain": 0,
                       "will_it_snow": 0,
                       "chance_of_snow": 0,
                       "vis_km": 10.0,
                       "vis_miles": 6.0,
                       "gust_mph": 10.7,
                       "gust_kph": 17.3,
                       "uv": 1.0
                    },
                    {
                       "time_epoch": 1688947200,
                       "time": "2023-07-10 01:00",
                       "temp_c": 15.7,
                       "temp_f": 60.3,
                       "is_day": 0,
                       "condition": {
                           "text": "Overcast",
                           "icon": "//cdn.weatherapi.com/weather/64x64/night/122.png",
                           "code": 1009
                        },
                       "wind_mph": 7.2,
                       "wind_kph": 11.5,
                       "wind_degree": 232,
                       "wind_dir": "SW",
                       "pressure_mb": 1021.0,
                       "pressure_in": 30.14,
                       "precip_mm": 0.0,
                       "precip_in": 0.0,
                       "humidity": 75,
                       "cloud": 100,
                       "feelslike_c": 15.7,
                       "feelslike_f": 60.3,
                       "windchill_c": 15.7,
                       "windchill_f": 60.3,
                       "heatindex_c": 15.7,
                       "heatindex_f": 60.3,
                       "dewpoint_c": 11.3,
                       "dewpoint_f": 52.3,
                       "will_it_rain": 0,
                       "chance_of_rain": 0,
                       "will_it_snow": 0,
                       "chance_of_snow": 0,
                       "vis_km": 10.0,
                       "vis_miles": 6.0,
                       "gust_mph": 10.5,
                       "gust_kph": 16.9,
                       "uv": 1.0
                    },
                  ...
                    {
                       "time_epoch": 1689026400,
                       "time": "2023-07-10 23:00",
                       "temp_c": 19.0,
                       "temp_f": 66.2,
                       "is_day": 0,
                       "condition": {
                           "text": "Overcast",
                           "icon": "//cdn.weatherapi.com/weather/64x64/night/122.png",
                           "code": 1009
                        },
                       "wind_mph": 9.4,
                       "wind_kph": 15.1,
                       "wind_degree": 192,
                       "wind_dir": "SSW",
                       "pressure_mb": 1015.0,
                       "pressure_in": 29.96,
                       "precip_mm": 0.0,
                       "precip_in": 0.0,
                       "humidity": 66,
                       "cloud": 100,
                       "feelslike_c": 19.0,
                       "feelslike_f": 66.2,
                       "windchill_c": 19.0,
                       "windchill_f": 66.2,
                       "heatindex_c": 19.0,
                       "heatindex_f": 66.2,
                       "dewpoint_c": 12.4,
                       "dewpoint_f": 54.3,
                       "will_it_rain": 0,
                       "chance_of_rain": 0,
                       "will_it_snow": 0,
                       "chance_of_snow": 0,
                       "vis_km": 10.0,
                       "vis_miles": 6.0,
                       "gust_mph": 13.0,
                       "gust_kph": 20.9,
                       "uv": 1.0
                    }
                ]
            }
        ]
    }
}

The core code is from file API_WEATHER.lua, and function name is get7DayWeatherapiByCityname.

WeatherHTTPAPI.png

The HMI offline simulator result:

WeatherHTTPtestdisplay.png

17 CSV File Reading

The example shows how to read the csv file into HMI, and then display as table.

Download link

The core code is from file readcsv.lua:

1) The file path is set as Sheet_2023_06_13_15_29_45.csv at the root path for USB;

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;

CSVreadwithsemicolon.png

3) The reading content is a two-dimensional table, can use the table index to locate the specify data cell;

For example, want to read the 2nd row C column from the table, the table index will be like table[2][3].

celllocation.png

18 How to convert a number to a string

The example shows how to convert a number to a string using the "tostring()" function.

Download link

Note:This demo converts a 64-bit floating to a string,you can change the data format of "@W_HDW0" depand on requirements

The core code is from file convert.lua:

convert.lua.png

The HMI offline simulator result:

Converttestdisplay.png

19 HUAWEI CLOUD & OpenCloud

About the details, please check the powerpoint or video of Lua MQTT & OpenCloud Jump To

Download link

20 UDP Multicast

The example shows how to use UDP multicast feature to send and receive data.

Download link

About the details, please check the UDP module

21 ATC module send SMS

The example shows how to use HMI to customize communication with the ATC SMS module via Lua script to send SMS.

Download link