This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

Automatic Ethernet connection checking and informing in case of troubles ?
#1
I can read "Automatic Ethernet connection checking and informing in case of troubles" in the LM5 description. ( what means ?)

Is it a already function who the logic machine can send to me "i am up and all is ok" when it is connected to ethernet ?

it use cloud service of logic machine ?

how can i use it ?

Best Regard.
Reply
#2
You can check some network futures yourself with the script below and do with it whatever you like. Run it as an init scrit and send an email with this information for example.
Have fun with it  Smile

require('json')
require('socket.http')

ip = io.readproc('if-json')
ip = json.decode(ip)
log("IP address: " ..ip.eth0.inetaddr)

socket.http.timeout = 5
data = socket.http.request('http://ip-api.com/json')
if data then
  data = json.decode(data)
  log("WAN IP: "..data.query)
  log("City: "..data.city)
  log("Region: "..data.region)
  log("Country: "..data.countryCode)
  log("Latitude: "..data.lat)
  log("Longitude: "..data.lon)
else
  log("There is possible no internet connection")
end

log("DNS name: " ..socket.dns.gethostname())

io.input("/sys/class/net/eth0/carrier")
carrier = io.read("*line")
if carrier == "1" then
  log("Connected to the network")
else
  log("No connection to the network")
end

io.input("/sys/class/net/eth0/operstate")
state = io.read("*line")
if state == "up" then
  log("The network connection is active")
else
  log("The network connection is inactive")
