Fibaro HC2: Monitoring the Internet connection availability

Fibaro HC2: Monitoring the Internet connection availability

Everyone has already experienced the lossof the internet connection and in modern homes there are a number of devices that cannot, partially or at all, perform their functions without the access to the internet. Whether it is a weather station or smart personal scales using the manufacturer’ server, wearable electronics with a connection to an expert database or multimedia players using remote accounts or storage sites. Not only the young generation but also our “things” are dependent on the internet. And with the development of the Internet of Things (IoT) it will no be better.

The owners of the home automation control unit - Fibaro Home Center 2 can, using the technology of virtual devices and having the minimal knowledge of the LUA program language, easily detect whether the internet is available in their home network or not, or they can permanently monitor the internet connection.

Why is this useful? You can smartly use the information obtained on the internet availability during the programming of home automation and ensure the safe triggering of the scenes “dependent” on functional internet connection, but it is also possible, for example, to use the information obtained when complaining to the service provider.

Those of you who have experienced compulsory military service or watched the film Tank Battalion surely remember the notorious message during the shift change of the supervisor: “Nothing special happened during my service!” Among other things, it is fun (especially for visitors) when after your arrival home the home automation system, in pleasant female voice, announces: “Welcome home, nothing special happened during your absence, except for the loss of the internet connection at 9 a.m., for 32 minutes.” The “WOW” effect is perfect.

Note: Those who are interested in how to force Fibaro HC2 to interactively communicate with the residents of the house in any language in a male or female voice, unrecognisable from a living person, should read one of the future articles on Fibaro HC2, SONOS and the use of TTS (Text To Speech) internet services.

How to do it?

The basis is the creation of a virtual device with one button:

fibaro internet

...plus filling in a few details. Of course, it does not matter where you place the virtual devices and how you call it. Only its IP address is important where you fill in only the quotation marks and TCP port 80:

fibaro internet

Then it is necessary to enter the LUA code mentioned below into the body of button no. 1 (or its part, see the program description):

fibaro internet

-- definition of the function that returns the value “On” or “Off”
local function testNet(testIP, testPort)
  tcpSocket = Net.FTcpSocket(testIP, testPort)
  tcpSocket:setReadTimeout(250)
  fibaro:sleep(250)
  bytes, errorCode = tcpSocket:write("ping")
  if errorCode == 0 then return "On"
  else return "Off"
  end
end
-- definition of the input parameters and calling out the function
local testIP = "www.upc.cz" -- optimally a web server of the connection provider
local testPort = 80 -- number of the TCP port
local pingNet = testNet(testIP, testPort)
-- setting of the global value
local currentDate = os.date("*t")
fibaro:setGlobal("PingNet", pingNet)

if (pingNet == "Off") then -- only in the case of an error
  fibaro:debug(currentDate.day .. "." .. currentDate.month .. "." .. currentDate.year .. ", " .. "PingNet = " .. pingNet)
end
-- error message
local number
local errorLog = fibaro:getGlobalValue("ErrorLog")
if (pingNet == "Off") then
  if (string.sub(errorLog,1,17) ~= "connection failed") then -- other error message
    fibaro:setGlobal("ErrorLog", "internet connection failure for 1 min")
  else -- correct error message
    number = tonumber(string.sub(errorLog,46,47)) + 1 -- increase by one minute
    errorLog = "internet connection failure for %d min"
    fibaro:setGlobal("ErrorLog", string.format(errorLog, number))
  end
end

The above-mentioned program code in the LUA language is, of course, only one of the possible solutions and can certainly be written in a different way. For the proper functioning of this particular program, it is necessary to pre-set the global variable PingNetwith the pre-defined values for On and Off:

fibaro internet

In the part of the code with an introductory comment “definition of input parameters”, it is necessary to replace the server www.upc.cz with the server of your connection provider but, in fact, you can use any other server on the internet that has the permission to answer a question of the pingtype. The part of the code with the comment “error message” is very specific, you should not use it at all, I just mentioned it for inspiration. It is used to prepare a part of the message about the internet connection failure time for the voice output of the home automation system using the above-mentioned TTS technology.

Despite the fact that some materials from the Fibaro manufacturer say that it is not possible to “call” the virtual devices from the common LUA scenes, or to “Press the virtual device button”, this is possible. In any LUA scene, simply use the following command:

fibaro:call(ID, "pressButton", "1") -- VD internet test

The ID in the command is the number of your virtual device, you can find it out for example by holding down the cursor over the icon of the virtual device, over the service key symbol:

fibaro internet

...and at the bottom of your web browser the required ID of the virtual device is displayed:

fibaro internet

The result of the above-mentioned command is the filling of the global variable PingNetwith the values of Onor Off, according to the availability of the selected server on the internet. Then, you only have to use the content of this variable when programming your own scenes, either in the LUA language or in the Fibaro HC2 block scenes.

Note: The Fibaro HC2 control unit, even with the latest version of FW, is not completely immune to the internet connection failure, despite the fact that the manufacturer states that permanent off-line HC2 operation is possible. It is almost certainly a minor HC2 error, which is manifested in a way that after a few days of off-line operation (sometimes after two and sometimes after four days) some LUA or group scenes cease to work, those that use the functions time of the dawnor duskin combination with global variables. As soon as the internet connection is restored, everything runs smoothly. You can surely imagine the possible consequences of this error when it occurs during your holiday abroad and nobody is at home.

Author: Jiří Chábera

Posted on 02/23/2018 Tutorials and Tips, Fibaro, Fibaro HC2 1 1697

1 Comment

  • RiderX

    RiderX 03/15/2018 Reply

    Už se těším na zmíněný článek :)

Leave a CommentLeave a Reply

Blog search

Latest Comments

Recently Viewed

No products

Menu

Compare 0