Skip to main content
Skip table of contents

LUA Code: Local Audio FIles UDP Trigger from Barionet to IPAC

Table of Contents


In this knowledge base article we will go through the explanation of how to setup your IP Audio Client device to playback files stored in its internal memory with a command sent over network using the UDP protocol, the command will be sent when the contact closure on Barionet goes ‘high’ (contact closed).

Applications

  • Panic button: Barix Barionet contact closure device can be programmed so that if its contacts are closed (i.e. from a Fire Alarm panel) a message is played out from IP Audio Client

  • Notifications or announcements: messages can be played manually from a trigger received from a network device (a CMS, a PC programmed to send out the UDP command based on a certain condition, repeated messages sent out by a scheduler to trigger scheduled playback announcements)

IPAC Setup

  1. Setup one of the SOURCES in IP Audio Client as "Local Files"

  2. Decide which port the device should start listen to for the incoming UDP command (any port from 8000 to 8999).

  3. Click on Submit

  4. Locate the AUDIO FILES tab and upload your audio file. 

    Take note of the name of the file that you want to trigger the playback (including its extension) - e.g. ‘myAlarm.mp3’

The device now is listening on the specified UDP port for the incoming command.
The command that the device expects to receive is the following: FILEPLAY=myFileName.ext\n (e.g. FILEPLAY=myAlarm.mp3\n) The command must be terminated with the ‘new line’ control character.

For more information on setting up IPAC Local File source check the user manual for the application.

Barionet Setup

Barix Barionet devices allow an easy way to send UDP messages when contact closure are closed. Here is what you need to run this setup: 

  • Barionet 1000 or Barionet 400

  • A lua or bash script to run the program

 In FW releases >= 2.17 Barix introduced an easy way of loading your scripts / programs into the device. 

Below is a LUA script that can be used to trigger the UDP message to be sent when the contact closure 1 is closed (valid for B1000 or B400). 

Configuration must be modified to suit your network configuration and fileName to trigger. 

ipspkr.lua
LUA
local socket = require "socket"

local udp = socket.udp()
-- Define here the list of IP Addresses of IPAC devices that must receive the command 
local ipspkr_list = {"192.168.3.20", "192.168.3.21"}
-- Define here the UDP port. It has to match exactly the port configured on IPAC
local ipspkr_udp_port = 8000
-- Define here the name of the file to be triggered. i.e. "FILEPLAY=myAlarm.mp3"
local udp_command = "FILEPLAY=acdc.mp3\n"

-- Open log file for writing errors, sotred in /mnt/data/custom_app/<app_name>/
local log_file = io.open(arg[0] .. ".log", "a")

-- Function to log errors to file
local function log(message)
    log_file:write(os.date("%c") .. ": " .. message .. "\n")
    log_file:flush()
    print(message)
end

-- Main Application
while true do
    -- Error handling if there is no file to read the value of the Input 1
    local success, file = pcall(io.open, '/dev/gpio/in1/value', 'r')
    if not success then
        log_error("Error opening file: " .. file)
        file = nil
    end
    -- If file exists and it is found to have value of 1 (high) the udp_command is sent to the list of IPAC devices
    if file then
        local in1 = file:read('*n')
        if in1 == 1 then
            for _, ip in pairs(ipspkr_list) do
                local success, err_msg = pcall(function()
                    udp:setpeername(ip, ipspkr_udp_port)
                    udp:send(udp_command)
                end)
                if not success then
                    log("Error sending UDP command to " .. ip .. ": " .. err_msg)
                else
                    log("UDP Command: " .. udp_command .. " sent to IP Speaker: " .. ip .. ":" .. ipspkr_udp_port)
                end
            end
        end
        file:close()
    end
    socket.sleep(1)
end

-- Close log file when script is done
log_file:close()

To upload the above script in your Barionet device: 

  1. Copy this script in a text editor and save it as <myScriptName>.lua (replace myScriptName with any name you want to use)

  2. Connect to the Barionet web interface and enter username and password to access the main configuration interface

  3. Find the "CUSTOM APPS" Tab

  4. Upload the script above as a .lua file

  5. Enter the EXECUTION COMMAND to be used as lua <myScriptName>.lua (replace myScriptName with the name used for the file)

  6. Flag the Run at Boot to have Barionet running the script automatically when the device boots.

  7. Click on SAVE 

  8. When the status shows RUNNING it means the application is monitoring the input 1, when this is closed (contact is closed) it sends the UDP message to the IP Address(es) and Port number of your IP Audio Client to trigger the Alarm 

The above script allows to enter multiple IP addresses in case more than one device must receive the command. It also stores a log file of any error or UDP command sent with timestamp in UTC. The log file is stored in /mnt/data/custom_app/<app_name>/ (replace app_name with the name assigned to this app in the Barionet UI when configuring it). To retrieve the log file is necessary to use ssh or scp.

See below the Barionet web interface to check the configuration

Example for testing the UDP command using Windows

A good application for Windows to send packets over UDP / TCP or other protocols is Packet Sender (https://packetsender.com/)

Its configuration is very easy and allows the possibility to test the functionality fast. There are few things to configure: 

  1. The ASCII text (the actual content of the message, thus the command that IP Audio Client expects to receive) 

  2. The IP Address where to send this command, so the IP Address of your IP Audio Client device

  3. The Port, must be the same as the one specified in the IP Audio Client Source tab 

  4. The Protocol has to be UDP 

In packet sender is also possible to save the messages so it is easier the next time to repeat the test. 

Click on SEND and make sure to be able to hear the audio output on your IP Audio Client device. 

Example of sending a UDP message using Linux CLI

Linux distributions offer a variety of ways to send UDP or TCP packest over network, here we will describe the ones built in that uses a built-in utility in the CLI. 

  1. Open the terminal application 

  2. Type the following command: 

    BASH
    echo FILEPLAY=fileName.xxx > /dev/udp/<IP_Address>/<Port>

    i.e.: echo FILEPLAY=Alarm.wav > /dev/udp/192.168.3.41/8888

Note that using this utility it is not necessary to enter the newline at the end of the string sent. Check if in your application the newline "\n" at the end of the string is required for the message to be interpreted correctly by IP Audio Client. 

Another method for sending UDP or TCP messages over the network i Linux is using Netcat. Full tutorial is available at this link: https://help.ubidots.com/en/articles/937233-sending-tcp-udp-packets-using-netcat

If the device doesn't behave as described above despite following all the steps described, please contact support@barix.com 

JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.