AN24 IP WatchDog 1 to 1 – Device LAN connectivity detection and indication (Lua script) | NETIO products: Smart power sockets controlled over LAN and WiFi
Tags: 
User library

AN24 is a Lua script that detects the presence of an IP address in the network and signals the result by switching a selected output on or off. In this way, it is possible for example to switch off certain equipment when a central printer is switched off, or to signal that someone is present in an office (reachable IP address of a notebook or a phone).

 

Do you have any questions?

Supported devices: NETIO 4All, NETIO PowerPDU 4C, NETIO 4

 

The Lua script periodically checks the network presence of the configured IP address in specified intervals. The “IP address OK” state (or “KO”, depending on the invertState variable) is indicated by switching on the selected output.

It is possible to run the script in a specified time interval and on specific days only. For example, Monday to Friday 9:00 to 16:00.

 

When the status is unknown (immediately after powering up the device) or the current time is outside of the specified time interval, the output is set to a specified default state (defaultState).

 

 

AN24 applications

  • Switching on the power of a back-up system when a server becomes unreachable / goes down
  • When a notebook is switched on, the lights and air conditioning are also automatically switched on
  • When a child turns on their notebook or tablet outside of an allowed time interval, an alarm is started (or the WiFi router is switched off)

 

Creating the rule

To create and run the Lua script, do the following:

1) In the Actions section of NETIO 4 web administration, click Create Rule to add a rule.


 

2) Fill in the following parameters:

  • Enabled: checked
  • Name: Copy ping state (user-defined)
  • Description: IP Watchdog (user-defined)
  • Trigger: System started up
  • Schedule: any (does not affect the function in this configuration)

 

3) Copy the following code and paste it into the field for the Lua script:

 
------------NETIO AN24------------

------------Section 1------------
local IP = "192.168.101.150" -- watched IP
local output = 1 -- signalizing output (1 - 4)
local invertState = 1 -- 0 = copy state, 1 = copy reversed state
local period = 2 -- period between pings [s]
local validPings = 3 -- number of pings for state change
local startTime = "08:30:00"
local endTime = "16:00:00"
local days = "1100000" -- 0 - disabled, 1 - enabled 
local defaultState = 0
---------End of Section 1---------

local results = {}
for i=1,validPings do
  results[i] = 0
end
local iterator = 1
local lastState = defaultState
local setToDefaultState = 0

local startT = (3600*tonumber(startTime:sub(1,2)) + 60*tonumber(startTime:sub(4,5)) + tonumber(startTime:sub(7,8)))

local endT = (3600*tonumber(endTime:sub(1,2)) + 60*tonumber(endTime:sub(4,5)) + tonumber(endTime:sub(7,8)))

function pingDevice()
  local day = tonumber(os.date("%w"))
  -- os.date("%w") returns 0 for Sunday
  if day == 0 then day = 7 end
  --check day
  if days:sub(day,day) == "1" then
    local stringTime = os.date("%X")
    local time = (3600*tonumber(stringTime:sub(1,2)) + 60*tonumber(stringTime:sub(4,5)) + tonumber(stringTime:sub(7,8)))
    --check time
    if (endT>startT and time>startT and time<endt) and="" or="" time="">=startT)or(time<=endT))) then
      setToDefaultState=1
      ping{address=IP, timeout=5, callback=checkResponse}
    else
      if toboolean(setToDefaultState) then
        setToDefaultState=0
        delay(5,function()
            devices.system.SetOut{output=output,value=defaultState}
            for i=1,validPings do
              results[i] = 0
            end
          end)
      end
    end
  end
  delay(period,function() pingDevice() end)
end

function checkResponse(o)
  if o.success then
    results[iterator] = 1
  else
    results[iterator] = 0
  end
  --check previous results
  local sum = 0
  for i=1,validPings do
    sum = sum + results[i]
  end
  if sum == 0 then
    devices.system.SetOut{output=output,value=invertState}
    lastState = invertState
  elseif sum == validPings then
    devices.system.SetOut{output=output,value=1-invertState}
    lastState = 1-invertState
  else
    devices.system.SetOut{output=output,value=lastState}
  end
  iterator = iterator%validPings + 1
end

logf("Watchdog on IP: %s started",IP)
devices.system.SetOut{output=output,value=defaultState}
pingDevice()

4) To finish creating the rule, click Create Rule at the bottom of the screen.

 

Method of operation

  • The script starts when the NETIO 4x device is powered up.
  • The specified signaling output (output) is set to the default state (defaultState).
  • The reachability check of the specified IP address (IP) only takes place on selected days (days) and during the time interval specified by the startTime and endTime variables.
  • The IP reachability is checked periodically. The period of the checks can be specified in the period variable (in seconds).
  • The output state is also set periodically (if the output state is changed e.g. through the web interface, after a certain time the output is switched back to the state determined by the script).

 

  • The output state is switched if the specified IP address responds (or fails to respond) to the number of ping requests specified in the validPings variable.
  • For example, if period = 5 and validPings=3, the IP address must correctly respond to 3 ping requests in a row (in the course of 15 seconds) for the output to be switched on.
  • After power up and outside of the specified time interval, the output is set to the specified default state (defaultState).
  • With the invertState variable, it is possible to invert the output state (switched on when the IP is unavailable, and vice versa).

 

