05 Lua Script
1 Arithmetic operations
The example shows how to do the Addition, Subtraction, Multiplication and Division for HMI internal address.
2 MQTT Client
The example shows how to connect to the MQTT server.(Test on MaQiaTTo)
3 Timer
The example shows how to use the Lua Timer function.
4 Trigger
The example shows how to use the Lua Trigger function.
5 User Free Protocol
The example shows the simple write and read of User Free protocol
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.
7 FTP Client
The example shows how to establish the FTP server at WIN10 OS and make V2.0 HMI as FTP client.
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).
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.
4. Change drive letter of file save path according to drop-down list (There are three option available: Flash, UDisk and SDCard).
5. Change the filename of the file that needs to be downloaded from PC(Server).
6. Set the default configuration in file "Script_BG.lua"
Common error:
In the demo project, if there are some errors in HMI for testing, please check the following steps.
Error 1: 550 Access is denied
Open Windows Settings → Update & Security→ Windows Security→ Firewall & network protection→ Advanced setting→ Inbound Rules→ New Rules→ Next → choose "This program path"→ input ‘’C:\Windows\System32\svchost.exe‘’→ Click “NEXT” continuously
Error 2: No route to host
The previous steps are the same as Error 1 → Port → specific local ports→ input port number “21”→ Click “NEXT” continuously
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.
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.
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".
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).
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:
The JSON parser function is located on mqttAction.lua, called mqtt_msg_callback.
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 shiftHigh = lowByte << 8
local highByte = val & 0xff00
local shiftLow = highByte >> 8
local newVal = shiftHigh|shiftLow
12 Week time setting
The example shows how to use day time to control functions:
13 MQTT Multi-link
The example shows use mqtt realize multi-machine function:
14 CSV File Generation
The example shows convert the custom lua table data into csv string, then output as a file:
The following steps take HMI simulator as example:
1. Click button "Create Table" to draw lua table in screen.
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.
3. Click button "upload" to upload csv file into server side. In this case, we can check the upload path for PC.
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":
The table drawing is mainly using the following function to implement:
15 String customized length word-wrapped
The example shows automatically line break for long string content according to user input length.
The following are some demonstrated strings from offline simulator:
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/
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.
The HMI offline simulator result:
17 CSV File Reading
The example shows how to read the csv file into HMI, and then display as table.
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;
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].
18 How to convert a number to a string
The example shows how to convert a number to a string using the "tostring()" function.
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:
The HMI offline simulator result:
19 HUAWEI CLOUD & OpenCloud
About the details, please check the powerpoint or video of Lua MQTT & OpenCloud Jump To
20 UDP Multicast
The example shows how to use UDP multicast feature to send and receive data.
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.
22 TCP Client
The example shows how to use the Lua Socket TCP module to connect with a TCP server.
1. Set the initial values of Server IP and port number.
2. Set the actual receive length of TCP message according to the actual needs.
23 Lua SMS
The example shows how to use HMI built-in 4G module to send SMS via Lua script. This demo requires the HMI model name with (G) or (AG).
24 ATC module Calling
The example shows how to use HMI to customize communication with the ATC-3101 module via Lua script to make phone call