end
Reply
#3
Hi Joep, welcome at the forum, took you a loooooooong time to join (:

The question was to send “I’m okay” but I don’t see the added value in that, I would want to know when it’s not okay anymore and that’s a bit tricky when you don’t have internet (: SMS could help you in that case or check the controller from another device.

If you want the okay message on interval bases you could simply add a scheduled script with a single command mail('me@gmail.com', 'Controller X status', 'I’m okay') as it will be send when connection is there (:

But after a few days of mail you will get crazy of those spam messages and you will start ignore them so what’s the point of adding it in the first place?

BR,

Erwin
Reply
#4
(05.11.2019, 20:21)Erwin van der Zwart Wrote: Hi Joep, welcome at the forum, took you a loooooooong time to join (:

The question was to send “I’m okay” but I don’t see the added value in that, I would want to know when it’s not okay anymore and that’s a bit tricky when you don’t have internet (: SMS could help you in that case or check the controller from another device.

If you want the okay message on interval bases you could simply add a scheduled script with a single command mail('me@gmail.com', 'Controller X status', 'I’m okay') as it will be send when connection is there (:

But after a few days of mail you will get crazy of those spam messages and you will start ignore them so what’s the point of adding it in the first place?

BR,

Erwin
Thanks for the welcome Erwin Smile

The good thing with my script is that it will send it's complete status after startup so you got all the information you need from the controller.
There's also a function to check wether or not there is an network connection so that part can help you detecting the fysical connection.
And beside that you could also check the internet connection so your half way.. Only thing is that you can't mail this information any longer if there's no internet connection.
But what you could do for example is to check and send an email every night. So when it will not arrive the next morning you know something is wrong.
Reply
#5
Good morning Joep
In the script the command to send this information by e-mail would be missing, right?
How can I send this information by e-mail every day at night as you comment? (only one e-mail per day, or every week)
Thank you very much for sharing your knowledge in the forum
Br Josema
Reply
#6
(13.11.2019, 05:02)josemabera Wrote: Good morning Joep
In the script the command to send this information by e-mail would be missing, right?
How can I send this information by e-mail every day at night as you comment? (only one e-mail per day, or every week)
Thank you very much for sharing your knowledge in the forum
Br Josema

Hi Josema,

Below the complete script to get all the information, write it to the log and send it by email using the default email function.
You can use this script as a scheduled script and run it every x days or once a week.
What you need to do is to set the correct receiver email address in the last line of the script and if you want you can change the Dutch words to your own language.

--status van de homeLYnk ophalen, loggen en mailen
require('json')
require('socket.http')

ip = io.readproc('if-json')
ip = json.decode(ip)
log("IP adres: " ..ip.eth0.inetaddr)

socket.http.timeout = 5
data = socket.http.request('http://ip-api.com/json')
if data then
  data = json.decode(data)
  log("WAN IP: "..data.query)
  log("Plaats: "..data.city)
  log("Provincie: "..data.region)
  log("Land: "..data.countryCode)
  log("Latitude: "..data.lat)
  log("Longitude: "..data.lon)
else
  log("Er is mogelijk geen internetverbinding actief")
end

log("Naam: " ..socket.dns.gethostname())

io.input("/sys/class/net/eth0/carrier")
carrier = io.read("*line")
if carrier == "1" then
  log("Verbonden met het netwerk")
else
  log("Geen verbinding met het netwerk")
end

io.input("/sys/class/net/eth0/operstate")
state = io.read("*line")
if state == "up" then
  log("De netwerkverbinding is actief")
else
  log("De netwerkverbinding is inactief")
end

log("Directe link pc/tablet visualisering: http://"..socket.dns.gethostname()..".local/scada-vis")
log("Directe link smartphone visualisering: http://"..socket.dns.gethostname()..".local/scada-vis/touch")
log("Interne link pc/tablet visualisering: http://"..ip.eth0.inetaddr.."/scada-vis")
log("Interne link smartphone visualisering: http://"..ip.eth0.inetaddr.."/scada-vis/touch")
log("Externe link pc/tablet visualisering: http://"..data.query.."/scada-vis")
log("Externe link smartphone visualisering: http://"..data.query.."/scada-vis/touch")

subject = "homeLYnk status: "..socket.dns.gethostname().." "..data.city
message = "Statusinformatie".."\r\n\r\n"..
"IP adres: " ..ip.eth0.inetaddr.."\r\n"..
"WAN IP: "..data.query.."\r\n"..
"Plaats: "..data.city.."\r\n"..
"Provincie: "..data.region.."\r\n"..
"Land: "..data.countryCode.."\r\n"..
"Latitude: "..data.lat.."\r\n"..
"Longitude: "..data.lon.."\r\n"..
"Naam: " ..socket.dns.gethostname().."\r\n\r\n"..
"Directe link pc/tablet visualisering: http://"..socket.dns.gethostname()..".local/scada-vis".."\r\n"..
"Directe link smartphone visualisering: http://"..socket.dns.gethostname()..".local/scada-vis/touch".."\r\n\r\n"..
"Interne link pc/tablet visualisering: http://"..ip.eth0.inetaddr.."/scada-vis".."\r\n"..
"Interne link smartphone visualisering: http://"..ip.eth0.inetaddr.."/scada-vis/touch".."\r\n\r\n"..
"Externe link pc/tablet visualisering: http://"..data.query.."/scada-vis".."\r\n"..
"Externe link smartphone visualisering: http://"..data.query.."/scada-vis/touch"

mail('your@email.com', subject, message)
Reply
#7
(13.11.2019, 07:58)Joep Wrote:
(13.11.2019, 05:02)josemabera Wrote: Good morning Joep
In the script the command to send this information by e-mail would be missing, right?
How can I send this information by e-mail every day at night as you comment? (only one e-mail per day, or every week)
Thank you very much for sharing your knowledge in the forum
Br Josema

Hi Josema,

Below the complete script to get all the information, write it to the log and send it by email using the default email function.
You can use this script as a scheduled script and run it every x days or once a week.
What you need to do is to set the correct receiver email address in the last line of the script and if you want you can change the Dutch words to your own language.

--status van de homeLYnk ophalen, loggen en mailen
require('json')
require('socket.http')

ip = io.readproc('if-json')
ip = json.decode(ip)
log("IP adres: " ..ip.eth0.inetaddr)

socket.http.timeout = 5
data = socket.http.request('http://ip-api.com/json')
if data then
  data = json.decode(data)
  log("WAN IP: "..data.query)
  log("Plaats: "..data.city)
  log("Provincie: "..data.region)
  log("Land: "..data.countryCode)
  log("Latitude: "..data.lat)
  log("Longitude: "..data.lon)
else
  log("Er is mogelijk geen internetverbinding actief")
end

log("Naam: " ..socket.dns.gethostname())

io.input("/sys/class/net/eth0/carrier")
carrier = io.read("*line")
if carrier == "1" then
  log("Verbonden met het netwerk")
else
  log("Geen verbinding met het netwerk")
end

io.input("/sys/class/net/eth0/operstate")
state = io.read("*line")
if state == "up" then
  log("De netwerkverbinding is actief")
else
  log("De netwerkverbinding is inactief")
end

log("Directe link pc/tablet visualisering: http://"..socket.dns.gethostname()..".local/scada-vis")
log("Directe link smartphone visualisering: http://"..socket.dns.gethostname()..".local/scada-vis/touch")
log("Interne link pc/tablet visualisering: http://"..ip.eth0.inetaddr.."/scada-vis")
log("Interne link smartphone visualisering: http://"..ip.eth0.inetaddr.."/scada-vis/touch")
log("Externe link pc/tablet visualisering: http://"..data.query.."/scada-vis")
log("Externe link smartphone visualisering: http://"..data.query.."/scada-vis/touch")

subject = "homeLYnk status: "..socket.dns.gethostname().." "..data.city
message = "Statusinformatie".."\r\n\r\n"..
"IP adres: " ..ip.eth0.inetaddr.."\r\n"..
"WAN IP: "..data.query.."\r\n"..
"Plaats: "..data.city.."\r\n"..
"Provincie: "..data.region.."\r\n"..
"Land: "..data.countryCode.."\r\n"..
"Latitude: "..data.lat.."\r\n"..
"Longitude: "..data.lon.."\r\n"..
"Naam: " ..socket.dns.gethostname().."\r\n\r\n"..
"Directe link pc/tablet visualisering: http://"..socket.dns.gethostname()..".local/scada-vis".."\r\n"..
"Directe link smartphone visualisering: http://"..socket.dns.gethostname()..".local/scada-vis/touch".."\r\n\r\n"..
"Interne link pc/tablet visualisering: http://"..ip.eth0.inetaddr.."/scada-vis".."\r\n"..
"Interne link smartphone visualisering: http://"..ip.eth0.inetaddr.."/scada-vis/touch".."\r\n\r\n"..
"Externe link pc/tablet visualisering: http://"..data.query.."/scada-vis".."\r\n"..
"Externe link smartphone visualisering: http://"..data.query.."/scada-vis/touch"

mail('your@email.com', subject, message')
Goedemorgen Joep!
Ik ben onder de indruk!
Het is verbazingwekkend wat je kunt doen met de kennis en controle die je hebt

Heel erg bedankt voor je hulp. Smile

Ik zal kijken of ik het in werking kan stellen
Reply
#8
(13.11.2019, 07:58)Joep Wrote:
(13.11.2019, 05:02)josemabera Wrote: Good morning Joep
In the script the command to send this information by e-mail would be missing, right?
How can I send this information by e-mail every day at night as you comment? (only one e-mail per day, or every week)
Thank you very much for sharing your knowledge in the forum
Br Josema

Hi Josema,

Below the complete script to get all the information, write it to the log and send it by email using the default email function.
You can use this script as a scheduled script and run it every x days or once a week.
What you need to do is to set the correct receiver email address in the last line of the script and if you want you can change the Dutch words to your own language.

Code:
--status van de homeLYnk ophalen, loggen en mailen
require('json')
require('socket.http')

ip = io.readproc('if-json')
ip = json.decode(ip)
log("IP adres: " ..ip.eth0.inetaddr)

socket.http.timeout = 5
data = socket.http.request('http://ip-api.com/json')
if data then
  data = json.decode(data)
  log("WAN IP: "..data.query)
  log("Plaats: "..data.city)
  log("Provincie: "..data.region)
  log("Land: "..data.countryCode)
  log("Latitude: "..data.lat)
  log("Longitude: "..data.lon)
else
  log("Er is mogelijk geen internetverbinding actief")
end

log("Naam: " ..socket.dns.gethostname())

io.input("/sys/class/net/eth0/carrier")
carrier = io.read("*line")
if carrier == "1" then
  log("Verbonden met het netwerk")
else
  log("Geen verbinding met het netwerk")
end

io.input("/sys/class/net/eth0/operstate")
state = io.read("*line")
if state == "up" then
  log("De netwerkverbinding is actief")
else
  log("De netwerkverbinding is inactief")
end

log("Directe link pc/tablet visualisering: http://"..socket.dns.gethostname()..".local/scada-vis")
log("Directe link smartphone visualisering: http://"..socket.dns.gethostname()..".local/scada-vis/touch")
log("Interne link pc/tablet visualisering: http://"..ip.eth0.inetaddr.."/scada-vis")
log("Interne link smartphone visualisering: http://"..ip.eth0.inetaddr.."/scada-vis/touch")
log("Externe link pc/tablet visualisering: http://"..data.query.."/scada-vis")
log("Externe link smartphone visualisering: http://"..data.query.."/scada-vis/touch")

subject = "homeLYnk status: "..socket.dns.gethostname().." "..data.city
message = "Statusinformatie".."\r\n\r\n"..
"IP adres: " ..ip.eth0.inetaddr.."\r\n"..
"WAN IP: "..data.query.."\r\n"..
"Plaats: "..data.city.."\r\n"..
"Provincie: "..data.region.."\r\n"..
"Land: "..data.countryCode.."\r\n"..
"Latitude: "..data.lat.."\r\n"..
"Longitude: "..data.lon.."\r\n"..
"Naam: " ..socket.dns.gethostname().."\r\n\r\n"..
"Directe link pc/tablet visualisering: http://"..socket.dns.gethostname()..".local/scada-vis".."\r\n"..
"Directe link smartphone visualisering: http://"..socket.dns.gethostname()..".local/scada-vis/touch".."\r\n\r\n"..
"Interne link pc/tablet visualisering: http://"..ip.eth0.inetaddr.."/scada-vis".."\r\n"..
"Interne link smartphone visualisering: http://"..ip.eth0.inetaddr.."/scada-vis/touch".."\r\n\r\n"..
"Externe link pc/tablet visualisering: http://"..data.query.."/scada-vis".."\r\n"..
"Externe link smartphone visualisering: http://"..data.query.."/scada-vis/touch"

mail('your@email.com', subject, message)

I was trying to use this script, this script is working, i see the results by log, but i got a error on line 65 'mail('your@email.com', subject, message)'
I know it is 'mail' but i dont know how to fix this.
What script do i need to use it and do i put it on user.libraries?
Reply
#9
mail() function should be in Common functions. What kind of error are you getting exactly?
Reply
#10
(07.03.2022, 07:31)admin Wrote: mail() function should be in Common functions. What kind of error are you getting exactly?

At the moment i cant take a look, But it means he cant find the mail template.
Because there isnt a script for setting for sending mail.

If i look at the log of the LM i see the values, so it is just the mail function what is not working.
Reply
#11
If you are getting attempt to call global 'mail' (a nil value) then the mail function is missing from common functions. You can add it back manually:
Code:
-- send an e-mail
function mail(to, subject, message)
  -- make sure these settings are correct
  local settings = {
    -- "from" field, only e-mail must be specified here
    from = 'example@gmail.com',
    -- smtp username
    user = 'example@gmail.com',
    -- smtp password
    password = 'mypassword',
    -- smtp server
    server = 'smtp.gmail.com',
    -- smtp server port
    port = 465,
    -- enable tls, required for gmail smtp
    secure = 'tlsv1_2',
  }

  local smtp = require('socket.smtp')

  if type(to) ~= 'table' then
    to = { to }
  end

  for index, email in ipairs(to) do
    to[ index ] = '<' .. tostring(email) .. '>'
  end

  -- fixup from field
  local from = '<' .. tostring(settings.from) .. '>'

  -- message headers and body
  settings.source = smtp.message({
    headers = {
      to = table.concat(to, ', '),
      subject = subject,
      ['From'] = from,
      ['Content-type'] = 'text/html; charset=utf-8',
    },
    body = message
  })

  settings.from = from
  settings.rcpt = to

  return smtp.send(settings)
end
Reply
#12
Thanks Admin, it is working
Reply


Forum Jump: