Tags: 
User library

AN09 demonstrates a Lua script that detects a dropped Internet connection and automatically restarts e.g. a microwave link. The script periodically sends PING (icmp) requests to the 1 or 2 configured IP addresses. When none of these two addresses respond for e.g. 60 seconds, one of the 230V sockets is turned off for 20 seconds (short off – restart).

Do you have any questions?

 

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

 

AN09 Ping to 1 of 2 IP addresses is a Lua script to detect lost connectivity to a specific device, or to the Internet in general (8.8.8.8 – Google’s publicly available DNS server).

 

Typical uses of NETIO AN09

  • Detection of lost internet connectivity from a LAN (to restart a router / modem / link)
  • Detection of a central IP printer going offline – to turn off power to certain desks in order to save energy
  • Detection of an IP link failure – to autonomously activate a back-up network connection
  • Restarting an unstable link without the customers experiencing any prolonged downtime or without having to physically travel to the remote site

 

How does it work?

The header of the Lua script specifies the primary and secondary IP address where the NETIO device sends the PING requests. When a reply is received from the primary or from the secondary IP address, everything is considered OK. If no reply is received for a given number of tries (missingPingAnswers, default is 10), an action is performed – typically an output is turned off for a specified time in order to restart the device connected to that output.

 

In case the reboot takes a longer time, or the problem is not in the terminal device, the firstLivingPulse = 1 parameter makes sure that the link is not restarted over and over again.

Continuous mode

To detect an unstable link (e.g. an infrared / laser link in foggy weather), set continuous = 1. In this mode, the number of failed attempts is added over a longer period, e.g. 24 hours, and the link is restarted in order to trigger its automatic calibration.

 

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: Watchdog (user-defined)
  • Description: Watchdog for IP device (user-defined)
  • Trigger: System started up
  • Schedule: Always

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


------------NETIO AN09------------

