How to scan for iot devices in a local wifi network/Manage IP address ?

Vinay Rasal
4 min readMar 12, 2021

Problem : You know managing the IP addresses of the devices in a local wiresless network that uses DHCP protocol is very hectic. Because the devices might change addresses everytime it reconnects to the network. Using mesh protocol is not best for performance oriented applications or not possible in case where devices use wifi and the devices cannot connect to multiple wifi network at the same time.

Solution:

The solution i am gonna propose is a very simple one and can also be called programming/networking hack.

The solution helps you build a reliable and efficient local iot network. This solution is very easy to implement.

The solution works best in a case where one device acts as head of all iot devices in the network.

The solution has three steps:

  1. Run a web service on iot controller
  2. Maintain Register on Server (on device that acts as head of the iot network)
  3. Scanning for all devices in the network
  1. Run a Web Service on iot controller

As first step of the solution , web service must run on all iot devices. The web service must respond with its name or with its unique identity whenever requested to reveal identity.

Two devices having similar identity can confuse the register. So , every single device must have its unique identity.

// express JS
if(req.body["requestType"] == "identify_yourself"){
res.send(JSON.stringify({status:'success',
me:'drone_controller'}))
}

All the devices must reside on a single local network .

2. Maintain Register on Server

The server (or head of the iot network) must maintain a register that manages/stores all the devices ip address with its unique identifier.

JSON file or a SQL or NO SQL database can be used for register. It must be made sure that the register is always upto date and there is no redundancy.

The register might confuse if there are two devices with same identifier. The confusion can only be overcome by using unique idenitifiers or a logic that can manage redundant identifiers.

3. Scanning All Devices in the network

Now comes the crucial and most important part of the solution. In this step we are gonna scan all iot devices in the network.

An wireless network router can provide upto 255 ip address , to be exact it is 254 as 192.168.0.1 is used by the router itself.

As the network uses DHCP Protocol , it is impossible to guess the IP addresses without scanning the network.From the first step , all the devices are now running web service that returns unique idenitifier on request. So , the scan function can be used to send web request from the ip address 192.168.0.1 to 192.168.0.255.

Whichever requests responds to the request can be logged in to the network register. The values that needs to be noted are IP address and unique_identifier.

//Javascript ( written as psuedo code)
for(var i=1;i<=255;i++){
var host = "192.168.0."+i;
var resp = makeRequest("192.168.0."+i);
if(success(resp)){
addToRegister(host,resp["device_name"])
}
if(failure(resp)){
// do nothing
}
}

Now , we have register of all devices in the local network. The server can simply check the device register to get the ip addres of the device it wants to communicate.

One use case where the scan might fail to identify devices is when the device responds failure to the scan request.

It can be overcome by scanning regularly or maintaining device count to scan again and again untill we find all the devices in the network.

How to Increase Spead of Scan ?

  1. Use asynchronous program
  2. Create multiple instance of a async function for every 50 address or even lesser.

3. Optimized program and fast processor.

//optimization
function scan(){
var start = 1;
while(start<256){
scanFromTo(start,start+50);// async
start = start + 50;
}
}
async function scanFromTo(start,end){
....
....
....
for(var i=start;i<end;i++){
//donot await
makeRequest("192.168.0."+i);
...
}
}

Advantages:

  1. Can be used as centralized DNS Server in a local network
  2. Highly efficient in managing Network Addresses of device in DHCP Network
  3. Offers O(1) speed to lookup for a device address.
  4. No need to hardcode IP addresses or MDNS

Disadvantages

  1. Register is maintained on only one device.
  2. Server needs to be contacted for ip lookup by non-server devices.
  3. The Register grows as the no of devices are added and it cannot be maintaned on low-end devices especially the devices which offer less memory.
  4. Scan must be performed at regular intervals to ensure all device are on the network and to note change of address.

--

--

Vinay Rasal

Developer and Reasearcher. Highly enthusiastic in technology.