LUA Code: How to send emails notifications from Barionet 200-400-1000-1100
In this knowledge base article we'll go through the process of configuring your new generation Barionet device to work as an SMTP mail sender, so that it could send emails in any condition where it is programmed to do so.
New Generation Barionet devices are based on Linux OpenWRT, a Linux distribution typically found on networking embedded devices. OpenWRT offers several modules that can be used to handle emails, we will use one of the simplest ones to manage and to configure: SSMTP. Later in the article we'll show also how to create a lua function that can be used in scripts so that i.e. you can send an email when a digital input on the device is closed, while also triggering the playback of a file on IP Audio Client.
Install SSMTP
The first thing to do is to install ssmtp in the Barionet, as it is not present by default on the image. To install this package (or any other) it is necessary to SSH as root in the device.
From any terminal or command prompt launch:
ssh root@<Barionet_IP>
The Barionet will ask you to input the password. If the password was never changed it is the default one, printed on the bottom of the device (PW). If the password was changed, enter your new password for the root user
NOTE: in any Linux terminal application, for security reasons, when a password is entered, it is never displayed to the user. Just type it all the way through and push RETURN on your keyboard when done.
The screen will look like this:
First update the package system repositories with
opkg update
Then install SSMTP as follow:
opkg install ssmtp
The installation will begin, wait until the process is over.
Configure SSMTP
Once installed SSMTP places 2 files that must be configured in order for the application to run correctly. The files are located in /etc/ssmtp/ and are:
- ssmtp.conf
- revaliases
To edit the files use VIM text editor, already present in the Barionet.
ssmtp.conf
To open this file with vim:
vim /etc/ssmtp/ssmtp.conf
For VIM beginners: To start type in VIM press "i" (input) on your keyboard. Now you can type and modify the file. Use the arrows to move between the lines.
This is the main configuration file of the mail sender app.
Here is where we connect the application with our SMTP service provider (i.e. GMAIL or any other). The file is quite easy and straightforward to configure, below is an example (note that text after – is a comment). In the below example is how to setup the conf file in a way that a username and password are provided to the SMTP server to send emails through it. It's also possible to use TLS Certificates with the right PKI management
ssmtp.conf
root=postmaster # example: myname@gmail.com
mailhub=smtp.provider.net:465 # example: smtp.gmail.com:587
rewriteDomain=provider.net # example: gmail.com
hostname=mein.server.com # example: <your_router_hostname>
FromLineOverride=YES # allows to use an email address in the "from line" of the envelope --
UseTLS=YES # Use TLS encryption to send emails
UseSTARTTLS=YES #
AuthUser=youremail@provider.net # example: myemail@gmail.com
AuthPass=your_password # example: the password provided by your provider. How to create an app specific password in GMAIL: https://support.google.com/mail/answer/185833?hl=en-GB
AuthMethod=LOGIN # The login method to be used
Once the file is configured according to your requirements press "esc" on your keyboard, this will tell VIM to exit the input mode. Now press ":x!" and then RETURN to Save and close VIM text editor.
revaliases
To open this file with vim:
vim /etc/ssmtp/revaliases
For VIM beginners: To start type in VIM press "i" (input) on your keyboard. Now you can type and modify the file. Use the arrows to move between the lines.
revaliases
# Format: local_account:outgoing_address:mailhub
# example: root:your_login@your.domain:mailhub.your.domain[:port]
root:myemail@gmail.com:smtp.gmail.com:587
Once the file is configured according to your requirements press "esc" on your keyboard, this will tell VIM to exit the input mode. Now press ":x!" and then RETURN to Save and close VIM text editor.
Send a test email
There are multiple ways to use ssmtp command to send emails.
Case 1: Send Mail Directly From The Command Line
For this, copy-paste the below command, and you're ready to send email from your command line:
echo "Test message from Linux server using ssmtp" | ssmtp -vvv receivers-email@gmail.com
-vvv: is the verbosity of the output after sending the command, it prints a log on the screen, useful to make sure there are no errors.
receivers-email@gmail.com: add here the receivers (i.e. receiver1@gmail.com)
Case 2: Send Mail from a Lua script
The below code block can be used as an "email function" to be added in your lua scripts.
Create your .lua file:
- On the Barionet directly using VIM (use the following path to make sure you create the file in the persistent directory that cannot be erased by resets to factory defaults: "/mnt/data/custom_app")
- Prepare your file using your favorite text editor and then upload the file in the Barionet using the dedicated "Custom App" area on the Barionet UI (NOTE: this area and feature has been added in FW release 2.17)
The function looks like this:
sendmail.lua
function email(receiver)
local sendto = receiver
local sendfrom = "yoursendingemail@gmail.com"
local subject = "Fire Alarm"
local body ="Text of the E-Mail:\nAttention, a Fire Alarm has been triggered on Input 1 of Barionet400"
local header ="Subject: "..subject .."\nFrom: "..sendfrom.."\nTo: "..sendto .."\n"
os.execute("echo -e \""..header .."\n"..body.."\" |ssmtp ".. sendto)
end
email("receiver-email@provider.net")
When this function is being called an email is sent to the configured receiver address (the argument of the function).
A complete example of a use case involves the possibility to send an email when a digital input (i.e. Input1) on the Barionet is closed, while sending the email it is also possible to trigger the playback of a file on an IP Audio Client (Exstreamer M400, ExstreamerMPA400, IP Former) so that a sound alarm is also played locally. A real life scenario is:
Barionet connected to the Fire Panel, input is closed when the panel detects fire, alarm is played and notifications is sent via email to a monitoring center.
Below is the complete script that does that, of course it has to be modified according to your network configuration.
fireAlarm.lua
local socket = require "socket" --dependencies
local udp = socket.udp()
-- The UDP Sender function is used to send the UDP command to a specific IP and Port - Insert the right IP and Port to the corresponding variables - Add the command to be sent via UDP to the udp_command variable
function UDP_sender_file1()
local socket = require "socket"
local udp = socket.udp()
ipspkr_ip = "XXX.XXX.XXX.XXX" -- Define Destination IP
ipspkr_udp_port = XXXX -- Define destionation Port
udp_command = "FILEPLAY=fileName.xxx\n" -- Define the string to be sent, the actual command
udp:setoption('reuseaddr',true)
udp:setsockname('*', 0)
udp:setpeername(ipspkr_ip, ipspkr_udp_port)
udp:send(udp_command)
end
-- Send email function
function email(receivers)
local sendto = receivers
local sendfrom = "youremail@gmail.com"
local subject = "Subject"
local body ="Text of the E-Mail\nAttention, a Fire Alarm has been triggered on Input 1 of Barionet400 located, please check immediately"
local header ="Subject: "..subject .."\nFrom: "..sendfrom.."\nTo: "..sendto .."\n"
os.execute("echo -e \""..header .."\n"..body.."\" |ssmtp ".. sendto)
end
-- Sleep function to temporize the iteratrion in the app function (see below)
function sleep(sec)
socket.sleep(sec)
end
-- my main application: a loop that keeps the digital input 1 of barionet is monitored and if value is 1 it calls the UDP sender function
-- This function can be re used to read changes in specific files of the system, just use the correct path
function mainApp()
while true do
local file = io.open('/dev/gpio/in1/value','r') -- Change the path according to where the change must be read. In this example the gpio value of Digital Input 1 is monitored.
local in1 = file:read('*n')
if in1 == 1 then
UDP_sender_file1();
email("receiver-email@provider.com") -- Separate email receivers with comma in between " ". Example: "email1@mail.com,email2@email.com,email3@mail.com"
sleep(5) --wait 5 seconds
end
sleep(1)
end
end
-- Call the app function to run the application
mainApp()
Once loaded on the Barionet, launch the script as follow:
sendmail.lua
lua /mnt/data/custom_app/fireAlarm/firAlarm.lua #Assuming the script is saved in the folder "fireAlarm" inside "/mnt/data/custom_app/"
If the device doesn't behave as described above despite following all the steps described, please contact support@barix.com