Jump to content

Welcome to Smart Home Forum by FIBARO

Dear Guest,

 

as you can notice parts of Smart Home Forum by FIBARO is not available for you. You have to register in order to view all content and post in our community. Don't worry! Registration is a simple free process that requires minimal information for you to sign up. Become a part of of Smart Home Forum by FIBARO by creating an account.

 

As a member you can:

  •     Start new topics and reply to others
  •     Follow topics and users to get email updates
  •     Get your own profile page and make new friends
  •     Send personal messages
  •     ... and learn a lot about our system!

 

Regards,

Smart Home Forum by FIBARO Team


  • 0

Get IP Address from MAC Address


eureka

Question

Hi,

 

Say that I know the MAC address of a specific device I have and I want to retreive the IP Address. How can I retreive it using LUA? 

Link to comment
Share on other sites

  • Answers 46
  • Created
  • Last Reply

Top Posters For This Question

Recommended Posts

  • 0
  • Inquirer
  • If I only use the main loop it means that it checks only 1 IP per 3 seconds. I would like to check all IP's in 3 seconds. Therefore I added the while loop within the the Main Loop so that it executes within those 3 seconds..

     

    Can try to do it in a scene, still think it's weird that it doesn't go through a while loop within 3 seconds.

    Link to comment
    Share on other sites

    • 0
    24 minutes ago, eureka said:

    If I only use the main loop it means that it checks only 1 IP per 3 seconds. I would like to check all IP's in 3 seconds. Therefore I added the while loop within the the Main Loop so that it executes within those 3 seconds..

     

    Can try to do it in a scene, still think it's weird that it doesn't go through a while loop within 3 seconds.

    The http client in scenes is totally different, and more modern. It uses callback function to process results and is is non-blocking! Usually people struggle with the fact that it is non-blocking but for your application it probably is a benefit! So forget everything you know on using the VD version!

    Edited by petergebruers
    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • I'll give it a try when I get back home! Will keep u posted on the result.

    Link to comment
    Share on other sites

    • 0
    4 hours ago, eureka said:

    I managed to fix the ip addresses of the devices I want to check (which is considerably less then 255). I put those devices in an array. Now looping through the array with the While loop. It now executes 2 ip's in 1 second but then waits 3 seconds and does the last IP. waits 3 seconds and does the first 2 ip's... In principle I can live with that, however, I would like to solve the issue to understand why this behaviour is still happening. I would expect it to loop through the current code in the main loop of VD before another instance of the main loop is executed.

    Please login or register to see this code.

     

    You could put in some fibaro:debug statements at points on the code to see how it is being executed... sometimes this helps to see how the loops / iteration is working ....

    Edited by AutoFrank
    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • @AutoFrank It loops correctly (so first ip1, then ip2, then ip3) and then ends the statement to start again. But somehow there is a 3 second lag between executing ip2 and ip3..

     

     

     

    @petergebruers I tried using the code below in a scene but I get an error: attempt to index global 'Net' (a nil value) on line: 

     

    Please login or register to see this code.

     

    Below full code:

    Please login or register to see this code.

     

    Link to comment
    Share on other sites

    • 0

    @eureka

     

    The http request format is different between a scene and a vd but the tcp socket connection capability is not supported (or at least not documented) yet for scenes so you can only run this as part of a vd 

     

    As @petergebruers stated, it will run continuously if you add the code to a vd main loop but if you add it to a button you can trigger it when you want...

     

    Sorry I can't test your code ad I'm not at home and won't be back there for another 10 days so I can't help explain the reason for the delay ...

     

    Could you post you full vd code ? 

    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • @AutoFrank this is the full code I use in the VD.

     

    Please login or register to see this code.

     

    debug screen:

    Please login or register to see this code.

     

    Edited by eureka
    Link to comment
    Share on other sites

    • 0
    34 minutes ago, eureka said:

    @petergebruers I tried using the code below in a scene but I get an error: attempt to index global 'Net' (a nil value) on line: 

     

    When I said "forget everything you know on using the VD version!" I wasn't joking... The Lua version (and how you access http servers) of scenes and VD is completely different! When I have time, I'll try to write a script for you.

     

    To give you an idea how you can do http access in scenes, have a quick look at this example (but do not worry to much, I'll try to write something more relevant to this topic). It is runnable code, but it does not print anything particularly interesting. The reason for me writing this script might not be obvious. But If you run the script 10  times, and look carefully at the result, you will notice something strange...

     

    Please login or register to see this code.

    Edit: regarding the seemingly fixed timeout of 3 seconds of your Net.FTcpSocket: it is quite likely that if the IP address does not respond, there is some fixed timeout. I think that setReadTimeout is only working AFTER a connection was established. I have to test that hypothesis. If I am right, a TCP connection to an existing machine with open port 55000 will be very fast, but a connection to a non-existing IP times out after a few seconds.

    Edited by petergebruers
    Add possible explanation for Net.FTcpSocket fixed delay
    Link to comment
    Share on other sites

    • 0
    Just now, AutoFrank said:

    @petergebruers

     

    Are tcp socket connections now supported in scenes? 

     

    @eureka

    Try and reduce your 55000 to 1000 and see if it makes any difference ? 

     

    Don't think so. But I don't think it matters, as long as the HC tries to access something on a machine with IP X and that machine responds, it will be in the ARP cache as X now has MAC Y...

     

    I edited my previous post so you both probably didn't notice this part I added:

     

    Edit: regarding the seemingly fixed timeout of 3 seconds of your Net.FTcpSocket: it is quite likely that if the IP address does not respond, there is some fixed timeout. I think that setReadTimeout is only working AFTER a connection was established. I have to test that hypothesis. If I am right, a TCP connection to an existing machine with open port 55000 will be very fast, but a connection to a non-existing IP times out after a few seconds.

    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • @AutoFrank that did not work (see log below). Also, I think the 55000 is the port, not timeout.

     

    Please login or register to see this code.

    @petergebruers I used your script and I notice that it does work for fibaro and google but won't work for example my laptop or phone ip (connection refused). 

    Please login or register to see this code.

     

    Edited by eureka
    Link to comment
    Share on other sites

    • 0
    1 hour ago, petergebruers said:

    Don't think so. But I don't think it matters, as long as the HC tries to access something on a machine with IP X and that machine responds

    @petergebruers

     

    Ahh, I see where you are going now .. ;)

    @eureka

     

    Sorry, misread with the port :(

     

    In order for the connection/request to be accepted the port needs to be open.

     

    The open port will be different per device so you'll need to either specify a port for a particular ip ( which is what I do with the lan and wwwapi monitors. 

    I store the ip and specific port in a table (local to the vd) or specify a set of ports that all devices will try. The former approach will execute faster as it's just checking one port per ip. 

     

    You could use some app like fing to find an open port on a particular device 

     

    On some devices like phones the port isn't available when the phone goes to sleep.

    Also some devices that communicate over the network to a cloud server (like my heatin system)  but they don't have a port open locally on my lan

     

    Lastly I have found some tv's leave the port open for up to 20 minutes after the tv is turned off. 

     

    Depending on the devices you are trying to connect to and why, you'll have to work around these nuances...

     

    A bit of rambling but hopefully some will help with what you're trying to achieve...

     

     

     

    Link to comment
    Share on other sites

    • 0
    On 17-8-2017 at 8:59 PM, eureka said:

    @petergebruers I used your script and I notice that it does work for fibaro and google but won't work for example my laptop or phone ip (connection refused). 

     

    Yes, but I think you are too excited about that demo script and jumped to conclusions, I hope you do not me mind saying that....

     

    1) As I said, the example does not directly solve the your problem. It demonstrates how scenes use different code than VD and that I'd make a better example to solve your question.

    2) I am still trying to solve your original question: get IP from MAC as in post #1 as the title of your topic says. I am not trying to "ping" your IP devices and I am also not (directly) trying to determine if a device with a certain device is "on" or "off". As @AutoFrank elaborates in the previous topic, there are several reasons why testing a TCP or UDP port does not give a 100% reliable answer (or is complicated)...

    3) Has this question morphed from "get IP from MAC" into "Determine if my phone is on the LAN and is switched on"? In that case, the ARP table may be still serve this purpose: if we can tickle all devices and they appear in the ARP table and because the table gets rid of unresponsive IP/MAC pairs after some time

     

    So, for fun and pleasure, can edit your script so it contains the three address of your phone in this form:

    Please login or register to see this code.

    Then run this code in a script or a VD (it uses an undocumented feature), it  dumps the arp table but also print IP address:

     

    Please login or register to see this code.

     

    I have done a quick test and I have to admit something is bothering me... I am not sure if reading the arp table on a HC works reliably. I do not remember, and I do not see many posts regarding this API call. I am pretty certain that reading a long table with Net.FTcpSocket was bound to fail on older HC firmware because this function did not support "chunked data encodeing" (a way of transferring large blocks of data, it is http specific). I do not know if this has been fixed. That is also the reason why I use api.post instead, although that function call is not documented. I usually refrain from doing so but I think the possible chunked encoding issue justifies the use of this feature. If you do not quite understand what I am talking about, do not worry. I hope to do some more test today or tomorrow, to clear up the mist...

     

    EDIT!

     

    I seem to have used the ARP API in 2016, and never ever since. That might explain why my memory was rusty.

     

    I have done some tests and I am under the impression that the API is broken. It scans the network, but it returns data to soon.

     

    I have contacted support, my case number is #103134

     

    I cannot do anything before I get a response from support, or if someone on this forum can tell me if and how the API is working now...

     

    EDIT2!

     

    I've got a response from support, and I did a quick test. Seems this issue has been solved or at a minimum it has improved a lot.

     

    To repeat: the code below is sufficient to scan the network (send ARP to all IP addresses in your subnet) in < 3 seconds and return a table with the results. I think, due to the response time of a device, there still might be one more or less IP in that table between runs. To be clear, you do not need ANY other code frol this topic.

     

    Can you please try it and tell me what you get?

     

    Please login or register to see this code.

     

    Edited by petergebruers
    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • Awesome!!!! this works!!

     

    Sorry for the late response. It's been quite a busy week so didn't have time to get around to playing with the HC.

     

    As said, script above works. However, only thing still is, that for Iphone there is a sleep mode so doesn't show up in the list above. 

    I did quite a lot of googling and found one post on this forum from @Robert Folbigg. He checks on his router for the Mac Address. I tried his code on my router (Asus RT-AC5300) but unfortunately the response from the router is empty.

     

    Would be a nice way to solve the Iphone issue instead of having to send a pushnotification..

     

     

    Link to comment
    Share on other sites

    • 0
    1 hour ago, eureka said:

    As said, script above works. However, only thing still is, that for Iphone there is a sleep mode so doesn't show up in the list above. 

     

    Thank you for reporting back.

     

    I will have a look at that other topic, but not right now.

     

    I am afraid you will have to add quite a bit of logic to to the bare API to make it useful. I cannot help at the moment because I have a backlog of messages/questions/issues.

     

    But here is the idea.

     

    Create a table that stores tuples of IP, MAC and "last seen time" if it does not exist.

    Call the api.

    Loop over the results and add/change info of the table. Possible delete entries that are to old (e.g older than 1 day).

    You can use the "last seen time" to guesstimate how reliable that MAC/IP pair is.

    If you want to survive scene restarts, you'll have to store to and retrieve from a global variable. Use json to encode/decode.

     

    Can you do that?

    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • Think I understand most of it. However, never worked with json encode/decode. Not sure what/why/where I would need that? I can just send data to global variable right?

    Link to comment
    Share on other sites

    • 0
    18 minutes ago, eureka said:

    Think I understand most of it. However, never worked with json encode/decode. Not sure what/why/where I would need that? I can just send data to global variable right?

     

    You cannot store tables in a global variable. Only strings (and numbers because they can be strings). So json encode/decode is one way to solve that problem.

     

    I can recommend this topic, to get you started with json:

     

     

     

    Link to comment
    Share on other sites

    • 0
    On 8/18/2017 at 5:53 AM, petergebruers said:

    local arpTable, status = api.post("/networkDiscovery/arp", {}) for k, arp in ipairs(arpTable) do fibaro:debug(string.format("Entry %d IP: %s MAC: %s",k,arp.ip,arp.mac)) end

    Ok, would it be possible to see in a VD if these devices (from the ARP list are online or offline, in making labels in a VD, where it says:

    iPhone 1    Online

    Router        Online

    Receiver     Offline

    Etc..

    Can someone help me with this? thanks a lot

    Link to comment
    Share on other sites

    • 0
    9 hours ago, samuel said:

    Ok, would it be possible to see in a VD if these devices (from the ARP list are online or offline, in making labels in a VD, where it says:

    iPhone 1    Online

    Router        Online

    Receiver     Offline

    Etc..

    Can someone help me with this? thanks a lot

     

    I cannot help you this week and probably also not the week after that. I've got too many things to do already... Sorry about that.

     

    It would work for certain mains powered devices, but it probably would not work for phones. If your phone turns off WiFi to save power, it will disappear from the arp table.

     

    Why don't you try it to write the code yourself? I gave a few hints some posts back:

     

    On 30-8-2017 at 11:02 AM, petergebruers said:

    I am afraid you will have to add quite a bit of logic to to the bare API to make it useful. I cannot help at the moment because I have a backlog of messages/questions/issues.

     

    But here is the idea.

     

    Create a table that stores tuples of IP, MAC and "last seen time" if it does not exist.

    Call the api.

    Loop over the results and add/change info of the table. Possible delete entries that are to old (e.g older than 1 day).

    You can use the "last seen time" to guesstimate how reliable that MAC/IP pair is.

    If you want to survive scene restarts, you'll have to store to and retrieve from a global variable. Use json to encode/decode.

     

     

    EDIT: maybe this is something for you. It requires some reading and some setup. It does not use "arp" though:

     

     

    Edited by petergebruers
    Link to comment
    Share on other sites

    • 0

    @samuel

    you could also take a look at the vd code for the network device monitor here 

     

     

    It uses labels to display online/ offline status although it is derived a different way....

     

     

     

     

    Link to comment
    Share on other sites

    Join the conversation

    You can post now and register later. If you have an account, sign in now to post with your account.

    Guest
    Answer this question...

    ×   Pasted as rich text.   Paste as plain text instead

      Only 75 emoji are allowed.

    ×   Your link has been automatically embedded.   Display as a link instead

    ×   Your previous content has been restored.   Clear editor

    ×   You cannot paste images directly. Upload or insert images from URL.


    ×
    ×
    • Create New...