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
1 hour ago, eureka said:

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? 

 

Hi @eureka

 

There is basic arp functionality that you may be able to leverage 

 

You'll find some info here 

 

Hope it helps 

 

_f

 

 

 

Link to comment
Share on other sites

  • 0
  • Inquirer
  • Thanks! Maybe you can help me a bit.. I was trying to get a list of all mac addresses in lua. From there I want to get to the IP of those mac addresses.

     

    I wrote the script below (using

    Please login or register to see this link.

    ) but most of the time I only get one MAC address while, when I look in my router, I see way more. I'm still learning but shouldn't this loop through a json which contains all mac addresses and return the mac addresses as i increases?

    Please login or register to see this code.

    What am I doing wrong?

    Link to comment
    Share on other sites

    • 0

    @eureka

     

    I'm away from home at the moment so I can't test but try leaving out the two places you are using local to declare a variable so just use 

     

    response, error.......etc

    JsonTable  = 

     

    You may be testing the variable at each loop ....

     

     

     

    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • Do you mean like this? If so, I tried it but it doesn't work. Must say that sometime I get 1 mac address and sometimes 2 mac addresses on the same timestamp...

     

    Please login or register to see this code.

    Link to comment
    Share on other sites

    • 0

    You might think that the code posted above looks up IP addresses, but it does not. That code shows "ephemeral data". It only shows a MAC-IP pair if there has been some form of communication between a device and the Home Center, so a HC cannot detect the IP of device X if X is not communicating with it. Second problem: after some time (maybe 10 minutes, I do not know, but it is certainly limited and it is not very long) if there is no more communication, the entry is removed from the arp table.

     

    I do not have a better solution. A fixed IP? But of course, you knew that already and you would not have asked this question if you could assign a fixed IP to your device.

     

    Can you write code on the device? So that the device, for example, starts a scene on your HC?

     

    Edited by petergebruers
    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • ok, so only way to go is to force homecenter to try and connect to a device through IP, no? 

     

    Edited by eureka
    Link to comment
    Share on other sites

    • 0
    11 minutes ago, eureka said:

    ok, so only way to go is to force homecenter to try and connect to a device through IP, no? 

     

     

    Could work. Let us assume your DHCP range is IP 100 - 120 so your device will be in that range. Write a script that opens a connection to those addresses (for instance, loop over all IP and start a http connection. It is not important to have a real connection.). This can be done in less than 10 minutes. Then, use the API to dump the arp table and you will have a snapshot of all available IP and MAC...

    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • wow! that is actually quite the idea... I'll try it when I get home. Will keep you guys updated on whether it works or not.

    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • tried it out with following code:

     

    Please login or register to see this code.

    If I look in debug I see it takes approx 3 sec to get 1 address. with 255 addresses it takes approx 13 minutes.. which might be to long. Any idea on how to speed this up?
     

    Link to comment
    Share on other sites

    • 0

    I think the 55000 is the timeout in ms. Please try 5000 or 1000. Or split the range in two and run 2 vd in parallel? Never tried that but I think it sounds good on paper...

    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • 55000 is the port. How can you split range in 2? Lua runs line by line right? Or do you mean making a seperate scene?

    Link to comment
    Share on other sites

    • 0

    @eureka

    Yes . You could try 2 running at the save time but I'm not sure how many socket connections the hc2 can have open at one time... if it is more than 2 you could run 4 vd/scenes at the same time but I wouldn't run more than that.

     

    Another approach could be split up your home network. For instance I have a mine set up as follows 

    Ip 1 - 8 gateway and admin

    9 - 140 static ( most of my devices have static ip setup)

    141 - 255 dynamic ( any visitors get assigned here)

     

    So depending on which category the device you are looking up belongs to you have IP's to search

     

    A vd main loop executes every 3 seconds but within that single loop you can check multiple network devices.

    I have a network monitor (see wwwapi monitor in my signature)  that uses the same approch as you're using to determine  online/offline status.

    I check 6 devices with a 250ms delay and it works without issue so you should be able to speed up your search. 

     

    Could I ask why you want the MAC for ? 

    Edited by AutoFrank
    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • With VD main loop you mean the for loop, am I correct?

     

    How would I set it up if I wanted to check multiple ip's in 1 loop? Surely not by putting in all ip addresses by hand?

     

     

     

     

    Link to comment
    Share on other sites

    • 0
    4 minutes ago, eureka said:

    With VD main loop you mean the for loop, am I correct?

     

    How would I set it up if I wanted to check multiple ip's in 1 loop? Surely not by putting in all ip addresses by hand?

     

     

     

     

     

    @eureka

     

    No.. the main loop I mentioned was referring to the code in the virtual device that get run continuously. If you create a new vd it will be the only place you can write code. ( if you add labels or buttons later, these give you other places to add code )

     

    This is one of the main differences between a virtual device and a scene. A scene is run on demand where as any code in a virtual device main loop is executed continuously . I'm sorry I can send you a screenshot as I'm not at home and working off my phone.

     

    If you do something like

     

    While i < 255 do ...

     

    -- add your code to check 

     

    end 

     

    The code in the "while loop"  will execute within one virtual device main loop cycle so it should run 255 checks. You won't be able to run this at full speed ( ie as fast as the code can run) as you will need to allow time for the socket connection to be made and receive back a response and close the connection before the next check.

     I have ran thus with a 100ms delay but in my code I use a 250ms delay. This should allow you to check all 255 ip  in about a minute.

     

    Make sense ? 

    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • Yes, makes sense but that's what I am already doing. I made a Virtual Device and pasted the for loop in there. However, every iteration of the for loop takes 3 seconds. 

     

    Please login or register to see this code.

    Link to comment
    Share on other sites

    • 0
    34 minutes ago, eureka said:

    Yes, makes sense but that's what I am already doing. I made a Virtual Device and pasted the for loop in there. However, every iteration of the for loop takes 3 seconds. 

     

    Please login or register to see this code.

     Yes. kind of .. but you are relying on the main loop to iterate through your ip addresses 

     

    If you were to use while loop then the iteration is within the while loop

     

    Something like this might work ...

     

    Please login or register to see this code.

     

    Edited by AutoFrank
    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • 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.

     

    Link to comment
    Share on other sites

    • 0

    I'm not at home and my memory is slow... I am pretty certain the http client in scenes has configurable timeout, maybe not the VD client. Maybe you can find some useful example if you search httpclient.

     

    Regarding "while" in a VD main loop, you cannot initialize the variable like "counter =1" because that will happen each time the loop runs. If I remember well, you can do this: "if not counter then counter = 1 end". The first time, counter is nil so the if statement sets it to one. The next run, the if statement does not alter the counter. I think...

     

    I can work on this tomorrow if this does lot help.

    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • Why is it not possible to set i=1? As I understand code, a while loop only loops through the part between while and end. If the i=1 is defined before that, it will not reset to that value. Only when the main loop restarts will it go back to i=1 which should not be a problem because it should redo the whole script right?

     

     

    Link to comment
    Share on other sites

    • 0
    15 hours ago, eureka said:

    Why is it not possible to set i=1? As I understand code, a while loop only loops through the part between while and end. If the i=1 is defined before that, it will not reset to that value. Only when the main loop restarts will it go back to i=1 which should not be a problem because it should redo the whole script right?

     

     

     

    I'm using a mobile phone so I my explanation may be a bit terse...

     

    The main loop code runs every three seconds. So, if you could do one IP address each run that might work.

     

    In pseudo code it would be like this:

     

    If counter does not exist, counter is 1

    If counter is 255 then counter is 1

    Test IP address at "counter"

    Counter = counter + 1

     

    This wil run forever. You might have to pauze after going from 256 to 1. I am sorry, I am thinking out loud.

     

    The test should take less than 3 seconds, and that piece of the puzzle is still missing.

     

    There is no "while" in my pseudo code!

     

    Also, to make it robust, the test must not throw an error, because an error kills the main loop. I guess you need "pcall" to wrap the http call (please use search of this forum to find pcall examples)

     

    Does it have to be a VD? I would try a scene instead, because of the configurable time-out.

     

    Also please have a look at topics by @AutoFrank

     

    Edit: changed maximum counter from 256 to 255 so it tests only from 1 to 254, because 255 is very likely to be a broadcast address. You do not want to send data to a broadcast address.

     

     

     

     

    Edited by petergebruers
    Remove broadcast address from loop
    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...