WAN IP Checker

From Domoticz
Revision as of 22:38, 10 December 2024 by Walter vl (talk | contribs) (→‎Why do you need one?)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Why do you need one?

Lots of people have experienced this: you are away from home, on a business trip or holidays, and your IP provider has given you a new external IP address. Lots of providers use dynamic IP, a static address is hard to come by. Unfortunately they don't tell you this in any way, but you find out the hard way: you can't get into your systems from remote.

There are ways around this, for instance by using a DynDNS service. But if the only thing you want is to VPN into your network, this is overkill.

There is a simple way to solve this: Install a small dzVents script on your system, that will automatically run periodically.

It will retrieve the external IP address and compare that to the address that was stored in the previous run. If they differ. it will notify you and give you the new IP address.

The script below was tested on Raspberry Pi, running Jesse.

Instructions

Create a Dummy text device named ' Wan IP' and copy and paste the dzvents script in the internal event editor as a dzvents script.


The Script

local getIp    = 'https://api.ipify.org/?format=json'
return {
  on = {
     timer = { 'every 6 hours' },
     httpResponses = { 'wanIP' }
},
logging = {
     level = domoticz.LOG_FORCE,
     marker = '[WAN IP]'
       },
  execute = function(dz, devNil)
     local actIP = dz.devices('Wan IP').text 
     local newIP = 'Unable to retrieve IP'
   
     if (devNil.isTimer)  then
        dz.log("Send WanIP request")
        dz.openURL({
           url = getIp,
           method = 'GET',
           callback = 'wanIP'
           })
     elseif (devNil.isHTTPResponse) then
        dz.log("WanIP response")
        if (devNil.ok) then -- statusCode == 2xx
           newIP = devNil.json.ip
        end 
        if newIP ~= actIP then 
           dz.devices('Wan IP').updateText(newIP)
        end 
     end
  end
}