05 Lua Script

Version 25.1 by Hunter on 2023/03/01 12:04

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

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