LUA6: IP Connections (TCP, UDP, HTTP, etc.)

HTTP example

in order to retrieve data via HTTP, you have to use the predefined XCON module. You can find the XCON module within the Server Item Tree. Here is a screenshot:

 

Within the subtree XCON, you will find another subtree for HTTP communication. This subtree contains several items that can be used to send HTTP requests. First, you have to enable the HTTP module by setting the “Enabled” item to true. Then, you can send an HTTP GET request by writing an URL to the “Send” item (within the provided example, the URL http://192.168.0.56/weather shall be retrieved). After having set the “Send” item, the NETx Server sends out the request and the corresponding HTTP result i.e. the full HTTP GET response is stored within the “RESULT” item.


The best way is to define a task on the “RESULT” item that invokes a LUA function which parses the result. For example:

This task definition specifies that the LUA function “GetWeatherData()” is invoked whenever an HTTP result is received. Within this LUA function you can parse the result. For example:

function GetWeatherData()
    local source = nxa.GetValue("NETx.XCON.HTTP.RESULT")

    -- retrieve date time
    local first,last = string.find(source, "MyJsonObject:")
    ...
end

Obviously, if you want to poll the HTTP URL at a regular time interval, you can invoke the HTTP GET within the predefined LUA timer functions. For example:

--==============================================================================
-- OnMinuteTimerEvent() - function is called every minute
--==============================================================================
function OnMinuteTimerEvent()
    nxa.SetValue("NETx.XCON.HTTP.GET", "http://192.168.0.56/weather/")
end

This LUA code sends an HTTP request every minute.

TCP/IP Example

For using serial ports or TCP/IP or UDP/IP in LUA, we have the XCON module. You can find the XCON module in the server item tree:

 

The screen shot above shows an example for TCP/IP. Follow these steps:

  1. Set the IP address and TCP port of the remote server (Items Config/RemoteHost and Config/RemotePort).
  2. Set the Enabled item to true. If the connection is working, the Connected item will change to True – if not you will see an error text in the LastError item. 
  3. Send and receive data via the TCP/IP connection. The OUT item is used to send data – responses will be shown in the IN item. 
  4. To use this in LUA, you just access the items. For example, you can define a server task that is connected to the IN item – this means when you receive data, your LUA Function is invoked and you can parse the received data for further processing. 

function MyTcpHandler()
    -- log incoming data
    nxa.LogInfo("TCP IN: " .. nxa.GetValue(nxa.SourceItemID()))
end


Follow the same principle for UDP/IP and for RS232 (COM) connections. See also Serial Communication with LUA.

Article applies to the following products:

  • NETx BMS Platform
  • NETx Multi Protocol Server
  • NETx BMS Server 2.0