------------Section 1------------
local primaryIP = "192.168.101.120" -- primary IP address for ping verification
local secondaryIP = "192.168.101.185" -- secondary IP address for ping verification (for watching only one IP use "" or "0.0.0.0"
local pingPeriod = 5 -- period between ping requests [s]
local missingPingAnswers = 5 -- max missing pings (shortest reaction time: pingPeriod * missingPingAnswers = 5 * 10 = 50s
local controlOutput = 1  -- select output (1 - 4)
local action = 2 -- action type (0 - turn off, 1 - turn on, 2 - short Off, 3 - short On,4 - toggle, 5 - do nothing)
local timeoutAfterWatchdogAction = 60 -- time to activate watchdog again after action [s], 0 = no waiting
local firstLivingPulse = 1 -- 1 = Watchdog function is activated after recieved ping answer (0/1)
local continuous = 0 -- absolute/continuous mode (0/1) (more in NETIO AN09)
local shortTimeMs = 2000 -- time used in states 2 and 3 [ms]

---------End of Section 1---------

local counter = 0
local hasSecondary = true


-- Setting "output" to state defined in variable "action"
function setOutput_wds1(output,action)
  if action == 0 then -- turn off
    devices.system.SetOut{output = output, value = false}
  elseif action == 1 then -- turn on
    devices.system.SetOut{output = output, value = true}
  elseif action == 2 then -- short off
    devices.system.SetOut{output = output, value = false}
    milliDelay(shortTimeMs,function() short_wds1(output,true) end)
  elseif action == 3 then -- short on
    devices.system.SetOut{output = output, value = true}
    milliDelay(shortTimeMs,function() short_wds1(output,false) end)   
  elseif action == 4 then -- toggle
    if devices.system["output" ..output.. "_state"] == 'on' then 
      devices.system.SetOut{output=output,value=false}
    else
      devices.system.SetOut{output=output, value=true}
    end
  elseif action == 5 then
    -- do nothing
  end
end


function short_wds1(output,state)
  devices.system.SetOut{output=output,value=state}
end


-- Incrementing counter and if counter exceeds "missingPingAnswers", it is set to 0 and function doAction is called
function incrementCounter_wds1()
  counter = counter + 1
  logf("Both IP addresses do not respond. Incrementing counter. Missing ping %d/%d", counter,missingPingAnswers)
  if counter >= missingPingAnswers then
    counter = 0
    doAction_wds1()
  else
    delay(pingPeriod,function() pingDevice_wds1() end)
  end
end  


-- Executes action and after delay activates Watchdog again or calls function pingWait
function doAction_wds1()
  logf("Executing action %d with output %d", action, controlOutput)
  setOutput_wds1(controlOutput,action)
  if (toboolean(firstLivingPulse) or (not toboolean(timeoutAfterWatchdogAction))) then
    delay(timeoutAfterWatchdogAction,function() pingWait_wds1() end)  
  else 
    delay(timeoutAfterWatchdogAction,function() pingDevice_wds1() end)
    delay(timeoutAfterWatchdogAction,function() log("Turning Watchdog on") end)
  end
end  


-- Waits for positive ping answer and then activates Watchdog again
function pingWait_wds1()
  ping{address=primaryIP, timeout=5, callback=function(o)
    if o.success then
      logf("Ping on IP %s OK. Turning watchdog on", primaryIP)
      pingDevice_wds1()
    else
      if hasSecondary then
        ping{address=secondaryIP, timeout=5, callback=function(p)
          if p.success then
            logf("Ping on IP %s OK. Turning watchdog on", secondaryIP)
            pingDevice_wds1()
          else
            delay(pingPeriod,function() pingWait_wds1() end)  
          end
        end
        }
      else
        delay(pingPeriod,function() pingWait_wds1() end)
      end
    end  
  end
  }
end


-- Check if primary IP is responding
function checkPrimary_wds1(o)
  if o.success then
    if toboolean(continuous) then
      counter = 0 
    end
    delay(pingPeriod,function() pingDevice_wds1() end)
  else
    logf("Ping on IP %s failed", primaryIP)
    if hasSecondary then
      ping{address=secondaryIP, timeout=5, callback=checkSecondary_wds1}
    else
      incrementCounter_wds1()
    end
  end
end


-- Check if secondary IP is responding
function checkSecondary_wds1(o)
  if o.success then
    if toboolean(continuous) then
      counter = 0 
    end
    delay(pingPeriod,function() pingDevice_wds1() end)
  else
    logf("Ping attempt on secondary IP %s failed", secondaryIP)
    incrementCounter_wds1()
  end
end  


-- Ping on primary IP --> starts Watchdog
function pingDevice_wds1()
  ping{address=primaryIP, timeout=5, callback=checkPrimary_wds1}
end


-- Detects whether secondary IP is used
function validateSecondaryIP_wds1()
  if secondaryIP == "0.0.0.0" or not toboolean(secondaryIP) then
    hasSecondary = false
  end  
end

------------Section delta------------
--[[
local resetTime = 86400 -- counter restart period [s]
function resetCounter_wds1()
  counter = 0
  logf("Counter restarted after %d", resetTime)
  delay(resetTime,function() resetCounter_wds1() end)
end
resetCounter_wds1()
]]--
---------End of Section delta---------

log("Watchdog script started")
validateSecondaryIP_wds1()
pingDevice_wds1()

 

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

 

Method of operation

Using ping requests, the script monitors the accessibility of the specified IP address (primaryIP) or of the back-up IP address (secondaryIP) with the given periodicity (pingPeriod).

If the primary IP does not respond, the accessibility of secondary IP is checked (again with a ping request).

When none of the IP addresses are accessible, the counter is incremented.

As soon as the counter exceeds the specified threshold (missingPingAnswers), the specified action is performed with the selected output (controlOutput). The shortest reaction time to a dropped connection is therefore equal to missingPingAnswers * (pingPeriod + 5) seconds. (Default timeout for ping command is 5 seconds).

The action numbers correspond to those for M2M protocols:

  • 0 = Output switched off (Off)
  • 1 = Output switched on (On)
  • 2 = Output switched off for a short time (short Off)
  • 3 = Output switched on for a short time (short On)
  • 4 = Output switched from one state to the other (toggle)

After performing the action, the script waits for the specified time (timeoutAfterWatchdogAction) and then, according to the value of firstLivingPulse, it either activates again (firstLivingPulse = 0) or waits until a reply to a ping request is received from at least one of the IP addresses (firstLivingPulse = 1).

The continuous variable specifies whether the Watchdog looks for a total outage lasting a certain time (continuous = 1). In this case, the counter is reset after every reply received from at least one of the IP addresses. When continuous = 0, the counter is not reset and the Watchdog monitors the overall number of failures from both IP addresses (that is, for a stable link, the action can be performed e.g. after several months).

 

Setting the variables

  • primaryIP
    • A string containing the primary IP address.
    • The address needs to be specified in the IPv4 format. To find the IP address for a given DNS name, use e.g. https://www.whatismyip.com/dns-lookup/.
    • Example – for IP 192.168.101.128: primaryIP="192.168.101.128"
  • secondaryIP
    • A string containing the secondary IP address.
    • This variable is optional. To monitor one IP address only, specify an empty string or 0.0.0.0.
    • Example – for IP 192.168.101.192: secondaryIP="192.168.101.192"
    • Example for monitoring the primary IP address only: secondaryIP="" or secondaryIP="0.0.0.0"
  • pingPeriod
    • Specifies the period (in seconds) for requesting responses.
    • The time is counted from the moment of receiving a reply, not from the previous request.
    • The minimum value is 1 second.
    • Example – to check every 10 seconds: pingPeriod = 10
  • missingPingAnswers
    • Number of missing replies before the respective action is performed.
    • The minimum value is 1.
    • Example – for 5 missing replies: missingPingAnswers = 5
  • controlOutput 
    •  Specifies the output to control.
    •  Example – to control socket 1: controlOutput= 1
  • action
    • Specifies the action to perform with the output.
    • Actions:
    •  0 – output switched off
    •  1 – output switched on
    •  2 – “short off”: output is set to 0, and after a delay specified in the shortTimeMs variable, the output is set to 1
    •  3 – “short on”: output is set to 1, and after a delay specified in the shortTimeMs variable, the output is set to 0
    •  4 – “toggle”, if the output was on, it is turned off, and vice versa
    •  5 – the output is unchanged
  • timeoutAfterWatchdogAction
    • Specifies the delay in seconds after performing the action. After this delay, the Watchdog is either activated again, or starts actively waiting for a ping (see the firstLivingPulse variable).
    • Example – for a 30 second delay: timeoutAfterWatchdogAction = 30
  • firstLivingPulse
    • Specifies whether the Watchdog is activated immediately after performing the action, or only after receiving a positive response from one of the IP addresses.
    • To wait for a positive reply: firstLivingPulse = 1
    • To activate immediately: firstLivingPulse = 0
  • continuous
    • Specifies whether to monitor outages that last at least a certain time (continuous mode), or the number of missing replies (absolute mode).
    • Continuous mode: continuous = 1
    • Absolute mode: continuous = 0
  • shortTimeMs
    •  Specifies (in milliseconds) for how long is the output on or off for actions 2 or 3 respectively.
    •  The minimum value is 100 ms.
    •  Example – for 2 seconds between state changes: shortTimeMs = 2000

 

Starting the script

After configuring all the parameters and saving the script, the NETIO smart sockets device needs to be restarted. After the device reboots, the script is started and begins to periodically check the accessibility of the IP address(es) in the network.

 

 

FAQ:

1) Is it possible to monitor more than 2 IP addresses with this script?

No. This script is designed to monitor the function (PING reply) of at least 1 or 2 IP addresses. However, it is possible to use AN25 Device failure indication - 1 of 10 PING answers not available indication (Lua script) which demonstrates how to detect an outage of 1 out of 10 IP addresses.

 

2) In the continuous mode, is it possible to reset the missing ping reply counter e.g. once a day?