Setting the variables

  • IP
    • Specifies the IP address to monitor.
    • It is a string, so it must be enclosed in double quotes.
    • For example, to monitor 192.168.101.20: IP = "192.168.101.20"
  • output
    • Specifies the output that signals the state of the monitored IP address.
    • Example – to specify output 2: output = 2
  • invertState
    • Specifies whether or not the output state is inverted.
    • Allowed values are 0 and 1.
    • When the value is 0 (invertState = 0), the output is not inverted (reachable IP address = output switched on, unreachable IP address = output switched off).
    • When the value is 1 (invertState = 1), the output is inverted (reachable IP address = output switched off, unreachable IP address = output switched on).
  • period
    • Specifies the period between successive checks (in seconds).
    • Example – to check every 60 seconds: period = 60
  • validPings
    • Specifies the number of successive replies that need to be received in order to switch the output state (this is to avoid rapid switching in case of brief temporary outages).
    • Example – to require 3 replies in a row: validPings=3 (to switch the output on, the script must receive 3 positive replies to ping requests in a row; to switch the output off, there must be 3 negative responses in a row).
  • startTime
    • Specifies the start of the time interval when the script should run.
    • It is a string, so it must be enclosed in double quotes.
    • The format is "hh:mm:ss".
    • Hours must be specified from 0 to 24.
    • Example – to specify the start time of 8:00 (8:00am): startTime="08:00:00"
  • endTime
    • Specifies the end of the time interval.
    • It is a string, so it must be enclosed in double quotes.
    • The format is "hh:mm:ss".
    • Hours must be specified from 0 to 24.
    • For the script to run non-stop, specify an endTime equal to startTime (e.g. startTime="00:00:00", endTime="00:00:00")
    • Example – to specify the end time of 18:00 (6:00pm): endTime="18:00:00"
  • days
    • Specifies the days of week when the script should be active.
    • It is a string, so it must be enclosed in double quotes.
    • A 1 at the position corresponding to a particular day means that the script should be active on that day; a 0 means that the script should be inactive.
    • The format is "mtwtfss".
    • Example – to run the script every day of week except on Sundays: days="1111110"
  • defaultState
    • Specifies the default state. The output is switched to this state when the device is powered up and outside of the specified interval of activity.
    • Allowed values are 0 (switch off) and 1 (switch on).
    • Example – to set the default state to 1: defaultState=1

 

Note on the time interval settings:

  • If the time interval crosses midnight, the day-of-week parameter takes priority. For example, if the script is set to run from 18:00 (startTime="18:00:00") to 5 am (endTime="05:00:00") on workdays only (days="1111100"), on Friday it will be activated at 18:00 (6 pm) and deactivated at 0:00 (midnight between Friday and Saturday). Then, it will be activated again on Monday at 0:00 (midnight).

 

Starting the script

After configuring all the parameters and saving the script, the NETIO smart sockets device needs to be restarted. After the system reboots, the script is started and the signaling output starts indicating the status of the monitored IP address. If the script doesn’t start, please check the settings.

 

FAQ:

1) How can I configure the script to run 24/7?

Set the startTime and endTime variables to the same value (such as startTime="00:00:00", endTime="00:00:00") and the days variable as follows: days="1111111".

 

2) Is it possible to monitor several IP addresses?

Yes, create a new script and set the variables (remember to also select another output to indicate the status).

 

 

 

NETIO supports various PING Watchdog formats:

 

 

 


 

Supported FW versions:

3.0.0 and higher (Firmware archive)

More about Lua:

https://wiki.netio-products.com

 


 

 

This Application Note is compatible with:

Smart power socket NETIO

 

NETIO 4

NETIO 4 is smart power socket (smart power strip) with four 230V/8A sockets, connected to LAN and WiFi. Each of the four power sockets can be individually switched on/off using various M2M API protocols. NETIO 4 is a unique product designed for IT, industry, smart homes, multimedia installations and other applications. Use the product whenever you need 230V sockets controlled by a mobile app, by a computer program (via M2M API) or by a custom script (Lua), and featuring a timer (Scheduler) or auto reboot functionality (IP WatchDog). 

More about NETIO 4

 

Smart power socket NETIO 4All

 

NETIO 4All

NETIO 4All is a PDU module featuring four 230V/8A power sockets with consumption metering for each socket as well as LAN and WiFi connectivity. Each of the four sockets can be individually switched on/off over the Web or using various M2M API protocols. Electricity consumption (A, W, kWh) can be measured at each power socket. NETIO 4All smart sockets are designed for remote measurement and control of electrical sockets. Use the product whenever you need 230V sockets controlled by a mobile app, by a computer program (via M2M API) or by a custom script (Lua) that runs directly in the NETIO 4All smart socket device.

More about NETIO 4All

 
NETIO PowerPDU 4C is small PDU with power measurement and IEC-320 outputs

 

NETIO PowerPDU 4C

NETIO PowerPDU 4C is a small 110/230V PDU (Power Distribution Unit). Each of the four IEC-320 C13 outlets can be independently controlled (On / Off / Reset / Toggle). Electrical parameters (A, W, kWh, TPF, V, Hz) are measured with high accuracy at each outlet. The device features two LAN ports (and a built-in Ethernet switch) for connecting to a LAN. Each power output supports ZCS (Zero Current Switching) to protect the connected equipment.

More about NETIO PowerPDU 4C

 

Ask for a price or technical parameters

For device testing use name/password demo/demo