Yes, at the end of the code there is a “Section delta” for exactly this purpose. Simply uncomment this section (by deleting “--[[” and “]]--”). Set the resetTime to the desired period for resetting the counter (in seconds).

For example, to reset the counter every 24 hours: local resetTime = 86400

Now, when the script is started (upon device restart), the counter is periodically reset.

The code should look like this:

 

3) Is it possible to detect if the device works correctly (e.g. if an IP camera is capturing images)?

No, this script only verifies the accessibility of an IP device in the network (PING answer). It cannot not verify its proper function. If the device is e.g. capable of communicating using .xml, it is possible to process such an .xml file in the Lua script (see e.g. AN13).

 

4) How can I monitor only one IP address?

Set secondaryIP="0.0.0.0" or secondaryIP=""

 

5) Is it possible to run several instances of the script for different IP addresses?

Yes. However, several modifications need to be made in the code due to the way NETIO Lua works. All function names end with _wds1. For the second script instance, this has to be changed in all functions e.g. to _wds2. The fastest way to do it is to copy the code into a text editor and use CTRL+H (Find and Replace) to replace all occurrences of _wds1 with _wds2.

 

6) Is it possible to send an e-mail together with performing the action?

To send e-mails, the NETIO power sockets device first needs to be configured using the web application (see NETIO AN07). Then, at the beginning of the doAction_wds1() function, insert this command: mail("my.email@example.com","NETIO Watchdog","Device is not responding!").

The code should now look like this:

 

7) Is it possible to activate the beeper when the action is performed?

The beeper function is not available at this time. It may be available in a future version.

 

8) How much time does the Watchdog need to start after a power outage and what will be the state of the socket?

After the power is restored, NETIO sets the outputs to the states before the outage. In about 2 minutes, the Watchdog function will be restored.

 

9) Is it possible to use the WatchDog function in the Web interface and this Lua script at the same time?

This combination is theoretically possible; however, we recommend using the Lua script only.

 

10) Is it possible to restart the connected device with the mobile app or the button when the Lua script is running?

Yes, the script runs regardless of the output state.

 

11) Is it possible to enter a DNS name instead of the IP address?

Yes; however, operation is not guaranteed in case of a DNS server failure.

 

 

 


 

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